zoukankan      html  css  js  c++  java
  • 利用微信企业号的告警功能,联动检测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 -w "$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=$'
    '
            Date=$(date '+%Y年%m月%d日 %H:%M:%S
    
    ')
            Tit=$(cat /root/title.txt)
            Mes=$Date$1-$2-$3
    
            printf '{
    '
            printf '	"touser": "'"$User""",
    "
            printf '	"toparty": "'"$PartyID""",
    "
            printf '	"totag": "'"$TagID""",
    "
            printf '	"msgtype": "news",
    '
            printf '	"agentid": "'" $AppID """,
    "
            printf '	"news": {
    '
            printf '	"articles": [
    '
            printf '{
    '
            printf '		"title": "'"$Tit"","
    "
            printf '	"description": "'"$Mes"","
    "
            printf '	}
    '
            printf '	]
    '
            printf '	}
    '
            printf '}
    '
            }
    
            /usr/bin/curl --data-ascii "$(body $1 $2 $3)" $PURL
  • 相关阅读:
    requets中urlencode的问题
    洛谷$P4503 [CTSC2014]$企鹅$QQ$ 哈希
    洛谷$P5446 [THUPC2018]$绿绿和串串 $manacher$
    洛谷$P5329 [SNOI2019]$字符串 字符串
    洛谷$P1390$ 公约数的和 欧拉函数
    洛谷$P4318$ 完全平方数 容斥+二分
    入门懵逼钨丝繁衍
    $ CometOJ-Contest#11 D$ $Kruscal$重构树
    洛谷$P4884$ 多少个1? 数论
    入门数论简单总结
  • 原文地址:https://www.cnblogs.com/dengcongcong/p/9132901.html
Copyright © 2011-2022 走看看