zabbix使用webhook方式对接企业微信机器人脚本

1、配置界面

image-20211013110422674

2、配置详情

参数:

Corpid wwa17a4a52fbd11b3f

Corpsecret 4enakt4tIy7rpthDNtPYf8tG2Ytel_tC3BeMg60OWxg

Message {ALERT.MESSAGE}

Subject {ALERT.SUBJECT}

To {ALERT.SENDTO} //需要使用企业微信内的微信号

脚本

var Wechat = {
    corpid: null,
    corpsecret: null,
    to: null,
    message: null,
    parse_mode: null,

    sendMessage: function() {
        var params = {
            "touser": Wechat.to,
            "msgtype":"text",
            "agentid":"1000002",
            text: {
               "content":Wechat.message
            },
            "safe":"0"
        },
        data,
        response,
        token_response,
        access_token,
        gettoken_request = new CurlHttpRequest(),
        request = new CurlHttpRequest();

        gettoken_url = 'https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=' + Wechat.corpid + '&corpsecret=' + Wechat.corpsecret;
        gettoken_request.AddHeader('Content-Type: application/json');

        token_data = JSON.stringify(params);       
        token_response = request.Get(gettoken_url,token_data);
        Zabbix.Log(4, 'token_response: ' + token_response);
        token_response = JSON.parse(token_response);
        access_token = token_response.access_token;
        Zabbix.Log(4, 'access_token: ' + access_token);
        send_url = 'https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=' + access_token;
        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] params: ' + data);
        response = request.Post(send_url, data);


        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 (['Markdown', 'HTML', 'MarkdownV2'].indexOf(params.ParseMode) !== -1) {
        Wechat.parse_mode = params.ParseMode;
    }
    Wechat.to = params.To;
    Wechat.message = params.Subject + '\n' + params.Message;
    Wechat.corpid = params.Corpid;
    Wechat.corpsecret = params.Corpsecret;
    Wechat.sendMessage();
    return 'OK';
}
catch (error) {
    Zabbix.Log(4, '[Wechat Webhook] notification failed: ' + error);
    throw 'Sending failed: ' + error + '.';
}
文章作者: Stars
版权声明: 本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Stars
network monitor
喜欢就支持一下吧