标签:lis awk .so version shel adf ssm 获取 monit
1、从https://pypi.python.org/pypi/docker-py#downloads 这里下载docker-py-xxx.tar.gz到本地再传进去linux系统里面进行解压2、在agentd那里加入自定义模块
UserParameter=docker_low_discovery[],/bin/bash /etc/zabbix/scripts/docker_low_discovery.sh $1
UserParameter=docker_stats[],sudo /usr/bin/python /etc/zabbix/scripts/docker_monitor.py $1 $2
UserParameter=docker_process[],/bin/bash /etc/zabbix/scripts/docker_processmonitor.sh $1 $2 $3
UserParameter=custom.vfs.dev.read.ops[],cat /proc/diskstats | grep $1 | head -1 | awk ‘{print $$4}‘ #磁盘读的次数
UserParameter=custom.vfs.dev.read.ms[],cat /proc/diskstats | grep $1 | head -1 | awk ‘{print $$7}‘ #磁盘读的毫秒数
UserParameter=custom.vfs.dev.write.ops[],cat /proc/diskstats | grep $1 | head -1 | awk ‘{print $$8}‘ #磁盘写的次数
UserParameter=custom.vfs.dev.write.ms[],cat /proc/diskstats | grep $1 | head -1 | awk ‘{print $$11}‘ #磁盘写的毫秒数
UserParameter=custom.vfs.dev.io.active[],cat /proc/diskstats | grep $1 | head -1 | awk ‘{print $$12}‘
UserParameter=custom.vfs.dev.io.ms[],cat /proc/diskstats | grep $1 | head -1 | awk ‘{print $$13}‘ #花费在IO操作上的毫秒数
UserParameter=custom.vfs.dev.read.sectors[],cat /proc/diskstats | grep $1 | head -1 | awk ‘{print $$6}‘ #读扇区的次数(一个扇区的等于512B)
UserParameter=custom.vfs.dev.write.sectors[*],cat /proc/diskstats | grep $1 | head -1 | awk ‘{print $$10}‘ #写扇区的次数(一个扇区的等于512B)
3、把两个shell脚本和一个python脚本放进去zabbix-agent的目录下
docker_monitor.py
[root@spark scripts]# cat docker_monitor.py
#!/usr/bin/env python
#-- coding: utf-8 --
from docker import Client
import sys
import subprocess
import os
global null
null=‘‘
def check_container_stats(container_name,collect_item):
container_collect=docker_client.stats(container_name)
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_num100.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 == ‘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)
elif collect_item == ‘network_rx_bytes‘:
network_check_command="""sudo /usr/bin/docker exec %s cat /proc/net/dev|sed -n 3p|awk ‘{print $2,$10}‘"""%container_name
result=os.popen(network_check_command).read().split()[0]
elif collect_item == ‘network_tx_bytes‘:
network_check_command="""sudo /usr/bin/docker exec %s cat /proc/net/dev|sed -n 3p|awk ‘{print $2,$10}‘"""%container_name
result=os.popen(network_check_command).read().split()[1]
return result
if name == "main":
docker_client = Client(base_url=‘unix://var/run/docker.sock‘, version=‘1.17‘)
container_name=sys.argv[1]
collect_item=sys.argv[2]
print check_container_stats(container_name,collect_item)
docker_low_discovery.sh
[root@spark scripts]# cat docker_low_discovery.sh
#!/bin/bash
#Fucation:docker low-level discovery
docker() {
port=($(sudo docker ps -a|grep -v "CONTAINER ID"|awk ‘{print $NF}‘))
printf ‘{\n‘
printf ‘\t"data":[\n‘
for key in ${!port[@]}
do
if [[ "${#port[@]}" -gt 1 && "${key}" -ne "$((${#port[@]}-1))" ]];then
printf ‘\t {\n‘
printf "\t\t\t\"{#CONTAINERNAME}\":\"${port[${key}]}\"},\n"
else [[ "${key}" -eq "((${#port[@]}-1))" ]]
printf ‘\t {\n‘
printf "\t\t\t\"{#CONTAINERNAME}\":\"${port[${key}]}\"}\n"
fi
done
printf ‘\t ]\n‘
printf ‘}\n‘
}
case $1 in
docker)
docker
;;
*)
echo "Usage:basename $0
{docker}"
;;
esac
docker_processmonitor.sh
[root@spark scripts]# cat docker_processmonitor.sh
#!/bin/bash
#license:GPL
processmem(){
sudo /usr/bin/docker exec $1 ps aux|grep $2|grep -v "grep"|grep -v "processstatus.sh"|awk ‘{sum+=$6}; END{print sum}‘
}
processcpu(){
sudo /usr/bin/docker exec $1 ps aux|grep $2|grep -v "grep"|grep -v "processstatus.sh"|awk ‘{sum+=$3}; END{print sum}‘
}
processport(){
sudo /usr/bin/docker exec $1 ss -antlp|grep $2|grep LISTEN|wc -l
}
case "$3" in
mem)
processmem $1 $2
;;
cpu)
processcpu $1 $2
;;
port)
processport $1 $2
;;
*)
echo "Usage: $0 {docker_containername}{processname}{mem|cpu|port}"
;;
esac
3、然后可以在zabbix_server端测试是否能够获取到相应的数据:
[root@mha ~]# zabbix_get -s 192.168.198.131 -p 10050 -k "docker_stats[docker_signage_1,network_tx_bytes]"
343128
[root@mha ~]# zabbix_get -s 192.168.198.131 -p 10050 -k "docker_stats[docker_signage_1,network_rx_bytes]"
10617111
[root@mha ~]# zabbix_get -s 192.168.198.131 -p 10050 -k "docker_stats[docker_signage_1,network_rx_bytes]"
9716403
[root@mha ~]# zabbix_get -s 192.168.198.131 -p 10050 -k "docker_stats[docker_signage_1,network_tx_bytes]"
137637
[root@mha ~]# zabbix_get -s 192.168.198.131 -p 10050 -k "docker_low_discovery[docker]"
{
"data":[
{
"{#CONTAINERNAME}":"docker_signage_1"}
]
}
4、在zabbix导入模板
http://down.51cto.com/data/2366161 从这里下载到windows本地,然后在web页面导入后,很快就加入自动发现了
![](http://i2.51cto.com/images/blog/201802/02/c603b4adfee2f69df0ac72b745268637.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
![](http://i2.51cto.com/images/blog/201802/02/82823f97ab270c836a2bdbd12e6cbe2c.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
5、磁盘IO
然后添加grahs(图形)和添加对应的自定义监控项
![](http://i2.51cto.com/images/blog/201802/02/a060481a23f919d034e27151406fe4fc.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
标签:lis awk .so version shel adf ssm 获取 monit
原文地址:http://blog.51cto.com/wsw26/2068179