-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker_containers.py
executable file
·82 lines (71 loc) · 3.11 KB
/
docker_containers.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
#!/usr/bin/env python
import sys
import re
import commands
from config import config
command_for_average_template = '/usr/bin/env python {0} --average=5 --field={1} --container-name={2}'
command_for_container_names_template = "/usr/bin/env python {0} --container-ids | cut -d' ' -f2"
batch_containers = re.findall('docker_containers_(cpu|mem|net)$', sys.argv[0])
single_container = re.findall('docker_containers_(.+)_(cpu|mem|net)$', sys.argv[0])
is_single_container = (bool)(single_container) and not (bool)(batch_containers)
if is_single_container:
container_name, field = single_container[0]
else:
field = batch_containers[0]
container_names = commands.getoutput(command_for_container_names_template.format(config['docker.spectator'])).split(
'\n')
if len(sys.argv) > 1 and sys.argv[1] == 'config':
print('graph_category docker_containers')
if field == 'cpu' or field == 'mem':
if is_single_container:
print('graph_title {0} usage for container {1}'.format(field, container_name))
print('{0}.label {0}'.format(field))
else:
print('graph_title {0} usage for containers'.format(field))
for container_name in container_names:
print('{0}.label {0}'.format(container_name))
print('graph_vlabel {0}'.format(field))
elif field == 'mem':
if is_single_container:
print('graph_title {0} usage for container {1}'.format(field, container_name))
print('{0}.label {0}'.format(field))
else:
print('graph_title {0} usage for containers'.format(field))
for container_name in container_names:
print('{0}.label {0}'.format(container_name))
print('graph_vlabel {0}'.format(field))
print('graph_args --base 1000')
elif field == 'net' and is_single_container:
print('graph_title Network usage for container {0}'.format(container_name))
print('graph_order down up')
print('graph_args --base 1000')
print('graph_vlabel bits in (-) / out (+) per ${graph_period}')
print('down.label received')
print('down.type COUNTER')
print('down.graph no')
print('down.cdef down,8,*')
print('up.label bps')
print('up.type COUNTER')
print('up.negative down')
print('up.cdef up,8,*')
else:
pass
sys.exit(0)
if field == 'cpu' or field == 'mem':
if is_single_container:
print('{1}.value {0}'.format(commands.getoutput(
command_for_average_template.format(config['docker.spectator'], field, container_name)
), field))
else:
for container_name in container_names:
print('{1}.value {0}'.format(commands.getoutput(
command_for_average_template.format(config['docker.spectator'], field, container_name)
), container_name))
elif field == 'net' and is_single_container:
down, up = (commands.getoutput(
command_for_average_template.format(config['docker.spectator'], field, container_name)
)).split()
print('down.value {0}'.format(down))
print('up.value {0}'.format(up))
else:
pass