利用微信企业号的告警功能,联动检测ICMP的shell脚本

  由于设备IP众多,为了及时发现IP地址有不可达现象,利用微信的联动报警,及时发现问题,以下是脚本内容!!!

ping.sh  
#!/bin/bash
###SCRIPT_NAME:icmp testing 
PATH=$PATH

ipfile="ip.txt"
declare -A ip_dic

for key in `cat $ipfile |awk '{print $1}'`
do
        ip_dic+=([$key]="0")
        done
        ping_ip(){
                dic_value=`echo ${ip_dic[$1]}`
                ping -c 3 -W 3 $1 > /dev/null 2>&1
                if [ $? -ne $dic_value ]
                then
                        S_IP=`cat $ipfile | grep "$1 "`
                        des=`cat $ipfile | grep ^"$S_ip "`
                        if [ $dic_value == 0 ]
                        then
                                ip_dic[$1]="1"
                                /bin/bash /root/wechat.sh "网络中断" "$S_IP" "$des"
                        else
                                ip_dic[$1]="0"
                                /bin/bash /root/wechat.sh "网络恢复" "$S_IP" "$des"
                        fi
                else
                        continue
                fi
}
while true 
do
        for ip in `cat $ipfile|awk '{print $1}'`
        do
              ping_ip $ip
        done
sleep 10
done
wechat.sh
#!/bin/bash
PATH=/usr/lib64/qt-3.3/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
###SCRIPT_NAME:weixin.sh###
###send message from weixin for icmp monitor###

CropID='your CropID '
Secret='your SecretID'
GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret"
Gtoken=$(/usr/bin/curl -s -G $GURL | awk -F'access_token\":\"' '{print $2}' | awk -F'"' '{print $1}')

PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Gtoken"

function body() {
        local int AppID=1000002
        local UserID=$1
        local PartyID=1
        local TagID=2
        Ent=$'\n'
        Date=$(date '+%Y年%m月%d日 %H:%M:%S\n\n')
        Tit=$(cat /root/title.txt)
        Mes=$Date$1-$2-$3

        printf '{\n'
        printf '\t"touser": "'"$User"\"",\n"
        printf '\t"toparty": "'"$PartyID"\"",\n"
        printf '\t"totag": "'"$TagID"\"",\n"
        printf '\t"msgtype": "news",\n'
        printf '\t"agentid": "'" $AppID "\"",\n"
        printf '\t"news": {\n'
        printf '\t"articles": [\n'
        printf '{\n'
        printf '\t\t"title": "'"$Tit"\","\n"
        printf '\t"description": "'"$Mes"\","\n"
        printf '\t}\n'
        printf '\t]\n'
        printf '\t}\n'
        printf '}\n'
        }

        /usr/bin/curl --data-ascii "$(body $1 $2 $3)" $PURL

猜你喜欢

转载自www.cnblogs.com/dengcongcong/p/9132901.html
今日推荐