Zabbix implements enterprise WeChat alarm
1. Download and install the mobile version of Enterprise WeChat
2. Register an account and create a company, then add employees to the company
3. Find my company -> Record the company ID: ww1d1845f4b25a12fb
4. Create a department under the company and record the department ID: 3
5. Create an application and record the application ID and secret, ID: 1000003, secret: ItmpP6TjPNvx6aSlvRCYrkgK5BPWiqTrMf3xhKjUnLo
6. Write a python script and fill in the information recorded above into the corresponding place (the script is in the zabbix-server server)
[root@zabbix-server ~]# vim /usr/lib/zabbix/alertscripts/wechat.py
The script content is:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import urllib,urllib2,json
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
class WeChat(object):
__token_id = ''
# init attribute
def __init__(self,url):
self.__url = url.rstrip('/')
self.__corpid = 'ww1d1845f4b25a12fb'
self.__secret = 'ItmpP6TjPNvx6aSlvRCYrkgK5BPWiqTrMf3xhKjUnLo'
# Get TokenID
def authID(self):
params = {
'corpid':self.__corpid, 'corpsecret':self.__secret}
data = urllib.urlencode(params)
content = self.getToken(data)
try:
self.__token_id = content['access_token']
# print content['access_token']
except KeyError:
raise KeyError
# Establish a connection
def getToken(self,data,url_prefix='/'):
url = self.__url + url_prefix + 'gettoken?'
try:
response = urllib2.Request(url + data)
except KeyError:
raise KeyError
result = urllib2.urlopen(response)
content = json.loads(result.read())
return content
# Get sendmessage url
def postData(self,data,url_prefix='/'):
url = self.__url + url_prefix + 'message/send?access_token=%s' % self.__token_id
request = urllib2.Request(url,data)
try:
result = urllib2.urlopen(request)
except urllib2.HTTPError as e:
if hasattr(e,'reason'):
print 'reason',e.reason
elif hasattr(e,'code'):
print 'code',e.code
return 0
else:
content = json.loads(result.read())
result.close()
return content
# send message
def sendMessage(self,touser,message):
self.authID()
data = json.dumps({
'touser':touser,
'toparty':3,
'msgtype':"text",
'agentid':"1000003",
'text':{
'content':message
},
'safe':"0"
},ensure_ascii=False)
response = self.postData(data)
print response
if __name__ == '__main__':
a = WeChat('https://qyapi.weixin.qq.com/cgi-bin')
a.sendMessage(sys.argv[1],sys.argv[3])
7. Modify permissions
[root@zabbix-server ~]# chmod 777 /usr/lib/zabbix/alertscripts/wechat.py
[root@zabbix-server ~]# chown zabbix:zabbix /usr/lib/zabbix/alertscripts/wechat.py
8. Perform script testing on the zabbix-server side
[root@zabbix-server alertscripts]# /usr/lib/zabbix/alertscripts/wechat.py ZhangYu test hellohellohello
root@zabbix-server alertscripts]# /usr/lib/zabbix/alertscripts/wechat.py ZhangYu www helphelphelp
Note: ZhangYu is the username in Enterprise WeChat.
A message was received on the machine indicating that the script was working normally.
8. Create an alarm media in the web interface of zabbix-server
9. Add an alarm media to the user (you can create a new user or directly use the administrator account admin), let this user associate the alarm media, and add recipients
Note: type (created alarm medium), recipient (the user in Enterprise WeChat)
10. Create an action to implement Enterprise WeChat alarm (when a trigger is triggered, send an alarm to a specific user)
Define action: use trigger Trigger, define the trigger by yourself. What I use here is the trigger created before to monitor the running status of nginx.
Define the operation: When the action is triggered, the set operation will be performed
. Content of the message sent:
Default recipient: Fault {TRIGGER.STATUS}, Server: {HOSTNAME1} Occurrence: {TRIGGER.NAME} Fault!
Default message:
Alarm Host: {HOSTNAME1}
Alarm time: {EVENT.DATE} {EVENT.TIME}
Alarm level: {TRIGGER.SEVERITY}
Alarm information: {TRIGGER.NAME}
Alarm item: {TRIGGER.KEY1}
Problem details: {ITEM.NAME} :{ITEM.VALUE}
Current status:{TRIGGER.STATUS}:{ITEM.VALUE1}
Event ID:{EVENT.ID}
11. Verification
When the trigger is triggered, Enterprise WeChat receives the alarm message