check_cpu 是依據從exchange.nagios.org 的腳本修改的。使用原始腳本結構,函數名稱、參數也沒怎麼變化,有看到的,勿拍磚。
ios
#!/bin/bashbash
ST_OK=0ide
ST_WR=1函數
ST_CR=2spa
ST_UK=3firefox
PROGNAME=`basename $0`get
warning=$2it
critical=$4io
process=$6class
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