-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathcheck_docker
executable file
·97 lines (70 loc) · 3.21 KB
/
check_docker
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
#! /usr/bin/env python
import argparse
import docker
import nagiosplugin
class Docker(nagiosplugin.Resource):
def __init__(self, url, api_version):
self.url = url
self.api_version = api_version
def probe(self):
'''Checks docker stats.
This method returns the following metrics:
`images` is total number of docker images
on the Docker system.
`go_routines` are the number of go threads.
`file_descriptors` are the number of open file descriptors.
`events_listeners` are the number of events listener processes.
'''
conn = docker.Client(base_url=self.url, version=self.api_version, timeout=20)
# try to connect the docker service, and catch an exception if we
# can't.
try:
docker_info = conn.info()
self.docker_running = True
except:
self.docker_running = False
if self.docker_running == True:
self.images = docker_info['Images']
self.go_routines = docker_info['NGoroutines']
self.file_descriptors = docker_info['NFd']
self.events_listeners = docker_info['NEventsListener']
metrics = [
nagiosplugin.Metric(
'service', self.docker_running),
nagiosplugin.Metric('images', self.images, context='default'),
nagiosplugin.Metric(
'go_routines', self.go_routines, context='default'),
nagiosplugin.Metric(
'file_descriptors', self.file_descriptors, context='default'),
nagiosplugin.Metric(
'events_listeners', self.events_listeners, context='default')
]
else:
metrics = [nagiosplugin.Metric('service', self.docker_running)]
return metrics
class DockerSummary(nagiosplugin.Summary):
def verbose(self, results):
''' Super-classes summary so we can get verbose results '''
super(DockerSummary, self).verbose(results)
# If the nagios plugin aborts with an uncaught exception or timeout, it exits with
# an unknown exit code and prints a traceback in a format acceptable by Nagios.
@nagiosplugin.guarded
def main():
argp = argparse.ArgumentParser()
argp.add_argument('-u', '--url', metavar='URL',
help='URL string for Docker service.',
default='unix://var/run/docker.sock'),
argp.add_argument('-a', '--api_version', default='1.16',
help='client API version (default 1.16)')
argp.add_argument('-t', '--timeout', default=10,
help='abort execution after TIMEOUT seconds')
argp.add_argument('-v', '--verbose', action='count', default=0,
help='increase output verbosity (use up to 3 times)')
args = argp.parse_args()
# create our check object, if service metric !=0, consider that critical.
check = nagiosplugin.Check(Docker(args.url, args.api_version),
nagiosplugin.ScalarContext('service', '0', '0', fmt_metric=''),
DockerSummary())
check.main(args.verbose, args.timeout)
if __name__ == '__main__':
main()