1、创建目录
[[email protected] script]# mkdir /usr/local/zabbix/etc/script
2、编辑容器发现脚本
[[email protected] script]# vim docker_discovery.py
#!/usr/bin/env python
import os
import simplejson as json
t=os.popen("""sudo docker ps |grep -v ‘CONTAINER ID‘|awk {‘print $NF‘} """)
container_name = []
for container in t.readlines():
r = os.path.basename(container.strip())
container_name += [{‘{#CONTAINERNAME}‘:r}]
print json.dumps({‘data‘:container_name},sort_keys=True,indent=4,separators=(‘,‘,‘:‘))
3、安装环境并测试自动发现
pip install docker
pip install simplejson
chmod 757 docker_discovery.py
chown zabbix:zabbix /usr/local/zabbix/etc/script -R
赋予zabbix权限,编辑/etc/sudoers,添加如下内容
zabbix ALL=(root) NOPASSWD:/usr/bin/docker,/usr/bin/python,/usr/local/zabbix/etc/script/docker_discovery.py
配置文件添加配置并重启:
vim /usr/local/zabbix/etc/zabbix_agentd.conf
UserParameter=docker_discovery,sudo /usr/bin/python /usr/local/zabbix/etc/script/docker_discovery.py
测试:
zabbix_get -s 192.168.199.133 -k docker_discovery
4、添加容器监控测试
[[email protected] script]# vim docker_monitor.py
#!/usr/bin/env python
import docker
import sys
import subprocess
import os
def check_container_stats(container_name,collect_item):
#docker_client = docker_client.containers.get(container_name)
container_collect=docker_client.containers.get(container_name).stats(stream=True)
old_result=eval(container_collect.next())
new_result=eval(container_collect.next())
container_collect.close()
if collect_item == ‘cpu_total_usage‘:
result=new_result[‘cpu_stats‘][‘cpu_usage‘][‘total_usage‘] - old_result[‘cpu_stats‘][‘cpu_usage‘][‘total_usage‘]
elif collect_item == ‘cpu_system_usage‘:
result=new_result[‘cpu_stats‘][‘system_cpu_usage‘] - old_result[‘cpu_stats‘][‘system_cpu_usage‘]
elif collect_item == ‘cpu_percent‘:
cpu_total_usage=new_result[‘cpu_stats‘][‘cpu_usage‘][‘total_usage‘] - old_result[‘cpu_stats‘][‘cpu_usage‘][‘total_usage‘]
cpu_system_uasge=new_result[‘cpu_stats‘][‘system_cpu_usage‘] - old_result[‘cpu_stats‘][‘system_cpu_usage‘]
cpu_num=len(old_result[‘cpu_stats‘][‘cpu_usage‘][‘percpu_usage‘])
result=round((float(cpu_total_usage)/float(cpu_system_uasge))*cpu_num*100.0,2)
elif collect_item == ‘mem_usage‘:
result=new_result[‘memory_stats‘][‘usage‘]
elif collect_item == ‘mem_limit‘:
result=new_result[‘memory_stats‘][‘limit‘]
elif collect_item == ‘network_rx_bytes‘:
result=new_result[‘networks‘][‘eth0‘][‘rx_bytes‘]
elif collect_item == ‘network_tx_bytes‘:
result=new_result[‘networks‘][‘eth0‘][‘tx_bytes‘]
elif collect_item == ‘mem_percent‘:
mem_usage=new_result[‘memory_stats‘][‘usage‘]
mem_limit=new_result[‘memory_stats‘][‘limit‘]
result=round(float(mem_usage)/float(mem_limit)*100.0,2)
return result
if __name__ == "__main__":
docker_client = docker.DockerClient(base_url=‘unix://var/run/docker.sock‘, version=‘1.27‘)
container_name=sys.argv[1]
collect_item=sys.argv[2]
print check_container_stats(container_name,collect_item)
[[email protected] script]# chmod 757 docker_monitor.py
编辑配置文件并重启:vim /usr/local/zabbix/etc/zabbix_agentd.conf
UserParameter=docker_status[*],sudo /usr/bin/python /usr/local/zabbix/etc/script/docker_monitor.py $1 $2
测试:zabbix_get -s 192.168.199.133 -k docker_status[mysql-sonar,cpu_total_usage]
5、web端制作模板
5.1、配置 -》模板 -》创建模板 (cpu_percent mem_percent mem_usage mem_limit)
Template name: Template Docker Auto Discovery
5.2、点击创建
5.3、找到创建的模板进去:点击自动发现
自动发现规则:
Name:collect docker container use resource
Type:Zabbix agent
Key:docker_discovery
过滤器:
{#CONTAINERNAME}
5.4、创建监控项原型
Name:container:{#CONTAINERNAME}:cpu_percent
Type:Zabbix agent
Key:docker_status[{#CONTAINERNAME},cpu_percent]
Type:Numeric (float)
Units:%
Name:container:{#CONTAINERNAME}:mem_percent
Type:Zabbix agent
Key:docker_status[{#CONTAINERNAME},mem_percent]
Type:Numeric (float)
Units:%
Name:container:{#CONTAINERNAME}:mem_usage
Type:Zabbix agent
Key:docker_status[{#CONTAINERNAME},mem_usage]
Type:数字(无正负)
Units:空
Name:container:{#CONTAINERNAME}:mem_limit
Type:Zabbix agent
Key:docker_status[{#CONTAINERNAME},mem_limit]
Type:数字(无正负)
Units:空
5.5、添加触发器
5.5、添加图形原型
Name:container:{#CONTAINERNAME}:cpu和内存使用率
监控项:
6、安装zabbix客户端
这里采用编译安装:
添加用户
#groupadd zabbix
#useradd -g zabbix -m zabbix
#./configure --prefix=/usr/local/zabbix --enable-agent
#make install
#vi /usr/local/zabbix/etc/zabbix_agentd.conf
Server= ip.ip.ip.ip #服务端ip地址
ServerActive= ip.ip.ip.ip #服务端ip地址
Hostname=client1 #必须与创建主机时的hostname一致
#cp zabbix-3.0.4/misc/init.d/tru64/zabbix_agentd /etc/init.d/
#chmod +x /etc/init.d/zabbix_ agentd
#ln -s /usr/local/zabbix/sbin/* /usr/local/sbin/
#ln -s /usr/local/zabbix/bin/* /usr/local/bin/
#vi /etc/rc.d/init.d/zabbix_ agentd
#在第二行添加如下内容
#chkconfig: 2345 10 90
#description: zabbix agent
保存后退出文件
#chkconfig --add zabbix_agentd
#chkconfig zabbix_agentd on
#service restart zabbix_ agentd
原文地址:http://blog.51cto.com/11962757/2121538