prometheus添加钉钉消息告警

2023-05-19 17:44:50 浏览数 (1)

# 安装prometheus-webhook-dingtalk插件
代码语言:javascript复制
#下载
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
# 创建启动服务文件
代码语言:javascript复制
#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

将prometheus-webhook-dingtalk.service文件移动到/usr/lib/systemd/system下

代码语言:javascript复制
#生效系统文件
systemctl daemon-reload
# 启动
systemctl start prometheus-webhook-dingtalk.service
# 停止
systemctl stop prometheus-webhook-dingtalk.service 

配置文件config.yml

代码语言:javascript复制
## 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" . }}'

告警模板default.tmpl

代码语言:javascript复制
{{ 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://img.yuanmabao.com/zijie/pic/2023/05/19/ks3ohb4buln.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" . }}

0 人点赞