zabbix微信报警脚本%被转义之修复

原来微信的报警脚本:

#!/bin/bash
CropID='企业id'
Secret='app secret'
GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret"
Gtoken=$(/usr/bin/curl -s -G $GURL|awk -F\" '{print $10}' )
PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Gtoken"
function body() {
local int AppID=1
local UserID=$1
local PartyID=1
local Msg=$(echo "$@" | cut -d" " -f3-)
printf '{\n'
printf '\t"touser": "'"$UserID"\"",\n"
printf '\t"toparty": "1",\n'
printf '\t"totag": "1",\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

如果报警消息中含有%,将会被转义,微信发送消息的结果中不能够正确的显示,以下是修改后的脚本,将不用之前的body方法:
#!/bin/bash
CropID='企业id'
Secret='app secret'
GURL="https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$CropID&corpsecret=$Secret"
Gtoken=$(/usr/bin/curl -s -G $GURL|awk -F\" '{print $10}' )
PURL="https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$Gtoken"
text(){
        userid=$1
        appid=5
        partyid=4
        Msg=$3
        message='{ "touser": "'$userid'", "toparty": "'$partyid'", "totag": "1", "msgtype": "text", "agentid": "'$appid'", "text": { "content": "'$Msg'" }, "safe":"0" }'
        echo "$message"
}
echo $(text $1 $2 "$3")
/usr/bin/curl --data-ascii "$(text $1 $2 "$3")" $PURL

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个基于 Python 实现的 Zabbix 微信报警脚本,需要先安装 WeixinBot 和 requests 库: ```python #!/usr/bin/env python # -*- coding: utf-8 -*- import requests import json import sys class WeChat(object): def __init__(self, corpid, corpsecret): self.url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken' self.corpid = corpid self.corpsecret = corpsecret self.params = { 'corpid': self.corpid, 'corpsecret': self.corpsecret } self.access_token = self.get_access_token() def get_access_token(self): r = requests.get(self.url, params=self.params) return r.json()['access_token'] def send_message(self, message, agentid, touser=None, toparty=None): url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send' data = { "touser": touser, "toparty": toparty, "msgtype": "text", "agentid": agentid, "text": { "content": message }, "safe": 0 } params = { 'access_token': self.access_token } headers = {'Content-Type': 'application/json;charset=utf-8'} r = requests.post(url, params=params, data=json.dumps(data), headers=headers) return r.json() if __name__ == "__main__": corpid = 'YOUR_CORPID' corpsecret = 'YOUR_CORPSECRET' agentid = 'YOUR_AGENTID' touser = sys.argv[1] subject = sys.argv[2] content = sys.argv[3] message = u"收件人:%s\n主题:%s\n内容:%s" % (touser, subject, content) wechat = WeChat(corpid, corpsecret) response = wechat.send_message(message, agentid, touser=touser) print(response) ``` 使用方法: 1. 用企业微信扫描二维码,加入企业; 2. 创建一个应用,并记录下应用的 `corpid`、`corpsecret`和 `agentid`; 3. 安装 WeixinBot 和 requests 库; 4. 将上述代码保存到文件 `wechat.py` 中; 5. 在 Zabbix 中配置告警媒介,将 `wechat.py` 的路径作为脚本名,并在参数中依次填入收件人、主题和内容。例如:`/usr/bin/python /path/to/wechat.py {ALERT.SENDTO} "{ALERT.SUBJECT}" "{ALERT.MESSAGE}"`; 6. 测试告警是否正常发送。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值