Friday, January 22, 2010

CPU monitor script

a CPU monitor script :

#!/bin/sh

# Configuration parameters start here -------------------

# Next is the maximum allowed cpu load average before producing an alarm
# example CPU_MAX=1.00
CPU_CRITICAL=0.97
CPU_MAJOR=0.95
CPU_MINOR=0.90
CPU_WARNING=0.87

# Configuration parameters end here -------------------


#HOST_OS=`uname -s`
#HOST_NAME=`hostname`


NUM_CPU_CRITICAL=`echo $CPU_CRITICAL | /bin/cut -d "." -f 1`
NUM_CPU_CRITICAL=${NUM_CPU_CRITICAL}`echo $CPU_CRITICAL | /bin/cut -d "." -f 2`

NUM_CPU_MAJOR=`echo $CPU_MAJOR | /bin/cut -d "." -f 1`
NUM_CPU_MAJOR=${NUM_CPU_MAJOR}`echo $CPU_MAJOR | /bin/cut -d "." -f 2`

NUM_CPU_MINOR=`echo $CPU_MINOR | /bin/cut -d "." -f 1`
NUM_CPU_MINOR=${NUM_CPU_MINOR}`echo $CPU_MINOR | /bin/cut -d "." -f 2`

NUM_CPU_WARNING=`echo $CPU_WARNING | /bin/cut -d "." -f 1`
NUM_CPU_WARNING=${NUM_CPU_WARNING}`echo $CPU_WARNING | /bin/cut -d "." -f 2`

IDLE=`/usr/bin/sar -u | /usr/bin/tail -2 | /bin/grep -v Average | /bin/awk '{print $8}'`
IDLE=`echo $IDLE | /bin/cut -d "." -f 1`
NUM_LOAD_AVG=`/usr/bin/expr 100 - $IDLE`

if [ $NUM_LOAD_AVG -lt 10 ]
then
NUM_LOAD_AVG=00$NUM_LOAD_AVG
else
if [ $NUM_LOAD_AVG -lt 100 ]
then
NUM_LOAD_AVG=0$NUM_LOAD_AVG
fi
fi

if [ $NUM_LOAD_AVG -ge $NUM_CPU_CRITICAL ]
then
echo "1 CPU The CPU Load is currently critical $NUM_LOAD_AVG%; some programs might be overloading the CPU; please execute the top command, check which processes are consuming high CPU resources and inform the system administrator. If this alarm remains not cleared, the performance of the machine will degrade and this will affect seriously any system running on it"
else
if [ $NUM_LOAD_AVG -ge $NUM_CPU_MAJOR ]
then
echo "2 CPU The status of the CPU Load is very serious $NUM_LOAD_AVG%, some programs might be overloading the CPU; please execute the top command, check which processes are consuming high CPU resources and inform the system administrator. If this alarm remains not cleared, the severity of the CPU Load may increase and the performance of the machine will degrade and this will affect seriously any system running on it"
else
if [ $NUM_LOAD_AVG -ge $NUM_CPU_MINOR ]
then
echo "3 CPU The status of the CPU Load is minor $NUM_LOAD_AVG%, you need to check which processes are consuming this load, please execute the top command and inform the system administrator. Please consider this alarm seriously in order to prevent any increase in its severity."
else
if [ $NUM_LOAD_AVG -ge $NUM_CPU_WARNING ]
then
echo "4 CPU This is just a warning alarm to inform you that the Load on the CPU is $NUM_LOAD_AVG%; some processes might be using high loads, please execute the top command and inform the system administrator. Kindly take this alarm into consideration in order to prevent any increase in its severity."
else
echo "5 CPU The Load of the CPU is normal ($NUM_LOAD_AVG%), it is below the warning level."
fi
fi
fi
fi

0 Comment :