学习本脚本时,请关注which command与while getopts命令。
通过使用/usr/bin/procinfo或/usr/bin/sar命令获取数据。
以下脚本源自:exchange.nagios.org。
#!/bin/sh
# Filename: check_cpu.sh
# Version 0.0.2 - Jan/2009
# Changes: improved grabbing of the idle cpu time
#
# by Thiago Varela - thiago@iplenix.com
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:
( [ "$command" == "procinfo" ] && idle=`$procinfo | grep idle | cut -d% -f1 | awk '{print $NF}' | cut -d. -f1`) || \
idle=`$sar | tail -1 | awk '{print $7}' | cut -d. -f1`
used=`expr 100 - $idle`
# 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
使用示例:
一、被监控机端
将脚本保存在被监控机的/usr/local/nagios/libexec目录中。
#chown nagios.nagios check_cpu.sh
编辑/usr/local/nagios/etc/nrpe.cfg,增加如下命令行:
command[check_cpu_233]=/usr/local/nagios/libexec/check_cpu.sh -w 60 -c 80
二、监控机端
增加如下的服务监测
define service{
host_name client-233
use generic-service
check_command check_nrpe!check_cpu_233
service_description check_cpu_233
notifications_enabled 1
event_handler_enabled 1
notification_period 24x7
check_period 24x7
max_check_attempts 3
check_interval 5
contact_groups tech-admins
retry_check_interval 2
notification_options w,u,c
}