zabbix--高级篇-监控docker服务(一)

一,配置zabbix 客户端环境

rpm -ivh https://mirrors.aliyun.com/zabbix/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
yum install zabbix-agent -y
vim /etc/zabbix/zabbix_agentd.conf
##需要改变的内容
PidFile=/var/run/zabbix/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
LogFileSize=0
Server=192.168.0.214
ServerActive=192.168.0.214
Hostname=k8s1
HostMetadataItem=system.uname
Include=/etc/zabbix/zabbix_agentd.d/*.conf

systemctl restart zabbix-agent.service

二,配置脚本

  

[root@k8s1 alertscripts]# cat docker.py
#!/usr/bin/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()
UserParameter=docker.discovery,/usr/lib/zabbix/alertscripts/docker.py
UserParameter=docker.[*],/usr/lib/zabbix/alertscripts/docker.py $1 $2

https://www.cnblogs.com/binglansky/p/9132714.html

 

 

 

posted on 2019-03-19 18:01  kingle-l  阅读(1486)  评论(0编辑  收藏  举报

levels of contents