-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
295 lines (261 loc) · 7.91 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"path"
"strconv"
"strings"
"syscall"
"time"
"github.com/containerd/cgroups"
"github.com/docker/docker/api/types"
"github.com/moby/moby/client"
)
var (
versionFlag = flag.Bool("version", false, "version")
version string
git string
address = flag.String("address", ":48900", "address")
cgroupPath = flag.String("cgroup-path", "/system.slice", "path to cgroup")
enableDocker = flag.Bool("metrics.docker", false, "docker container metrics")
)
func main() {
flag.Parse()
if *versionFlag {
fmt.Printf("version %s, git %s\n", version, git)
return
}
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGTERM, syscall.SIGINT)
system, err := cgroups.Load(subsystem, cgroups.StaticPath(*cgroupPath))
if err != nil {
log.Fatalf("cgroups load: %s", err)
}
var dockerClient *client.Client
if *enableDocker {
dockerClient, err = client.NewEnvClient()
if err != nil {
log.Fatalf("%v", err)
}
defer dockerClient.Close()
}
http.HandleFunc("/metrics", exportMetrics(system, dockerClient))
server := &http.Server{
Addr: *address,
}
go func() {
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Printf("http server ListenAndServe: %v", err)
}
}()
<-sig
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Printf("http server shutdown: %s", err)
}
}
type ProcessStats struct {
FdCount int `json:"fd_count"`
SocketCount int `json:"socket_count"`
}
func processStats(pid int) *ProcessStats {
dir := fmt.Sprintf("/proc/%d/fd", pid)
fds, err := ioutil.ReadDir(dir)
if err != nil {
return nil
}
var socketCount int
for _, fd := range fds {
fdPath := path.Join(dir, fd.Name())
linkName, err := os.Readlink(fdPath)
if err != nil {
continue
}
if strings.HasPrefix(linkName, "socket") {
socketCount++
}
}
return &ProcessStats{
FdCount: len(fds),
SocketCount: socketCount,
}
}
func subsystem() ([]cgroups.Subsystem, error) {
root := "/sys/fs/cgroup"
s := []cgroups.Subsystem{
cgroups.NewDevices(root),
cgroups.NewCpuacct(root),
cgroups.NewMemory(root),
}
return s, nil
}
type cgroupMetrics struct {
*cgroups.Metrics
Process *ProcessStats `json:"process_stats"`
}
func statsCgroups(ctx context.Context, system cgroups.Cgroup) (map[string]*cgroupMetrics, error) {
processes, err := system.Processes(cgroups.Devices, true)
if err != nil {
return nil, fmt.Errorf("cgroups load: %s", err)
}
groups := make(map[string]*cgroupMetrics, len(processes))
for _, p := range processes {
name := strings.TrimPrefix(p.Path, "/sys/fs/cgroup/devices")
name = strings.TrimSuffix(name, "/")
if _, ok := groups[name]; ok {
continue
}
control, err := cgroups.Load(subsystem, func(subsystem cgroups.Name) (string, error) {
return name, nil
})
if err != nil {
log.Printf("cgroups load: %s", err)
continue
}
stats, err := control.Stat(cgroups.IgnoreNotExist)
if err != nil {
log.Printf("control stat: %s", err)
continue
}
ps := processStats(p.Pid)
groups[name] = &cgroupMetrics{
Metrics: stats,
Process: ps,
}
}
return groups, nil
}
type dockerStats struct {
CPU types.CPUStats `json:"cpu_stats,omitempty"`
PreCPU types.CPUStats `json:"precpu_stats,omitempty"` // "Pre"="Previous"
Memory types.MemoryStats `json:"memory_stats,omitempty"`
Process *ProcessStats `json:"process_stats"`
}
func statsDockerContainers(ctx context.Context, dockerClient *client.Client) (map[string]dockerStats, error) {
if dockerClient == nil {
return nil, nil
}
containers, err := dockerClient.ContainerList(ctx, types.ContainerListOptions{
All: true,
Limit: 0,
})
if err != nil {
return nil, fmt.Errorf("list docker containers: %s", err)
}
dockerContainers := make(map[string]dockerStats, len(containers))
for _, container := range containers {
res, err := dockerClient.ContainerStats(ctx, container.ID, false)
if err != nil {
log.Printf("failed to stats docker container %s: %s", container.ID, err)
continue
}
var stats dockerStats
if err := json.NewDecoder(res.Body).Decode(&stats); err != nil {
res.Body.Close()
return nil, fmt.Errorf("failed to decode stats json: %s", err)
}
res.Body.Close()
name := fmt.Sprintf("/docker%s", strings.Join(container.Names, "/"))
inspect, err := dockerClient.ContainerInspect(ctx, container.ID)
if err == nil {
if inspect.State != nil {
stats.Process = processStats(inspect.State.Pid)
}
}
dockerContainers[name] = stats
}
return dockerContainers, nil
}
func exportMetrics(system cgroups.Cgroup, dockerClient *client.Client) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
groups, err := statsCgroups(ctx, system)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
dockerContainers, err := statsDockerContainers(ctx, dockerClient)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprintln(w, `# HELP container_cpu_user_seconds_total Cumulative user cpu time consumed in seconds.
# TYPE container_cpu_user_seconds_total counter`)
for name, stats := range groups {
fmt.Fprintf(w, `container_cpu_user_seconds_total{id=%s} %.2f`, strconv.Quote(name), float64(stats.CPU.Usage.User)/1000000000.0)
fmt.Fprintln(w)
}
for name, stats := range dockerContainers {
fmt.Fprintf(w, `container_cpu_user_seconds_total{id=%s} %.2f`, strconv.Quote(name), float64(stats.CPU.CPUUsage.UsageInUsermode)/1000000000.0)
fmt.Fprintln(w)
}
fmt.Fprintln(w, `# HELP container_memory_usage_bytes Current memory usage in bytes, including all memory regardless of when it was accessed
# TYPE container_memory_usage_bytes gauge`)
for name, stats := range groups {
fmt.Fprintf(w, `container_memory_usage_bytes{id=%s} %d`, strconv.Quote(name), stats.Memory.Usage.Usage)
fmt.Fprintln(w)
}
for name, stats := range dockerContainers {
fmt.Fprintf(w, `container_memory_usage_bytes{id=%s} %d`, strconv.Quote(name), stats.Memory.Usage)
fmt.Fprintln(w)
}
fmt.Fprintln(w, `# HELP container_memory_rss Size of RSS in bytes.
# TYPE container_memory_rss gauge`)
for name, stats := range groups {
fmt.Fprintf(w, `container_memory_rss{id=%s} %d`, strconv.Quote(name), stats.Memory.RSS)
fmt.Fprintln(w)
}
for name, stats := range dockerContainers {
fmt.Fprintf(w, `container_memory_rss{id=%s} %d`, strconv.Quote(name), stats.Memory.Stats["rss"])
fmt.Fprintln(w)
}
fmt.Fprintln(w, `# HELP container_open_fds Number of open file descriptors
# TYPE container_open_fds gauge`)
for name, stats := range groups {
if stats.Process == nil {
continue
}
fmt.Fprintf(w, `container_open_fds{id=%s} %d`, strconv.Quote(name), stats.Process.FdCount)
fmt.Fprintln(w)
}
for name, stats := range dockerContainers {
if stats.Process == nil {
continue
}
fmt.Fprintf(w, `container_open_fds{id=%s} %d`, strconv.Quote(name), stats.Process.FdCount)
fmt.Fprintln(w)
}
fmt.Fprintln(w, `# HELP container_open_sockets Number of open sockets
# TYPE container_open_sockets gauge`)
for name, stats := range groups {
if stats.Process == nil {
continue
}
fmt.Fprintf(w, `container_open_sockets{id=%s} %d`, strconv.Quote(name), stats.Process.SocketCount)
fmt.Fprintln(w)
}
for name, stats := range dockerContainers {
if stats.Process == nil {
continue
}
fmt.Fprintf(w, `container_open_sockets{id=%s} %d`, strconv.Quote(name), stats.Process.SocketCount)
fmt.Fprintln(w)
}
processStats := processStats(os.Getpid())
if processStats != nil {
fmt.Fprintln(w, `# HELP process_open_fds Number of open file descriptors
# TYPE process_open_fds gauge`)
fmt.Fprintf(w, `process_open_fds %d`, processStats.FdCount)
fmt.Fprintln(w)
}
return
}
}