Zabbix realizes WeChat alarm function

Source: Internet
Author: User
first, apply for business account, apply for address https://qy.weixin.qq.com/


Second, the Landing enterprise account

Figure A

Figure II

2. Add Account

Figure A

Figure II

Complete the above steps and then complete the account add

Iii. New applications

Figure A

Figure II

Might

Figure Four

The above four pictures are completed and the application is created

Iv. Setting up Rights Management

Figure A

Figure II

Might

Complete the above three map operation, the Rights management settings are complete; This setting is complete!

V. Zabbix Server Configuration

Figure A

Figure II

Might

Complete the configuration in the above three diagrams, the configuration of Zabbix server is complete.

Vii. contents of weixin.py Program

#!/usr/bin/env python# encoding:utf-8# Create time 2016-10-08#auth chenpengimport urllib2import jsonimport sysimport Tim Eclass webchat (object):d EF __init__ (self,cropid,secret): self. Cropid = Cropidself.secret = Secretdef Get_token (self,info): "':p Aram Info: Store execution results and execute program status code code (0 for execution success, non-zero for unsuccessful): return: ' Self.info = Infogurl = ' https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s '% (self. Cropid,self. Secret) Try: #通过Get方式获取tokenreq = urllib2. Request (gurl) response = Urllib2.urlopen (req) G_result = Json.loads (Response.read (), "UTF-8") if G_result. Has_key (' Access_token '): self.info[' result ']= g_result [' Access_token ']self.info[' code '] = 0else:self.info[' result '] = G_ resultself.info[' Code ' = 1except exception,e:self.info[' code '] = 1self.info[' result ' = Edef send_msg (self,touser, Toparty,agentid,access_token,content,info,*args,**kwargs): "Send message to: param Touser: The recipient defined in the Department member Id,zabbix, List of member IDs (message recipients, multiple recipients with ' | ' up to 1000). Special case: Designated as @all, send to all members who are concerned about the Enterprise app: param toparty: Department ID, defined scope, group members areCan receive message, Department ID list, multiple receivers with ' | ' Separated, up to 100 supported. This parameter is ignored when Touser is @all: param Agentid: The ID of the Enterprise app, integral type. Available on the app's settings page: param access_token: Access token value obtained from Cropid,secret: param content: Filters out the third parameter of the Zabbix Pass, which represents the content of the message being sent, Up to 2048 bytes in length, note: The text message on the home page app pushes up to 20 words in the end (contains both Chinese and English):p Aram info: Returns the execution result information {' result ': None, ' Code ': none}; ' Code ': 0 or not 0; 0 indicates success nonzero indicates failure: param args::p Aram kwargs::return: ' self.touser = Touserself.toparty = Topartyself.agentid = Agentidself.conntent = Contentself.access_token = Access_tokenself.info = Infopurl = "https://qyapi.weixin.qq.com/ cgi-bin/message/send?access_token=%s "% (access_token) data = {" Touser ":" "," Toparty ":" "," Totag ":" ", #标签ID列表, multiple receivers with ' | ' Separated, up to 100 supported. When Touser is @all, this parameter must not be "Msgtype": "Text", #必须 "Agentid": "", #必须 "text": {"Content": "" #必须}, "safe": "0" # Indicates whether it is a confidential message, 0 indicates no, 1 means yes, default 0}data[' touser ' = self.touserdata[' agentid '] = self.agentiddata[' toparty '] = self.topartydata[' text ' [' Content ']=self.conntentdata = json.dumps (data,ensure_ascii=false) Try: #通过PUT方式获取发送数据req = urllib2. Request (purl, data) response= Urllib2.urlopen (req) res = json.loads (Response.read ()) self.info[' Code ' = res[' errcode ']self.info[' result '] = res[' ErrMsg ']except exception,e:self.info[' result ' = eself.info[' code '] = 1if __name__ = = ' __main__ ': Reload (SYS) Sys.setdefaultencoding (' utf-8 ') def log (date, Touser, content,info): "Send log print log: param date: Time: param Touser: Who to send: Param content: Information sent: param info: Send execution Result: return: ' msg = '%s%s%s ' send result-%s\n '% (date, touser, content, info) with open (' Msg.log ', ' a ') as F:f.write (msg) Agentid = sys.argv[1] #agentid = 1touser = ' xxxxxxx@qq.com ' toparty = ' content = sys.argv [2:]content = ' \ n '. Join (content) #content = ' Test ' cropid = ' xxxxxxxxxxxxxxxxxxx ' Secret = ' Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ' info={' result ': None, ' code ': none}date = Time.strftime ('%y-%m-%d%H:% m:%s ') res=webchat (Cropid,secret) Res. Get_token (Info) If info[' code '] = = 0:access_token = info[' result ']res. Send_msg (Touser=touser, Toparty=toparty, Agentid=agentid, Access_token=access_token,content=content,info=info) ifinfo[' code ' = = 0:content = eval (content) log (date, Touser, Content,info) else:log (date, touser, content, info) else:log ( Date,touser,content,info)

where code 114, 115 lines of cropid and Secret corresponds to the fourth step of "set rights Management" in figure three corresponding cropid and Secret

Data in line 63 of the Code, refer to the interface documentation

Address: Http://qydev.weixin.qq.com/wiki/index.php?title=%E5%8F%91%E9%80%81%E6%8E%A5%E5%8F%A3%E8%AF%B4%E6%98%8E

The above is a small part of the Zabbix to introduce the implementation of the alarm function, I hope we have some help, if you have any questions please give me a message, small series will promptly reply to you. Thank you very much for the support of the Scripting House website!

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.