Step1.安装sendmail来发邮件
# yum -y install sendmail
# /etc/init.d/sendmail start
# chkconfig sendmail on
Step2.安装邮件客户端
# yum -y install mutt
2.1添加发件人信息,如下
# vim /etc/Muttrc
set charset="utf-8" #设置发邮件编码
set envelope_from=yes
set rfc2047_parameters=yes #解决附件乱码问题
set realname="报警" #发件人别名
set use_from=yes #指定是否显示别名
set from=monitor@test.com #发送人地址
Step3.Ping主机状态脚本
# vim ping.sh
#!/bin/bash # DATE=`date +%F_%H:%M` LOG=/root/ping-monitor/ping.log IP=/root/ping-monitor/ip.txt CLEAN=`cat $LOG | wc -l` for i in $(cat $IP); do ping -c 2 $i &>/dev/null if [ $? -eq 0 ];then echo "$DATE $i Host ping is success." >> $LOG else echo -e "Date : $DATE\nHost : $i\nProblem : Ping is failed, Please check ! ! !" | mutt -s "Ping" xxx@163.com fi done #如果日志文件超30条,则清空 if [ $CLEAN -ge 30 ];then echo "" > $LOG fi
Step4.添加任务计划
# crontab -e
*/1 * * * * /bin/bash /root/ping-monitor/ping.sh
本文出自 ““企鹅”那点事儿” 博客,请务必保留此出处http://golab.blog.51cto.com/7876557/1534285
线上一个简单检测Ping状态的邮件报警脚本,布布扣,bubuko.com
原文地址:http://golab.blog.51cto.com/7876557/1534285