Zabbix webhook 自定义报警媒介

场景一:使用企业微信机器人报警

 

 图中的token是:在群组中添加机器人,机器人的webhook地址的key

var Wechat = {
    token: null,
    to: null,
    message: null,
    parse_mode: null,

    sendMessage: function() {
        var params = {
            msgtype: "text",
            chat_id: Wechat.to,
            text: {
                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 + '.';
}

 

场景二:电话报警

 

var Wechat = {
    tel: null,
    
    calluser: function (){
        var response,
        request = new CurlHttpRequest(),
        url = 'http://phone_server.com/TRinterfaceYun/CheckSystem.aspx?phone=' + Wechat.tel + '&guid=1234';    // 此处URL 为电话接口
        
        Zabbix.Log(4, '[Wechat Webhook] URL: ' + url.replace(Wechat.tel, '<Tel>'));
        response = request.Post(url);
        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');
        }
    }

}

try {
    var params = JSON.parse(value);
    
    if (typeof params.Tel === 'undefined') {
        throw 'Incorrect value is given for parameter "Tel": parameter is missing';
    }    
    
    Wechat.tel = params.Tel;
    Wechat.calluser();
    return 'OK';

}
catch (error) {
    Zabbix.Log(4, '[Wechat Webhook] notification failed: ' + error);
    throw 'Sending failed: ' + error + '.';
}

 

posted @ 2021-09-02 15:08  语~默  阅读(1018)  评论(0编辑  收藏  举报