设为首页 收藏本站
查看: 792|回复: 0

[经验分享] Zabbix微信企业订阅号报警设置

[复制链接]

尚未签到

发表于 2019-1-18 14:13:25 | 显示全部楼层 |阅读模式
Zabbix微信企业订阅号报警设置

  官方提供了较全的api,使用个人订阅号测试时,发现很多接口没有权限,无法获取订阅者openid,导致无法发送消息,然后要来了公司的企业订阅号来进行报警。
  微信公众号登录:https://mp.weixin.qq.com/
  微信api参考:http://mp.weixin.qq.com/wiki/16/992df48524118c3e89945856694b30cc.html
  微信api在线调试:http://mp.weixin.qq.com/debug/
微信消息发送
1、获取access_token
  官方api文档:
  http://mp.weixin.qq.com/wiki/11/0e4b294685f817b95cbed85ba5e82b8f.html
  说明:
  根据应用ID和应用密钥,获取access_token,每日获取token上限2000次,token的有效期为2小时。
2、获取订阅者openid
  官方api文档:
  http://mp.weixin.qq.com/wiki/0/d0e07720fc711c02a3eab6ec33054804.html
  说明:
  官方api不能直接根据用户帐号进行获取openid,而是通过获取所有的openid,在遍历openid找到用户名,最后得到用户名与openid的对应关系,然后把接受报警的用户的openid找到,并记录。
3、发送消息
  官方api文档:
  http://mp.weixin.qq.com/wiki/1/70a29afed17f56d537c833f89be979c9.html
  说明:
  群发每天限制了4条,只能使用客服接口发送消息,条数上限为50w。
  客服接口限制:当用户主动发消息给公众号的时候:(包括发送信息、点击自定义菜单、订阅事件、扫描二维码事件、支付成功事件、用户维权),开发者在一段时间内(目前修改为48小时)可以调用客服消息接口,通过POST一个JSON数据包来发送消息给普通用户,在48小时内不限制发送次数。所以48小时后,如果用户没有再触发上述事件,报警消息就无法发送。
4、微信报警python脚本
#!/usr/bin/env python
#coding=utf-8
#author: yangrong
#date: 2015-8-19
#本微信报警脚本应用于企业订阅号
#当用户主动发消息给公众号的时候(包括发送信息、点击自定义菜单、订阅事件、扫描二维
#码事件、支付成功事件、用户维权),微信将会把消息数据推送给开发者,开发者在一段时
#间内(目前修改为48小时)可以调用客服消息接口,通过POST一个JSON数据包来发送
#消息给普通用户,在48小时内不限制发送次数。

import os
import urllib2
import requests
import sys
import time
import json
import pickle
appid='wxc88**************'     
secret='b9b8925aaa0eafc***********'
token_file='/tmp/token_file.txt'
log_file='/tmp/wechat.log'
openid_user_file='/tmp/openid_user.txt'
openid_list=["omPAFj8PBaE4UbdOGmgjFfq-shFM",   #杨容
            "omPAFj27U-7PJkgYyHMk1wvDI27o",   #阿飞
            ]  #这是微信接收者的openid
#报警格式,脚本名  收件人  标题  内容
#这是zabbix发送内容格式,所以
#这里取出标题和内容就行了
#帮助信息,要求必须传参4个
if len(sys.argv) != 4:
  print 'Usage: %s mail-to title content'%sys.argv[0]
  print 'Example: '
  print '       %syangrong@jpush.cn  "this is testtitle"  "this is test content."'%sys.argv[0]
  sys.exit()
title=sys.argv[2]
content=sys.argv[3]
current_hour=time.strftime('%H',time.localtime(time.time()))   

#日志记录函数,把标题,用户id,状态记录
def log(title,openid,status):
         withopen(log_file,'ab') as f:
                   current_time=time.strftime('%Y-%m-%d%H:%M:%S',time.localtime(time.time()))
                   f.write('%s| %s | %s | %s\n'%(current_time,openid,status,title))

