微信报警
1.准备企业微信
2.企业微信id 、机器人id 、密钥信息
ww9827e86e50442eea
1000002
Rr_XwMRaLTRNWDGaMFdA-1-po47mn9pzPSVG-xXLyuw
https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=addbf6f7-6e6c-4e79-9e95-64caebb483c2
3.python 脚本
4.发件人和收件人配置
5.动作配置
两种方式
1.微信机器人应用,可以发送消息给指定部门(这种方式需要企业认证通过后)
获取 企业微信id 、机器人id 、密钥信息
企业微信id:我的企业 --> 企业信息 --> 企业ID
机器人id:应用管理 --> 自建 --> 新建机器人 --> 获取 AgentId
机器人密钥:进入机器人界面 --> 获取 Secret
查看 zabbix 脚本路径
grep 'AlertScriptsPath' /etc/zabbix/zabbix_server.conf

cd /usr/lib/zabbix/alertscripts
新建 python 脚本并添加执行权限,修改其中的 corpid、appsecret、agentid
#!/usr/bin/env python
#-*- coding: utf-8 -*-
import requests
import sys
import os
import json
import logging
logging.basicConfig(level = logging.DEBUG, format = '%(asctime)s, %(filename)s, %(levelname)s, %(message)s',
datefmt = '%a, %d %b %Y %H:%M:%S',
filename = os.path.join('/tmp','weixin.log'),
filemode = 'a')
corpid='ww2a1f8fa4a4ce55ed'
appsecret='7oHs9WWabR_NeKeQBTpwwdOUV2GKWKd0YQJzkwQiA_M'
agentid='1000002'
#获取accesstoken
token_url='https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + corpid + '&corpsecret=' + appsecret
req=requests.get(token_url)
accesstoken=req.json()['access_token']
#发送消息
msgsend_url='https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + accesstoken
touser=sys.argv[1]
subject=sys.argv[2]
#toparty='3|4|5|6'
message=sys.argv[2] + "\n\n" +sys.argv[3]
params={
"touser": touser,
# "toparty": toparty,
"msgtype": "text",
"agentid": agentid,
"text": {
"content": message
},
"safe":0
}
req=requests.post(msgsend_url, data=json.dumps(params))
logging.info('sendto:' + touser + ';;subject:' + subject + ';;message:' + message)
需要修改的部分有:
corpid='微信企业号corpid'
appsecret='应用的Secret'
agentid=应用的id
脚本的三个参数都是zabbix内置变量,分别为:
报警收件人:{ALERT.SENDTO},
报警标题:{ALERT.SUBJECT},
报警内容:{ALERT.MESSAGE}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
此时运行脚本会出现错误:ImportError: No module named requests,解决:yum install python-requests
后续步骤参考**《2.群微信机器人,发送消息到群》**,也是新建媒介,配置用户和动作等等
2.群微信机器人,发送消息到群
打开需要机器人通知的企业微信聊天群,在右上角 ... 添加群机器人,添加后,复制提供的 url
在 zabbix 放置脚本的路径下新建 python 文件并添加执行权限,修改其中的 API_URL
#!/usr/bin/python
# -*- coding: utf-8 -*-
import requests
import json
import sys
# 机器人的webhook地址
API_URL = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=c217fe49-a91d-4c32-8f90-5e46f1f45f59"
# HTTP请求头部信息
HEADERS = {
'Content-Type': 'application/json;charset=utf-8'
}
# 定义发送消息的函数
def send_text(text):
# 构建消息体
texts = {
"msgtype": "text",
"text": {
"content": text
}
}
# 发送HTTP POST请求
response = requests.post(API_URL, json=texts, headers=HEADERS)
# 打印响应内容(一般用于调试)
print(response.content)
# 如果该文件被直接执行,则从命令行接收一个参数并将其发送到机器人的webhook地址
if __name__ == '__main__':
if len(sys.argv) != 2:
print("Usage: python send_text.py <text>")
sys.exit(1)
text = sys.argv[1] # 获取命令行参数
send_text(text) # 发送消息
脚本传参:{ALERT.MESSAGE}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
打开 web,管理 --> 媒介 --> 创建媒介类型,类型选择脚本


给用户添加报警媒介

配置动作,当系统有警告时通知此用户


接下来通过开关 nginx 来验证是否配置成功,发现是正常的
