Bootstrap

Zabbix 6.4 版本企业微信群机器人报警配置

从Zabbix 5.4升级后配置企业微信报警发下原来的脚本代码报错,搜索一圈发下使因为新版本对里面的部分代码不支持,几经周折终于找到这段可用的代码。

告警-媒介-创建媒介类型

直接添加webhook类型脚本

Token是企业微信内建好的机器人webhook地址后面的参数

其他照填,很简单

之前版本的代码在新版zabbix里部分代码不支持了所以需要换掉

下面是脚本的内容全部复制就行

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 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());
        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.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';
    }
 
    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 + '.';
}

代码这个是从《蓝~天~》这位老哥文章里复制的https://blog.csdn.net/u010533742/article/details/125280581

报警消息模板 

实际效果

## 【Zabbix平台告警信息❌】
------------------
##### <font color="#FF0000">【故障】{TRIGGER.NAME} </font>
------------------
##### <font color="#FF0000">【故障发生时间】</font>:{EVENT.DATE} {EVENT.TIME}
##### <font color="#FF0000">【告警级别】</font>:{TRIGGER.SEVERITY}
##### <font color="#FF0000">【故障前状态】</font>:{ITEM.LASTVALUE}
##### <font color="#FF0000">【故障主机IP】</font>:{HOST.IP}
##### <font color="#FF0000">【故障主机名】</font>:{HOST.NAME}
##### <font color="#FF0000">【故障时长】</font>:{EVENT.AGE}
##### <font color="#FF0000">【故障是否确认】</font>:{EVENT.ACK.STATUS}
-------------------
#####  <font color="#F56C6C">事件状态:{EVENT.STATUS}</font>

 

实际效果

## 【Zabbix平台恢复信息✅】
------------------
##### <font color="#67C23A">【故障】{TRIGGER.NAME} 已经恢复 </font>
------------------
##### <font color="#02b340">【故障恢复时间】</font>:{EVENT.RECOVERY.DATE} {EVENT.RECOVERY.TIME}
##### <font color="#02b340">【告警级别】</font>:{TRIGGER.SEVERITY}
##### <font color="#02b340">【故障前状态】</font>:{ITEM.LASTVALUE}
##### <font color="#02b340">【故障主机IP】</font>:{HOST.IP}
##### <font color="#02b340">【故障主机名】</font>:{HOST.NAME}
##### <font color="#02b340">【故障时长】</font>:{EVENT.AGE}
##### <font color="#02b340">【故障是否确认】</font>:{EVENT.ACK.STATUS}
------------------
#####  <font color="#F56C6C">事件状态:{EVENT.STATUS}</font>

 

## 【Zabbix监控平台故障更新信息⚠】
------------------
##### <font color="#67C23A"> 【故障】{USER.FULLNAME} 已经确认故障原因 </font>
------------------
##### <font color="#FF0000">【故障确认时间】</font>:{ACK.DATE} {ACK.TIME}
##### <font color="#FF0000">【告警级别】</font>:{TRIGGER.SEVERITY} 
##### <font color="#FF0000">【故障前状态】</font>:{ITEM.LASTVALUE}
##### <font color="#FF0000">【故障主机IP】</font>:{HOST.IP}
##### <font color="#FF0000">【故障主机名】</font>:{HOST.NAME}
##### <font color="#FF0000">【故障时长】</font>:{EVENT.AGE}
##### <font color="#FF0000">【故障原因】</font>:{ACK.MESSAGE}
##### <font color="#FF0000">【故障是否确认】</font>:{EVENT.ACK.STATUS}✅
------------------
#####  <font color="#F56C6C">事件状态:{EVENT.STATUS}</font>

;