监控web服务并邮件提醒

/ Linux / 没有评论 / 360浏览
转载请标明出处:
原文首发于:https://www.zhangruibin.com
本文出自RebornChang的博客

监控web服务并邮件提醒

首先说下在基于Linux操作系统的邮件发送。

Linux操作系统的邮件发送

简单的说使用mailX:

1.安装mailx

yum  install  mailx -y

2.测试基本邮件发送

命令行: mail -s "theme" emailAddress,回车后输入内容按Ctrl+D发送邮件.
管道符: echo "mail main content" | mail -s "theme" emailAddress
文件内容作为邮件内容: mail -s "theme" emailAddress < /tmp/t.txt

具体的关于sendEmail的设置之类的这里就不说了,可以参考下面两个博文: https://www.cnblogs.com/liutao97/p/8387244.html https://blog.csdn.net/lyf844692713/article/details/81479066

3.编写要进行监控的文件url

vim url

输入要进行监控的网址,例如博主要监控的是两个域名,所以文件中的内容是:

www.zhangruibin.com www.92cnb.com

4.编写监控脚本monitor.sh

本脚本采摘自网络,部分自己修改(面向百度编程),如有侵权请告知:

 #!/bin/bash
  #监控web服务并邮件提醒
  while true
  do
      Mail="itbin@foxmail.com"
      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.启动脚本,将echo输出至指定文件monitor.log

nohup sh monitor.sh >>monitor.log &

6.退出系统之后重新登录查看脚本运行状态

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.查看monitor.log及邮箱验证

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.....
itbin@foxmail.com... 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:<root@zhrb.localdomain> SIZE=304
250 2.1.0 Ok
>>> RCPT To:<itbin@foxmail.com>
>>> DATA
250 2.1.5 Ok
354 End data with <CR><LF>.<CR><LF>
>>> .
250 2.0.0 Ok: queued as 5C4001D6E
itbin@foxmail.com... Sent (Ok: queued as 5C4001D6E)
Closing connection to [127.0.0.1]
>>> QUIT
221 2.0.0 Bye

并且邮箱中收到了邮件,验证成功。

Over!

亲,博主的微信公众号

‘程序员小圈圈’开始持续更新了哟~~

长按图片识别二维码或者微信扫描二维码或者直接搜索名字 ‘CXYXQQ’ 即可关注本公众号哟~~

不只是有技术哟~~

还可以学下教育知识以及消遣娱乐哟~~

求关注哟~~