Zabbix 5.2 由浅入深之钉钉机器人告警(webhook方式)

运维 系统运维
实际上钉钉机器人也是通过webhook的方式来实现的,Zabbix 5.2版本原生支持webhook的方式去推送事件。

[[403158]]

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

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

  1. var dingding = { 
  2.   keynull
  3.   message: null
  4.   msgtype: "markdown"
  5.   proxy: null
  6. sendMessage: function () { 
  7.     var params = { 
  8.        msgtype: dingding.msgtype, 
  9.        markdown: { 
  10.           title: "IT小白Kasar",(注意这里可以修改) 
  11.           text: dingding.message 
  12.         }, 
  13.       }, 
  14.       data, 
  15.       response, 
  16.       request = new CurlHttpRequest(), 
  17.       url = 
  18.         "https://oapi.dingtalk.com/robot/send?access_token=" + 
  19.         dingding.key
  20.     if (dingding.proxy) { 
  21.       request.setProxy(dingding.proxy); 
  22.     } 
  23.     request.AddHeader("Content-Type: application/json"); 
  24.     data = JSON.stringify(params); 
  25.     // Remove replace() function if you want to see the exposed key in the log file. 
  26.     Zabbix.Log( 
  27.       4, 
  28.       "[dingding Webhook] URL: " + url.replace(dingding.key"<BOT KEY>"
  29.     ); 
  30.     Zabbix.Log(4, "[dingding Webhook] params: " + data); 
  31.     response = request.Post(url, data); 
  32.     Zabbix.Log(4, "[dingding Webhook] HTTP code: " + request.Status()); 
  33.     try { 
  34.       response = JSON.parse(response); 
  35.     } catch (error) { 
  36.       response = null
  37.     } 
  38.     if (request.Status() !== 200 || response.errcode !== 0) { 
  39.       if (typeof response.errmsg === "string") { 
  40.         throw response.errmsg; 
  41.       } else { 
  42.         throw "Unknown error. Check debug log for more information."
  43.       } 
  44.     } 
  45.   }, 
  46. }; 
  47. try { 
  48.   var params = JSON.parse(value); 
  49.   if (typeof params.Key === "undefined") { 
  50.     throw 'Incorrect value is given for parameter "Key": parameter is missing'
  51.   } 
  52.   dingding.key = params.Key
  53.   if (params.HTTPProxy) { 
  54.     dingding.proxy = params.HTTPProxy; 
  55.   } 
  56.   dingding.to = params.To
  57.   dingding.message = params.Subject + "\n" + params.Message; 
  58.   dingding.sendMessage(); 
  59.   return "OK"
  60. } catch (error) { 
  61.   Zabbix.Log(4, "[dingding Webhook] notification failed: " + error); 
  62.   throw "Sending failed: " + error + "."

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

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

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

然后添加下默认告警媒介

最终效果

未美化效果

写在最后

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

 

责任编辑:姜华 来源: 今日头条
相关推荐

2020-12-31 08:36:03

Zabbix邮箱告警Python

2022-12-06 08:00:16

awscli工具监控

2021-04-21 09:16:04

Python开发钉钉群机器人

2020-04-14 15:33:37

Python 钉钉机器人

2023-02-13 07:40:35

ChatGPT钉钉机器人

2021-03-19 08:47:38

Zabbix 5.2VMware监控

2021-04-23 08:41:17

Zabbix 5.2Nginx监控

2021-04-15 08:45:25

Zabbix 5.2Apache监控

2021-04-25 08:28:44

Zabbix 5.2监控IIS服务运维

2021-05-12 08:13:31

Zabbix 5.2php-fpm监控

2021-05-13 07:42:06

Zabbix汉化调整开源

2023-04-28 10:15:20

2021-02-01 09:13:34

Zabbix5.2拓扑图运维

2021-06-11 06:54:00

Zabbix 5.2yum仓库监控

2021-05-28 08:31:51

Zabbix监控公有云

2020-02-17 15:17:57

钉钉

2021-03-25 08:21:07

Zabbix主机群组监控

2021-05-20 06:14:00

Zabbix 5.2华为交换机监控

2021-01-04 08:55:07

ZabbixProxy分布式部署

2021-05-29 14:14:16

阿里云钉钉低代码开发
点赞
收藏

51CTO技术栈公众号