写一个脚本,当内存用量达到80%的时候触发邮件报警。
#!/bin/bash
contact='123456@qq.com'
mail_warning=0
mailsubject="$(hostname) Warning:Memory over 80%"
mailbody="$(date +'%F %T'): $mailsubject"
mailsubject1="$(hostname) Info:Memory recovers to below 80%"
mailbody1="$(date +'%F %T'): $mailsubject1"
while true
do
mem_used=`free -m | awk '/Mem/{print int($3*100/$2)}'`
[ "$mem_used" -ge 80 ] && [ "$mail_warning" -eq 0 ] && \
echo $mailbody | mail -s $mailsubject $contact && mail_warning=1
[ "$mem_used" -lt 80 ] && [ "$mail_warning" -eq 1 ] && \
echo $mailbody1 | mail -s $mailsubject1 $contact && mail_warning=0
sleep 10
done