Send email notification if CPU usage is constantly exceeding a certain amount

On a Linux / Unix server, when the CPU utilization exceeds the threshold, an e-mail notification is required. Suggest a way to do this through cron tab and shell scripting.

+6
source share
1 answer

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; 
+3
source

Source: https://habr.com/ru/post/973938/


All Articles