Zabbix implements WeChat alarm function, and zabbix implements alarm

Source: Internet
Author: User

Zabbix implements the alarm function and zabbix implements the alarm function.

1. Apply for an enterprise account, Application address https://qy.weixin.qq.com/


2. log on to the enterprise account

Figure 1

Figure 2

2. Add an account

Figure 1

Figure 2

After completing the preceding steps, the account is added.

3. Create an application

Figure 1

Figure 2

Figure 3

Figure 4

After the above four images are completed, the application is created.

Iv. Set permission management

Figure 1

Figure 2

Figure 3

After you complete the operations on the above three images, the permission management settings are complete. This setting has been completed!

V. Zabbix Server Configuration

Figure 1

Figure 2

Figure 3

The configuration of zabbix server has been completed.

VII. weixin. py program content

#! /Usr/bin/env python # encoding: UTF-8 # Create time 2016-10-08 # Auth chenpengimport urllib2import jsonimport sysimport timeclass WebChat (object): def _ init _ (self, CropID, Secret): self. cropID = CropIDself. secret = Secretdef Get_Token (self, info): ''': param info: stores the execution result and status code of the execution program. (0 indicates that the execution is successful, and non-zero indicates that the execution is unsuccessful.): return: '''self. info = infogurl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken? Corpid = % s & Cortana cret = % 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'] = 0 else: self.info ['result'] = g_resultself.info ['code'] = 1 failed t Exception, e: self.info ['code'] = 1self.info ['result'] = edef Send_Msg (self, touser, toparty, agentid, access_token, content, info, * args, ** kwargs): ''' send information to: param touser: department member id, list of recipients and member IDs defined in zabbix. Multiple recipients are separated by '|'. A maximum of 1000 recipients are supported ). In special cases: if it is specified as @ all, all members of the enterprise application will be sent: param toparty: department id, which defines the range. all members in the group can receive the message and department ID list, multiple receivers are separated by '|'. A maximum of 100 recipients are supported. When touser is @ all, ignore this parameter: param agentid: the id and integer of the enterprise application. You can view the following information on the application settings page: param access_token: the access token value obtained by the CropID and Secret: param content: filters out the third parameter passed by zabbix, which indicates the content of the message sent, the maximum length is 2048 bytes. Note: the text message pushed by the homepage application can only display 20 characters (including Chinese and English) at the end: param info: return execution result information {'result': None, 'code': None}; 'code': 0 or non-zero; 0 indicates success. Non-zero indicates failure: param args: param kwargs:: return: ''' self. touser = touserself. toparty = topartyself. agentid = agentidself. conntent = contentself. access_token = access_tokenself.info = infopur L = "https://qyapi.weixin.qq.com/cgi-bin/message/send? Access_token = % s "% (access_token) data = {" touser ":" "," toparty ":" "," totag ":" ", # tag ID list, multiple receivers are separated by '|'. A maximum of 100 recipients are supported. This parameter is ignored when touser is @ all. It is not required to "msgtype": "text", # Must "agentid": "", # Must "text": {"content ": "" # required}, "safe": "0" # indicates whether the message is confidential, 0 indicates no, and 1 indicates yes. The default value is 0} data ['touser'] = self. touserdata ['agentid'] = self. agentiddata ['toparty '] = self. topartydata ['text'] ['content'] = self. conntentdata = json. dumps (data, ensure_ascii = False) try: # Get the sent data req = urllib2.Request (purl, data) response = urllib2.urlopen (req) res = json. loads (response. read () self.info ['code'] = res ['errcode'] self.info ['result'] = res ['errmsg '] failed t Exception, e: self.info ['result'] = eself.info ['code'] = 1if _ name _ = '_ main _': reload (sys) sys. setdefaultencoding ('utf-8') def log (date, touser, content, info): ''' output log: param date: Time: param touser: sent: param content: sent message content: param info: Result of sending execution: return: '''msg = '% s sending result-% s \ n' % (date, touser, content, info) with open ('msg. log', 'A') as f: f. write (msg) agentid = sys. argv [1] # agentid = 1 touser = 'xxxxxxx @ qq.com 'toparty = ''content = sys. argv [2:] content = '\ n '. join (content) # content = 'test' CropID = 'xxxxxxxxxxxxxxxxxxxx' Secret = 'secret' 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) if info ['code'] = 0: content = eval (content) log (date, touser, content, info) else: log (date, touser, content, info) else: log (date, touser, content, info)

The CropID and Secret in lines 114 and 115 correspond to the CropID and Secret in step 3 "set permission management ".

For data in line 63 of the Code, see the interface documentation.

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

The above is a small series of Zabbix to introduce you to implement the alarm function, I hope to help you, if you have any questions, please leave a message, the small series will reply to you in a timely manner. Thank you very much for your support for the help House website!

Related Article

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.