Chinaunix首页 | 论坛 | 博客
  • 博客访问: 512968
  • 博文数量: 173
  • 博客积分: 4112
  • 博客等级: 上校
  • 技术积分: 1577
  • 用 户 组: 普通用户
  • 注册时间: 2010-02-26 10:12
文章分类

全部博文(173)

文章存档

2012年(1)

2010年(172)

我的朋友

分类:

2010-02-26 10:54:18

VERSION="1.4.5"
IOSTAT=/usr/bin/iostat
GREP=/bin/grep
AWK=/usr/bin/awk
TAIL=/usr/bin/tail
FLAG_VERBOSE=FALSE
FLAG_TPS=FALSE
FLAG_READS=FALSE
FLAG_WRITES=FALSE
FLAG_READWRITES=FALSE
TMP="/var/tmp/iostat.`date +'%s'`"
COUNT=2
LEVEL_WARN=""
LEVEL_CRIT=""
RESULT=""
EXIT_STATUS=$STATE_OK
###############################################
#
## FUNCTIONS
#
## Print usage
usage() {
        echo " check_iostat $VERSION - Nagios I/O statistics check script"
        echo ""
        echo " Usage: check_iostat -w -c -l -t|i|o|a [ -v ] [ -h ]"
        echo ""
        echo " NOTE: When specifying device, /dev/ is assumed, e.g. for /dev/hda0 you should just enter hda0 for the device"
        echo ""
        echo "          -w  Warning trigger level"
        echo "          -c  Critical trigger level"
        echo "          -t  I/O transactions per second (TPS/IOPS) on "
        echo "          -i  Kilobytes read IN per second on "
        echo "          -o  Kilobytes written OUT per second on "
        echo "          -a  Kilobytes written OUT and read IN per Second on "
        echo "          -l  Number of samples to take (must be greater than 1)"
        echo "          -v  Verbose output (ignored for now)"
        echo "          -h  Show this page"
        echo ""
}
 
## Process command line options
doopts() {
        if ( `test 0 -lt $#` )
        then
                while getopts w:c:l:t:i:o:a:vh myarg "$@"
                do
                        case $myarg in
                                h|\?)
                                        usage
                                        exit;;
                                w)
                                        LEVEL_WARN=$OPTARG;;
                                c)
                                        LEVEL_CRIT=$OPTARG;;
                                t)
                                        FLAG_TPS=TRUE
                                        DEVICE=$OPTARG;;
                                i)
                                        FLAG_READS=TRUE
                                        DEVICE=$OPTARG;;
                                o)
                                        FLAG_WRITES=TRUE
                                        DEVICE=$OPTARG;;
                                a)
                                        FLAG_READWRITES=TRUE
                                        DEVICE=$OPTARG;;
                                l)
                                        COUNT=$OPTARG;;
                                v)
                                        FLAG_VERBOSE=TRUE;;
                                *)      # Default
                                        usage
                                        exit;;
                        esac
                done
        else
                usage
                exit
        fi
}

# Write output and return result
theend() {
        rm -f $TMP
        echo $RESULT
        exit $EXIT_STATUS
}

#
## END FUNCTIONS
#
#############################################
#
## MAIN
#

# Handle command line options
doopts $@
# Do the do
if [ $COUNT -ge 2 ] ; then
        $IOSTAT -k -d $DEVICE 1 $COUNT | $GREP $DEVICE| $TAIL -`expr $COUNT - 1` > $TMP
else
        RESULT="ERROR Count must be creater as 1"
        EXIT_STATUS=$STATE_UNKNOWN
        theend
