自动化监控Zabbix运维驿站IT在线课程

zabbix企业微信发送告警脚本,解决企业号升级后告警发送失败问

2017-06-07  本文已影响290人  Zero___

最近企业微信企业号升级为企业微信,导致了原先使用微信企业号发送告警的用户无法正常发送告警。这里提供已经测试通过的脚本以供参考。

微信告警配置可以参考:http://www.jianshu.com/p/9ae2900f4029

微信企业号API:https://work.weixin.qq.com/api/doc#10167

企业微信告警脚本:
调整这一行Gtoken=$(/usr/bin/curl -s -G $GURL | awk -F" '{print $10}'),升级后字段位置调整,把原来的$4改为$10即可。
还有printf '\t"agentid": '"$AppID"",\n" ,这里要求是整形,不能有空格

#!/bin/bash
###SCRIPT_NAME:weixin.sh###
###send message from weixin for zabbix monitor###

CropID='xxxxxxxxxxxxxxxxxxxxxxxx'
Secret='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret"
Gtoken=$(/usr/bin/curl -s -G $GURL | awk -F\" '{print $10}')   ###升级后字段位置调整,$4改为$10即可

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

function body() {
        local int AppID=1                       ###APP ID
        local UserID=$1                         ###USER ID
        local PartyID=2                         ###PARTY ID
        local Msg=$(echo "$@" | cut -d" " -f3-)
        printf '{\n'
        printf '\t"touser": "'"$User"\"",\n"
        printf '\t"toparty": "'"$PartyID"\"",\n"
        printf '\t"msgtype": "text",\n'
        printf '\t"agentid": '"$AppID"",\n"    ###注意这里的字段类型是整型,不能有空格
        printf '\t"text": {\n'
        printf '\t\t"content": "'"$Msg"\""\n"
        printf '\t},\n'
        printf '\t"safe":"0"\n'
        printf '}\n'
}
/usr/bin/curl --data-ascii "$(body $1 $2 $3)" $PURL

升级前的微信企业号告警脚本:

#!/bin/bash
###SCRIPT_NAME:weixin.sh###
###send message from weixin for zabbix monitor###

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

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

function body() {
        local int AppID=1                       ###APP ID
        local UserID=$1                         ###USER ID
        local PartyID=2                         ###PARTY ID
        local Msg=$(echo "$@" | cut -d" " -f3-)
        printf '{\n'
        printf '\t"touser": "'"$User"\"",\n"
        printf '\t"toparty": "'"$PartyID"\"",\n"
        printf '\t"msgtype": "text",\n'
        printf '\t"agentid": '"$AppID"",\n"
        printf '\t"text": {\n'
        printf '\t\t"content": "'"$Msg"\""\n"
        printf '\t},\n'
        printf '\t"safe":"0"\n'
        printf '}\n'
}
/usr/bin/curl --data-ascii "$(body $1 $2 $3)" $PURL

上一篇下一篇

猜你喜欢

热点阅读