最近,我发现zabbix没有监控linux的磁盘io,于是借助百度,我做了一个zabbix模板,用来监控磁盘io。
1、添加自动寻找磁盘脚本:
cat /home/monitor/disk_scan.sh
######################################
#!/bin/bash
#written by lenwood
diskarray=(`cat /proc/diskstats |grep -E "\bsd[abcdefg]\b|\bxvd[abcdefg]\b"|grep -i "\b$1\b"|awk '{print $3}'|sort|uniq 2>/dev/null`)
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"
###########################
注释:grep -E 里面的\b表示匹配空字符串的意思。
The Backslash Character and Special Expressions
The symbols \< and \> respectively match the empty string at the
beginning and end of a word. The symbol \b matches the empty string at
the edge of a word, and \B matches the empty string provided it’s not
at the edge of a word. The symbol \w is a synonym for [[:alnum:]] and
\W is a synonym for [^[:alnum:]].
2、后台执行iostat命令
nohup iostat -m -x -d 30 > /home/script/log/iostat_output &
-c 仅显示CPU统计信息.与-d选项互斥.
-d 仅显示磁盘统计信息.与-c选项互斥.
-k 以K为单位显示每秒的磁盘请求数,默认单位块.
-p device | ALL
与-x选项互斥,用于显示块设备及系统分区的统计信息.也可以在-p后指定一个设备名,如:
# iostat -p hda
或显示所有设备
# iostat -p ALL
-t 在输出数据时,打印搜集数据的时间.
-V 打印版本号和帮助信息.
-x 输出扩展信息.
-m 以兆为单位
3、为了下次开启也生效,把上述的nohup iostat命令加入/etc/rc.local里面
4、在zabbix_agentd.conf中加入如下UserParameters:
############################
#通过iostat -m -x -d 30 获得的磁盘io信息
UserParameter=io.scandisk[*],/home/script/disk_scan.sh $1
UserParameter=io.rps[*],/usr/bin/tail /home/script/log/iostat_output |grep "\b$1\b"|tail -1|awk '{print $$4}'
UserParameter=io.wps[*],/usr/bin/tail /home/script/log/iostat_output |grep "\b$1\b" |tail -1|awk '{print $$5}'
UserParameter=io.rMBps[*],/usr/bin/tail /home/script/log/iostat_output |grep "\b$1\b" |tail -1|awk '{print $$6}'
UserParameter=io.wMBps[*],/usr/bin/tail /home/script/log/iostat_output |grep "\b$1\b" |tail -1|awk '{print $$7}'
UserParameter=io.avgrq-sz[*],/usr/bin/tail /home/script/log/iostat_output |grep "\b$1\b" |tail -1|awk '{print $$8}'
UserParameter=io.avgqu-sz[*],/usr/bin/tail /home/script/log/iostat_output |grep "\b$1\b" |tail -1|awk '{print $$9}'
UserParameter=io.await[*],/usr/bin/tail /home/script/log/iostat_output |grep "\b$1\b" |tail -1|awk '{print $$10}'
UserParameter=io.svctm[*],/usr/bin/tail /home/script/log/iostat_output |grep "\b$1\b" |tail -1|awk '{print $$11}'
UserParameter=io.util[*],/usr/bin/tail /home/script/log/iostat_output |grep "\b$1\b" |tail -1|awk '{print $$12}'
#通过cat /proc/diskstats获得的磁盘io信息
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}'
UserParameter=custom.vfs.dev.read.sectors[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$6}'
UserParameter=custom.vfs.dev.write.sectors[*],cat /proc/diskstats | grep $1 | head -1 | awk '{print $$10}'
##################
5、重启zabbix agent
/etc/init.d/zabbix_agentd restart
6、确认zabbix agent是否启动
[ ps -elf |grep zabbix
7、导入我弄的模板:linux磁盘io模板-czxin
8、关联你要监控的主机
####################
参考资料:
http://www.tuicool.com/articles/eqERru
http://blog.chinaunix.net/uid-26446098-id-4964263.html
附件列表
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/28916011/viewspace-1848833/,如需转载,请注明出处,否则将追究法律责任。