因为最进要增加对所有主机的IO检测,首先上nagios exchange看了下插件,不能满足我的个人需求。于是到网上先转转吧,实在不行就自己写。果然在网上找到了一个还不错的脚本,是通过第三方工具sysstat里的iostat工具实现的检测。(看来和我的想法一致,本来我自己写也打算用这个工具。)具体脚本如下:

 1#!/bin/sh
 2iostat=`which iostat 2>/dev/null`
 3bc=`which bc 2>/dev/null`
 4function help {
 5echo -e "This plugin shows the I/O usage_rate of the specified disk, using the iostat external program.nt example nt ./io -d sda2 -w 10 -c 20"
 6        exit -1
 7}
 8# Ensuring we have the needed tools:
 9( [ ! -f $iostat ] ) && ( echo "ERROR: iostat command not found .Please install" && exit -1 )
10# Getting parameters:
11while getopts "d:w:c:h" OPT; do
12        case $OPT in
13                "d") disk=$OPTARG;;
14                "w") warning=$OPTARG;;
15                "c") critical=$OPTARG;;
16                "h") help;;
17        esac
18done
19# Adjusting the three warn and crit levels:
20crit_util=`echo $critical`
21warn_util=`echo $warning`
22# Checking parameters:
23[ ! -b "/dev/$disk" ] && echo "ERROR: Device incorrectly specified" && help
24( [ $warn_util == "" ] || [ $crit_util == "" ] ) && echo "ERROR: You must specify all warning and critical levels" && help
25( [[ "$warn_util" -ge  "$crit_util" ]] ) && echo "ERROR: critical levels must be highter than warning levels" && help
26# Doing the actual check:
27util=`$iostat -dx 1 10 $disk | grep $disk | awk '{print $12}'|sort -nr | head -n 1 `
28# Comparing the result and setting the correct level:
29if ( echo ${util} ${crit_util}|awk '!($1>=$2){exit 1}' );then
30        msg="CRITICAL"
31        status=2
32else if ( echo ${util} ${warn_util} |awk '!($1>=$2){exit 1}');then
33                msg="WARNING"
34                status=1
35     else
36        msg="OK"
37        status=0
38     fi
39fi
40# Printing the results:
41echo "$msg - I/O stats util_rate=$util  "
42# Bye!
43exit $status

不过该脚本在对所有的磁盘进行检测时,需要一个一个去配置。感觉有点麻烦,所以我将该脚本改成了:

 1#!/bin/sh
 2iostat=`which iostat 2>/dev/null`
 3bc=`which bc 2>/dev/null`
 4function help {
 5echo -e "This plugin shows the I/O usage_rate of the specified disk, using the iostat external program.nt example nt ./io  -w 10 -c 20"
 6        exit -1
 7}
 8# Ensuring we have the needed tools:
 9( [ ! -f $iostat ] ) && ( echo "ERROR: iostat command not found .Please install" && exit -1 )
10# Getting parameters:
11while getopts "w:c:h" OPT; do
12        case $OPT in
13                "w") warning=$OPTARG;;
14                "c") critical=$OPTARG;;
15                "h") help;;
16        esac
17done
18# Adjusting the three warn and crit levels:
19crit_util=`echo $critical`
20warn_util=`echo $warning`
21# Checking parameters:
22#[ ! -b "/dev/$disk" ] && echo "ERROR: Device incorrectly specified" && help
23( [ $warn_util == "" ] || [ $crit_util == "" ] ) && echo "ERROR: You must specify all warning and critical levels" && help
24( [[ "$warn_util" -ge  "$crit_util" ]] ) && echo "ERROR: critical levels must be highter than warning levels" && help
25# Doing the actual check:
26getio=`$iostat -dx 1 3 -p ALL| awk '{print $NF,$1}'|grep sda|sort -nr|head -n 1`
27util=`echo $getio|awk '{ print $1}'`
28getdisk=`echo $getio|awk '{print $2}' `
29# Comparing the result and setting the correct level:
30if ( echo ${util} ${crit_util}|awk '!($1>=$2){exit 1}' );then
31        msg="CRITICAL"
32        status=2
33else if ( echo ${util} ${warn_util} |awk '!($1>=$2){exit 1}');then
34                msg="WARNING"
35                status=1
36     else
37        msg="OK"
38        status=0
39     fi
40fi
41# Printing the results:
42echo "$msg - $getdisk I/O stats util_rate=$util  "
43# Bye!
44exit $status

这样可以检测所有的sda及其分区(如:/dev/sda1、/dev/sda2)的所有IO情况。并在结果中提示是那个分区或整个磁盘的IO比较高。