Prometheus实战教程 05 - 告警通知实现 - 邮件 + 钉钉 + 自定义告警模板

文章目录


📚 我的专栏|持续更新中

🐍 Vben Admin框架实战(Python Flask后端)

🐹 Golang 运维开发实战手册

🐧 Linux性能优化原理和实战

💬 Kafka原理与运维实战

🚀 Linux 运维进阶必备【案例分享】


上 一节内容,已经部署好了Alertmanager服务,同时也接受到了页面上的告警,本节将介绍通过多种方式将告警发送出来。

主流告警通知方式

  • 邮件通知:最基础通用的方式,配置简单,适合所有场景,可附带告警详情和图表链接。
  • 即时通讯工具:企业常用,响应速度快,支持 Slack、钉钉、企业微信、Telegram 等,需通过对应机器人或 API 集成。
  • 短信通知:适合紧急告警(如服务宕机),需对接短信服务商(如云片网、Twilio),注意成本控制。
  • WebHook 通知:灵活性最高,可自定义转发告警到自研系统、监控平台(如 Grafana)或第三方工具(如 PagerDuty)。
  • 语音通知:极端紧急场景使用,对接语音呼叫服务商,通过电话语音播报告警内容。

所有通知方式均需在 Alertmanager 的配置文件(alertmanager.yml)中定义,核心步骤:

  • 配置 "receivers"(接收者),指定通知方式的具体参数(如邮件服务器地址、钉钉机器人 token)。
  • 配置 "route"(路由规则),将不同类型的告警分发到对应的接收者。

邮件告警 (163邮箱)

163 邮箱账号设置

  1. 拥有 163 邮箱账号,并开启 SMTP 服务(163 邮箱需单独开启,默认关闭):
  2. 登录 163 邮箱 → 进入「设置」→「POP3/SMTP/IMAP」→ 开启「SMTP 服务」
  3. 开启时可能需要验证手机,最终会获得一个 授权码(注意:不是邮箱登录密码,后续配置需用此授权码)

参考地址:

如何开启客户端协议?

编辑 Alertmanager 配置文件

cd /data/middleware/alertmanager-0.28.1

vim alertmanager.yml

bash 复制代码
global:
  # SMTP服务器配置(163邮箱的SMTP服务器)
  smtp_smarthost: 'smtp.163.com:25'  # 163 SMTP服务器地址和端口(25或465,25更常用)
  smtp_from: 'your_email@163.com'    # 发件人邮箱(必须是你的163邮箱)
  smtp_auth_username: 'your_email@163.com'  # 邮箱账号(同发件人)
  smtp_auth_password: 'your_auth_code'      # 163邮箱的授权码(不是登录密码)
  smtp_require_tls: false  # 163 SMTP默认支持TLS,但部分环境可能需要关闭(根据实际情况调整)

route:
  group_by: ['alertname']  # 按alertname分组
  group_wait: 10s          # 组内第一个警报触发后等待10s再发送
  group_interval: 10s      # 组内再次发送的间隔
  repeat_interval: 1h      # 重复发送相同警报的间隔(避免频繁通知)
  receiver: 'email-notifications'  # 关联到下面的receiver

receivers:
- name: 'email-notifications'
  email_configs:
  - to: 'recipient@example.com'  # 收件人邮箱(可以是任意邮箱,如个人QQ/企业邮箱)
    send_resolved: true  # 当警报解除时,是否发送恢复通知(建议开启)

重启 Alertmanager 使配置生效:

bash 复制代码
# 若用systemd管理
systemctl restart alertmanager
# 若直接运行
kill -HUP $(pgrep alertmanager)  # 热重载配置(推荐)

若仍报错,查看 Alertmanager 日志(如journalctl -u alertmanager)

测试邮件通知

可以通过模拟一个警报触发来测试:

主机关机后,接受到告警:

处理好告警后,也接受到了告警恢复通知。

钉钉通知

在 Alertmanager 中配置钉钉通知,需要通过 webhook 方式对接钉钉机器人。

