check_cpu 是依据从exchange.nagios.org 的脚本修改的。使用原始脚本结构,函数名称、参数也没怎么变化,有看到的,勿拍砖。


#!/bin/bash


ST_OK=0

ST_WR=1

ST_CR=2

ST_UK=3


PROGNAME=`basename $0`


warning=$2

critical=$4

process=$6



get_wcdiff() {

   if [ ! -z "$warning" -a ! -z "$critical" ]

   then

       wclvls=1

       if [ ${warning} -gt ${critical} ]

       then

           wcdiff=1

       fi

   elif [ ! -z "$warning" -a -z "$critical" ]

   then

       wcdiff=2

   elif [ -z "$warning" -a ! -z "$critical" ]

   then

       wcdiff=3

   fi

}


val_wcdiff() {

   if [ "$wcdiff" = 1 ]

   then

       echo "Please adjust your warning/critical thresholds. The warning must be lower than the critical level!"

       exit $ST_UK

   elif [ "$wcdiff" = 2 ]

   then

       echo "Please also set a critical value when you want to use warning/critical thresholds!"

       exit $ST_UK

   elif [ "$wcdiff" = 3 ]

   then

       echo "Please also set a warning value when you want to use warning/critical thresholds!"

       exit $ST_UK

   fi

}


get_vals() {


   tmp_output=$(top -n 1 -b | grep $process )


   top_cpu=$(echo ${tmp_output} | awk '{print $9}')


   int_cpu=$(echo ${top_cpu%.*})


}


do_perfdata() {

perfdata="cpu=${top_cpu}%"

}



get_wcdiff

val_wcdiff

get_vals

do_perfdata


if [ -n "$warning" -a -n "$critical" ]

then

   if [ "$int_cpu" -ge "$warning" -a "$int_cpu" -lt "$critical" ]

   then

       echo "WARNING - ${perfdata} | ${perfdata}"

exit $ST_WR

   elif [ "$int_cpu" -ge "$critical" ]

   then

       echo "CRITICAL - ${perfdata} | ${perfdata}"

exit $ST_CR

   else

       echo "OK - ${perfdata} | ${perfdata} "

exit $ST_OK

   fi

else

   echo "Usage Example: $PROGNAME -w 3 -c 12 -p firefox "

   exit $ST_OK

fi