版权声明:本文为木偶人shaon原创文章,转载请注明原文地址,非常感谢。 https://cloud.tencent.com/developer/article/1434800
Zabbix-3.0监测服务器硬盘IO状态
一、环境准备
被监控主机已安装zabbix-agent,且zabbix-server能正常监控到zabbix-agent相关数据。
系统环境:
Zabbix-server:192.168.1.142
Zabbix-agent:192.168.1.156
Zabbix-agent安装目录在/opt/zabbix
二、解决思路
1、先在zabbix-agent上面通过zabbix的low-discovery功能自动发现服务器硬盘的名称。
2、通过脚本,利用iostat收集服务器硬盘的相关数据,保存到指定目录,并创建crontab,定时收集服务器硬盘IO数据,便于zabbix-web端绘图。
3、修改zabbix相关配置文件,使zabbix-agent能通过自定义的low-discovery获取硬盘的名称,同时zabbix-server能通过zabbix-get程序去zabbix-agent上面收集iostat脚本采集的zabbix-agent的硬盘IO数据。
4、到zabbix-web上面创建硬盘IO检测模板,并观察绘图数据,创建触发器。
三、详细步骤
1、Zabbix-agent端:
root@mysql-slave ~# cd /opt/zabbix/
root@mysql-slave zabbix# mkdir scripts #存放diskname_discovery脚本目录
root@mysql-slave zabbix# mkdir cron #存放采集硬盘IO数据的脚本目录
root@mysql-slave zabbix# mkdir data #存放脚本采集的硬盘IO数据目录
注:data目录在脚本中会自动创建,此处可以不手动创建。
ll查看zabbix目录下面都有哪些子目录。
root@mysql-slave zabbix# cd scripts/
root@mysql-slave scripts# vim diskname_discovery.sh #获取硬盘名称
root@mysql-slave scripts# bash diskname_discovery.sh
{
"data":[
{"{#DISK\_NAME}":"xvda"}
]
}
#执行脚本已经能够获取硬盘名称,便于zabbix中进行宏调用。
#下面贴出脚本具体内容:
#!/bin/bash
diskarray=($(grep '\ba-za-z+\b' /proc/diskstats|awk '{print $3}'))
length=${#diskarray@}
printf "{\n"
printf '\t'"\"data\":["
for ((i=0;i<$length;i++))
do
printf '\n\t\t{'
printf "\"{#DISK\_NAME}\":\"${diskarray[$i]}\"}"
if [ $i -lt $[$length-1] ];then
printf ','
fi
done
printf "\n\t]\n"
printf "}\n"
2、创建收集硬盘IO数据脚本
root@mysql-slave scripts# cd ../cron/
root@mysql-slave cron# vim iostat_cron.sh
下面贴出脚本具体信息:
#!/bin/bash
##################################
#auther: shaonbean
##################################
##################################
iostat_bin="/usr/bin/iostat"
frequency="10 2"
zabbix_base_dir=/opt/zabbix
-d $zabbix_base_dir/data || mkdir -p $zabbix_base_dir/data
dest_data=$zabbix_base_dir/data/iostat_data
tmp_data=$zabbix_base_dir/data/iostat_data.tmp
#script_conf=$zabbix_base_dir/conf/iostat_check.conf #iostat_check.conf为自定义的iostat 检测的频率,可自定义,此处可注释。
# -e "$script_conf" && source $script_conf
$iostat_bin -dxm $frequency > $tmp_data
mv $tmp_data $dest_data
3、把刚刚创建的脚本加到crontab中。
root@mysql-slave conf# crontab -e
*/1 * * * * /bin/bash /opt/zabbix/cron/iostat_cron.sh
4、修改zabbix-agnet的相关配置文件
root@mysql-slave conf# cd /opt/zabbix/etc/
root@mysql-slave etc# egrep -v "^#|^$" zabbix_agentd.conf
PidFile=/opt/zabbix/pid/zabbix_agentd.pid
LogFile=/var/log/zabbix/zabbix_agentd.log
Server=127.0.0.1,192.168.1.142 #被动模式,server为zabbix-server ip
ServerActive=192.168.1.142:10051 #主动模式
Hostname=mysql-slave #使用主动模式收集数据并发送到zabbix-server时开启,被动模式不需要这个参数,注释掉。
Timeout=5
Include=/opt/zabbix/etc/zabbix_agentd.conf.d/ #存放zabbix-server需要使用的配置文件目录
UnsafeUserParameters=1 #开启之后,可以使用自定义的脚本收集zabbix-agent的数据。
#UnsafeUserParameters=0
5、新建自定义的配置文件
root@mysql-slave etc# cd /opt/zabbix/etc/zabbix_agentd.conf.d/
下面贴出配置文件内容:
#diskname discovery
UserParameter=disk.name.discovery*,/opt/zabbix/scripts/diskname_discovery.sh $1
#gather iostat value
UserParameter=io.rrqmps*,/usr/bin/tail /opt/zabbix/data/iostat_data |grep "\b$1\b"|tail -1|awk '{print $$3}'
UserParameter=io.wrqmps*,/usr/bin/tail /opt/zabbix/data/iostat_data |grep "\b$1\b"|tail -1|awk '{print $$3}'
UserParameter=io.rps*,/usr/bin/tail /opt/zabbix/data/iostat_data |grep "\b$1\b"|tail -1|awk '{print $$4}'
UserParameter=io.wps*,/usr/bin/tail /opt/zabbix/data/iostat_data |grep "\b$1\b" |tail -1|awk '{print $$5}'
UserParameter=io.rMBps*,/usr/bin/tail /opt/zabbix/data/iostat_data |grep "\b$1\b" |tail -1|awk '{print $$6}'
UserParameter=io.wMBps*,/usr/bin/tail /opt/zabbix/data/iostat_data |grep "\b$1\b" |tail -1|awk '{print $$7}'
UserParameter=io.avgrq-sz*,/usr/bin/tail /opt/zabbix/data/iostat_data |grep "\b$1\b" |tail -1|awk '{print $$8}'
UserParameter=io.avgqu-sz*,/usr/bin/tail /opt/zabbix/data/iostat_data |grep "\b$1\b" |tail -1|awk '{print $$9}'
UserParameter=io.await*,/usr/bin/tail /opt/zabbix/data/iostat_data |grep "\b$1\b" |tail -1|awk '{print $$10}'
UserParameter=io.svctm*,/usr/bin/tail /opt/zabbix/data/iostat_data |grep "\b$1\b" |tail -1|awk '{print $$11}'
UserParameter=io.util*,/usr/bin/tail /opt/zabbix/data/iostat_data |grep "\b$1\b" |tail -1|awk '{print $$12}'
################
#rrqm/S: merge number of read operations per second. Delta(rmerge)/s
#wrqm/S: per second operation number and write merge. Delta(wmerge)/s
#r/Read the I/O device s: completion times per second. Delta(rio)/s
#w/S: finished writing I/O times per second. Delta(wio)/s
#rsec/S: read the number of sectors per second. Delta(rsect)/s
#wsec/S: write the number of sectors per second. Delta(wsect)/s
#rkB/S: read K bytes per second. Is rsect/Half of the s, because each sector size of 512 bytes. (calculated)
#wkB/S: write K bytes per second. Is wsect/Half s. (calculated)
#avgrqAverage -sz: per I/O device data size (sector). delta(rsect+wsect)/delta(rio+wio)
#avgqu-sz: I/O average queue length. Delta(aveq)/s/1000 (because the aveq milliseconds).
#Average await: per device I/The waiting time of O operation (MS). Delta(ruse+wuse)/delta(rio+wio)
#Average svctm: per device I/The O operation business hours (MS). Delta(use)/delta(rio+wio)
6、本地测试相关功能
root@mysql-slave etc# cd /opt/zabbix/bin/
root@mysql-slave bin# ./zabbix_get -s 127.0.0.1 -p 10050 -k disk.name.discovery
下面能看到我们自定义的key已经能够使用
然后根据/opt/zabbix/etc/zabbix_agentd.conf.d目录下面的配置文件测试具体key的使用:
root@mysql-slave bin# ./zabbix_get -s 127.0.0.1 -p 10050 -k io.utilxvda
可以看到,通过自定义的key可以正常取值。
分步取值:
查看脚本收集到的硬盘IO状态详细数据,如下图:
然后通过下面命令获取具体硬盘IO各项参数的数值:
root@mysql-slave ~# /usr/bin/tail /opt/zabbix/data/iostat_data | grep "\bxvda\b" | tail -1 | awk '{print $12}'
这里取得是util的数据,$1代表自动发现的硬盘名称。其他参数取值参考util即可。
Zabbix-server:服务端
/opt/zabbix/bin/zabbix_get -s 192.168.1.156 -p 10050 -k io.utilxvda
能够看出在zabbix-server已经能够通过自定义的key及发现函数,实现zabbix-agent硬盘IO状态的数据采集,
四、zabbix-web前端设置
创建硬盘IO状态检测模板,自定义,不多说
下一步,创建发现规则:
创建完成,然后添加监控项以及触发器,并创建相关图形,便于查看,
添加完成如图:
设置触发器;
添加图形显示;
查看硬盘IO的状态变化图:
下面导出检测模板,可以根据不同的需求自定义相关参数。
由于模板格式问题,所以相关模板及脚本到