创建钉钉机器人并获取 webhook

  1. 打开钉钉群,点击右上角「群设置」→「智能群助手」→「添加机器人」→「自定义机器人」
  2. 给机器人命名(如 Alertmanager-Notify),设置安全策略(推荐「自定义关键词」,例如添加 监控报警 作为关键词,确保通知内容包含该词)
  3. 点击「完成」后,复制机器人的 webhook 地址(格式:https://oapi.dingtalk.com/robot/send?access_token=xxxx)

部署格式转换服务(prometheus-webhook-dingtalk)

prometheus-webhook-dingtalk 是官方推荐的工具,可将 Alertmanager 告警格式转为钉钉支持的格式。

下载地址: https://github.com/timonwong/prometheus-webhook-dingtalk/releases

bash 复制代码
cd /data/middleware/

wget https://github.com/timonwong/prometheus-webhook-dingtalk/releases/download/v2.1.0/prometheus-webhook-dingtalk-2.1.0.linux-amd64.tar.gz

tar xvf prometheus-webhook-dingtalk-2.1.0.linux-amd64.tar.gz

根据配置文件模板,创建一个新的配置。cp config.example.yml config.yml

bash 复制代码
[root@monitor-20 prometheus-webhook-dingtalk-2.1.0.linux-amd64]# cat config.yml 
## Request timeout
# timeout: 5s

## Uncomment following line in order to write template from scratch (be careful!)
#no_builtin_template: true

## Customizable templates path
#templates:
#  - contrib/templates/legacy/template.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"
# 这里要注意: default, 后面Alertmanager对接web-hook的地址要用到。
# access_token, 要填写自己申请的。
targets:
  default:
    url: https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxx

启动 prometheus-webhook-dingtalk

bash 复制代码
[root@monitor-20 prometheus-webhook-dingtalk-2.1.0.linux-amd64]# ./prometheus-webhook-dingtalk --web.enable-ui
ts=2025-11-06T07:40:29.076Z caller=main.go:59 level=info msg="Starting prometheus-webhook-dingtalk" version="(version=2.1.0, branch=HEAD, revision=8580d1395f59490682fb2798136266bdb3005ab4)"
ts=2025-11-06T07:40:29.076Z caller=main.go:60 level=info msg="Build context" (gogo1.18.1,userroot@177bd003ba4d,date20220421-08:19:05)=(MISSING)
ts=2025-11-06T07:40:29.076Z caller=coordinator.go:83 level=info component=configuration file=config.yml msg="Loading configuration file"
ts=2025-11-06T07:40:29.076Z caller=coordinator.go:91 level=info component=configuration file=config.yml msg="Completed loading of configuration file"
ts=2025-11-06T07:40:29.076Z caller=main.go:97 level=info component=configuration msg="Loading templates" templates=
ts=2025-11-06T07:40:29.077Z caller=main.go:113 component=configuration msg="Webhook urls for prometheus alertmanager" urls=http://localhost:8060/dingtalk/default/send
ts=2025-11-06T07:40:29.077Z caller=web.go:208 level=info component=web msg="Start listening for connections" address=:8060

配置 Alertmanager 对接钉钉

bash 复制代码
global:
  # SMTP服务器配置(163邮箱的SMTP服务器)
  smtp_smarthost: 'smtp.163.com:25'  # 163 SMTP服务器地址和端口(25或465,25更常用)
  smtp_from: 'your_email@163.com'    # 发件人邮箱(必须是你的163邮箱)
  smtp_auth_username: 'your_email@163.com'  # 邮箱账号(同发件人)
  smtp_auth_password: 'your_auth_code'      # 163邮箱的授权码(不是登录密码)
  smtp_require_tls: false  # 163 SMTP默认支持TLS,但部分环境可能需要关闭(根据实际情况调整)

route:
  group_by: ['alertname']  # 按alertname分组
  group_wait: 10s          # 组内第一个警报触发后等待10s再发送
  group_interval: 10s      # 组内再次发送的间隔
  repeat_interval: 1h      # 重复发送相同警报的间隔(避免频繁通知)
  #receiver: 'email-notifications'  # 关联到下面的receiver
  receiver: 'dingtalk'  # 关联到下面的receiver

receivers:
- name: 'dingtalk'
  webhook_configs:
  - url: 'http://127.0.0.1:8060/dingtalk/default/send'  # 替换为实际地址,这里的default和上节对应。
    send_resolved: true

重启 Alertmanager 使配置生效:

bash 复制代码
# 若用systemd管理
systemctl restart alertmanager
# 若直接运行
kill -HUP $(pgrep alertmanager)  # 热重载配置(推荐)

钉钉群测试效果

告警效果

告警恢复效果

prometheus-webhook-dingtalk插件自定义告警模板

定义模板文件
bash 复制代码
mkdir -p templates/cuiwin/

vim templates/cuiwin/template.tmpl

bash 复制代码
{{ define "__subject" }}[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}] {{ .GroupLabels.SortedPairs.Values | join " " }} {{ if gt (len .CommonLabels) (len .GroupLabels) }}({{ with .CommonLabels.Remove .GroupLabels.Names }}{{ .Values | join " " }}{{ end }}){{ end }}{{ end }}
{{ define "__alertmanagerURL" }}{{ .ExternalURL }}/#/alerts?receiver={{ .Receiver }}{{ end }}

