Linux 如果CPU使用率持续高于一定数量,则发送警报电子邮件

Linux 如果CPU使用率持续高于一定数量,则发送警报电子邮件,linux,shell,unix,crontab,Linux,Shell,Unix,Crontab,在Linux/Unix服务器中,当CPU使用率超过阈值时,需要发送电子邮件警报。提出一种通过cron选项卡和shell脚本实现的方法。这可以通过以下shell脚本和频繁的cron作业来实现 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 "Norm

在Linux/Unix服务器中,当CPU使用率超过阈值时,需要发送电子邮件警报。提出一种通过cron选项卡和shell脚本实现的方法。

这可以通过以下shell脚本和频繁的cron作业来实现

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>

为什么不通过cron选项卡和shell脚本来实现呢请注意,
echo“Normal”
会在cron每次运行时向我发送一封电子邮件。。。因为CRON向我发送了所有预期为错误的输出。
*/5 * * * * cd /full/path/to/script/; ./cpu_monitor.sh;