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

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

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

798qvoo8

798qvoo81#

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

wlsrxk512#

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


# get the number of CPU cores using /proc/cpuinfo

# 

ncpu=$(egrep -c '^processor' /proc/cpuinfo)

# 

# get the last number (only integer part) of uptime output

# 

load=$(LC_ALL=C uptime  | sed -e 's/.*average: .* \([0-9]*\)\.[0-9][0-9]$/\1/')

# 

# divide the $load number by the $ncpu and check if greather than alert level

# 

if [ $(($load/$cpu)) -gt 2 ]
then
   # send an e-mail to alerts@domain.tld.
   echo -e  "\nCPU usage is greather than twice the number of CPU cores for more than 15 minutes.\n" \
   | fold -s -w75 | mailx -s "CPU alert for $(hostname)" alerts@domain.tld
fi

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

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

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

相关问题