Zabbix-3.0.3 Implementation of WeChat (WeChat) Alarm

Source: Internet
Author: User

Zabbix-3.0.3 implementation (WeChat) Alarm
GuideZabbix can send alarm messages to specified persons in multiple ways. Common alarm methods include email and SMS. However, more and more enterprises begin to use zabbix as the main alarm method, in this way, the alert information can be pushed to the recipient in a timely and effective manner to facilitate timely handling of alerts.

1. Registration and use of enterprise numbers

Registration Enterprise: https://qy.weixin.qq.com/

2. Enterprise account usage tutorial2.1 Add employees to the address book

Log on to the newly created enterprise account. By adding the enterprise member information to the organization or department in advance, you must enter the mobile phone number, number, or email address, so that others can scan the code to follow the enterprise public account, push messages to enterprise members for the following enterprise numbers.

# Add an account and enter information

3. Create an application in the Application Center

# You can add different management groups to the visible range to receive messages pushed by the same application.

4. Set an administrator for a department

Set ---> function settings ----> permission management ----> Create a Management Group

The administrator needs to pay attention to the enterprise number and set the email address.

# Make sure that the Administrator has the permission to use the application to send messages. The Administrator's CorpID and Sercrt are required. (Important)
# Preparations:
Enterprise no.
The enterprise number has been noticed by Department members.
Enterprise ID has an application that can send messages (test-msg), an authorized Administrator (test-msg), and can use the application to send messages to members.
# You need to add the Administrator information first, and then make it follow the enterprise number
# Required information
Member account
Organization Department ID
Application ID
CorpID and Secret

5. Interface call

To call an interface, you need a credential: access_token.

Access_token can be obtained through CorpID and Secret

Enterprise Interface debugging address: http://qydev.weixin.qq.com/debug

6. script calling Principle

# Set the script execution path, edit the zabbix_server.conf file, and add a line

AlertScriptsPath=/opt/zabbix/share/zabbix/alertscripts
6.1 use Shell scripts

Get AccessToken

curl -s -G url

Call the enterprise ID interface for sending creden

curl --data url
[Root @ zabbix alertscripts] # cat wechat. sh #! /Bin/bash #################################### ###################################### File Name: wechat. sh # Author: shaonbean # Email: shaonbean@qq.com # Created Time: sun 24 Jul 2016 05:48:14 am cst ################################# ######################################## # Functions: send messages to wechat app # set variablesCropID = 'xxxxxx' Secret = 'm3fmhnfh8nti6sxlaebbllzaj-1BpZIyqkJRskeMMUXObGx4mfQsAg7 Jw-nUMXe9 'gurl = "https://qyapi.weixin.qq.com/cgi-bin/gettoken? Corpid = $ CropID & corpsecret = $ Secret "# get acccess_tokenGtoken = $ (/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 int AppID = 10 # application idlocal UserID =" touser "# department member id, the receiver's local PartyID = 8 # Department id defined in zabbix defines the range, all Group members can receive the message local Msg =$ (echo "$ @" | cut-d ""-f3 -) # filter out the third parameter printf' {\ n' printf' \ t "touser": "'" $ UserID "\" "passed by zabbix "\"", \ n "printf' \ t" toparty ":" '"$ PartyID" \ "", \ n "printf' \ t" msgtype ":" text ", \ n' printf' \ t "agentid": "'" $ AppID "\" ", \ 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 $! $2 $3) "$ PURL

# Http://qydev.weixin.qq.com/wiki/index.php? Title = Message Type and Data Format
# Test:

bash wechat.sh test hello.world!{"errcode":0,"errmsg":"ok","invaliduser":"all user invalid"}
6.2. python script

# Install simplejson

wget https://pypi.python.org/packages/f0/07/26b519e6ebb03c2a74989f7571e6ae6b82e9d7d81b8de6fcdbfc643c7b58/simplejson-3.8.2.tar.gztar zxvf simplejson-3.8.2.tar.gz && cd simplejson-3.8.2python setup.py buildpython setup.py install

# Download the wechat. py script

git clone https://github.com/X-Mars/Zabbix-Alert-WeChat.gitcp Zabbix-Alert-WeChat/wechat.py /opt/zabbix/share/zabbix/alertscripts/chmod +x wechat.py && chown zabbix:zabbix wechat.py

# Script Modification

