prometheus通过webhook推送告警至钉钉

时间:2024-05-23 20:31:32

注意 :二当家 在安装使用过程发现使用prometheus 结合叮叮推送

遇到了一些坑 这里记录一下 大家可以规避一下 

1 所在的linux 服务器要安装golang环境 老铁们自己要先装一下 

参考 https://blog.****.net/AlbertFly/article/details/83414713

我是折腾了一会才发现 因为make过不去 另外还要装git

我在使用叮叮参考了http://www.cnblogs.com/pyuh/articles/9544121.html (大家仔细看看)

2 其中没有提到  并且叮叮的git文件拉下来后要放入go的环境变量 

 https://blog.****.net/AlbertFly/article/details/83414713

export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
#$HOME = /usr/local/soft/go
export GOPATH=$HOME/gopath

要放入GOROOT下面  这个目录自己创建一下

在当前目录make会生成如下图的红框的可执行文件

prometheus通过webhook推送告警至钉钉

效果图

prometheus通过webhook推送告警至钉钉

 

以下转载: 

转载:

http://www.cnblogs.com/pyuh/articles/9544121.html 重要

 http://theo.im/blog/2017/10/16/release-prometheus-alertmanager-webhook-for-dingtalk/

将钉钉接入 Prometheus AlertManager WebHook

 

TL;DR

Disclaimer: Personally I dislike DingTalk(a.k.a DingDing) at all ?.

Project Repo: https://github.com/timonwong/prometheus-webhook-dingtalk

Why

在敝人的「忽悠」下,敝厂在 2016 年中就全面使用了 Prometheus 作为监控系统,这个系统现在已经比较火了,这里就不再多加口舌阐述。后来,敝厂使用了钉钉作为公司内部的指定通(dă)讯(kă)软件,取代了 Slack。不得不说,比起 Slack 来,显示报警通知,钉钉不美观。

按照我个人的看法,一个合格的报警通知,应该包含如下内容:

  • 报警级别
  • 简短的报警名称
  • 简要说明
  • 详细说明
  • 附加上下文信息
  • 一个链接,点击后进入指标的 Graph

幸运的是,Prometheus 已经包含了一个做得比较好的 Slack 报警模板,只需要从这里开始就可以了。

Routes

prometheus-webhook-dingtalk 默认监听在8060端口(可配置)上,提供了以下路由供 AlertManager 的 webhook_configs 使用:

  • /dingtalk/<profile>/send

注意这里的 <profile> 需要在 -ding.profile 中指定相应的名称(<profile>)以及钉钉的自定义机器人 WebHook URL(<dingtalk-webhook-url>)。

Usage

首先,我个人不喜欢随处都是配置文件的现象,因此 prometheus-webhook-dingtalk 的所有配置通过命令行参数完成:

 

1

2

3

4

5

6

7

8

9

10

11

 

usage: prometheus-webhook-dingtalk --ding.profile=DING.PROFILE [<flags>]

Flags:

-h, --help Show context-sensitive help (also try --help-long and --help-man).

--web.listen-address=":8060"

The address to listen on for web interface.

--ding.profile=DING.PROFILE ...

Custom DingTalk profile (can specify multiple times, <profile>=<dingtalk-url>).

--ding.timeout=5s Timeout for invoking DingTalk webhook.

--log.level=info Only log messages with the given severity or above. One of: [debug, info, warn, error]

--version Show application version.

关于这里的 -ding.profile 参数:为了支持同时往多个钉钉自定义机器人发送报警消息,因此 -ding.profile 可以在命令行中指定多次,比如:

 

1

2

3

 

prometheus-webhook-dingtalk \

--ding.profile="webhook1=https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxx" \

--ding.profile="webhook2=https://oapi.dingtalk.com/robot/send?access_token=yyyyyyyyyyy"

这里就定义了两个 WebHook,一个 webhook1,一个 webhook2,用来往不同的钉钉组发送报警消息。

然后在 AlertManager 的配置里面,加入相应的 receiver(注意下面的 url)即可:

 

1

2

3

4

5

6

7

8

9

 

receivers:

- name: send_to_dingding_webhook1

webhook_configs:

- send_resolved: false

url: http://localhost:8060/dingtalk/webhook1/send

- name: send_to_dingding_webhook2

webhook_configs:

- send_resolved: false

url: http://localhost:8060/dingtalk/webhook2/send

In Action

参见官方文章:https://open-doc.dingtalk.com/docs/doc.htm?treeId=257&articleId=105735&docType=1,然后将 webhook 地址拷贝下来,传给 prometheus-webhook-dingtalk

最后,来个截图看看效果吧:

prometheus通过webhook推送告警至钉钉

参考 : https://blog.****.net/u010871982/article/details/79229953?utm_source=blogxgwz8

Prometheus
添加钉钉机器人,获取webhook
下载插件并编译(不会编译的也可以直接下载编译好的二进制文件)
编辑启动脚本(请替换为自己的webhook URL 及 ding.profile)
cat dingding_start.sh

nohup /usr/local/alertmanager-0.8.0.linux-amd64/prometheus-webhook-dingtalk --ding.profile="ops_dingding=https://oapi.dingtalk.com/robot/send?access_token=11111111111"  --ding.profile="dev_dingding=https://oapi.dingtalk.com/robot/send?access_token=222222222222" 2>&1 1>/usr/local/alertmanager-0.8.0.linux-amd64/dingding.log & 

编辑alertmanager.yml,增加web_hook配置并重启alertmanager
- name: "ops_dingding" 
  webhook_configs:
    - send_resolved: true 
      url: "http://localhost:8060/dingtalk/ops_dingding/send"
- name: "dev_dingding"
  webhook_configs:
    - send_resolved: true
      url: "http://localhost:8060/dingtalk/dev_dingding/send"

这两个字符串代表发往对两个叮叮群组的推送, 对应的token也是不同的

 

prometheus通过webhook推送告警至钉钉