-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker_multigraph
executable file
·156 lines (132 loc) · 7.01 KB
/
docker_multigraph
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
#!/usr/bin/env python3
import sys
import docker
import threading
import queue
__author__ = 'Björn Braunschweig <[email protected]>'
# https://github.com/TomasTomecek/sen/blob/b52a7a55a7de64554da6a79ba963a1fcf9f5330d/sen/util.py#L162
def calculate_cpu_percent(stats):
cpu_count = len(stats['cpu_stats']['cpu_usage']['percpu_usage'])
cpu_percent = 0.0
cpu_delta = float(stats['cpu_stats']['cpu_usage']['total_usage']) - \
float(stats['precpu_stats']['cpu_usage']['total_usage'])
system_delta = float(stats['cpu_stats']['system_cpu_usage']) - \
float(stats['precpu_stats']['system_cpu_usage'])
if system_delta > 0.0:
cpu_percent = cpu_delta / system_delta * 100.0 * cpu_count
return cpu_percent
def worker(container_name, q):
client = docker.from_env()
stats = client.containers.get(container_name).stats(stream=False)
q.put({'stats': stats, 'container_name': container_name})
def get_stats_for_all(container_names):
"""
Getting stats for a container via docker python api takes at least
2 seconds. Therefore schedule it in parallel.
"""
threads = []
q = queue.Queue()
for container_name in container_names:
t = threading.Thread(target=worker, args=(container_name, q))
threads.append(t)
t.start()
for t in threads:
t.join()
all_stats = []
while not q.empty():
all_stats.append(q.get())
return all_stats
def print_docker_blkio_read(container_name, stats):
read_bytes = sum([obj['value'] for obj in stats['blkio_stats']['io_service_bytes_recursive'] if
obj['op'].lower() == 'read'])
print(container_name + '.value ' + str(read_bytes))
def print_docker_blkio_write(container_name, stats):
read_bytes = sum([obj['value'] for obj in stats['blkio_stats']['io_service_bytes_recursive'] if
obj['op'].lower() == 'write'])
print(container_name + '.value ' + str(read_bytes))
def print_docker_network_rx(container_name, stats):
networks = stats.get('networks', None)
if networks is not None:
memory_usage = networks['eth0']['rx_bytes']
print(container_name + '.value ' + str(memory_usage))
def print_docker_network_tx(container_name, stats):
networks = stats.get('networks', None)
if networks is not None:
memory_usage = networks['eth0']['tx_bytes']
print(container_name + '.value ' + str(memory_usage))
def print_docker_cpu(container_name, stats):
cpu_usage = calculate_cpu_percent(stats)
print(container_name + '.value ' + str(cpu_usage))
def print_docker_memory(container_name, stats):
memory_usage = stats['memory_stats']['usage']
print(container_name + '.value ' + str(memory_usage))
def main():
client = docker.from_env()
container_names = [c.name for c in client.containers.list()]
graphs = [{'config_values': ['multigraph docker_cpu',
'graph_title Docker container CPU usage',
'graph_args --base 1000 -r --lower-limit 0 --upper-limit 100',
'graph_vlabel %',
'graph_scale no',
'graph_period second',
'graph_category container',
'graph_info This graph shows docker container CPU usage.'],
'config_data_values': ['{0}.label {0}', '{0}.draw LINE2', '{0}.min 0'],
'print_func': print_docker_cpu},
{'config_values': ['multigraph docker_memory',
'graph_title Docker container memory usage',
'graph_args --base 1024 --lower-limit 0',
'graph_vlabel Bytes',
'graph_category container',
'graph_info This graph shows docker container memory usage.'],
'config_data_values': ['{0}.label {0}', '{0}.draw LINE2', '{0}.min 0'],
'print_func': print_docker_memory},
{'config_values': ['multigraph docker_network_rx',
'graph_title Docker container network received',
'graph_args --base 1024 --lower-limit 0',
'graph_vlabel Bytes',
'graph_category container',
'graph_info This graph shows docker container network received bytes'],
'config_data_values': ['{0}.label {0}', '{0}.draw LINE2'],
'print_func': print_docker_network_rx},
{'config_values': ['multigraph docker_network_tx',
'graph_title Docker container network transmitted',
'graph_args --base 1024 --lower-limit 0',
'graph_vlabel Bytes',
'graph_category container',
'graph_info This graph shows docker container network transmitted bytes'],
'config_data_values': ['{0}.label {0}', '{0}.draw LINE2'],
'print_func': print_docker_network_tx},
{'config_values': ['multigraph docker_blkio_write',
'graph_title Docker container block io write',
'graph_args --base 1024 --lower-limit 0',
'graph_vlabel Bytes',
'graph_category container',
'graph_info This graph shows docker container block io write bytes'],
'config_data_values': ['{0}.label {0}', '{0}.draw LINE2'],
'print_func': print_docker_blkio_write},
{'config_values': ['multigraph docker_blkio_read',
'graph_title Docker container block io read',
'graph_args --base 1024 --lower-limit 0',
'graph_vlabel Bytes',
'graph_category container',
'graph_info This graph shows docker container block io read bytes'],
'config_data_values': ['{0}.label {0}', '{0}.draw LINE2'],
'print_func': print_docker_blkio_read}
]
if len(sys.argv) > 1 and sys.argv[1] == 'config':
for graph in graphs:
for cv in graph['config_values']:
print(cv)
for container_name in container_names:
for cvd in graph['config_data_values']:
print(cvd.format(container_name))
else:
all_stats = get_stats_for_all(container_names) # dict: {'container_name': ..., 'stats': ...}
for graph in graphs:
print(graph['config_values'][0]) # print only eg. 'multigraph docker_cpu'
for stats_data in all_stats:
graph['print_func'](stats_data['container_name'],
stats_data['stats'])
if __name__ == '__main__':
main()