zabbix3.0 微信报警

    xiaoxiao2026-04-12  5

    先有一个微信企业号 微信号怎么注册就不多说了。 https://qy.weixin.qq.com/ 要收微信的帐号必须关注该企业号成功 为了更好方便 我们要收集几个条件 一个部门ID: 一个应用ID: 一个CorpID: 一个Secret: 第一获取部门ID: 第二获取应用ID: 第三获取CorpID,Secret: 第四配置脚本 root@xxxxxxx:/usr/lib/zabbix/alertscripts# pwd /usr/lib/zabbix/alertscripts #vi wxpython.py #!/usr/bin/env python # -*- coding: utf-8 -*- import urllib,urllib2,json import sys reload(sys) sys.setdefaultencoding( "utf-8" ) class WeChat(object): __token_id = '' # init attribute def __init__(self,url): self.__url = url.rstrip('/') self.__corpid = 'xxxxx' self.__secret = 'xxxxx' # Get TokenID def authID(self): params = {'corpid':self.__corpid, 'corpsecret':self.__secret} data = urllib.urlencode(params) content = self.getToken(data) try: self.__token_id = content['access_token'] # print content['access_token'] except KeyError: raise KeyError # Establish a connection def getToken(self,data,url_prefix='/'): url = self.__url + url_prefix + 'gettoken?' try: response = urllib2.Request(url + data) except KeyError: raise KeyError result = urllib2.urlopen(response) content = json.loads(result.read()) return content # Get sendmessage url def postData(self,data,url_prefix='/'): url = self.__url + url_prefix + 'message/send?access_token=%s' % self.__token_id request = urllib2.Request(url,data) try: result = urllib2.urlopen(request) except urllib2.HTTPError as e: if hasattr(e,'reason'): print 'reason',e.reason elif hasattr(e,'code'): print 'code',e.code return 0 else: content = json.loads(result.read()) result.close() return content # send message def sendMessage(self,touser,message): self.authID() data = json.dumps({ 'touser':touser, 'toparty':"2", 'msgtype':"text", 'agentid':"1", 'text':{ 'content':message }, 'safe':"0" },ensure_ascii=False) response = self.postData(data) print response if __name__ == '__main__': a = WeChat('https://qyapi.weixin.qq.com/cgi-bin') a.sendMessage(sys.argv[1],sys.argv[3]) 修改的地方有14 15 65 67 四行的值 #chmod 777 wxpython.py 加权限 第五配置WEB 下面开始在WEB界面配置。 我的是zabbix 3.0 @all 是该组所有人。 可以指定单个 也可以多个。中间用|分开 权限 自己配置就好了 最后可以测试了。 测试方法 把监控的IP端口改掉,让服务器DOWN机器 发消息 脚本是参考 http://www.cnblogs.com/hanyifeng/p/5368102.html 最后是一个SHELL脚本。虽然没有测试成功。但是用用还是可以的 #!/bin/bash CropID='xxxxx' Secret='xxxxxx' 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 userid=$1; local groupid=2; local agentid=1; local msg=$(echo "$2" |cut -d"" -f3-); $local msg=$2; printf ' {\n' printf ' \t"touser": "'""$userid""\"",\n" printf ' \t"toparty": "'"$groupid"\"",\n" printf ' \t"msgtype": "'"text"\"",\n" printf ' \t"agentid": "'"$agentid"\"",\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)" $PURL 加权限就可以了。 调用方法 ./name 接收微信ID 接收内容 中间空格
    转载请注明原文地址: https://ju.6miu.com/read-1308767.html
    最新回复(0)