# Http://qydev.weixin.qq.com/wiki/index.php? Title = Message Type and Data Format
# Test:
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 Media8.2 create a user8.3 create a trigger action and send contentAlert 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