zabbix6.2.4企业微信群机器人告警

zabbix6.2.4企业微信群机器人告警

基于https://blog.csdn.net/stars_moon1024/article/details/123042369调试

创建媒介

在这里插入图片描述

脚本如下

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

  escapeMarkup: function (str, mode) {
    switch (mode) {
      case 'markdown':
        return str.replace(/([_*\[`])/g, '\\$&');

      case 'markdownv2':
        return str.replace(/([_*\[\]()~`>#+\-=|{}.!])/g, '\\$&');

      default:
        return str;
    }
  },

  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());

    try {
      response = JSON.parse(response);
    }
    catch (error) {
      response = null;
      Zabbix.log(4, '[Wechat Webhook] response parse error');
    }

    if (request.getStatus() !== 200 || response.errcode !== 0) {
      if (typeof response.description === 'string') {
        throw response.description;
      }
      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;
  if (['markdown', 'markdownv2'].indexOf(params.ParseMode) !== -1) {
    Wechat.message = Wechat.escapeMarkup(Wechat.message, params.ParseMode);
  }
  Wechat.sendMessage();

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

消息模板

问题:【监控告警】<font color="warning">{HOST.DESCRIPTION}</font>/<font color="warning">{HOST.CONN}</font>
信息:
>主机名称:**<font color="comment">{HOST.DESCRIPTION}</font>**
>主机IP:<font color="comment">{HOST.CONN}</font>
>告警时间:<font color="comment">{EVENT.DATE}-{EVENT.TIME}</font>
>告警等级:<font color="comment">{TRIGGER.SEVERITY}</font>
>告警信息:<font color="warning">{TRIGGER.NAME} 故障!</font>
>告警项目:<font color="comment">{TRIGGER.KEY1}</font>
>问题详情:<font color="comment">{ITEM.NAME}: {ITEM.VALUE}</font>
>当前状态:<font color="warning">{TRIGGER.STATUS}: {ITEM.VALUE1}</font>
>事件ID:<font color="comment">{EVENT.ID}</font>

效果如图
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值