使用Shell脚本的方式监控WEB服务状态

实际工作中我们需要知道部署在服务器上的应用有没有问题,但是人为的操作太麻烦,最简单的方式就是sehll脚本的方式,下面为大家分享一下使用Shell脚本监控WEB服务状态具体方法。

使用Shell脚本的方式监控WEB服务状态插图

安装sendmail来发邮件

# yum -y install sendmail

# /etc/init.d/sendmail start

# chkconfig sendmail on

安装mutt邮件客户端,并设置相关信息

# yum -y install mutt

# vim /etc/Muttrc

set charset="utf-8"           #设置发邮件编码

set envelope_from=yes

set rfc2047_parameters=yes    #解决附件乱码问题

set realname="报警"           #发件人别名

set use_from=yes              #指定是否显示别名

set [email protected]     #发送人地址

脚本如下

#!/bin/bash
Mail="[email protected]"
FailCount=0
Retval=0 GetUrlStatus() { for ((i=1;i#使用i++判断访问次数,如果wget两次超时则判断网站异常
do
wget -T 3 --tries=1 --spider http://${1} >/dev/null 2>&1  #-T超时时间,--tries尝试1次,--spider蜘蛛
[ $? -ne 0 ] && let FailCount+=1; #访问超时时,$?不等于0,则FailCount加1
done
if [ $FailCount -gt 1 ];then
Retval=1
Date=`date +%F" "%H:%M`  echo -e "Date : $Date Problem : $url is not running." | mutt -s "URL Monitor" $Mail
else
Retval=0 fi
return $Retval  #如果返回值为0,就正常退出循环,不为0则继续循环
}for url in `cat url | sed '/^#/d'`do
#GetUrlStatus $url && echo yes || echo no
GetUrlStatus $urldonesleep 2m   #死循环,设置没2分钟运行一次don
THE END