zabbix监控容器情况和磁盘IO

1、从https://pypi.python.org/pypi/docker-py#downloads 这里下载docker-py-xxx.tar.gz到本地再传进去linux系统里面进行解压

python setup.py install

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
[[email protected] 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
[[email protected] 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
[[email protected] 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端测试是否能够获取到相应的数据:
[[email protected] ~]# zabbix_get -s 192.168.198.131 -p 10050 -k "docker_stats[docker_signage_1,network_tx_bytes]"
343128
[[email protected] ~]# zabbix_get -s 192.168.198.131 -p 10050 -k "docker_stats[docker_signage_1,network_rx_bytes]"
10617111
[[email protected] ~]# zabbix_get -s 192.168.198.131 -p 10050 -k "docker_stats[docker_signage_1,network_rx_bytes]"
9716403
[[email protected] ~]# zabbix_get -s 192.168.198.131 -p 10050 -k "docker_stats[docker_signage_1,network_tx_bytes]"
137637
[[email protected] ~]# 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=)

原文地址:http://blog.51cto.com/wsw26/2068179

时间: 2024-10-09 12:26:53

zabbix监控容器情况和磁盘IO的相关文章

zabbix自动发现规则之磁盘IO监控

本文章介绍zabbix自动发现规则之磁盘IO监控的案例实践. 主机名:mysql-slave 首先在zabbix_agent下定义一个获取磁盘IO信息的脚本: cat /etc/zabbix/scripts/disk_io.sh #!/bin/bash Disk=$1 Option=$2 case $Option in rrqm) iostat -dxk 1 2|grep "\b$Disk\b" |tail -1|awk '{print $2}' ;; wrqm) iostat -dx

cacti监控下添加对磁盘io的监控方法(Linux主机和Windows主机)

添加对磁盘io的监控方法 一.Linux下 1.下载snmpdiskio-0.9.6 将snmpdiskio 放到 /usr/local/bin/snmpdiskio chmod +x /usr/local/bin/snmpdiskio 2.修改snmpd.conf access  notConfigGroup ""      any       noauth    exact  all none none view all    included  .1               

zabbix监控linux服务器的磁盘I/O

基本原理:通过分析/proc/diskstats文件,来对IO的性能进行监控.解释如下: +++++++++++++++++++++++++++对/proc/diskstats的解释++++++++++++++++++++++++++++++++++++++++++++ [[email protected] bin]# cat /proc/diskstats | grep sda | head -1 8 0 sda 73840 10263 3178156 91219 1110085 419256

cacti 监控磁盘IO需要注意的地方

监控linux机器的磁盘IO,使用cacti官方社区模板diskio http://docs.cacti.net/usertemplate:data:host_mib:diskio 具体用法不讲,有一个bug,监控64位linux机器时,数据不准确,原因是diskio.xml文件中统一定义了counter32的数据 <interface>         <name>Get Disk IO Information</name>         <index_ord

zabbix监控磁盘IO

监控磁盘IO的基本原理:通过分析/proc/diskstats文件,来对IO的性能进行监控. ++++++++++++++++++++++++对/proc/diskstats的解释++++++++++++++++++++++++++++++ # cat /proc/diskstats |grep sda |head -1    8    0     sda 2280 1445 76478 11956 249 188 3490 932 0 9913 12805 解释说明: 第一至第三个域,分别是主

zabbix应用之Low-level discovery监控磁盘IO

zabbix自带的"Template OS Linux"模板支持监控已挂载的磁盘空间利用率,是利用LLD(Low-level discovery)实现的,却没有对磁盘IO监控.本篇文章就介绍利用zabbix LLD功能监控磁盘IO的方法. 思路:首先创建Discovery rules,在agent端配置对应的UserParameter,调用编写好的shell脚本,动态查找出已经挂载的磁盘分区:然后创建Item prototypes,使用vfs.dev.read[device,<t

第十篇、【Zabbix监控项之自定义监控磁盘IO】

一.自定义监控项获取磁盘IO所需的值 1.在zabbix Agent端配置文件配置如下内容 [[email protected]_3WYY_WEB ~]# vi /usr/local/zabbix-4.4.3/etc/zabbix_agentd.conf 305 ### Option: UserParameter 306 # User-defined parameter to monitor. There can be several user-defined parameters. 307 #

zabbix监控磁盘IO(二)

1.磁盘发现脚本 vim disk_io.sh  #!/bin/bash  diskarray=(`cat /proc/diskstats |grep -E "\bsd[a-z]\b|\bxvd[a-z]\b|\bvd[a-z]\b"|awk '{print $3}'|sort|uniq   2>/dev/null`)    length=${#diskarray[@]} printf "{\n" printf  '\t'"\"data\&

zabbix监控磁盘 IO

磁盘IO:  单位时间内读写数据量  单位时间内读写次数 取值脚本: #!/use/bin/env python #coding:utf-8 # Description:for zabbix discovery disk partition on linux machine import os import re,json cmd = """cat /proc/diskstats |grep -E '\ssd[a-z]\s|\sxvd[a-z]\s|\svd[a-z]\s'|