zabbix6.2.4企业微信群机器人告警
基于https://blog.csdn.net/stars_moon1024/article/details/123042369调试
创建媒介
脚本如下
var Wechat = {
token: null,
to: null,
message: null,
parse_mode: null,
escapeMarkup: function (str, mode) {
switch (mode) {
case 'markdown':
return str.replace(/([_*\[`])/g, '\\$&');
case 'markdownv2':
return str.replace(/([_*\[\]()~`>#+\-=|{}.!])/g, '\\$&');
default:
return str;
}
},
sendMessage: function () {
var params = {
msgtype: "markdown",
chat_id: Wechat.to,
markdown: {
content: Wechat.message
},
disable_web_page_preview: true,
disable_notification: false
},
data,
response,
request = new HttpRequest(),
url = 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=' + Wechat.token;
if (Wechat.parse_mode !== null) {
params['parse_mode'] = Wechat.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, '[Wechat Webhook] URL: ' + url.replace(Wechat.token, '<TOKEN>'));
Zabbix.log(4, '[Wechat Webhook] params: ' + data);
response = request.post(url, data);
Zabbix.log(4, '[Wechat Webhook] HTTP code: ' + request.getStatus());
try {
response = JSON.parse(response);
}
catch (error) {
response = null;
Zabbix.log(4, '[Wechat Webhook] response parse error');
}
if (request.getStatus() !== 200 || response.errcode !== 0) {
if (typeof response.description === 'string') {
throw response.description;
}
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';
}
Wechat.token = params.Token;
if (['markdown', 'html', 'markdownv2'].indexOf(params.ParseMode) !== -1) {
Wechat.parse_mode = params.ParseMode;
}
Wechat.to = params.To;
Wechat.message = params.Subject + '\n' + params.Message;
if (['markdown', 'markdownv2'].indexOf(params.ParseMode) !== -1) {
Wechat.message = Wechat.escapeMarkup(Wechat.message, params.ParseMode);
}
Wechat.sendMessage();
return 'OK';
}
catch (error) {
Zabbix.log(4, '[Wechat Webhook] notification failed: ' + error);
throw 'Sending failed: ' + error + '.';
}
消息模板
问题:【监控告警】<font color="warning">{HOST.DESCRIPTION}</font>/<font color="warning">{HOST.CONN}</font>
信息:
>主机名称:**<font color="comment">{HOST.DESCRIPTION}</font>**
>主机IP:<font color="comment">{HOST.CONN}</font>
>告警时间:<font color="comment">{EVENT.DATE}-{EVENT.TIME}</font>
>告警等级:<font color="comment">{TRIGGER.SEVERITY}</font>
>告警信息:<font color="warning">{TRIGGER.NAME} 故障!</font>
>告警项目:<font color="comment">{TRIGGER.KEY1}</font>
>问题详情:<font color="comment">{ITEM.NAME}: {ITEM.VALUE}</font>
>当前状态:<font color="warning">{TRIGGER.STATUS}: {ITEM.VALUE1}</font>
>事件ID:<font color="comment">{EVENT.ID}</font>
效果如图