fi
## General sanity check
if [ -n "$LEVEL_WARN" -a -n "$LEVEL_CRIT" -a "$LEVEL_WARN" -lt "$LEVEL_CRIT" ]; then
        ## Handle Transactions per second
        if [ $FLAG_TPS = "TRUE" ] ; then
                TPS=`cat $TMP | $AWK '{ sum += $2 } END { print sum / NR } '`
                TPS_ABS=`echo $TPS | $AWK '{printf "%d",$1}'`
                RESULTPERF="on $DEVICE| io=$TPS;$LEVEL_WARN;$LEVEL_CRIT;0;"
                if [ "$TPS_ABS" -lt "$LEVEL_WARN" ] ; then
                        RESULT="IOSTAT OK - $RESULTPERF"
                        EXIT_STATUS=$STATE_OK
                else
                        if [ "$TPS_ABS" -ge "$LEVEL_CRIT" ] ; then
                                RESULT="IOSTAT CRITICAL - $RESULTPERF"
                                EXIT_STATUS=$STATE_CRITICAL
                        else
                                if [ "$TPS_ABS" -ge "$LEVEL_WARN" ] ; then
                                        RESULT="IOSTAT WARNING - $RESULTPERF"
                                        EXIT_STATUS=$STATE_WARNING
                                fi
                        fi
                fi
        fi
        ## Handle Reads per second
        if [ $FLAG_READS = "TRUE" ] ; then
                READSS=`CAT $TMP | $AWK '{ sum += $3} END { print sum / NR } '`
                READSS_ABS=`echo $READSS | $AWK '{printf "%d",$1}'`
                RESULTPERF="on $DEVICE| read=$READSS;$LEVEL_WARN;$LEVEL_CRIT;0;"
                if [ "$READSS_ABS" -lt "$LEVEL_WARN" ] ; then
                        RESULT="IOSTAT OK $RESULTPERF"
                        EXIT_STATUS=$STATE_OK
                else
                        if [ "$READSS_ABS" -ge "$LEVEL_CRIT" ] ; then
                                RESULT="IOSTAT CRITICAL $RESULTPERF"
                                EXIT_STATUS=$STATE_CRITICAL
                        else
                                if [ "$READSS_ABS" -ge "$LEVEL_WARN" ] ; then
                                        RESULT="IOSTAT WARNING $RESULTPERF"
                                        EXIT_STATUS=$STATE_WARNING
                                fi
                        fi
                fi
        fi
        ## Handle Writes per second
        if [ $FLAG_WRITES = "TRUE" ] ; then
                WRITESS=`cat $TMP | $AWK '{ sum += $4} END { print sum / NR } '`
                WRITESS_ABS=`echo $WRITESS | $AWK '{printf "%d",$1}'`
                RESULTPERF="on $DEVICE| write=$WRITESS;$LEVEL_WARN;$LEVEL_CRIT;0;"
                if [ "$WRITESS_ABS" -lt "$LEVEL_WARN" ] ; then
                        RESULT="IOSTAT OK $RESULTPERF"
                        EXIT_STATUS=$STATE_OK
                else
                        if [ "$WRITESS_ABS" -ge "$LEVEL_CRIT" ] ; then
                                RESULT="IOSTAT CRITICAL $RESULTPERF "
                                EXIT_STATUS=$STATE_CRITICAL
                        else
                                if [ "$WRITESS_ABS" -ge "$LEVEL_WARN" ] ; then
                                        RESULT="IOSTAT WARNING $RESULTPERF"
                                        EXIT_STATUS=$STATE_WARNING
                                fi
                        fi
                fi
        fi
       ## Handle Reads and Writes per second
       if [ $FLAG_READWRITES = "TRUE" ] ; then
                READSS=`cat $TMP | $AWK '{ sum += $3 } END { print sum / NR } '`
                READSS_ABS=`echo $READSS | $AWK '{printf "%d",$1}'`
                WRITESS=`cat $TMP | $AWK '{ sum += $4 } END { print sum / NR } '`
                WRITESS_ABS=`echo $WRITESS | $AWK '{printf "%d",$1}'`
                RESULTPERF="on $DEVICE| read=$READSS;$LEVEL_WARN;$LEVEL_CRIT;0; write=$WRITESS;$LEVEL_WARN;$LEVEL_CRIT;0 "
                if [ "$READSS_ABS" -lt "$LEVEL_WARN" ] && [ "$WRITESS_ABS" -lt "$LEVEL_WARN" ]  ; then
                        RESULT="IOSTAT OK $RESULTPERF"
                        EXIT_STATUS=$STATE_OK
                else
                        if [ "$READSS_ABS" -ge "$LEVEL_CRIT" ] || [ "$WRITESS_ABS" -ge "$LEVEL_CRIT" ] ; then
                                RESULT="IOSTAT CRITICAL $RESULTPERF"
                                EXIT_STATUS=$STATE_CRITICAL
                        else
                                if [ "$READSS_ABS" -ge "$LEVEL_WARN" ] || [ "$WRITESS_ABS" -ge "$LEVEL_WARN" ] ; then
                                        RESULT="IOSTAT WARNING $RESULTPERF"
                                        EXIT_STATUS=$STATE_WARNING
                                fi
                        fi
                fi
        fi
else
        echo "ERROR: Invalid warning/critical values"
        usage
        exit
fi
# Quit and return information and exit status
theend
阅读(1213) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2010-04-04 23:54:12

兄台这个脚本不是perl吧~~ 为啥要加pl后缀?