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

标签 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 "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/

相关文章:

linux - Ubuntu apt-get 找不到任何版本的 gcc 和 g++

linux - 用双引号分隔两个字符串

c - C 中的父子进程通过公共(public)文件进行通信

sockets - 使用 os.OpenFile() 而不是 net.Listen()

c - 如何将 C/C++ 中的 Linux 应用程序转换为 Linux 发行版的桌面环境?

linux - 如何使用 cURL 测试并等待 HTTP 服务?

shell - head 命令在 MAC OSX 上跳过文件的最后几行

regex - shell :Extract a string betwen two characters in different states

RHEL 上的 Docker CE - 需要 : container-selinux >= 2. 9

php - 检测是否有人安装了 selinux/启用了 httpd_can_network_connect