Sign inSign up

lushirong/prometheus-webhook-dingtalk

By lushirong

•Updated over 1 year ago

Image
0

286

lushirong/prometheus-webhook-dingtalk repository overview

用于将 Alertmanager 的告警信息通过钉钉机器人的 Webhook 接口将告警信息发送到指定的钉钉群。

1.运行容器

docker run -d \
  --name dingtalk \
  --restart=always \
  -p 8060:8060 \
  -v ./dingtalk/config.yml:/etc/prometheus-webhook-dingtalk/config.yml:ro \
  -v ./dingtalk/template.tmpl:/etc/prometheus-webhook-dingtalk/template.tmpl:ro \
  lushirong/prometheus-webhook-dingtalk:latest \
  --web.listen-address=:8060 \
  --web.enable-ui \
  --web.enable-lifecycle \
  --config.file=/etc/prometheus-webhook-dingtalk/config.yml \
  --log.level=info \
  --log.format=logfmt

启动的时候加入 --web.enable-lifecycle 参数,然后通过 HTTP PUT 或 HTTP POST 方法访问 /-/reload 即可不重启就重新加载配置文件。例如执行以下命令进行热更新:

curl -XPOST http://localhost:8060/-/reload

以下是以docker-compose的方式运行

dingtalk:
    image: lushirong/prometheus-webhook-dingtalk:latest
    restart: always
    volumes:
      - ./dingtalk/config.yml:/etc/prometheus-webhook-dingtalk/config.yml:ro
      - ./dingtalk/template.tmpl:/etc/prometheus-webhook-dingtalk/template.tmpl:ro
    command:
      - '--web.listen-address=:8060'
      - '--web.enable-ui'
      - '--web.enable-lifecycle'
      - '--config.file=/etc/prometheus-webhook-dingtalk/config.yml'
      - '--log.level=info'       # debug, info, warn, error
      - '--log.format=logfmt'    # logfmt, json
    ports:
      - "8060:8060"

配置文件config.yml的样例:

templates:
  - /etc/prometheus-webhook-dingtalk/template.tmpl

targets:
  # 告警1
  webhook1:
    url: https://oapi.dingtalk.com/robot/send?access_token=xxxxxx-xxxxx-xxxxx-xxxxxx-xxxxxxx
    #secret: yyyyyy-yyyyy-yyyyy-yyyyyy-yyyyyyy    # 如果钉钉加签,则配置加签的密钥
    #mention:
       #all: true  # 艾特所有人
       #mobiles: ['156xxxx8827', '189xxxx8325']  # 通过手机号艾特指定人员

xxxxxx-xxxxx-xxxxx-xxxxxx-xxxxxxx 是钉钉机器人生成的 Webhook 地址,yyyyyy-yyyyy-yyyyy-yyyyyy-yyyyyyy 是钉钉机器人的加签密钥。

模板文件template.yml的样例:

{{ define "__subject" }}
[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}]
{{ end }}


{{ define "__alert_list" }}{{ range . }}
---
**告警主题**: <font color="#FF8C00">{{ .Annotations.summary }}</font>  
**告警名称**: <font color="#FF8C00">{{ .Labels.alertname }}</font>  
**告警级别**: <font color="#FF8C00">{{ .Labels.severity }}</font>  
**告警主机**: <font color="#FF8C00">{{ .Labels.instance }}</font>  
**开始时间**: <font color="#FF8C00">{{ dateInZone "2006-01-02 15:04:05" (.StartsAt) "Asia/Shanghai" }}</font>  
**持续时间**: <font color="#FF8C00">{{ now.Sub (.StartsAt) }}</font>  
**告警详情**: <font color="#FF8C00">{{ index .Annotations "description" }}</font>  
{{ end }}
{{ end }}

{{ define "__resolved_list" }}{{ range . }}
---
**告警主题**: <font color="#0000FF">{{ .Annotations.summary }}</font>  
**告警名称**: <font color="#0000FF">{{ .Labels.alertname }}</font>  
**告警级别**: <font color="#0000FF">{{ .Labels.severity }}</font>  
**告警主机**: <font color="#0000FF">{{ .Labels.instance }}</font>  
**开始时间**: <font color="#0000FF">{{ dateInZone "2006-01-02 15:04:05" (.StartsAt) "Asia/Shanghai" }}</font>  
**恢复时间**: <font color="#0000FF">{{ dateInZone "2006-01-02 15:04:05" (.EndsAt) "Asia/Shanghai" }}</font>  
**持续时间**: <font color="#0000FF">{{ .EndsAt.Sub (.StartsAt) }}</font>  
**告警详情**: <font color="#0000FF">{{ index .Annotations "description" }}</font>  
{{ end }}
{{ end }}


{{ define "default.title" }}
{{ template "__subject" . }}
{{ end }}

{{ define "default.content" }}
{{ if gt (len .Alerts.Firing) 0 }}
<font color="#FF0000">[Prometheus告警信息]</font>

**侦测到{{ .Alerts.Firing | len  }}个告警**

2.配置 Alertmanager

在 Alertmanager 的配置文件中,添加 Webhook 接收器,指向上述容器的地址。

receivers:
- name: webhook-test                                                                                                           
  webhook_configs:                                                                                                             
  - url: 'http://127.0.0.1:8060/dingtalk/webhook1/send'

3.测试

curl -X POST http://127.0.0.1:8060/dingtalk/webhook1/send \
-H "Content-Type: application/json" \
-d '{
  "alerts": [
    {
      "status": "firing",
      "labels": {
        "instance": "172.16.20.20",
        "severity": "critical",
        "alertname": "InstanceDown"
      },
      "annotations": {
        "summary": "Instance 172.16.20.20 down",
        "description": "The instance 172.16.20.20 is down."
      },
      "startsAt": "2025-03-01T00:30:30Z",
      "endsAt": "0001-01-01T00:00:00Z"
    },
    {
      "status": "resolved",
      "labels": {
        "instance": "172.16.20.20",
        "severity": "critical",
        "alertname": "InstanceDown"
      },
      "annotations": {
        "summary": "Instance 172.16.20.20 is back up",
        "description": "The instance 172.16.20.20 has recovered."
      },
      "startsAt": "2025-03-04T14:30:30Z",
      "endsAt": "2025-03-04T14:47:05Z"
    }
  ]
}'

Tag summary

Content type

Image

Digest

sha256:2be36c995…

Size

17 MB

Last updated

over 1 year ago

docker pull lushirong/prometheus-webhook-dingtalk