-
Notifications
You must be signed in to change notification settings - Fork 27
/
zabbix-docker-inspect.py
executable file
·66 lines (53 loc) · 1.64 KB
/
zabbix-docker-inspect.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
#!/usr/bin/python
#################################################################
#
# zabbix-docker-inspect.py
#
# A program that parses the "docker inspect" values for
# reporting data to Zabbix.
#
# Version: 1.0
#
# Author: Richard Sedlak
#
#################################################################
import sys
import os
import time
import json
import re
#################################################################
# sys.argv[1] - the instanceID of the docker container
# sys.argv[2] - the JSON value of the key to collect
#################################################################
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").read()
return strings
cmd="docker inspect " + sys.argv[1]
strings = local_run_command(cmd,"/tmp/zabbix-docker-inspect-"+sys.argv[1]+".out")
parsed_json = json.loads(strings)
key_path = sys.argv[2].split('.')
ptr = parsed_json[0]
for i in range(0,len(key_path)):
ptr=ptr[key_path[i]]
# make sure passwords are hidden in zabbix items
if sys.argv[2] == 'Config.Env':
pwd_pattern = re.compile(r'^(?P<KEY>.*(PASSWORD|PWD).*)=(?P<VALUE>.+)$')
p_out = []
for i in ptr:
match = pwd_pattern.match(i)
if match:
p_out.append(match.group('KEY') + "=" + 'x' * len(match.group('VALUE')))
else:
p_out.append(i)
ptr = p_out
print ptr