首页 系统 Zabbix3.0.4操作iostat工具监控CentOS主机磁盘IO

Zabbix3.0.4操作iostat工具监控CentOS主机磁盘IO

ASP站长网该监控基于iostat,然后iostat 命令用来监视系统输入/输出设备负载
 
1.安装IOSTAT工具
 
# yum install sysstat -y
 
测试iostat 查看所有硬盘io
 
# iostat
Linux 2.6.32-431.11.25.el6.ucloud.x86_64 (test8_Hadoop_kaf) 07/27/2017 _x86_64_    (4 CPU)
 
avg-cpu: %user %nice %system %iowait %steal %idle
0.04 0.00 0.04 0.00 0.00 99.92
 
Device: tps Blk_read/s Blk_wrtn/s Blk_read Blk_wrtn
vda 0.10 0.23 0.92 447172 1765488
vdb 0.00 0.00 0.15 4298 277632
 
2.部署脚本
 
# mkdir -p /usr/local/zabbix-agent/scripts/
# vim /usr/local/zabbix-agent/scripts/zabbix-iostat.sh
 
#!/bin/bash
 
ZBX_DEV="$1"
ZBX_METRIC="$2"
IOSTAT_LOG=/tmp/iostat.log
 
# Check iostat util
if ! which iostat 2>&1>/dev/null; then
  >&2 echo "Can't find 'iostat' binary in \$PATH"
  >&2 echo "Make sure that you've installed 'sysstat' package"
  exit 1
fi
 
# Check args
[[ $# -lt 1 ]] && { echo "Give some more arguments, please :)"; exit 1; }
 
# Discovering devices and spits out JSON
if [ "$ZBX_DEV" = 'discovery' ]; then
  iostat -d | \
  tail -n +4 |
  awk 'BEGIN {
    ORS="";
    print "{\"data\":["}
    /\d/ {printf "%s{\"%s\":\"%s\"}", separator, "{#DEVICENAME}", $1, separator = ","}
    END {print "]}" }'
  exit 0
fi
 
# Test iostat log
[[ ! -f "${IOSTAT_LOG}" ]] && echo "[m|ZBX_NOTSUPPORTED] [No such file ${IOSTAT_LOG}]" && exit 1
 
 
# Parse iostat file
function parse_iostat() {
  grep ${ZBX_DEV} ${IOSTAT_LOG} | \
  tr ',' '.' | \
  awk -v field="$1" '
  {sum+=$field; n++;}
  END {if (n > 0) print sum/n;
  else {print "[m|ZBX_NOTSUPPORTED] [Nodata.]" > "/dev/stderr"; exit 1};}'
}
 
# Exec parse funcion with number of column
case ${ZBX_METRIC} in
  rrqm/s    ) parse_iostat '2';;
  wrqm/s    ) parse_iostat '3';;
  r/r      ) parse_iostat '4';;
  w/r      ) parse_iostat '5';;
  rkb/s    ) parse_iostat '6';;
  wkb/s    ) parse_iostat '7';;
  avgrq-sz  ) parse_iostat '8';;
  avgqu-sz  ) parse_iostat '9';;
  await    ) parse_iostat '10';;
  r_await  ) parse_iostat '11';;
  w_await  ) parse_iostat '12';;
  svctm    ) parse_iostat '13';;
  util      ) parse_iostat '14';;
  *        ) echo "[m|ZBX_NOTSUPPORTED] [Unsupported item key.]" && exit 1;;
esac
 
添加脚本执行权限
# chmod +x /usr/local/zabbix-agent/scripts/zabbix-iostat.sh
 
# 设置参数
 
# echo 'UserParameter=iostat[*],/usr/local/zabbix-agent/scripts/zabbix-iostat.sh "$1" "$2"' > /etc/zabbix/zabbix_agentd.d/iostat.conf
 
#重启zabbix_agentd
 
service zabbix-agent restart
 
# 测试自动发现
 
zabbix_agentd -t iostat[discovery]
 
3.加入crontab
 
# crontab -e
 
* * * * * ( sleep 10 && iostat -dxk 1 20 > /tmp/iostat.tmp && mv /tmp/iostat.tmp /tmp/iostat.log )
* * * * * ( sleep 40 && iostat -dxk 1 20 > /tmp/iostat.tmp && mv /tmp/iostat.tmp /tmp/iostat.log )
 
# 重启计划任务
/etc/init.d/crond restart
测试监控项
 
# zabbix_agentd -t iostat[vda,rkb/s]
iostat[vda,rkb/s] [t|0.007]

关于作者: dawei

【声明】:九江站长网内容转载自互联网,其相关言论仅代表作者个人观点绝非权威,不代表本站立场。如您发现内容存在版权问题,请提交相关链接至邮箱:bqsm@foxmail.com,我们将及时予以处理。

热门文章