Zabbix webhook配置

5.x版本脚本

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

6.x版本

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/stars_moon1024/article/details/123042369
https://blog.csdn.net/u010533742/article/details/109068187
https://www.cnblogs.com/solita1y/articles/17163152.html
https://www.zabbix.com/documentation/6.2/en/manual/config/notifications/media/webhook/webhook_examples
https://developer.work.weixin.qq.com/document/path/91770

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值