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
  • 相关阅读:
    ProtoBuf开发者指南(转)
    kafka的c/c++高性能客户端librdkafka简介
    SQL的执行顺序:
    zookeeper与卡夫卡集群搭建
    记一次ping: unknown host错误
    nginx服务器的rewrite功能
    nginx做http向https的自动跳转
    jQuery数据缓存
    jQuery的无new构建
    位操作符的计算优势
  • 原文地址:https://www.cnblogs.com/dengcongcong/p/9132901.html
Copyright © 2011-2022 走看看