1

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

我已经编写了一个用于 url 检查的 shell 脚本,并使用 cronjob 每 30 分钟运行一次,并按照要求合并。

实际要求是:

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

该脚本有效。

我的要求是,由于我正在学习 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
4

1 回答 1

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
于 2013-11-06T05:15:06.237 回答