0、前置条件
已经申请到企业微信机器人webhook,参考链接https://developer.work.weixin.qq.com/document/path/91770
1、创建报警媒介类型
在报警媒介类型
右上角创建媒体类型
新增Token
参数,将申请获得的Token
填入
在脚本处填入脚本:
脚本来源:https://blog.csdn.net/weixin_44857388/article/details/134415430
var Wechat = {token: null,to: null,message: null,parse_mode: null,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 CurlHttpRequest(),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.Status());Zabbix.Log(4, '[Wechat Webhook] response: ' + response);try {response = JSON.parse(response);}catch (error) {response = null;Zabbix.Log(4, '[Wechat Webhook] response parse error');}if (request.Status() !== 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';}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;Wechat.sendMessage();return 'OK';
}
catch (error) {Zabbix.Log(4, '[Wechat Webhook] notification failed: ' + error);throw 'Sending failed: ' + error + '.';
}
2、配置消息模板
填入自定义消息模板
### 【Zabbix监控告警】IP:<font color="warning">{HOST.IP}</font>> 主机名称:**<font color="comment">{HOST.NAME}</font>**
> 主机IP:<font color="comment">{HOST.IP}</font>
> 事件名称:**<font color="warning">{EVENT.NAME}</font>**
> 告警时间:<font color="comment">{EVENT.DATE}-{EVENT.TIME}</font>
> 监控项:<font color="comment">{ITEM.NAME}</font>
> 监控详情:<font color="comment">{ITEM.VALUE}</font>
参数解释
ITEM:监控项
EVENT:触发器
3、配置用户报警媒介
4、创建触发器动作
新建新的触发器动作,配置动作和操作。操作里的自定义消息内容可以不用设置,经过尝试,[配置消息模板](# 2、配置消息模板)中的消息模板优先级更高,这里的自定义消息内容不生效。
5、调试
触发事件,满足触发器条件,查看企微机器人通知