Copyright: code word is not easy, please indicate the source ~~ https://blog.csdn.net/m0_37190495/article/details/91438699
Reproduced, please indicate the source:
Original starting in: http://www.zhangruibin.com
article from RebornChang's blog
Monitoring web services and e-mail alert
First, let me send mail under the Linux operating system.
Linux operating system to send e-mail
Simply use mailX:
1. Install mailx
yum install mailx -y
2. Test basic mail sent
命令行: mail -s "theme" emailAddress,回车后输入内容按Ctrl+D发送邮件.
管道符: echo "mail main content" | mail -s "theme" emailAddress
文件内容作为邮件内容: mail -s "theme" emailAddress < /tmp/t.txt
Specific settings on sendEmail the like can not say here, you can refer to the following two blog posts:
https://www.cnblogs.com/liutao97/p/8387244.html
https://blog.csdn.net/lyf844692713 / article / details / 81479066
3. Write to monitor file url
vim url
Enter the URL to be monitored, such as Bo main monitor are two domain names, so the contents of the file are:
www.zhangruibin.com www.92cnb.com
4. Write a script monitoring monitor.sh
This script picking from the network, some of their modifications (Baidu oriented programming), if infringement, please inform:
#!/bin/bash
#监控web服务并邮件提醒
while true
do
Mail="[email protected]"
FailCount=0
Retval=0
GetUrlStatus() {
for ((i=1;i<=3;i++)) #使用i++判断访问次数,如果wget两次超时则判断网站异常
do
wget -T 3 --tries=1 --spider ${1} >/dev/null 2>&1
#-T超时时间,--tries尝试1次,--spider蜘蛛
[ $? -ne 0 ] && let FailCount+=1; #访问超时时,$?不等于0,则FailCount加1
done
Date=`date +%F" "%H:%M`
if [ $FailCount -gt 1 ];then
Retval=1
echo "check fail!!!,sendmail....."
## 使用mutt
#echo -e "Date : $Date\nProblem : $url is not running." | mutt -s "web server Monitor" $Mail
echo "Date : $Date \n Problem :$url服务挂了" | mailx -v -s "服务监控提醒" $Mail
else
Retval=0
echo "Date : $Date $url is running."
fi
return $Retval #如果返回值为0,就正常退出循环,不为0则继续循环
}
for url in `cat url | sed '/^#/d'`
do
#GetUrlStatus $url && echo yes || echo no
GetUrlStatus $url
done
sleep 600 #死循环,设置每600s运行一次
done
5. Start the script, will echo to the specified output file monitor.log
nohup sh monitor.sh >>monitor.log &
6. After exit the system to re-sign in to view the status script runs
ps -ef|grep monitor
结果:
root 454 328 0 03:00 pts/2 00:00:00 grep --color=auto monitor
root 27535 1 0 Jun10 ? 00:00:00 sh monitor.sh
7. Review monitor.log and email verification
cat monitor.log
结果:
Date : 2019-06-10 21:47 www.zhangruibin.com is running.
Date : 2019-06-10 21:47 www.92cnb.com is running.
Date : 2019-06-10 21:57 www.zhangruibin.com is running.
Date : 2019-06-10 21:57 www.92cnb.com is running.
Date : 2019-06-10 22:07 www.zhangruibin.com is running.
Date : 2019-06-10 22:07 www.92cnb.com is running.
Date : 2019-06-10 22:17 www.zhangruibin.com is running.
Date : 2019-06-10 22:17 www.92cnb.com is running.
Date : 2019-06-10 22:27 www.zhangruibin.com is running.
Date : 2019-06-10 22:27 www.92cnb.com is running.
Date : 2019-06-10 22:37 www.zhangruibin.com is running.
Date : 2019-06-10 22:37 www.92cnb.com is running.
Date : 2019-06-10 22:47 www.zhangruibin.com is running.
Date : 2019-06-10 22:47 www.92cnb.com is running.
Date : 2019-06-10 22:57 www.zhangruibin.com is running.
Date : 2019-06-10 22:57 www.92cnb.com is running.
check fail!!!,sendmail.....
[email protected]... Connecting to [127.0.0.1] via relay...
220 zhrb.localdomain ESMTP Postfix
>>> EHLO zhrb.localdomain
250-zhrb.localdomain
250-PIPELINING
250-SIZE 10240000
250-VRFY
250-ETRN
250-ENHANCEDSTATUSCODES
250-8BITMIME
250 DSN
>>> MAIL From:<[email protected]> SIZE=304
250 2.1.0 Ok
>>> RCPT To:<[email protected]>
>>> DATA
250 2.1.5 Ok
354 End data with <CR><LF>.<CR><LF>
>>> .
250 2.0.0 Ok: queued as 5C4001D6E
[email protected]... Sent (Ok: queued as 5C4001D6E)
Closing connection to [127.0.0.1]
>>> QUIT
221 2.0.0 Bye
E-mail and receive mail, verify success.
Over!