Zabbix微信企业订阅号报警设置 推荐

简介: Zabbix微信企业订阅号报警设置 官方提供了较全的api,使用个人订阅号测试时,发现很多接口没有权限,无法获取订阅者openid,导致无法发送消息,然后要来了公司的企业订阅号来进行报警。

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添加报警介质

 

wKiom1XcHTCAlzOYAAEMTBMykkA437.jpg


3)添加报警动作

 wKioL1XcH0bTtq6sAAC2VLMO60Q026.jpg


4)设置用户报警

 wKiom1XcHTGz_FkPAAEYeBgbspk980.jpg

 


相关文章
|
6月前
|
监控
zabbix设置邮件报警
zabbix设置邮件报警
|
8月前
|
小程序
UniApp video 使用(自定义进度条,及微信无法暂停播放设置进度问题)
UniApp video 使用(自定义进度条,及微信无法暂停播放设置进度问题)
794 0
|
4月前
|
Web App开发 Linux Android开发
Chrome谷歌浏览器的WeChat微信模拟器,既可以设置模拟很多型号的手机设备Mozilla
Chrome谷歌浏览器的WeChat微信模拟器,既可以设置模拟很多型号的手机设备Mozilla
|
7月前
|
JSON 小程序 数据格式
微信小程序 - 设置单个页面/导航栏/TabBar等背景颜色
微信小程序 - 设置单个页面/导航栏/TabBar等背景颜色
641 0
微信小程序 - 设置单个页面/导航栏/TabBar等背景颜色
|
4月前
|
自然语言处理 网络协议 中间件
手把手做一个公众号GPT智能客服【二】实现微信公众号回复(订阅送源码!)
手把手做一个公众号GPT智能客服【二】实现微信公众号回复(订阅送源码!)
53 0
手把手做一个公众号GPT智能客服【二】实现微信公众号回复(订阅送源码!)
|
4月前
微信小游戏制作工具中文字设置的粗体不显示,怎么解决?
微信小游戏制作工具中文字设置的粗体不显示,怎么解决?
37 1
|
4月前
微信小游戏制作工具中关于背景图的设置
微信小游戏制作工具中关于背景图的设置
97 0
|
9天前
|
监控 关系型数据库 应用服务中间件
zabbix自定义监控、钉钉、邮箱报警
zabbix自定义监控、钉钉、邮箱报警,实验准备,安装,添加监控对象,添加自定义监控项,监控mariadb,监控NGINX,钉钉报警设置,邮件报警
129 0
|
4月前
|
JSON 小程序 前端开发
微信公众号开发(三)设置底部菜单
填写access_token值,关于如何获取accesstoken值,请参见《微信公众号开发(二)微信公众号的access_token》 最后,将想要设置菜单的json写入body中。
108 0
|
6月前
|
小程序 前端开发 数据安全/隐私保护
微信小程序开发笔记—设置页面密码
本文主要介绍了微信小程序实现设置页面密码的功能实现
125 0

热门文章

最新文章

推荐镜像

更多