一、配置服务器的smtp信息

vim /etc/mail.rc
set from=XXX@XXX.com smtp=smtp.xxx.com smtp-auth-user=XXX@XXX.com smtp-auth-password=xxxxx smtp-auth=login

二、service_check.sh检测脚本内容

#!/bin/bash
for port in 80 3306 10050 10022; do
if `netstat -tnlp |grep $port &> /dev/null` ; then
   if `tail -4 /root/lee/service_status.log |grep "$port failed" &> /dev/null` ;then
echo "`date +%F-%H:%M:%S` [`hostname`] `/sbin/ifconfig eth0 |grep "inet addr" |awk '{split($2,a,":"); print a[2]}'` [$port recovery]"> /root/lee/mail_content.txt
mail -s "$port recovery" XXXXXXXXXX@139.com < /root/lee/mail_content.txt
   fi
echo "`date +%F-%H:%M:%S` $port no problem" >> /root/lee/service_status.log
else
echo "`date +%F-%H:%M:%S` $port failed................"  >> /root/lee/service_status.log
echo "`date +%F-%H:%M:%S` [`hostname`] `/sbin/ifconfig eth0 |grep "inet addr" |awk '{split($2,a,":"); print a[2]}'` [$port down]"> /root/lee/mail_content.txt
mail -s "$port failed" XXXXXXXXXXXX@139.com < /root/lee/mail_content.txt
fi
done

报警机制说明:如果检测到端口不存在时会发信息,在端口恢复正常后会发送recovery信息确认


三、加入定时任务

*/5 * * * *  sh /root/lee/service_check.sh

四、测试截图

手动关闭mysqld服务

152038598.png

过段时间会收到短信,短信截图

152114954.png

手动开启mysqld服务

152144989.png

过5分钟会有个恢复的回执

152229295.png


五、脚本内容已经在附件中,可以自行测试