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>
# 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
2条答案
按热度按时间798qvoo81#
这可以通过以下shell脚本和频繁的cron作业来完成。
cpu_monitor.sh
mail_content.html
在这里,脚本将每1秒获取CPU的理想百分比。并将获取5个样本。然后,该理想百分比的平均值将传递给变量
CPU
。当理想百分比低于20%时,邮件将被发送出去。我们可以设置持续时间为5分钟的cron。
wlsrxk512#
我将使用
uptime
命令来显示(结束时)过去15分钟的平均CPU使用率。这应该是一个合理的时间范围,以避免CPU使用率出现短暂的峰值。uptime
命令显示处于可运行状态的进程数。如果这些进程数少于CPU核心数,则计算机不受CPU限制,而如果这些进程数多于CPU核心数,假设我们同意进程数量是内核数量的2倍以上是我们希望收到的警报级别。它可以从crontab运行,并将此文本添加为
/etc/cron.d/simplecpucheck
:请注意,我更喜欢从shell脚本而不是crontab守护进程发送电子邮件,因为我可能会使用主题行提供更多信息。我仍然在crontab文件中保留电子邮件地址,以便接收任何其他错误(主要是脚本中的语法错误和找不到命令)。