我们可以通过Python提供的模块docker来取各个docker容器的监控参数。参考文档:http://docker-py.readthedocs.io/en/stable/containers.html
pip install docker #安装docker模块
通过脚本获取各个监控项的值:
#!coding=utf-8 #python2.7.12测试通过 #监控docker状态脚本,包括内存、cpu和网速 #UserParameter=docker_monitor[*],/usr/bin/python /usr/local/zabbix/scripts/docker_monitor.py $1 $2 import docker import sys def get_value(item): mem_rss_usage = docker_stat['memory_stats']['stats']['total_rss'] mem_cache_usage = docker_stat['memory_stats']['stats']['total_cache'] mem_limit = docker_stat['memory_stats']['limit'] cpu_total_usage = docker_stat['cpu_stats']['cpu_usage']['total_usage'] cpu_usermode_usage = docker_stat['cpu_stats']['cpu_usage']['usage_in_usermode'] cpu_kernelmode_usage = docker_stat['cpu_stats']['cpu_usage']['usage_in_kernelmode'] cpu_system_usage = docker_stat['cpu_stats']['system_cpu_usage'] percpu_usage = docker_stat['cpu_stats']['cpu_usage']['percpu_usage'] # 'cpu_percent': round(((float(cpu_total_usage)+float(cpu_usermode_usage)+float(cpu_kernelmode_usage))/float(cpu_system_usage)) * len(percpu_usage) * 1000.0, 2), 暂不确定 items = { 'mem_rss_usage': mem_rss_usage, 'mem_cache_usage': mem_cache_usage, 'mem_limit': mem_limit, 'cpu_total_usage': cpu_total_usage, 'cpu_system_usage': cpu_system_usage, 'mem_percent': round((float(mem_rss_usage)/float(mem_limit)) * 100.0, 2), 'net_in': docker_stat['networks']['eth0']['rx_bytes'], 'net_out': docker_stat['networks']['eth0']['tx_bytes'], 'status': int( docker.status == 'running') } print items.get(item) if __name__ == '__main__': conn = docker.from_env() docker = conn.containers.get(sys.argv[1]) docker_stat = docker.stats(stream=False,decode=True) try: get_value(sys.argv[2]) except KeyError: print 0
docker自动发现脚本:
#!/bin/bash
docker_amount() { port=($(sudo docker ps -a|grep -v "CONTAINER ID"|awk '{print $NF}')) printf '{ ' printf ' "data":[ ' for key in ${!port[@]} do if [[ "${#port[@]}" -gt 1 && "${key}" -ne "$((${#port[@]}-1))" ]];then printf ' { ' printf " "{#CONTAINERNAME}":"${port[${key}]}"}, " else [[ "${key}" -eq "((${#port[@]}-1))" ]] printf ' { ' printf " "{#CONTAINERNAME}":"${port[${key}]}"} " fi done printf ' ] ' printf '} ' } case $1 in docker) docker_amount ;; *) echo "Usage:`basename $0` {docker}" ;; esac
UserParameter=auto_discovery[*],/bin/bash /usr/local/zabbix/scripts/auto_discovery.sh $1
如果在服务端执行zabbix_get时报权限错误,在/etc/sudoers中添加:
zabbix ALL=(root) NOPASSWD: /usr/bin/docker
然后在监控页添加自动发现、监控项即可。
.
.
.
.
.
另可用简单的Load Modules的方式监控Docker(感谢万能的先行者....)
Git地址:https://github.com/monitoringartist/Zabbix-Docker-Monitoring
步骤如下:
1、在Git上下载相应版本的加载模块zabbix_module_docker.so和相应监控方式(主动和被动)监控模板Zabbix-Template-App-Docker.xml
2、将zabbix_module_docker.so放在${libdir}/modules下并修改配置文件:
LoadModulePath=/usr/local/zabbix/lib/modules
LoadModule=zabbix_module_docker.so
3、重启zabbix_agentd,并将Zabbix-Template-App-Docker.xml导入模板中,自动发现10分钟后取得数据
4、如果监控项报错:Cannot open memory.stat file,将zabbix agent中的用户改成root即可