依据前面文章《Zabbix 5.0 LTS URL 健康监测》环境,实现企业微信(Webhook)自动发送告警信息。
先在自己的企业微信群里创建一个机器人,并获取其 WebHook 地址。右击群聊天卡片,添加群机器人。
获得一个类似下图的 WebHook 地址。注意,这个 WebHook 地址非常重要,请不要泄露。
# key 的值是唯一的
https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=b55ed8ad-6765-4e8b-827c-xxxxxxxxxx
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 + ".";
}
Custom message
故障信息:{TRIGGER.STATUS}
服务器:{HOSTNAME1}发生: {TRIGGER.NAME}故障!
告警主机:{HOSTNAME1}
告警时间:{EVENT.DATE} {EVENT.TIME}
告警等级:{TRIGGER.SEVERITY}
告警信息: {TRIGGER.NAME}
告警项目:{TRIGGER.KEY1}
问题详情:{ITEM.NAME}:{ITEM.VALUE}
当前状态:{TRIGGER.STATUS}:{ITEM.VALUE1}
事件ID:{EVENT.ID}
恢复信息:{EVENT.NAME}
服务器: {HOST.NAME}发生:{EVENT.NAME}故障恢复!
告警主机: {HOST.NAME}
告警地址:{HOST.IP}
监控项目:{ITEM.NAME}
监控取值:{ITEM.LASTVALUE}
告警等级:{TRIGGER.SEVERITY}
当前状态:{TRIGGER.STATUS}
告警信息:{TRIGGER.NAME}
告警时间:{EVENT.DATE} {EVENT.TIME}
事件ID:{EVENT.ID}
更新信息:{EVENT.NAME}
服务器: {HOST.NAME}发生:{EVENT.NAME}故障更新!
告警主机: {HOST.NAME}
告警地址:{HOST.IP}
监控项目:{ITEM.NAME}
监控取值:{ITEM.LASTVALUE}
告警等级:{TRIGGER.SEVERITY}
当前状态:{TRIGGER.STATUS}
告警信息:{TRIGGER.NAME}
告警时间:{EVENT.DATE} {EVENT.TIME}
事件ID:{EVENT.ID}
Web场景-- 步骤 -- 故意写错URL触发告警