首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Zabbix 随笔:钉钉机器人告警(webhook 方式)

Zabbix 随笔:钉钉机器人告警(webhook 方式)

作者头像
IT小白Kasar
发布2022-02-16 18:41:04
发布2022-02-16 18:41:04
3.4K4
举报
文章被收录于专栏:个人技术随笔个人技术随笔

前面有提到通过 python 的方式告警,但由于每个人的环境都不一样,容易出现格式缩进等一些问题,而实际上钉钉机器人也是通过 webhook 的方式来实现的,Zabbix 5.2 版本原生支持 webhook 的方式去推送事件。

首先贴下代码,下面的 title 部分记得修改为自己的

代码语言:javascript
复制
var dingding = {
  key: null,
  message: null,
  msgtype: "markdown",
  proxy: null,
sendMessage: function () {
    var params = {
       msgtype: dingding.msgtype,
       markdown: {
          title: "IT小白Kasar",(注意这里可以修改)
          text: dingding.message
        },
      },
      data,
      response,
      request = new CurlHttpRequest(),
      url =
        "https://oapi.dingtalk.com/robot/send?access_token=" +
        dingding.key;
    if (dingding.proxy) {
      request.setProxy(dingding.proxy);
    }
    request.AddHeader("Content-Type: application/json");
    data = JSON.stringify(params);
    // Remove replace() function if you want to see the exposed key in the log file.
    Zabbix.Log(
      4,
      "[dingding Webhook] URL: " + url.replace(dingding.key, "<BOT KEY>")
    );
    Zabbix.Log(4, "[dingding Webhook] params: " + data);
    response = request.Post(url, data);
    Zabbix.Log(4, "[dingding Webhook] HTTP code: " + request.Status());
    try {
      response = JSON.parse(response);
    } catch (error) {
      response = null;
    }
    if (request.Status() !== 200 || response.errcode !== 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';
  }
  dingding.key = params.Key;
  if (params.HTTPProxy) {
    dingding.proxy = params.HTTPProxy;
  }
  dingding.to = params.To;
  dingding.message = params.Subject + "\n" + params.Message;
  dingding.sendMessage();
  return "OK";
} catch (error) {
  Zabbix.Log(4, "[dingding Webhook] notification failed: " + error);
  throw "Sending failed: " + error + ".";
}

这个脚本贴在下图的这个位置,在报警媒介类型下

需要将key部分修改为自己的机器人的token,主要是下图里的”access_token=”的一串字符串。

添加完成后,我们就可以测试下

然后添加下默认告警媒介

最终效果

写在最后

这种方式就摆脱了格式问题,可以实现开箱即用,而且无需装额外的环境即可实现,快去试试吧。最后感谢官方的模板,有借鉴部分。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2021-10-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 写在最后
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档