Zabbix 6.0 企业微信Webhook接收告警消息
近期更新zabbix至6.2后,使用微信进行告警,发现网上的对应的zabbix 5版本的企业微信webhook脚本报错,无法使用,经检查略作调整修复。
微信接收告警方式
目前发现一下几种方式可以接收到微信的告警:
-
微信接收邮件通知
zabbix 告警发送Email,在邮箱中设置“新邮件通知”。如邮箱支持,此方案设置简单。
-
企业微信中群机器人🤖️
在企业微信中群中选择“添加机器人”,获取到webhook地址,此方案适合需要接收的人员均在群内。
-
企业微信中内部应用
需要在企业微信中创建内部应用,获取api及secret,获取accesskey后可使用webhook方式发送告警。 需要注意accesskey有超时机制,需要定时更新,且需要提供回调地址,相对较复杂,适用于需要交互的场景。
企业微信群机器人
zabbix 6 主要修改webhook中的request函数为HttpRequest并作相应的调整,更新后的脚本如下:
var Wework = {
url: null,
token: null,
to: null,
message: null,
parse_mode: null,
sendMessage: function() {
var params = {
msgtype: "markdown",
//chat_id: Wework.to,
markdown: {
content:Wework.message
}
//disable_web_page_preview: true,
//disable_notification: false
},
data,
response,
request = new HttpRequest(),
//url = Wework.url + Wework.token;
url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=' + Wework.token;
if (Wework.parse_mode !== null) {
params['parse_mode'] = Wework.parse_mode;
}
request.addHeader('Content-Type: application/json');
data = JSON.stringify(params);
// Remove replace() function if you want to see the exposed token in the log file.
Zabbix.Log(4, '[Wework Webhook] URL: ' + url.replace(Wework.token, '<TOKEN>'));
Zabbix.Log(4, '[Wework Webhook] params: ' + data);
response = request.post(url, data);
Zabbix.Log(4, '[Wework Webhook] HTTP code: ' + request.getStatus());
Zabbix.Log(4, '[Wework Webhook] response: ' + response);
try {
response = JSON.parse(response);
}
catch (error) {
response = null;
Zabbix.Log(4, '[Wework Webhook] response parse error');
}
if (request.getStatus() !== 200 || response.errcode !== 0 || response.errmsg !== 'ok') {
if (typeof response.errmsg === 'string') {
throw response.errmsg;
}
else {
throw 'Unknown error. Check debug log for more information.'
}
}
}
}
try {
var params = JSON.parse(value);
if (typeof params.Token === 'undefined') {
throw 'Incorrect value is given for parameter "Token": parameter is missing';
}
Wework.token = params.Token;
if (['Markdown', 'HTML', 'MarkdownV2'].indexOf(params.ParseMode) !== -1) {
Wework.parse_mode = params.ParseMode;
}
Wework.to = params.To;
Wework.message = params.Subject + '\n' + params.Message;
Wework.sendMessage();
return 'OK';
}
catch (error) {
Zabbix.Log(4, '[Wework Webhook] notification failed: ' + error);
throw 'Sending failed: ' + error + '.';
}
使用方法:
- 在zabbix中选择administration->media types,选择
create media type - Parameters中的Token参数设置为群机器人字符串中的key
- 在script中粘贴上面的代码
- 在action中配置告警动作及接收的 media type
参考:
最后修改于 2023-02-04