Frank的学习之路

2.7_zabbix微信告警集成

1: 使用企业微信告警,地址如下:
https://work.weixin.qq.com/wework_admin/frame
没有企业微信需要注册:https://work.weixin.qq.com/

2: 主要是用获取的token去发送微信消息
利用企业id和企业secret去获取tocken,利用获取到的tocken来发送微信消息

3: 具体的python脚本实现如下
#!/usr/bin/python
# -*- coding: utf-8 -*-
import json
import sys
import urllib,urllib2

#需要三个变量corpid、corpsecret、agentid
agentid = 'xxx'
corpid = 'xxxxx'
corpsecret = 'xxxx'

#获取tocken,存在my_token里面
gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + corpsecret
token_file = urllib2.urlopen(gettoken_url)
token_data = token_file.read().decode('utf-8')
token_json = json.loads(token_data)
my_token = token_json['access_token']

#利用获取到的tocken发送微信信息
touser=sys.argv[1] #发送给谁,多个用分号分享,例如'zhangsan|wangwu'
content=sys.argv[2] #发送的内容
post_content = {
"touser":touser,
"agentid":agentid,
"msgtype": "text",
"text":{
"content":content,
}
}
json_content = json.dumps(post_content)

url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + my_token
response = urllib2.urlopen(url,json_content)

#查看返回结果
print response.read().decode('utf-8')

4: 调用
python /usr/local/src/script 'zhuang_weihong' '所剩余内存不足100M'

5: zabbix设置(到alertscripts目录下)
vim zabbix_wx.py
把上述脚本拷贝进去

6: 报警媒介添加wx告警
{ALERT.SENDTO} 发送给谁
{ALERT.SUBJECT} 发送主题
{ALERT.MESSAGE} 发送内容

7: 设置企业微信用户名
就是企业微信的账号

8: 添加触发微信告警的动作

9: 进行测试

配置流程:
报警没接类型(设置报警触发后执行的脚本)
配置--->动作(设置触发器告警信息发给谁)
用户-->报警媒介(设置接收消息的人)
cd /usr/local/zabbix/alertscripts
vim zabbix_wx.py
#!/usr/bin/python
# -*- coding: utf-8 -*-
import json
import sys
import urllib,urllib2

#需要三个变量corpid、corpsecret、agentid
agentid = '1000002'
corpid = 'ww6cd40c9fa23e8436'
corpsecret = '8rq7pcDUzNcH8O5YUi8BdRVQaKJJY7mpj80piGPTIgk'

#获取tocken,存在my_token里面
gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + corpsecret
token_file = urllib2.urlopen(gettoken_url)
token_data = token_file.read().decode('utf-8')
token_json = json.loads(token_data)
my_token = token_json['access_token']
print(my_token)
#利用获取到的tocken发送微信信息
touser=sys.argv[1] #发送给谁,多个用分号分享,例如'zhangsan|wangwu'
content=sys.argv[2] #发送的内容
post_content = {
"touser":touser,
"agentid":agentid,
"msgtype": "text",
"text":{
"content":content,
}
}
json_content = json.dumps(post_content)

url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + my_token
response = urllib2.urlopen(url,json_content)

#查看返回结果
print response.read().decode('utf-8')
返回顶部