prometheus添加钉钉消息告警
# 安装prometheus-webhook-dingtalk插件
#下载
wget https://github.com/timonwong/prometheus-webhook-dingtalk/releases/download/v1.4.0/prometheus-webhook-dingtalk-1.4.0.linux-amd64.tar.gz
#解压
tar -zxf prometheus-webhook-dingtalk-1.4.0.linux-amd64.tar.gz
# 移动到安装目录
mv prometheus-webhook-dingtalk-1.4.0.linux-amd64 /opt/apps/dingding/prometheus-webhook-dingtalk
# 进入目录
cd /opt/apps/dingding/prometheus-webhook-dingtalk
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 创建启动服务文件
#prometheus-webhook-dingtalk.service
[Unit]
Description=prometheus-webhook-dingtalk
After=network-online.target
[Service]
Restart=on-failure
ExecStart=/opt/apps/dingding/prometheus-webhook-dingtalk/prometheus-webhook-dingtalk --config.file=/opt/apps/dingding/prometheus-webhook-dingtalk/config.yml
[Install]
WantedBy=default.target
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
将prometheus-webhook-dingtalk.service文件移动到/usr/lib/systemd/system下
#生效系统文件
systemctl daemon-reload
# 启动
systemctl start prometheus-webhook-dingtalk.service
# 停止
systemctl stop prometheus-webhook-dingtalk.service
1
2
3
4
5
6
2
3
4
5
6
配置文件config.yml
## Request timeout
# timeout: 5s
## Customizable templates path
templates:
- /opt/apps/dingding/prometheus-webhook-dingtalk/contrib/templates/default.tmpl
## You can also override default template using `default_message`
## The following example to use the 'legacy' template from v0.3.0
# default_message:
# title: '{{ template "legacy.title" . }}'
# text: '{{ template "legacy.content" . }}'
## Targets, previously was known as "profiles"
targets:
webhook:
url: https://oapi.dingtalk.com/robot/send?access_token=xxx
message:
title: '{{ template "default.title" . }}'
text: '{{ template "default.content" . }}'
webhook1:
url: https://oapi.dingtalk.com/robot/send?access_token=xxx
message:
title: '{{ template "default.title" . }}'
text: '{{ template "default.content" . }}'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
告警模板default.tmpl
{{ define "__subject" }}
[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}]
{{ end }}
{{ define "__alert_list" }}{{ range . }}
---
**告警名称**: {{ index .Annotations "summary" }}
**告警级别**: {{ .Labels.severity }}
**告警主机**: {{ .Labels.instance }}
**告警信息**: {{ index .Annotations "description" }}
**维护团队**: {{ .Labels.team | upper }}
**告警时间**: {{ dateInZone "2006.01.02 15:04:05" (.StartsAt) "Asia/Shanghai" }}
{{ end }}{{ end }}
{{ define "__resolved_list" }}{{ range . }}
---
**告警名称**: {{ index .Annotations "summary" }}
**告警级别**: {{ .Labels.severity }}
**告警主机**: {{ .Labels.instance }}
**告警信息**: {{ index .Annotations "description" }}
**维护团队**: {{ .Labels.team | upper }}
**告警时间**: {{ dateInZone "2006.01.02 15:04:05" (.StartsAt) "Asia/Shanghai" }}
**恢复时间**: {{ dateInZone "2006.01.02 15:04:05" (.EndsAt) "Asia/Shanghai" }}
{{ end }}{{ end }}
{{ define "default.title" }}
{{ template "__subject" . }}
{{ end }}
{{ define "default.content" }}
![警报 图标](https://oss-test-v1.1quant.com/monitor/logo.png)
{{ if gt (len .Alerts.Firing) 0 }}
**====侦测到{{ .Alerts.Firing | len }}个故障====**
{{ template "__alert_list" .Alerts.Firing }}
---
{{ end }}
{{ if gt (len .Alerts.Resolved) 0 }}
**====恢复{{ .Alerts.Resolved | len }}个故障====**
{{ template "__resolved_list" .Alerts.Resolved }}
{{ end }}
{{ end }}
{{ define "ding.link.title" }}{{ template "default.title" . }}{{ end }}
{{ define "ding.link.content" }}{{ template "default.content" . }}{{ end }}
{{ template "default.title" . }}
{{ template "default.content" . }}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
上次更新: 2023/06/09, 19:14:23