#!/bin/bashsource `dirname $0`/lib/lib.conffunction num_mod_add(){        local ltask num taskdir        ltask=$1        taskdir=$2        num=$CONTINUER        lnum=`cat $counterfile |grep $ltask|awk -F'_' '{print $3}'`        [ -n "$lnum" ] || echo ${ltask}_1 >> $counterfile        let lnum++        sed -i 's/'$ltask'.*/'$ltask'_'$lnum'/g' $counterfile        if [ $lnum -eq $num ]        then                echo 0 $lnum > $taskdir/ret_add        elif [ $lnum -gt $num ]        then                echo 1 $lnum > $taskdir/ret_add        elif [ $lnum -lt $num ]        then                echo 1 $lnum > $taskdir/ret_add        fi}function num_del(){        local ltask num taskdir        ltask=$1        taskdir=$2        num=$CONTINUER        lnum=`cat $counterfile |grep $ltask|awk -F'_' '{print $3}'`        if [ "$lnum" != "" ]        then                if [ "$lnum" -lt "$((num-1))" ]                then                        sed -i '/'$ltask'/d' $counterfile                        let lnum++                        echo "1 $lnum $ltask" > $taskdir/ret_del                        echo nihao                elif [ "$lnum" -eq "$((num-1))" ]                then                        sed -i '/'$ltask'/d' $counterfile                        let lnum++                        echo "0 $lnum $ltask" > $taskdir/ret_del                elif [ "$lnum" -ge "$num" ]                then                        sed -i '/'$ltask'/d' $counterfile                        let lnum++                        echo "0 $lnum $ltask" > $taskdir/ret_del                fi        fi}function sendwarning(){        local ltask taskdir time state numwar curlret ret        ltask=$1        taskdir=$2        time=`date "+%F %T"`        CURL="/usr/bin/curl"        for i in `seq $tasknum`        do                task=task$i                eval task=\$$task                if [ $ltask == $task ]                then                        url=url$i                        eval url=\$$url                        $CURL -I --connect-timeout $EXECTIMEOUT "$url"|grep "HTTP/1.1 200 OK" &                        curlret=$?                        child=$!                        sleep 2 && kill -9 $child                        wait                        if [ $curlret -eq 0 ]                        then                                state="HTTP连接OK"                                sendrecovery $ltask $taskdir $state                        else                                num_mod_add $ltask $taskdir                                ret=`cat $taskdir/ret_add |awk '{print $1}'`                                numwar=`cat $taskdir/ret_add|awk '{print $2}'`                                state="HTTP连接异常"                                if [ $ret -eq 0 ]                                then                                        for phone in `echo $OP_PHONE|sed 's/,/ /g'`                                        do                                                sendtime=" (`date +%F' '%T`)"                                                URL="http://192.168.6.13/fetion.php?phone=PHONE&msg=MSG"                                                sendtask=`echo $ltask连续$numwar次异常 $sendtime|/usr/bin/perl -e 'use URI::Escape;while(
){print uri_escape($_);}'` ##拼装告警信息 MSGURL=`echo $URL|sed "s/PHONE/$phone/g;s/MSG/$sendtask/g;"` $CURL "$MSGURL" mon_log "$ltask" "$state" "$ltask连续$numwar次异常" "$time" "$phone" done else mon_log $ltask $state "$ltask连续$numwar次异常" "$time" fi fi fi done rm -rf $taskdir/*}function sendrecovery(){ local ltask phone taskdir time state numrec ltask=$1 taskdir=$2 state=$3 time=`date "+%F %T"` CURL="/usr/bin/curl" sendtime=" (`date +%F' '%T`)" num_del $ltask $taskdir ##发送短信接口 if [ -f $taskdir/ret_del ] then cat $taskdir/ret_del|grep $ltask if [ $? -eq 0 ] then ret=`cat $taskdir/ret_del|grep $ltask |awk '{print $1}'` if [ "$ret" -eq 0 ] then numrec=`cat $taskdir/ret_del|awk '{print $2}'` ####拼装告警信 URL="http://192.168.6.13/fetion.php?phone=PHONE&msg=MSG" sendtask=`echo "$ltask第$numrec次恢复" "$sendtime"|/usr/bin/perl -e 'use URI::Escape;while(
){print uri_escape($_);}'` for phone in `echo $OP_PHONE|sed 's/,/ /g'` do MSGURL=`echo $URL|sed "s/PHONE/$phone/g;s/MSG/$sendtask/g;"` $CURL "$MSGURL" mon_log "$ltask" "$state" "$ltask第$numrec次恢复" "$time" "$phone" done fi fi rm -rf $taskdir/* fi}function mon_log(){ local task time state msg phone task=$1 state=$2 msg=$3 time=$4 phone=$5 echo $phone $task $state $msg $time >> /tmp/amos_mon_task.log}function mon_task(){ local task taskdir for i in `seq $tasknum` do task=task$i eval taskdir=$dirname/\$$task [ -d $taskdir ] || mkdir -p $taskdir eval echo \$$task eval sendwarning \$$task $taskdir #& done# wait}mon_task