#! /Usr/bin/python # _ * _ coding: UTF-8 _ * _ import urllib, urllib2import jsonimport sysimport simplejsondef gettoken (corpid, corpid cret): gettoken_url = 'https: // encode? Corpid = '+ corpid +' & Cortana cret = '+ Cortana cretprint gettoken_urltry: token_file = urllib2.urlopen (gettoken_url) failed t urllib2.HTTPError as e: print e. codeprint e. read (). decode ("utf8") sys. exit () token_data = token_file.read (). decode ('utf-8') token_json = json. loads (token_data) token_json.keys () token = token_json ['Access _ token'] return tokendef senddata (access_token, user, subject, content): send_url = 'https: // Qyapi.weixin.qq.com/cgi-bin/message/send? Access_token = '+ access_tokensend_values = {"touser": "touser", # the user account in the enterprise ID is configured in zabbix user Media. If the configuration is not normal, it will be sent by department. "Toparty": "8", # Department id in the enterprise number. "Msgtype": "text", # message type. "Agentid": "10", # Application id in enterprise number. "Text": {"content": subject + '\ n' + content}, "safe": "0"} # send_data = json. dumps (send_values, ensure_ascii = False) send_data = simplejson. dumps (send_values, ensure_ascii = False ). encode ('utf-8') send_request = urllib2.Request (send_url, send_data) response = json. loads (urllib2.urlopen (send_request ). read () print str (response) if _ name _ = '_ main _': user = str (sys. argv [1]) # subject = str (sys. argv [2]) # content = str (sys. argv [3]) # zabbix passed the third parameter corpid = 'xxxxxx' # CorpID is the enterprise ID Cortana cret = 'your-secret' # Cortana cretsecret is the Management Group's credential key accesstoken = gettoken (corpid, corw.cret) senddata (accesstoken, user, subject, content)

# Replace lines 28, 29, and 31 with the user account, department ID, and Application ID respectively.

# Change to CropID and Secret for 48, 49

# The user used in this article is test-msg, department iD is 8, and Application ID is 10.

# Script test

[root@zabbix alertscripts]# ./wechat.py test-msg test hellohttps://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=wx11ac451376ae0e98&corpsecret=M3FMhnFh8nTI6SxLAEbbLLZaj-1BpZIyqkJRskeMMUXObGx4mfQsAg7Jw-nUMXe9{u'invaliduser': u'all user invalid', u'errcode': 0, u'errmsg': u'ok'}

# Successfully displayed

7. Script path settings

# Place the script in zabbix's default execution path

mv wechat.php wechat.sh /opt/zabbix/share/zabbix/alertscripts/chown zabbix:zabbix /opt/zabbix/share/zabbix/alertscripts/wechat.phpchmod +x /opt/zabbix/share/zabbix/alertscripts/wechat.php

Or

chown zabbix:zabbix /opt/zabbix/share/zabbix/alertscripts/wechat.shchmod +x /opt/zabbix/share/zabbix/alertscripts/wechat.sh

# Set the script startup user to zabbix and grant the script executable permission

# Modify the zabbix_server.conf file and add the script execution directory

AlertScriptsPath=/opt/zabbix/share/zabbix/alertscripts

# Modify and restart zabbix_server

/etc/init.d/zabbix_server restart
8. Zabbix-web Front-end settings8.1 set notification Media

8.2 create a user

8.3 create a trigger action and send content

Alert subject:

Default subject:{TRIGGER.STATUS}: {TRIGGER.NAME}Trigger host:{HOSTNAME}Trigger ip:{HOST.IP}Trigger time:{EVENT.DATE}:{EVENT.TIME}Trigger: {TRIGGER.NAME}Trigger status: {TRIGGER.STATUS}Trigger severity: {TRIGGER.SEVERITY}Trigger URL: {TRIGGER.URL}Item values:{ITEM.NAME1} ({HOST.NAME1}:{ITEM.KEY1}): {ITEM.VALUE1}{ITEM.NAME2} ({HOST.NAME2}:{ITEM.KEY2}): {ITEM.VALUE2}Original event ID: {EVENT.ID}

Restoration topic:

Default subject:{TRIGGER.STATUS}: {TRIGGER.NAME}Trigger host:{HOSTNAME}Trigger ip:{HOST.IP}Trigger time:{EVENT.DATE}:{EVENT.TIME}Trigger: {TRIGGER.NAME}Trigger status: {TRIGGER.STATUS}Trigger severity: {TRIGGER.SEVERITY}Trigger URL: {TRIGGER.URL}Item values:{ITEM.NAME1} ({HOST.NAME1}:{ITEM.KEY1}): {ITEM.VALUE1}{ITEM.NAME2} ({HOST.NAME2}:{ITEM.KEY2}): {ITEM.VALUE2}Original event ID: {EVENT.ID}

# Add/update)

9. Send test alarms

# Trigger alerts to view the sending status

# To This Zabbix-3.0.3 combined with alarm completed.

10. Precautions and troubleshooting

# Add User permission processing on the Zabbix-web page and select the sending object (Application ID)

# Select the visible range in the application (select the object to be sent (Department and Department member ))

Address: http://www.linuxprobe.com/zabbix-alert-with-wechat.html


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.