-
Notifications
You must be signed in to change notification settings - Fork 27
/
zabbix-docker-stats.py
executable file
·111 lines (88 loc) · 1.98 KB
/
zabbix-docker-stats.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
#!/usr/bin/python
#################################################################
#
# zabbix-docker-stats.py
#
# A program that produces information for Zabbix to
# process Docker container statistics.
#
# Version: 1.0
#
# Author: Richard Sedlak
#
#################################################################
import sys
import os
import time
import re
def B(b):
return int(float(b))
def KB(b):
return int(float(b) * 1024)
def MB(b):
return int(float(b) * 1024 * 1024)
def GB(b):
return int(float(b) * 1024 * 1024 * 1024)
def TB(b):
return int(float(b) * 1024 * 1024 * 1024 * 1024)
def PCT(b):
return float(b)
size_options = {
'k':KB,
'K':KB,
'm':MB,
'M':MB,
'g':GB,
'G':GB,
't':TB,
'T':TB,
'b':B,
'B':B,
'%':PCT
}
def recalc(data):
pat = re.compile(r'([0-9.]+)([a-zA-Z%])')
mo = pat.match(data)
if mo:
number, unit = mo.group(1,2)
value = size_options[unit](number)
else:
value = False
return value
def pcpu(data):
return recalc(data[2])
def umem(data):
return recalc(data[3])
def lmem(data):
return recalc(data[5])
def pmem(data):
return recalc(data[6])
def inet(data):
return recalc(data[7])
def onet(data):
return recalc(data[9])
options = {
'pcpu':pcpu,
'umem':umem,
'lmem':lmem,
'pmem':pmem,
'inet':inet,
'onet':onet
}
def local_run_command(cmd,file):
cmd = cmd + " | tee > " + file
if os.path.isfile(file) == False:
os.system(cmd)
else:
(mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime) = os.stat(file)
ticks=int(time.time())
delta=ticks-mtime
if (delta > 60):
os.system(cmd)
strings = open(file,"r").readlines()
return strings[1].split()
container=sys.argv[1]
key=sys.argv[2]
cmd="docker stats --no-stream=true " + container
strings = local_run_command(cmd,"/tmp/zabbix-docker-stats-"+container+".out")
print options[key](strings)