IO监控

1、磁盘状态:cat /proc/diskstats

server ui 配置

导入模板

添加自动发现规则(如果没有则创建)

agent 配置

配置文件

vim /etc/zabbix/zabbix_agentd.conf.d/userparameter_iostat.conf

UserParameter=custom.vfs.dev.discovery,/etc/zabbix/zabbix_scripts/dev-discovery.sh
UserParameter=iostat[*],/etc/zabbix/zabbix_scripts/iostat-chk.sh $1 $2
UserParameter=disk.status[*],/bin/bash /etc/zabbix/zabbix_scripts/check_disk.sh -d $1 -o $2

脚本

cat /etc/zabbix/zabbix_scripts/dev-discovery.sh

#!/bin/bash

DEVICES=`iostat | awk '{ if ($1 ~ "^([shxv]|xv)d[a-z]$") { print $1 } }'`

COUNT=`echo "$DEVICES" | wc -l`
INDEX=0
echo '{"data":['
echo "$DEVICES" | while read LINE; do
    echo -n '{"{#DEVNAME}":"'$LINE'"}'
    INDEX=`expr $INDEX + 1`
    if [ $INDEX -lt $COUNT ]; then
        echo ','
    fi
done
echo ']}'

#!/bin/bash

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"

cat /etc/zabbix/zabbix_scripts/check_disk.sh

#!/bin/sh 
# 80 sda 76880282531 8117316 937263519846 4237854729 62989000438 35668103347 777694636224 1190396494 3 2895102137 899093508 
while getopts "d:o:" opt 
do 
case $opt in 
d ) disk=$OPTARG;; 
o ) option=$OPTARG;; 
? ) 
echo 'parameter is wrong!' 
exit 1;; 
esac 
done 
if [ ! "${disk}" ] || [ ! "${option}" ];then 
echo "parameter is null" 
exit 1 
fi 
if [[ ${option} == "read" ]];then 
cat /proc/diskstats |grep "${disk} "|awk '{print $6}' 
elif [[ ${option} == "write" ]];then 
cat /proc/diskstats |grep "${disk} "|awk '{print $10}' 
elif [[ ${option} == "readops" ]];then 
cat /proc/diskstats |grep "${disk} "|awk '{print $4}' 
elif [[ ${option} == "writeops" ]];then 
cat /proc/diskstats |grep "${disk} "|awk '{print $8}' 
elif [[ ${option} == "readtime" ]];then 
cat /proc/diskstats |grep "${disk} "|awk '{print $7}' 
elif [[ ${option} == "writetime" ]];then 
cat /proc/diskstats |grep "${disk} "|awk '{print $11}' 
fi 

/etc/init.d/zabbix_agentd restart

cat /proc/diskstats

设备号     编号  设备      读完成次数   合并完成次数      读扇区次数       读操作花费毫秒数    写完成次数       合并写完成次数     写扇区次数       写操作花费的毫秒数       正在处理的输入/输出请求数       输入/输出操作花费的毫秒数       输入/输出操作花费的加权毫秒数。
   8       0    sda       437179787      111140         88148407612     256090773           504526543       4574433         57747952507     870869581                   0                           215482123                           1126127633

从diskstats采集,磁盘使用率计算方式为:
两次采集的输入/输出操作花费的毫秒数之差 / 采集间隔时间

例如:第一次采集输入/输出操作花费的毫秒数为90258834,间隔10秒后采集的值为90258710
那么磁盘使用率为 (90258710ms - 90258834ms)/ 10*1000ms = 0.0124,也就是1.24%

参考:
https://www.aliyun.com/jiaocheng/433252.html
https://www.cnblogs.com/zhangs1986/p/8118788.html
https://www.cnblogs.com/dachenzi/p/8232001.html

2. IOSTAT状态

server ui 配置

导入模板

zbx_export_templates -1-

添加自动发现规则(如果没有则创建)

agent 配置

配置文件

脚本

自动发现脚本:
cat /etc/zabbix/zabbix_scripts/dev-discovery.sh

#!/bin/bash

DEVICES=`iostat | awk '{ if ($1 ~ "^([shxv]|xv)d[a-z]$") { print $1 } }'`

COUNT=`echo "$DEVICES" | wc -l`
INDEX=0
echo '{"data":['
echo "$DEVICES" | while read LINE; do
    echo -n '{"{#DEVNAME}":"'$LINE'"}'
    INDEX=`expr $INDEX + 1`
    if [ $INDEX -lt $COUNT ]; then
        echo ','
    fi
done
echo ']}'

数据采集脚本
cat /etc/zabbix/zabbix_scripts/disk_status.sh

#/bin/sh
device=$1
item=$2

/usr/bin/iostat -dxkt 2 2 > /var/log/zabbix/iostat_output 2>/dev/null

case $item in

rrqm)
/usr/bin/tail -n20 /tmp/iostat_output |grep "\b$device\b"|tail -1|awk '{print $2}'
;;

wrqm)
/usr/bin/tail -n20 /tmp/iostat_output |grep "\b$device\b"|tail -1|awk '{print $3}'
;;

rps)
/usr/bin/tail -n20 /tmp/iostat_output |grep "\b$device\b"|tail -1|awk '{print $4}'
;;

wps)
/usr/bin/tail -n20 /tmp/iostat_output |grep "\b$device\b" |tail -1|awk '{print $5}'
;;

rKBps)
/usr/bin/tail -n20 /tmp/iostat_output |grep "\b$device\b" |tail -1|awk '{print $6}'
;;

wKBps)
/usr/bin/tail -n20 /tmp/iostat_output |grep "\b$device\b" |tail -1|awk '{print $7}'
;;

avgrq-sz)
/usr/bin/tail -n20 /tmp/iostat_output |grep "\b$device\b" |tail -1|awk '{print $8}'
;;

avgqu-sz)
/usr/bin/tail -n20 /tmp/iostat_output |grep "\b$device\b" |tail -1|awk '{print $9}'
;;

await)
/usr/bin/tail -n20 /tmp/iostat_output |grep "\b$device\b" |tail -1|awk '{print $10}'
;;

r_await)
/usr/bin/tail -n20 /tmp/iostat_output |grep "\b$device\b" |tail -1|awk '{print $11}'
;;

w_await)
/usr/bin/tail -n20 /tmp/iostat_output |grep "\b$device\b" |tail -1|awk '{print $12}'
;;

svctm)
/usr/bin/tail -n20 /tmp/iostat_output |grep "\b$device\b" |tail -1|awk '{print $13}'
;;

util)
/usr/bin/tail -n20 /tmp/iostat_output |grep "\b$device\b" |tail -1|awk '{print $14}'
;;
esac

/etc/init.d/zabbix_agentd restart

参考: