如果URL仅关闭2次,则发送电子邮件

时间:2013-11-03 15:33:02

标签: bash shell

我们想检查网址是否已关闭。但有时候,环境会因维护3-4小时而停机,我们不想在此期间继续发送电子邮件。

我已经为url检查编写了一个shell脚本,并使用cronjob每30分钟运行一次,并将其纳入下面的要求。

实际要求是:

  1. 检查网址是否已启动。如果网址已关闭,请发送电子邮件。
  2. cronjob将再次执行该脚本。如果步骤1发送了电子邮件,然后再次发送电子邮件,询问环境是否在维护中?
  3. cronjob将再次执行该脚本。如果它仍然失效,请不要做任何事情。
  4. 继续检查网址,如果有响应则不要做任何事情。但是按照步骤1-3再次关闭。
  5. 脚本有效。

    我的请求是,请您查看并建议是否有更好的方法来编写脚本,因为我正在学习shell脚本,但不知道所有可用的选项。

    #!/bin/bash
    #Checking urls from urls.txt
    MAddr="jsing002@internalsvcs.pte.com"
    TIME=`date +%d-%m-%Y_%H.%M.%S`
    SCRIPT_LOC=/user/inf/ete4/eteabp4/eid_scripts/jsing002
    for url in `awk '{print $1}' $SCRIPT_LOC/urls.txt`
    do
        /usr/bin/wget -t 0 --spider --no-check-certificate $url > wget.output  2>&1
        HTTPCode=`(/usr/bin/wget -t 0 --spider --no-check-certificate $url) 2>&1 | grep HTTP| tail -1|cut -c 41-43`
            ENV=`(grep $url $SCRIPT_LOC/urls.txt | awk '{print $2}')`
            echo $HTTPCode
            E1=`/bin/grep -ise  'refused' -ise 'failed'  wget.output`
            if [ "$E1" != "" ] || [ $HTTPCode -ge 500 ]
                then
                        status="DOWN"
                        echo "Step 1"
                        echo "${ENV}""_DOWN"
    
                        if [ -f "${ENV}""_DOWN" ];
                            then
                                echo "step 2"
                                echo "Please check if $ENV in Maintanance window.The check for $url has failed twice.Next The next failure email will be sent if preceding test was SUCCESSFUL" | /bin/mail -s "Is $ENV in Maintanance Window ?" $MAddr
                                mv "${ENV}""_DOWN" "${ENV}""_DOWN""_2"
                                echo "Step 3"
                            elif [ -f "${ENV}""_DOWN""_2" ];
                                then
                                    echo "this is elif statement"
    
                            else
                                echo "E1 is empty. Site is down"
                                echo "Site is down. $url is not accessible" | /bin/mail -s "$ENV is $status" $MAddr
                                touch  "${ENV}""_DOWN"
                            fi
    
                else    
                            if [ $HTTPCode -eq 200 ]
                                then
                                    status="UP"
                                    echo $status
                                    rm "${ENV}""_DOWN""_2"
                            fi
            fi
    done
    

    urls.txt的内容:

    http://mer01bmrim:30270/rim/web         E2E-RIMLITE4
    http://mer01csmap:18001/console         ABP_WL-E2E1
    http://mer02sitap:18051/console         ABP_WL-E2E2
    http://mer03sitap:18101/console         ABP_WL_E2E3
    

1 个答案:

答案 0 :(得分:0)

我就是这样做的......我没有测试它,所以它可能包含错误...

#!/bin/bash
#Checking urls from urls.txt

# destination address for mail
MAddr="jsing002@internalsvcs.pte.com"

# directory with the last operational stats of the sites
TMP="/tmp/site_stats" ; mkdir -p "$TMP"

#the mail that gets send the first time
Mail1() { cat<<EOF | /bin/mail -s "$ENV is Down" "$MAddr"

Site is down. $URL is not accessible

EOF
}

#the mail that gets send the second time
Mail2() { cat <<EOF | /bin/mail -s "is $1 in Maintenance Windows ?" "$MAddr"

Please check if $ENV in Maintanance window.
The check for $URL has failed twice.
The next failure email will be sent if preceding test was SUCCESSFUL

EOF
}

#the logic
while read URL ENV
do
    touch "$TMP/$ENV"
    if wget -O /dev/null "$URL" &>/dev/null
    then
        echo "up" > "$TMP/$ENV"
    else
        case $( cat "$TMP/$ENV" ) in 
        "maint." ) ;;
        "down"   ) Mail2 "$URL" "$ENV" ; echo "maint." >"$TMP/$ENV" ;;
        "up"     ) Mail1 "$URL" "$ENV" ; echo "down" >"$TMP/$ENV" ;;
        esac
    fi
done < urls.txt