This can be done using the following shell script and the 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: donotreply@sample.com To: info@sample.com 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 ideal percentage of CPU for every 1 second. And 5 samples will be taken. Then the average of this ideal percentage will be passed to the CPU variable. When the ideal drops below 20% of mail, it will be sent.
We can configure cron with a 5 minute duration.
*/5 * * * * cd /full/path/to/script/; ./cpu_monitor.sh;
source share