unix 如果CPU使用率持续高于某个值,则发送警报电子邮件

kmbjn2e3  于 2022-11-04  发布在  Unix
关注(0)|答案(2)|浏览(152)

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

798qvoo8

798qvoo81#

这可以通过以下shell脚本和频繁的cron作业来完成。
cpu_monitor.sh

  1. CPU=$(sar 1 5 | grep "Average" | sed 's/^.* //')
  2. if [ $CPU -lt 20 ]
  3. then
  4. cat mail_content.html | /usr/lib/sendmail -t
  5. else
  6. echo "Normal"
  7. fi

mail_content.html

  1. From: donotreply@sample.com
  2. To: info@sample.com
  3. Subject: Subject of the mail
  4. Mime-Version: 1.0
  5. Content-Type: text/html
  6. <h1>CPU usage increased heigh</h1>

在这里,脚本将每1秒获取CPU的理想百分比。并将获取5个样本。然后,该理想百分比的平均值将传递给变量CPU。当理想百分比低于20%时,邮件将被发送出去。
我们可以设置持续时间为5分钟的cron。

  1. * /5 * * * * cd /full/path/to/script/; ./cpu_monitor.sh;
展开查看全部
wlsrxk51

wlsrxk512#

我将使用uptime命令来显示(结束时)过去15分钟的平均CPU使用率。这应该是一个合理的时间范围,以避免CPU使用率出现短暂的峰值。uptime命令显示处于可运行状态的进程数。如果这些进程数少于CPU核心数,则计算机不受CPU限制,而如果这些进程数多于CPU核心数,假设我们同意进程数量是内核数量的2倍以上是我们希望收到的警报级别。

  1. # get the number of CPU cores using /proc/cpuinfo
  2. #
  3. ncpu=$(egrep -c '^processor' /proc/cpuinfo)
  4. #
  5. # get the last number (only integer part) of uptime output
  6. #
  7. load=$(LC_ALL=C uptime | sed -e 's/.*average: .* \([0-9]*\)\.[0-9][0-9]$/\1/')
  8. #
  9. # divide the $load number by the $ncpu and check if greather than alert level
  10. #
  11. if [ $(($load/$cpu)) -gt 2 ]
  12. then
  13. # send an e-mail to alerts@domain.tld.
  14. echo -e "\nCPU usage is greather than twice the number of CPU cores for more than 15 minutes.\n" \
  15. | fold -s -w75 | mailx -s "CPU alert for $(hostname)" alerts@domain.tld
  16. fi

它可以从crontab运行,并将此文本添加为/etc/cron.d/simplecpucheck

  1. MAILTO=alerts@domain.tld
  2. 10,40 * * * * root /path/name/of/shell-script

请注意,我更喜欢从shell脚本而不是crontab守护进程发送电子邮件,因为我可能会使用主题行提供更多信息。我仍然在crontab文件中保留电子邮件地址,以便接收任何其他错误(主要是脚本中的语法错误和找不到命令)。

展开查看全部

相关问题