nagios监控插件编写之check_cpu

时间 : 13-09-22 栏目 : Shell进阶 作者 : 老薛 评论 : 0 点击 : 712 次

#!/bin/sh
# Filename: check_cpu.sh

procinfo=`which procinfo 2>/dev/null`
sar=`which sar 2>/dev/null`
function help {
 echo -e "\n\tThis plugin shows the % of used CPU, using either procinfo or sar (whichever is available)\n\n\t$0:\n\t\t-c <integer>\tIf the % of used CPU is above <integer>, returns CRITICAL state\n\t\t-w <integer>\tIf the % of used CPU is below CRITICAL and above <integer>, returns WARNING state\n"
 exit -1
}
# Getting parameters:
while getopts "w:c:h" OPT; do
 case $OPT in
  "w") warning=$OPTARG;;
  "c") critical=$OPTARG;;
  "h") help;;
 esac
done
# Checking parameters:
( [ "$warning" == "" ] || [ "$critical" == "" ] ) && echo "ERROR: You must specify warning and critical levels" && help
[[ "$warning" -ge  "$critical" ]] && echo "ERROR: critical level must be highter than warning level" && help
# Assuring that the needed tools exist:
( ( [ -f $procinfo ] && command="procinfo") ||  [ -f $sar ] ) || \
 ( echo "ERROR: You must have either procinfo or sar installer in order to run this plugin" && exit -1 )
# Doing the actual check:

idle=`top -b -n 1 | grep Cpu |awk '{print $5}' |cut -f 1 -d "%"`
used=10
# Comparing the result and setting the correct level:
if [[ $used -ge $critical ]]; then
        msg="CRITICAL"
        status=2
else if [[ $used -ge $warning ]]; then
        msg="WARNING"
        status=1
     else
        msg="OK"
        status=0
     fi
fi
# Printing the results:
echo "$msg - CPU used=$used% idle=$idle% | 'CPU Usage'=$used%;$warning;$critical;"
# Bye!

exit $status

Linux中常用的正则表达式

本文标签

除非注明,文章均为( 老薛 )原创,转载请保留链接: http://www.bdkyr.com/shell/2168.html

nagios监控插件编写之check_cpu:等您坐沙发呢!

发表评论

8 + 9 = ?


博主微信号,很高兴为您提供帮助

随便看看

为您推荐

0