-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathdocker_stats_check.py
executable file
·147 lines (131 loc) · 5.37 KB
/
docker_stats_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
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python
"""Rackspace Cloud Monitoring Plugin for Docker Stats."""
# Copyright 2015 Nachiket Torwekar <[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 containers via the 'docker stats' command.
# By default the monitor fails if the check does not complete successfully.
# Metrics for:
#
# - cpu_total_usage
# - cpu_system_usage
# - cpu_kernel_mode_usage
# - cpu_user_mode_usage
# - cpu_user_mode_usage
# - memory_max_usage
# - memory_total_cache
# - network_rx_bytes
# - network_rx_packets
# - network_tx_bytes
# - network_tx_packets
#
# 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_stats_check.py -u <URL> -c <container>
#
# The URL is optional and can be a TCP or Unix socket, e.g.
#
# docker_stats_check.py -u tcp://0.0.0.0:2376
# or
# docker_stats_check.py -u unix://var/run/docker.sock
#
# The default URL is unix://var/run/docker.sock.
#
# The container can be name or id
# docker_stats_check.py -u unix://var/run/docker.sock -c agitated_leakey
# or
# docker_stats_check.py -u unix://var/run/docker.sock -c 1f3b3b8f0fcc
#
# 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
from subprocess import call
import json
class DockerService(object):
"""Create an object for a Docker service. Assume it is stopped."""
def __init__(self, url, container):
self.url = url
self.container = container
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:
stats = docker_conn.stats(self.container)
self.docker_running = True
# Apologies for the broad exception, it just works here.
except Exception:
self.docker_running = False
if self.docker_running:
print 'status ok succeeded in obtaining docker container stats.'
for stat in stats:
s = json.loads(stat)
print 'metric cpu_total_usage int64', s['cpu_stats']['cpu_usage']['total_usage']
print 'metric cpu_system_usage int64', s['cpu_stats']['system_cpu_usage']
print 'metric cpu_kernel_mode_usage int64', s['cpu_stats']['cpu_usage']['usage_in_kernelmode']
print 'metric cpu_user_mode_usage int64', s['cpu_stats']['cpu_usage']['usage_in_usermode']
print 'metric memory_max_usage int64', s['memory_stats']['max_usage']
print 'metric memory_total_cache int64', s['memory_stats']['stats']['total_cache']
print 'metric pids_current int64', s['pids_stats']['current']
if s.has_key('network'):
print_network_stat(s['network'])
elif s.has_key('networks'):
tot = { "rx_bytes": 0, "rx_packets": 0, "tx_bytes": 0, "tx_packets": 0 }
for ifname in s['networks']:
tot['rx_bytes'] += s['networks'][ifname]['rx_bytes']
tot['rx_packets'] += s['networks'][ifname]['rx_packets']
tot['tx_bytes'] += s['networks'][ifname]['tx_bytes']
tot['tx_packets'] += s['networks'][ifname]['tx_packets']
print_network_stat(s['networks'][ifname], suffix='_' + ifname)
print_network_stat(tot)
sys.exit(0);
else:
print 'status err failed to obtain docker container stats.'
sys.exit(1)
def print_network_stat(n, suffix=''):
print "metric network_rx_bytes%s int64 %d" % (suffix, n['rx_bytes'])
print "metric network_rx_packets%s int64 %d" % (suffix, n['rx_packets'])
print "metric network_tx_bytes%s int64 %d" % (suffix, n['tx_bytes'])
print "metric network_tx_packets%s int64 %d" % (suffix, n['tx_packets'])
def main():
"""Instantiate a DockerStats 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).')
parser.add_option('-c', '--container',
help='Name or Id of container that you want to monitor')
(opts, args) = parser.parse_args()
if opts.container is None:
parser.error("options -c is mandatory")
docker_service = DockerService(opts.url, opts.container)
docker_service.docker_stats()
if __name__ == '__main__':
main()