在 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>
此处脚本将每 1 秒占用 CPU 理想百分比。并取 5 个样本。然后该理想百分比的平均值将传递给变量 CPU
。当理想值低于 20% 时,将发送邮件。
我们可以设置 5 分钟持续时间的 cron。
*/5 * * * * cd /full/path/to/script/; ./cpu_monitor.sh;
关于linux - 如果 CPU 使用率持续高于一定数量,则发送警报电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25351723/