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.
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;