【问题标题】:Send alert email if CPU usage is continuously higher than a certain amount如果 CPU 使用率持续高于一定数量,则发送警报电子邮件
【发布时间】:2014-10-10 16:23:15
【问题描述】:

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

【问题讨论】:

  • 你为什么不通过 cron 选项卡和 shell 脚本来做呢

标签: linux shell unix crontab


【解决方案1】:

这可以通过以下 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;

【讨论】:

  • 请注意,每次 CRON 运行时,echo "Normal" 都会向我发送一封电子邮件...因为 CRON 会向我发送所有预期为错误的输出。
猜你喜欢
  • 2021-12-29
  • 1970-01-01
  • 1970-01-01
  • 2019-10-26
  • 1970-01-01
  • 2016-07-24
  • 1970-01-01
  • 2016-04-29
  • 1970-01-01
相关资源
最近更新 更多