用于网站监控的shell脚本

分享一个用于监控网站运行情况的shell脚本,功能和结构都很简单,但很大程度上能够让监控自动化。此脚本的作用是按固定频率不停访问给出的URL,当网站不可访问时自动给设定邮箱发送告警邮件以通知用户。

脚本的完整内容如下:

#!/bin/sh
# Author:       Bill JaJa
# Purpose:      Moniter the web status, send a alert mail when it's not available.
export LANG=C

EMAIL=”<a href="mailto:EMAIL=”xxx@xxxmail.com">xxx@xxxmail.com</a>”
LOG_FILE="/var/monitor/log/web_status_`date '+%Y%m'`.log"
TMP_EMAIL="/var/monitor/.tmp.mail.`date '+%s'`"
URL="$1" # DO NOT CHANGE IT.

if [ $2 ]
then
        sleep $2
fi

# Define function "ECHO", append the timestamp at the head of every record.
ECHO () {
printf "%s " `date '+%Y-%m-%d %H:%M:%S'`
echo $1
}

# Define function HTTP_CODE, obtain the status of web service.
HTTP_CODE () {
http_code=`curl -m 20 -o /dev/null -s -w %{http_code} $URL`
}

# Define function MAIL.
MAIL () {
echo "$URL is not available now, pls pay attention." &gt; $TMP_EMAIL
echo "And the Server 's time is: " &gt;&gt; $TMP_EMAIL
date &gt;&gt; $TMP_EMAIL
echo &gt;&gt; $TMP_EMAIL
echo "------" &gt;&gt; $TMP_EMAIL
echo "BR" &gt;&gt; $TMP_EMAIL
echo "Shell Robot." &gt;&gt; $TMP_EMAIL
mail -s "Server Alert: $URL" $EMAIL &lt; $TMP_EMAIL
rm $TMP_EMAIL
}

n=0
HTTP_CODE
if [ $http_code -eq 200 ]
then
        ECHO "|http_code:200|+$n|webpage visit success.|$URL" &gt;&gt; $LOG_FILE
else
        while [ $http_code -ne 200 ]
        do
                n=`expr $n + 1 `
                ECHO "|http_code:$http_code|+$n|webpage visit failed. |$URL" &gt;&gt; $LOG_FILE
                if [ $n -eq 5 ]; then
                        MAIL $1; exit 0
                fi
                sleep 10
                HTTP_CODE
        done
fi
# THE END.

这段脚本的核心其实只有如下一行:

http_code=`curl -m 20 -o /dev/null -s -w %{http_code} $URL`

也就是使用Linux自带的curl工具访问给出网址,并返回http_code,当http_code不等于200时即可认为网站存在访问故障,但考虑到实际的不稳定因素,当第一次返回http_code不等200时,脚本休眠10秒钟,然后继续访问,累计连续不可访问次数达到5次后触发告警邮件。

可配置字段,脚本第二段中有如下三个可配置字段:

EMAIL:接收告警信息的email地址。
LOG_FILE:日志文件,建议使用绝对路径;最后的web_status_`date ‘+%Y%m’`.log代表以月为单位分割日志,如web_status_201105.log
TMP_EMAIL:临时邮件文件,同样建议使用绝对路径,保持所在目录有写权限即可。

使用方法:

脚本后面需跟两个参数:第一个参数为监控网站的URL,第二个参数为延时时间(可选,以秒为单位,建议在对多个网站进行监控时添加),将此脚本添加到操作系统的crontab里面,按需求设定运行频率,建议2分钟一次。若有多个网站需监控,在crontab里面添加多行即可,每个URL一行,如下为同时对三个网站进行监控:

*/2 * * * * sh /var/monitor/web_monitor.sh http://www.qq.com
*/2 * * * * sh /var/monitor/web_monitor.sh http://www.qqq.com 5
*/2 * * * * sh /var/monitor/web_monitor.sh http://www.qqqq.com 10

同时此脚本会将所有访问结果记录至日志文件,日志片段如下:

[bill@JAJA log]$ tail -f web_status_201105.log
2011-05-23 19:28:01 |http_code:200|+0|webpage visit success.|http://www.qq.com
2011-05-23 19:28:05 |http_code:200|+0|webpage visit success.|http://www.qqq.com
2011-05-23 19:28:11 |http_code:200|+0|webpage visit success.|http://www.qqqq.com
2011-05-23 19:28:20 |http_code:200|+0|webpage visit success.|http://www.qq.com
2011-05-23 19:28:20 |http_code:200|+0|webpage visit success.|http://www.qqq.com
2011-05-23 19:30:01 |http_code:200|+0|webpage visit success.|http://www.qqqq.com

补充:使用此脚本前请确保系统的邮件服务可行,Linux 邮件服务为sendmail
查看邮件服务运行状态的命令:service sendmail status
启用邮件服务:service sendmail start

在Github上查看源代码:https://github.com/bostoneboy/shell-scripts/blob/master/web-monitor.sh