1.1微信企业号的申请
1.2创建用户
1.3创建应用
1.4调用微信接口
我们配置微信脚本 首先我们需要调用微信接口
调用微信接口需要一个调用接口的凭证:access_token
通过:GropID,Secret
才能获取到access_token
,但是获取到的token
有效期为两分钟
微信企业号接口调试工具地址:http://qydev.weixin.qq.com/debug
打开上面的url,使用上面monitops
企业号的monit-manager
管理组的CorpID
和Secret
号获得调用接口凭证access_token
,如下
1.5配置zabbix脚本
[root@c7-41 alertscripts]# pwd
/usr/lib/zabbix/alertscripts
[root@c7-41 alertscripts]# wget https://pypi.python.org/packages/f0/07/26b519e6ebb03c2a74989f7571e6ae6b82e9d7d81b8de6fcdbfc643c7b58/simplejson-3.8.2.tar.gz
[root@c7-41 alertscripts]# tar -zxvf simplejson-3.8.2.tar.gz
[root@c7-41 alertscripts]# python setup.py build
[root@c7-41 alertscripts]# python setup.py install
[root@c7-41 alertscripts]# cat weixin.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":"***", #企业号中的用户帐号,在zabbix用户Media中配置,如果配置不正常,将按部门发送。
"toparty":"1", #企业号中的部门id。
"msgtype":"text", #消息类型。
"agentid":"1000002", #企业号中的应用id。
"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]) #zabbix传过来的第一个参数
subject = str(sys.argv[2]) #zabbix传过来的第二个参数
content = str(sys.argv[3]) #zabbix传过来的第三个参数
corpid = 'wwffd78d********' #企业号的标识(上文中提到获取位置)
corpsecret = 'jIBdzJfxwVl11riAvHO3fPVbuO7************' #管理组凭证密钥(上文中提到获取位置)
accesstoken = gettoken(corpid,corpsecret)
senddata(accesstoken,user,subject,content)
[root@c7-41 alertscripts]# chmod 755 weixin.py
[root@c7-41 alertscripts]# chown -R zabbix.zabbix weixin.py
1.6手动测试weixin.py脚本
#可以使用用户账号或应用ID去测试,观察微信是否能收到信息:
#cr7是我们成员的账号
[root@c7-41 alertscripts]# ./weixin.py cr7 test test
https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=wwffd78d1c0a4eaf70&corpsecret=jIBdzJfxwVl11riAvHO3fPVbuO7rwebubDDEAPHNUwo
{u'invalidparty': u'1', u'invaliduser': u'', u'errcode': 0, u'errmsg': u'ok'}
1.7创建报警媒介
(这里我选用python脚本:weixin.py)
注意下面要填写的脚本参数:
{ALERT.SENDTO}
{ALERT.SUBJECT}
{ALERT.MESSAGE}
1.8创建用户weixin
密码一栏可以选择跟之前的超级管理员zabbix一样的密码;用户类型也可以设置成超级管理员
1.9创建动作
{TRIGGER.STATUS}: {TRIGGER.NAME}
告警主机:{HOST.NAME}
主机IP: {HOST.IP}
告警时间:{EVENT.DATE} {EVENT.TIME}
告警等级:{TRIGGER.SEVERITY}
告警信息:{TRIGGER.NAME}
问题详情:{ITEM.NAME}:{ITEM.VALUE}
当前状态: {TRIGGER.STATUS}:{ITEM.VALUE1}
事件ID: {EVENT.ID}
root@c7-42 ~]# systemctl stop httpd
到这里我们就配置完毕了 但是这样的问题是只能用企业微信接到消息 那么如何让我们平时使用的微信也能接到消息呢
这里我们用手机扫描二维码进行关注 下面我们来测试下
root@c7-42 ~]# systemctl stop httpd