{{ define "__text_alert_list" }}{{ range . }}
**Labels**
{{ range .Labels.SortedPairs }}> - {{ .Name }}: {{ .Value | markdown | html }}
{{ end }}
**Annotations**
{{ range .Annotations.SortedPairs }}> - {{ .Name }}: {{ .Value | markdown | html }}
{{ end }}
**Source:** [{{ .GeneratorURL }}]({{ .GeneratorURL }})
{{ end }}{{ end }}

{{ define "cuiwin.__text_alert_list" }}{{ range . }}
#### \[{{ .Labels.severity | upper }}\] {{ .Annotations.summary }}

**Description:** {{ .Annotations.description }}

**Graph:** [?链接]({{ .GeneratorURL }})

**Details:**
{{ range .Labels.SortedPairs }}{{ if and (ne (.Name) "severity") (ne (.Name) "summary") }}> - {{ .Name }}: {{ .Value | markdown | html }}
{{ end }}{{ end }}
{{ end }}{{ end }}

{{/* cuiwin */}}
{{ define "cuiwin.title" }}{{ template "__subject" . }}{{ end }}
{{ define "cuiwin.content" }}#### \[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}\] **[{{ index .GroupLabels "alertname" }}]({{ template "__alertmanagerURL" . }})**
{{ if gt (len .Alerts.Firing) 0 -}}
**?告警通知?**
{{ template "cuiwin.__text_alert_list" .Alerts.Firing }}
{{ range .AtMobiles }}@{{ . }}{{ end }}
{{- end }}
{{ if gt (len .Alerts.Resolved) 0 -}}
**?告警恢复?**
{{ template "cuiwin.__text_alert_list" .Alerts.Resolved }}
{{ range .AtMobiles }}@{{ . }}{{ end }}
{{- end }}
{{- end }}
prometheus-webhook-dingtalk引入模板
bash 复制代码
[root@monitor-20 prometheus-webhook-dingtalk-2.1.0.linux-amd64]# cat config.yml 
## Request timeout
# timeout: 5s

## Uncomment following line in order to write template from scratch (be careful!)
#no_builtin_template: true

## Customizable templates path
templates:
  - templates/cuiwin/template.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:
  default:
    url: https://oapi.dingtalk.com/robot/send?access_token=1625b24f3da3c4861b6d9d25685a0a5c682a5b1193230df3133a14ce56c022f8
    # Customize template content
    message:
      # Use my  template
      title: '{{ template "cuiwin.title" . }}'
      text: '{{ template "cuiwin.content" . }}'

执行 ./prometheus-webhook-dingtalk --web.enable-ui 启动插件,可以看到已经加载了我们的模板

访问 http://192.168.37.20:8060/ui/ 可以预览模板样式。

自定义模板告警效果


相关推荐
杰克逊的日记1 天前
通过Prometheus对GPU集群进行监控以及搭建(小型集群)
prometheus·gpu算力·gpu服务器监控
happy_king_zi2 天前
RabbitMQ-Exporter 监控 TLS 加密的 RabbitMQ 集群
分布式·安全·rabbitmq·prometheus
CHN悠远3 天前
debian13 安装钉钉后,钉钉无法运行问题的解决办法
linux·运维·服务器·钉钉·debian13
梁正雄4 天前
6、prometheus资源规划
运维·服务器·服务发现·prometheus·监控
梁正雄5 天前
4、prometheus-服务发现k8s api-2
kubernetes·服务发现·prometheus
PyAIGCMaster6 天前
钉钉的设计理念方面,我可以学习
人工智能·深度学习·学习·钉钉
是孑然呀7 天前
【钉钉多元表格(自动化)】钉钉群根据表格 自动推送当天值日生信息
运维·自动化·钉钉
IT小哥哥呀7 天前
Node.js 实现企业内部消息通知系统(钉钉/企业微信机器人)
node.js·钉钉·企业微信·webhook·后端开发·自动化通知·mysql实战
麦兜*8 天前
Spring Boot 应用 Docker 监控:Prometheus + Grafana 全方位监控
spring boot·后端·spring cloud·docker·prometheus