Zabbix监控Docker容器配置
#zabbix #Docker
三,监控Docker容器
用python脚本监控容器状态 vi docker_status.py
#!/usr/bin/env python
import sys
import os
import json
def discover():
d = {}
d['data'] = []
with os.popen("docker ps -a --format {{.Names}}") as pipe:
for line in pipe:
info = {}
info['{#CONTAINERNAME}'] = line.replace("\n","")
d['data'].append(info)
print json.dumps(d)
def status(name,action):
if action == "ping":
cmd = 'docker inspect --format="{{.State.Running}}" %s' %name
result = os.popen(cmd).read().replace("\n","")
if result == "true":
print 1
else:
print 0
else:
cmd = 'docker stats %s --no-stream --format "{{.%s}}"' % (name,action)
result = os.popen(cmd).read().replace("\n","")
if "%" in result:
print float(result.replace("%",""))
else:
print result
if __name__ == '__main__':
try:
name, action = sys.argv[1], sys.argv[2]
status(name,action)
except IndexError:
discover()vi docker_status.conf
UserParameter=docker_status[*],sudo python /etc/zabbix/zabbix_agentd.d/docker_status.py $1 $2模版文件:将以下代码保存为 .xml文件,导入zabbix模版即可
最后更新于