zoukankan      html  css  js  c++  java
  • shell 监控网站是否异常的脚本

    shell 监控网站是否异常的脚本,如有异常自动发电邮通知管理员。

    流程:
    1.检查网站返回的http_code是否等于200,如不是200视为异常。
    2.检查网站的访问时间,超过MAXLOADTIME(10秒)视为异常。
    3.发送通知电邮后,在/tmp/monitor_load.remark记录发送时间,在一小时内不重复发送,如一小时后则清空/tmp/monitor_load.remark。

    #!/bin/bash
    
    SITES=("http://web01.example.com" "http://web02.example.com") # 要监控的网站
    NOTICE_EMAIL='me@example.com'                                 # 管理员电邮
    MAXLOADTIME=10                                                # 访问超时时间设置
    REMARKFILE='/tmp/monitor_load.remark'                         # 记录时否发送过通知电邮,如发送过则一小时内不再发送
    ISSEND=0                                                      # 是否有发送电邮
    EXPIRE=3600                                                   # 每次发送电邮的间隔秒数
    NOW=$(date +%s)
    
    if [ -f "$REMARKFILE" ] && [ -s "$REMARKFILE" ]; then
        REMARK=$(cat $REMARKFILE)
        
        # 删除过期的电邮发送时间记录文件
        if [ $(( $NOW - $REMARK )) -gt "$EXPIRE" ]; then
            rm -f ${REMARKFILE}
            REMARK=""
        fi
    else
        REMARK=""
    fi
    
    # 循环判断每个site
    for site in ${SITES[*]}; do
    
        printf "start to load ${site}
    "
        site_load_time=$(curl -o /dev/null -s -w "time_connect: %{time_connect}
    time_starttransfer: %{time_starttransfer}
    time_total: %{time_total}" "${site}")
        site_access=$(curl -o /dev/null -s -w %{http_code} "${site}")
        time_total=${site_load_time##*:}
    
        printf "$(date '+%Y-%m-%d %H:%M:%S')
    "
        printf "site load time
    ${site_load_time}
    "
        printf "site access:${site_access}
    
    "
    
        # not send
        if [ "$REMARK" = "" ]; then
            # check access
            if [ "$time_total" = "0.000" ] || [ "$site_access" != "200" ]; then
                echo "Subject: ${site} can access $(date +%Y-%m-%d' '%H:%M:%S)" | sendmail ${NOTICE_EMAIL}
                ISSEND=1
            else
                # check load time
                if [ "${time_total%%.*}" -ge ${MAXLOADTIME} ]; then
                    echo "Subject: ${site} load time total:${time_total} $(date +%Y-%m-%d' '%H:%M:%S)" | sendmail ${NOTICE_EMAIL}
                    ISSEND=1
                fi
            fi
        fi
    
    done
    
    # 发送电邮后记录发送时间
    if [ "$ISSEND" = "1" ]; then
        echo "$(date +%s)" > $REMARKFILE
    fi
    
    exit 0


  • 相关阅读:
    忽然背后冒冷汗
    随机获取中国境内ip地址的php代码
    复制粘贴的句子
    Winform使用BackGroundWorker代替线程执行后台代码
    在IE中测试调用Web Service
    在存储过程中编写正确的事务处理代码
    【转】使用HttpWebRequest POST图片等文件,带参数
    LINQ to Entities 不识别方法"System.String ToString()"
    Asp.net 出现:HTTP 错误 404.0 Not Found
    C# 用内存映射文件读取大日志文件(.log)
  • 原文地址:https://www.cnblogs.com/fdipzone/p/3715107.html
Copyright © 2011-2022 走看看