Zabbix Wechat alarm script
# yum install-y git python-pip
# pip install requests simplejson
# git clone https://github.com/X-Mars/Zabbix-Alert-WeChat.git
View the directory where the script is stored
# cat / etc/zabbix/zabbix_server.conf | grep alert
AlertScriptsPath=/usr/lib/zabbix/alertscripts
# cp ~ / Zabbix-Alert-WeChat/wechat.py / usr/lib/zabbix/alertscripts/
# vi / usr/lib/zabbix/alertscripts/wechat.py
#! / usr/bin/python
# _ * _ coding:utf-8 _ * _
Import urllib,urllib2
Import json
Import sys
Import simplejson
Reload (sys)
Sys.setdefaultencoding ('utf-8')
Def gettoken (corpid,corpsecret):
Gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid +' & corpsecret=' + corpsecret
Print gettoken_url
Try:
Token_file = urllib2.urlopen (gettoken_url)
Except urllib2.HTTPError as e:
Print e.code
Print 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 token
Def senddata (access_token,user,subject,content):
Send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + access_token
Send_values = {
"touser": the user account in user, # Enterprise ID, which is configured in zabbix user Media. If the configuration is abnormal, it will be sent by department.
"toparty": "2", department id in # Enterprise.
"msgtype": "text", # message type
"agentid": "6", application id in # Enterprise.
"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]) # the first parameter passed by zabbix
Subject = str (sys.argv [2]) # the second parameter passed by zabbix
Content = str (sys.argv [3]) # the third parameter passed by zabbix
Corpid = 'wx5c55b26119f82df5' # CorpID is the identity of the enterprise number
Corpsecret = 'XqHkOZfq9NpiXRU1Z6Vh0aRlZstG0o8LnclmzGJ45VqofUGvf3f8JedJ-XgMsWCy' # corpsecretSecret is the administrative group credential key
Accesstoken = gettoken (corpid,corpsecret)
Senddata (accesstoken,user,subject,content)
Give permission or you can't use it.
# chmod + x / usr/lib/zabbix/alertscripts/wechat.py
# chown zabbix:zabbix / usr/lib/zabbix/alertscripts/wechat.py
Https://github.com/X-Mars/Zabbix-Alert-WeChat