This repository has been archived by the owner on Feb 25, 2022. It is now read-only.
/
main.go
511 lines (457 loc) · 12.8 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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
package main
import (
"context"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
_ "net/http/pprof"
"os"
"regexp"
"strconv"
"strings"
"sync"
"syscall"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// #include <unistd.h>
//import "C"
const (
good = iota
bad
int64max int64 = int64(9223372036854775807)
)
var (
wg sync.WaitGroup
// example: 17.09.0-ce
versionScrubber = regexp.MustCompile(`[^\d]+`)
// command line arguments
pperf = flag.Int("pperf", 0, "Port for pperf to listen on. 0 is disabled")
listen = flag.String("listen", ":8080", "Address to listen on")
interval = flag.Duration("interval", 1*time.Minute, "Interval between docker scrapes")
// maps container id -> closer channel
scrapers = make(map[string]chan bool)
// prometheus metrics
restartCounter = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "docker_container_restart_count",
Help: "Current amount of restarts.",
},
[]string{"name", "image_id"},
)
containerHealthStatus = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "docker_container_healthy",
Help: "Healthy and running if 0, and 1 if anything else",
},
[]string{"name", "image_id"},
)
inspectTimeoutStatus = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "docker_container_stuck_inspect",
Help: "Inspect worked if 0, and 1 if timed out",
},
[]string{"name", "image_id"},
)
dockerVersion = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "docker_version",
Help: "The version of dockerd",
},
[]string{"docker_version"},
)
zombieProcesses = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "docker_zombie_processes",
Help: "Current number of zombie processes that start with [docker",
},
[]string{},
)
dockerContainerCount = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "docker_container_count",
Help: "The number of containers found.",
},
[]string{"docker_container_count"},
)
dockerContainerStatus = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "docker_container_status",
Help: "The number of containers found.",
},
[]string{"name", "image_id", "docker_container_status"},
)
dockerLongestRunning = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "docker_longest_running",
Help: "Runtime for docker processes",
},
[]string{"cmdline"},
)
)
func init() {
// Metrics have to be registered to be exposed:
prometheus.MustRegister(restartCounter)
prometheus.MustRegister(containerHealthStatus)
prometheus.MustRegister(inspectTimeoutStatus)
prometheus.MustRegister(dockerVersion)
prometheus.MustRegister(zombieProcesses)
prometheus.MustRegister(dockerContainerCount)
prometheus.MustRegister(dockerContainerStatus)
prometheus.MustRegister(dockerLongestRunning)
}
type inspectResult struct {
inspect types.ContainerJSON
err error
}
func scrapeContainer(container types.Container, cli *client.Client, closer <-chan bool) {
inspect, err := cli.ContainerInspect(context.Background(), container.ID)
if err != nil {
log.Printf("first inspect failed for container %s : %s", container.ID, err)
return
}
name := inspect.Name
imageID := inspect.Image
log.Printf("Start scraping %s", name)
perContainerLabels := prometheus.Labels{"name": name, "image_id": imageID}
timeout := time.Duration(interval.Nanoseconds() * 3 / 4) // 3/4 of the interval seems like a reasonable timeout
inspectDone := make(chan inspectResult, 1)
tick := time.Tick(*interval)
var result inspectResult
var timeoutContext context.Context
var cancel context.CancelFunc
for {
select {
case <-closer:
if cancel != nil {
cancel()
}
closer = nil
log.Printf("Stop scraping %s", name)
return
case <-tick:
// because we timeout before it is possible to have the next tick,
// this will not get clobbered by the next tick
timeoutContext, cancel = context.WithTimeout(context.Background(), timeout)
go func() {
inspect, err := cli.ContainerInspect(timeoutContext, container.ID)
cancel()
timeoutContext = nil
cancel = nil
inspectDone <- inspectResult{inspect, err}
}()
case result = <-inspectDone:
if result.err == nil {
inspectTimeoutStatus.With(perContainerLabels).Set(good)
} else {
if result.err == context.DeadlineExceeded {
inspectTimeoutStatus.With(perContainerLabels).Set(bad)
} else {
log.Printf("inspecting %s : %s", name, result.err)
}
continue
}
inspect = result.inspect
restartCounter.With(perContainerLabels).Set(float64(inspect.RestartCount))
dockerContainerStatus.With(prometheus.Labels{"name": name, "image_id": imageID, "docker_container_status": inspect.State.Status}).Set(1)
if inspect.State.Health != nil {
if inspect.State.Health.Status == types.Healthy ||
inspect.State.Health.Status == types.NoHealthcheck {
// we treat NoHealthcheck as healthy container, if we got here
containerHealthStatus.With(perContainerLabels).Set(good)
} else {
containerHealthStatus.With(perContainerLabels).Set(bad)
}
} else {
if inspect.State.Running {
// running container considered as healthy, the rest are not
containerHealthStatus.With(perContainerLabels).Set(good)
} else {
containerHealthStatus.With(perContainerLabels).Set(bad)
}
}
}
}
}
func scrapeContainers(cli *client.Client) {
tick := time.Tick(*interval)
for {
// Since we will never close scrapeContainers, don't bother to make this async following the pattern in scrapeContainer
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{All: true})
if err != nil {
log.Panic(err)
}
newScrapers := make(map[string]chan bool)
containerCount := 0
for _, container := range containers {
containerCount++
if _, present := scrapers[container.ID]; present {
newScrapers[container.ID] = scrapers[container.ID]
} else {
newScrapers[container.ID] = make(chan bool)
go scrapeContainer(container, cli, newScrapers[container.ID])
}
}
dockerContainerCount.With(prometheus.Labels{"docker_container_count": strconv.Itoa(containerCount)}).Set(float64(containerCount))
// detect containers which have gone away and kill their scrapers
for containerID, closer := range scrapers {
if _, present := newScrapers[containerID]; !present {
//log.Printf("container gone: %s", containerId)
closer <- true
close(closer)
}
}
scrapers = newScrapers
<-tick
}
}
func scrapeDockerVersion(cli *client.Client) {
tick := time.Tick(*interval)
//log.Printf("Starting docker version scraper")
for {
// Since we will never close scrapeContainers, don't bother to make this async following the pattern in scrapeContainer
server, err := cli.ServerVersion(context.Background())
if err != nil {
log.Panic(err)
}
//log.Printf("version: %s", server.Version)
scrubbedVersion := versionScrubber.ReplaceAllString(server.Version, "")
dockerVersionFloat, err := strconv.ParseFloat(scrubbedVersion, 64)
if err != nil {
log.Panic(err)
}
dockerVersion.With(prometheus.Labels{"docker_version": server.Version}).Set(dockerVersionFloat)
<-tick
}
}
type UnixProcess struct {
pid int
ppid int
state rune
pgrp int
sid int
tty_nr int
tpgit int
flags uint
minflt uint32
cminflt uint32
utime uint32
stime uint32
cutime int32
cstime int32
nice int32
num_threads int32
itrealvalue int32
starttime int64
vsize uint32
rss int32
rsslim uint32
startcode uint32
endcode uint32
startstack uint32
kstkesp uint32
kstkeip uint32
signal uint32
blocked uint32
binary string
cmdline string
}
func (p *UnixProcess) Refresh() error {
statPath := fmt.Sprintf("/proc/%d/stat", p.pid)
dataBytes, err := ioutil.ReadFile(statPath)
if err != nil {
return err
}
// First, parse out the image name
// Example stat string:
// 9249 (haproxy) S 5003 5003 5003 0 -1 4194560 21133 0 0 0 36 2 0 0 20 0 1 0 176991 99676160 21269 18446744073709551615 94539338215424 94539339626788 140727663170416 140727663169928 140237674944734 0 0 4096 3146245 1 0 0 17 4 0 0 0 0 0 94539341725792 94539341790336 94539349725184 140727663173220 140727663173296 140727663173296 140727663173600 0
data := string(dataBytes)
binStart := strings.IndexRune(data, '(') + 1
binEnd := strings.IndexRune(data[binStart:], ')')
p.binary = data[binStart : binStart+binEnd]
// Move past the image name and start parsing the rest
data = data[binStart+binEnd+2:]
_, err = fmt.Sscanf(data,
"%c %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d %d",
&p.state,
&p.ppid,
&p.pgrp,
&p.sid,
&p.tty_nr,
&p.tpgit,
&p.flags,
&p.minflt,
&p.cminflt,
&p.utime,
&p.stime,
&p.cutime,
&p.cstime,
&p.nice,
&p.num_threads,
&p.itrealvalue,
&p.starttime,
&p.vsize,
&p.rss,
&p.rsslim,
&p.startcode,
&p.endcode,
&p.startstack,
&p.kstkesp,
&p.kstkeip,
&p.signal,
&p.blocked)
return err
}
func (p *UnixProcess) RefreshCmdline() error {
cmdlinePath := fmt.Sprintf("/proc/%d/cmdline", p.pid)
cmdline, err := ioutil.ReadFile(cmdlinePath)
if err != nil {
return err
}
p.cmdline = string(cmdline)
return err
}
func newUnixProcess(pid int) (*UnixProcess, error) {
p := &UnixProcess{pid: pid}
return p, p.Refresh()
}
type procResult struct {
zombieCount int
cmdline string
startTime int64
}
func scrapeProc() (procResult, error) {
d, err := os.Open("/proc")
if err != nil {
return procResult{}, err
}
defer d.Close()
zombieCount := 0
cmdline := ""
smallestStartTime := int64max
for {
fis, err := d.Readdir(10)
if err == io.EOF {
break
}
if err != nil {
return procResult{}, err
}
for _, fi := range fis {
// We only care about directories, since all pids are dirs
if !fi.IsDir() {
continue
}
// We only care if the name starts with a numeric
name := fi.Name()
if name[0] < '0' || name[0] > '9' {
continue
}
// From this point forward, any errors we just ignore, because
// it might simply be that the process doesn't exist anymore.
pid, err := strconv.ParseInt(name, 10, 0)
if err != nil {
continue
}
p, err := newUnixProcess(int(pid))
if err != nil {
continue
}
// Look for executables with names like [docker-stop] <defunct>
// I believe the [] in the name is syntactic sugar from ps, or at least this leads to a count of 0 across all servers.
if p.binary != "docker" {
continue
}
// We are only interested in Zombie processes and per
// http://man7.org/linux/man-pages/man5/proc.5.html
// /proc/[pid]/stat state field maps to Z for zombie / defunct
if p.state != 'Z' {
zombieCount++
}
// Find oldest startcount
if p.starttime < smallestStartTime {
p.RefreshCmdline()
log.Printf("lrp: %s", p.cmdline)
smallestStartTime = p.starttime
cmdline = p.cmdline
}
}
}
return procResult{zombieCount, cmdline, smallestStartTime}, nil
}
type countResult struct {
scrapeProcResults procResult
err error
}
func scrapeDefunct() {
countDone := make(chan countResult, 1)
labels := prometheus.Labels{}
tick := time.Tick(*interval)
//clockTicksPerSec := float64(C.sysconf(C._SC_CLK_TCK))
// Linking callouts to C libraries is a nightmare.
// 100 is the standard value for this, so...
clockTicksPerSec := float64(100)
for {
select {
case <-tick:
go func() {
r, err := scrapeProc()
countDone <- countResult{r, err}
}()
case result := <-countDone:
if result.err != nil {
log.Panic(result.err)
}
r := result.scrapeProcResults
zombieProcesses.With(labels).Set(float64(r.zombieCount))
// From http://man7.org/linux/man-pages/man5/proc.5.html
// r.startTime is
// The time the process started after system boot.
// ... snip ...
// Since Linux 2.6, the value is expressed
// in clock ticks (divide by sysconf(_SC_CLK_TCK)).
s := syscall.Sysinfo_t{}
err := syscall.Sysinfo(&s)
if err != nil {
log.Panic(err)
}
c := r.cmdline
t := float64(s.Uptime) - float64(r.startTime)/clockTicksPerSec
if t < 0 {
c = "nil"
t = 0
}
dockerLongestRunning.With(prometheus.Labels{"cmdline": c}).Set(t)
}
}
}
func main() {
flag.Parse()
wg.Add(1)
if *pperf > 0 {
pperfAddr := fmt.Sprintf("localhost:%d", *pperf)
go func() {
log.Println(http.ListenAndServe(pperfAddr, nil))
}()
}
go func() {
// Since we have imported pperf support, we must not use DefaultServerMux
mux := http.NewServeMux()
mux.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(*listen, mux))
}()
cli, err := client.NewEnvClient()
if err != nil {
log.Panic(err)
}
go scrapeDockerVersion(cli)
go scrapeContainers(cli)
go scrapeDefunct()
wg.Wait() // will never end
}