231gsd 发表于 2016-4-6 09:42:30

zabbix 监控磁盘IO状态

1.1 导入模板1.2 添加脚本,配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/bin/bash
sh_file=/data/sh
agent_file=/usr/local/zabbix/etc/zabbix_agentd.conf.d/disk_io.conf
#zabbix_bin="/usr/local/zabbix"
#nohup soft install
rpm -qa coreutils >> /dev/null && [ $? = 0 ] || yum -y install coreutils
rpm -qa sysstat   >> /dev/null && [ $? = 0 ] || yum -y installsysstat   

zabbix_bin=`echo ${agent_file%/*}`
echo $zabbix_bin
if [ ! -e"${zabbix_bin}" ];then
      exit 2
fi
#發現腳本
if [ ! -f"${sh_file}/disk_discovery.sh" ];then
cat >> ${sh_file}/disk_discovery.sh<<"EOF"
#!/bin/bash
diskarray=(`cat /proc/diskstats |grep -E "\bsd\b|\bxvd\b|\bvd\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"
EOF
fi
#磁盤狀態監控腳本
if [ ! -f "${sh_file}/disk_status.sh" ];then
cat >> ${sh_file}/disk_status.sh <<"EOF"
#/bin/sh
device=$1
item=$2

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}' ;;
   svctm)
    /usr/bin/tail -n20 /tmp/iostat_output |grep "\b$device\b" |tail -1|awk '{print $11}' ;;
   util)
    /usr/bin/tail -n20 /tmp/iostat_output |grep "\b$device\b" |tail -1|awk '{print $12}' ;;
esac
EOF
fi
#添加配置文件
if [ ! -f " ${agent_file}" ];then
cat >> ${agent_file} <<"EOF"
UserParameter=disk.discovery,/bin/bash ${sh_file}/discovery.sh
UserParameter=disk.status[*],${sh_file}/disk_status.sh $1 $2
EOF
#添加清空iostat产生的日志文件
if [ ! -f${sh_file}/clean_output.sh];then
cat >> ${sh_file}/clean_output.sh <<"EOF"
#!/bin/bash
#清空/tmp/iostat_output文件,重启进程
Num=`ps aux | grep iostat | grep -v grep |awk '{print $2}' | wc -l`
file='/tmp/iostat_output'
echo $Num
if [$Num -ne 1 ];then
      pkill iostat
      sleep 1
      nohup /usr/bin/iostat -dxkt 1> $file2>/dev/null &
fi
Pid=`ps aux | grep iostat | grep -v grep |awk '{print $2}'`
if [ $Pid -gt 1 ];then
      echo $Pid
else
      nohup /usr/bin/iostat -dxkt 1> $file2>/dev/null &
fi
count=`cd /tmp/ && ls -lh|grep iostat_output|awk '{print $5}'`
if [[ $count > 100K ]];then
      kill -9 $Pid
      sleep 1
      \mv $file/opt
      rm -f /opt/iostat_output
      nohup /usr/bin/iostat -dxkt 1> $file2>/dev/null &
fi
EOF
fi
[ -e /etc/init.d/zabbix-agentd] && /etc/init.d/zabbix-agentd reload




1.3 模板请查看附件


页: [1]
查看完整版本: zabbix 监控磁盘IO状态