Zabbix 配置企业微信群机器人监控报警脚本

添加报警媒介

image

var weixin = {
    key: null,
    message: null,
    msgtype: "makrdown",
    proxy: null,
    sendMessage: function () {
        var params = {
            msgtype: weixin.msgtype,
            markdown: {
                content: weixin.message
            },
        },
            data,
            response,
            request = new CurlHttpRequest(),
            url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=" + weixin.key;
        if (weixin.proxy) {
            request.setProxy(weixin.proxy);
        }
        request.AddHeader("Content-Type:application/json");
        data = JSON.stringify(params);
        Zabbix.Log(4,"[weixin Webhook] URL:" + url.replace(weixin.key,"<BOT KEY>"));
        Zabbix.Log(4,"[weixin Webhook] params:" + data);
        response = request.Post(url,data);
        Zabbix.Log(4,"[weixin Webhook] HTTP code:" + request.Status());
        try{
            response = JSON.parse(response);
        }catch(error){
            response = null;
        }
        if(request.Status() != 200 || response.errorcode != 0){
            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.Key == "undefined"){
        throw 'Incorrect value is given for parameter "Key": parameter is missing';
    }
    weixin.key =  params.Key;
    if(params.HTTPProxy){
        weixin.proxy = params.HTTPProxy;
    }
    weixin.to = params.To;
    weixin.message = params.Subject + "\n" + params.Message;
    weixin.sendMessage();
    return "OK";
}catch(error){
    Zabbix.Log(4,"[weixin Webhook] notification failed: " + error);
    throw "Sending failed:" + error;
}

添加消息模板

  • 问题模板

默认标题

故障告警:{TRIGGER.STATUS}: {TRIGGER.NAME} 

消息内容

故障告警:{TRIGGER.STATUS}: {TRIGGER.NAME} 
告警主机:{HOST.NAME} 
主机地址:{HOST.IP} 
告警时间:{EVENT.DATE} {EVENT.TIME} 
告警等级:{TRIGGER.SEVERITY} 
告警信息:{TRIGGER.NAME} 
问题详情:{ITEM.NAME}:{ITEM.VALUE} 
事件代码:{EVENT.ID} 
  • 问题恢复模板

默认标题

故障解除:{TRIGGER.STATUS}: {TRIGGER.NAME} 

消息内容

恢复主机:{HOST.NAME} 
主机地址:{HOST.IP} 
恢复时间:{EVENT.DATE} {EVENT.TIME} 
恢复等级:{TRIGGER.SEVERITY} 
恢复信息:{TRIGGER.NAME} 
问题详情:{ITEM.NAME}:{ITEM.VALUE} 
事件代码:{EVENT.ID}

配置触发动作

image

测试

posted @ 2023-02-28 16:54  Solita1y  阅读(975)  评论(0编辑  收藏  举报