#获取token
class Token(object):
   def __init__(self, appid, secret):
       self.baseurl ='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}'.format(
           appid,secret)
       self.expire_time = sys.maxint
   def get_token(self):
       if self.expire_time > time.time():
           request = urllib2.Request(self.baseurl)
           response = urllib2.urlopen(request)
           ret = response.read().strip()
           ret = json.loads(ret)
           if 'errcode' in ret.keys():
                print >> ret['errmsg'],sys.stderr
                sys.exit(1)
           self.expire_time = time.time() + ret['expires_in']
           self.access_token = ret['access_token']
             token_pre=[current_hour,self.access_token]
             with open(token_file,'wb') as f:
                      pickle.dump(token_pre,f)
       return self.access_token
#access_token = Token(appid=appid,secret=secret).get_token()  #这是获取access_token的代码
#print access_token

#获取所有的openid,然后根据openid获取用户信息,提取出用户名,最后输出用户名与openid的对应关系。
class get_user():
         def__init__(self):
                   self.access_token=Token(appid,secret).get_token()
         defget_openid_list(self):
                   openid_list_url='https://api.weixin.qq.com/cgi-bin/user/get?access_token={0}&next_openid='.format(self.access_token)
                   request=urllib2.Request(openid_list_url)
                   response=urllib2.urlopen(request)
                   ret=response.read().strip()
                   openid_list= json.loads(ret)
                   #printopenid_list['data']['openid']
                   openid_list= openid_list['data']['openid']
                   foropenid in openid_list:
                            user_info_url='https://api.weixin.qq.com/cgi-bin/user/info?access_token={0}&openid={1}'.format(self.access_token,openid)
                            user_info_request=urllib2.Request(user_info_url)
                            user_info_response=urllib2.urlopen(user_info_request).read().strip()
                            user_info=json.loads(user_info_response)
                            if'errcode' in user_info.keys():
                                     print>> user_info['errmsg'],sys.stderr
                                     sys.exit()
                            withopen(openid_user_file,'wb') as f:
                                     f.write('openid:%s  nickname:%s'%(openid,user_info['nickname']))   
#使用post方式发送报警
def send_msg(title,content):
       #一天能够获取的access_token次数是2000次,每次取到的token有效时间2小时,所以pickle dump时,把当前小时数与access_token写入文件,每一小时获取一次token.
         current_hour=time.strftime('%H',time.localtime(time.time()))   
         ifnot os.path.exists(token_file):
                   access_token=Token(appid,secret).get_token()
         withopen(token_file,'rb') as f:
                   token_pre=pickle.load(f)
                   #print'token_pre:',token_pre
         access_token_pre=token_pre[1]
         current_hour_pre=token_pre[0]
         ifcurrent_hour == current_hour_pre:
                   access_token=access_token_pre
         else:
                   access_token=Token(appid,secret).get_token()
         #print'access_token:',access_token
       #循环openid_list,给每个成员单独推送微信消息
         foropenid in openid_list:
                   #print'openid:',openid
                   url='https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=%s'%access_token
                   payload={
                    "touser": '%s'%openid,
                      "msgtype": "text",
                      "text": {
                     "content": "Title: %s\nContent:%s"%(title,content)
                                }
                   }      
                   ret= requests.post(url, data=json.dumps(payload, ensure_ascii=False),verify=False)
                   result=ret.json()
                   #printresult
                   #如果这一次发送失败,则代表可能access_token有问题,删除pickle dump文件,重新生成一次access_token
                   if  result['errcode']:
                            log(title,openid,'sendfail')      
                            os.remove(token_file)
                            access_token=Token(appid,secret).get_token()
                   else:
                            log(title,openid,'sendsuccess')      
                   #printpost(url, data)

#get_user().get_openid_list()   #这是遍历所有openid,获取openid和用户名的对应关系。
send_msg(title,content)  #发送微信信息zabbix报警设置
1)把报警脚本放到zabbix报警路径下,默认是:
  /usr/local/zabbix/share/zabbix/alertscripts
2)在web添加报警介质

  

3)添加报警动作

  

4)设置用户报警

  





运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-664901-1-1.html 上篇帖子: zabbix应用之监控磁盘坏块(windows) 下篇帖子: zabbix lld结合pycurl批量监控网站服务质量
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表