zabbix6.2.0 webhook脚本

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

注:报警媒介--消息模板,需添加markdown格式的参数配置

主题:

### 【监控告警】主机: <font color="warning">{HOST.NAME}</font> IP地址:<font color="warning">{HOST.IP}</font>

消息:

>主机名称:**<font color="comment">{HOST.NAME}</font>**
>主机IP:<font color="comment">{HOST.IP}</font>
>告警时间:<font color="comment">{EVENT.DATE}-{EVENT.TIME}</font>
>问题名称:<font color="warning">{EVENT.NAME}</font>
>问题详情:<font color="comment">{ITEM.NAME}:{ITEM.VALUE}</font>
>目前状态:<font color="comment">{TRIGGER.STATUS}</font>
>操作数据:<font color="comment">{EVENT.OPDATA}</font>

告警样式(markdown)

<font size="18">**Zabbix告警通知**</font>:<font size="16" color="warning">{HOSTNAME1}发生{TRIGGER.NAME}故障!</font>

<font size="18">**Zabbix告警通知**</font>:<font size="16" color="info">{HOSTNAME1} {TRIGGER.NAME}已恢复!</font>

**告警主机:**{HOSTNAME1}
**主机地址:**{HOST.IP}
**告警时间:**{EVENT.DATE} {EVENT.TIME}
**告警等级:**{TRIGGER.SEVERITY}
**告警信息:** {TRIGGER.NAME}
**告警项目:**{TRIGGER.KEY1}
**问题详情:**{ITEM.NAME}:{ITEM.VALUE}
**当前状态:**{TRIGGER.STATUS}:{ITEM.VALUE1}
**事件ID:**{EVENT.ID}

 

zabbix5.0和zabbix6.2 在脚本是有些许区别,

参考链接:https://www.zabbix.com/documentation/6.2/en/manual/config/notifications/media/webhook/webhook_examples

                  https://blog.csdn.net/rendongxingzhe/article/details/125781776

 

posted @ 2023-03-16 15:21  Me-lihu  阅读(213)  评论(0编辑  收藏  举报