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

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


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 -telse   echo "Normal"fi

mail_content.html

From: donotreply@sample.comTo: info@sample.comSubject: Subject of the mailMime-Version: 1.0Content-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;