Send alert email if CPU usage is continuously higher than a certain amount

11,193

This can be done through following shell script and a frequent cron job.

cpu_monitor.sh

CPU=$(sar 1 5 | grep "Average" | sed 's/^.* //')

if [ $CPU -lt 20 ]
then
   cat mail_content.html | /usr/lib/sendmail -t
else
   echo "Normal"
fi

mail_content.html

From: [email protected]
To: [email protected]
Subject: Subject of the mail
Mime-Version: 1.0
Content-Type: text/html

<h1>CPU usage increased heigh</h1>

Here the script will take the CPU ideal percentage for each 1 seconds. And 5 samples will be taken. Then average of that ideal percentage will be passed to variable CPU. When the ideal goes below the 20% mail will be send out.

We can setup the cron with 5 minute duration.

*/5 * * * * cd /full/path/to/script/; ./cpu_monitor.sh;
Share:
11,193
sugunan
Author by

sugunan

I am an aspiring entrepreneur who subscribes to the lean startup ideology and creator of "positive affirmations", a web for the business. I have been working professionally in software development since 2009 in the web and mobile spaces with experience across the finance, shopping card, online payment and SEO. I occasionally dabble in 3D animation (3DS Max) and photo editing (Photoshop).

Updated on July 13, 2022

Comments

  • sugunan
    sugunan almost 2 years

    In a Linux / Unix server when the CPU usage go above a threshold value it need to send out a email alert. Propose a way to do it through cron tab and shell scripting.