-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathdocker_check.py
executable file
·135 lines (114 loc) · 4.55 KB
/
docker_check.py
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
#!/usr/bin/env python
"""Rackspace Cloud Monitoring Plugin for Docker."""
# Copyright 2015 Frank Ritchie <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# -----
#
# This plugin monitors the Docker service via the 'docker info' command.
# By default the monitor fails if the check does not complete successfully.
# Metrics for:
#
# - the number of images
# - the number of containers
# - the number of go routines
# - the driver used
# - data space used
# - total data space
# - metadata space used
# - total metadata space
#
# are also reported.
#
# Requires:
# Python 2.6 or greater
# docker-py: https://github.com/docker/docker-py
#
# Usage:
# Place script in /usr/lib/rackspace-monitoring-agent/plugins.
# Ensure file is executable (755).
#
# Set up a Cloud Monitoring Check of type agent.plugin to run
#
# docker_check.py -u <URL>
#
# The URL is optional and can be a TCP or Unix socket, e.g.
#
# docker_check.py -u tcp://0.0.0.0:2376
# or
# docker_check.py -u unix://var/run/docker.sock
#
# The default URL is unix://var/run/docker.sock.
#
# There is no need to define specific custom alert criteria.
# As stated, the monitor fails if the stats cannot be collected.
# It is possible to define custom alert criteria with the reported
# metrics if desired.
#
import sys
from docker import Client
from optparse import OptionParser
class DockerService(object):
"""Create an object for a Docker service. Assume it is stopped."""
def __init__(self, url):
self.url = url
self.docker_running = False
def docker_stats(self):
"""Connect to the Docker object and get stats. Error out on failure."""
docker_conn = Client(base_url=self.url)
try:
docker_info = docker_conn.info()
self.docker_running = True
# Apologies for the broad exception, it just works here.
except Exception:
self.docker_running = False
if self.docker_running:
# Create a dict from the list of lists 'docker info' uses
# to report Driver Status stats.
driver_status = dict([(metric[0], metric[1]) for metric in \
docker_info['DriverStatus']])
print 'metric images int64', docker_info['Images']
print 'metric containers int64', docker_info['Containers']
print 'metric go_routines int64', docker_info['NGoroutines']
print 'metric driver string', docker_info['Driver']
data_space_used_scalar, data_space_used_unit = \
driver_status['Data Space Used'].split()
print 'metric data_space_used float', \
data_space_used_scalar, data_space_used_unit
data_space_total_scalar, data_space_total_unit = \
driver_status['Data Space Total'].split()
print 'metric data_space_total float', \
data_space_total_scalar, data_space_total_unit
metadata_space_used_scalar, metadata_space_used_unit = \
driver_status['Metadata Space Used'].split()
print 'metric metadata_space_used float', \
metadata_space_used_scalar, metadata_space_used_unit
metadata_space_total_scalar, metadata_space_total_unit = \
driver_status['Metadata Space Total'].split()
print 'metric metadata_space_total float', \
metadata_space_total_scalar, metadata_space_total_unit
print 'status ok succeeded in obtaining docker stats.'
else:
print 'status err failed to obtain docker stats.'
sys.exit(1)
def main():
"""Instantiate a DockerService object and collect stats."""
parser = OptionParser()
parser.add_option('-u', '--url', default='unix://var/run/docker.sock',
help='URL for Docker service (Unix or TCP socket).')
(opts, args) = parser.parse_args()
docker_service = DockerService(opts.url)
docker_service.docker_stats()
if __name__ == '__main__':
main()