Monday, September 20, 2010

CPULOAD and send email to admin

This script is very useful for system admins, it checks Cpuload and get info of which process takes the cpuload, if cpuload is or above 70%  it sends alert email to admin
==============================
#!/bin/bash
# Shell script to monitor or watch the high cpu-load
# It will send an email to $ADMIN, if the (cpu load is in %) percentage
# of cpu-load is >= 70%
# If you have any suggestion or question please email to raviindiangnu org
# set admin email so that you can get email
# set alert level 70% is default
# you can set it to string LOAD with your value

AWK=/bin/awk
SAR=/usr/bin/sar
GREP=/bin/grep
TR=/usr/bin/tr
HEAD=/usr/bin/head
PS=/bin/ps
SORT=/bin/sort
HOSTNAME=yeswedeal.com
SED=/bin/sed
LOAD=70
CAT=/bin/cat
MAILFILE=/tmp/mailviews$$
MAILER=/bin/mail
mailto=”rajatjpatel@yeswedeal.com”
for path in $PATHS
do
CPU_LOAD=`$SAR -P ALL 1 2 | $GREP ‘Average.*all’ | $AWK -F” ” ‘{ print 100.0 -$NF}’`
echo $CPU_LOAD
if [[ $CPU_LOAD > $LOAD ]];
then
PROC=`$PS -eo pcpu,pid -o comm= | $SORT -k1 -n -r | $HEAD -1`
echo “Please check your processess on ${HOSTNAME} the value of cpu load is $CPU_LOAD % & $PROC” > $MAILFILE
$CAT $MAILFILE | $MAILER -s “CPU Load is $CPU_LOAD % on ${HOSTNAME}” $mailto
fi
done
=============================
After  end of  schedule cron job for this script like below
*/30 * * * * /bin/sh /root/cpuload.sh >/dev/null 2>&1

2 comments:

gis mapserver said...

This would indeed prove to be of a very good use at many of the places as there are plenty of such instances where in there is a sudden rise in the use of the CPU. This would ensure that there is limited amount of such instances as we could know which program would be causing the problem.

Unknown said...

Its feel very good when my posts are useful to anybody.....
N thanks for your complement...