当前位置: > Linux服务器 > 监控工具 >

nagios监控cpu使用率的脚本

时间:2014-10-26 00:56来源:linux.it.net.cn 作者:it

学习本脚本,请首先学习掌握which command与while getopts命令,以助于理解。
通过使用/usr/bin/procinfo或/usr/bin/sar命令获取数据。
 

复制代码代码如下:

#!/bin/sh
# Filename: check_cpu.sh
# Version 0.0.2 - Jan/2009
# Changes: improved grabbing of the idle cpu time
#
# by Thiago Varela -

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 $8}' | 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    t247
   check_period           t247
   max_check_attempts     3
   check_interval         5
   contact_groups         tech-admins
   retry_check_interval   2
   notification_options   w,u,c
}
 
(责任编辑:IT)
------分隔线----------------------------