Nagios CPU Average Checker for Linux and NRPE

Yo, so check out this script.  It is pretty simple and basically just uses sar to check for overall CPU utilization on the system.  It’s setup to work with Nagios and can be called via NRPE.

Here is the syntax, which should look familiar to you, as a Nagios user:

./check_avg -w 30 -c 50

If you don’t have sar, you can install it using a command like this, on RHEL/CentOS:

yum install sysstat

Here’s the script, throw this in with your other nrpe plugins and call it in your nrpe configuration like the other checks.  You’ll have to change the numbers

#!/bin/sh
# simple CPU average Nagios checker
# works with nrpe
# depends on sar (sysstat)

die () {
echo $*
exit 1
}

if [ $1 != -w ] && [ $3 != -c ]; then
echo “usage example -> ./check_avg -w 30 -c 50”
fi

TOTAL=100
CURRENT_IDLE=`/usr/bin/sar -P ALL | grep all | tail -2 | head -n1 | awk ‘{print $9}’ | awk -F”.” ‘{print $1}’` || die Failed to pull current idle
CURRENT_AVG=$(($TOTAL – $CURRENT_IDLE)) || die Failed to do math like a computer

 if [ “$CURRENT_AVG” -ge “$2” ] && [ “$CURRENT_AVG” -lt “$4” ] ; then

echo “WARNING – CPU $CURRENT_AVG%”
exit 1

elif [ “$CURRENT_AVG” -ge “$4” ] ; then

echo “CRITICAL – CPU $CURRENT_AVG%”
exit 2
else
echo “OK – CPU $CURRENT_AVG%”
exit 0

fi