1. 在企业微信群聊中建立一个机器人
- 获取webhook地址
2. zabbix中选择管理–报警媒介类型
- 创建媒体类型,填写以下内容
- 脚本内容参考下面的内容
var Qiyeweixin = {
key: null,
message: null,
msgtype: "markdown",
proxy: null,
sendMessage: function () {
var params = {
msgtype: Qiyeweixin.msgtype,
markdown: {
content: Qiyeweixin.message,
},
},
data,
response,
request = new CurlHttpRequest(),
url =
"https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=" +
Qiyeweixin.key;
if (Qiyeweixin.proxy) {
request.setProxy(Qiyeweixin.proxy);
}
request.AddHeader("Content-Type: application/json");
data = JSON.stringify(params);
// Remove replace() function if you want to see the exposed key in the log file.
Zabbix.Log(
4,
"[Qiyeweixin Webhook] URL: " + url.replace(Qiyeweixin.key, "<BOT KEY>")
);
Zabbix.Log(4, "[Qiyeweixin Webhook] params: " + data);
response = request.Post(url, data);
Zabbix.Log(4, "[Qiyeweixin Webhook] HTTP code: " + request.Status());
try {
response = JSON.parse(response);
} catch (error) {
response = null;
}
if (request.Status() !== 200 || response.errcode !== 0) {
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.Key === "undefined") {
throw 'Incorrect value is given for parameter "Key": parameter is missing';
}
Qiyeweixin.key = params.Key;
if (params.HTTPProxy) {
Qiyeweixin.proxy = params.HTTPProxy;
}
Qiyeweixin.to = params.To;
Qiyeweixin.message = params.Subject + "\n" + params.Message;
Qiyeweixin.sendMessage();
return "OK";
} catch (error) {
Zabbix.Log(4, "[Qiyeweixin Webhook] notification failed: " + error);
throw "Sending failed: " + error + ".";
}
- 选择Message templates,把所有默认的模板都添加上
3. zabbix点击配置–动作
- 选择Trigger actions
- 编辑默认Report problems to Zabbix administrators
- 点击操作:添加
- 最后更新即可
4. zabbix点击管理–用户
- 点击admin
- 点击报警媒介
- 点击添加,报警类型自行更改。
5. 附zabbix6.0代码
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