Alertmanager 告警配置文件和告警规则详解

一、什么是 Alertmanager?

Alertmanager 是 Prometheus 生态中的一个重要组件,用于处理 Prometheus 发送的告警(Alerts)。它提供了告警分组、抑制、去重、路由以及告警通知等功能。它可以在发现问题时立即通知相关负责人,使运维人员能快速响应并采取措施。

下面是告警是流程图:

Alertmanager 的主要功能:

告警分组(Grouping):将相似的告警合并,减少告警数量。去重(Deduplication):如果同一告警在短时间内重复触发,Alertmanager 只会发送一次。抑制(Inhibition):当某个高优先级告警触发时,屏蔽低优先级的相关告警。路由(Routing):将不同类型的告警发送到不同的接收者(如邮件、Slack、Webhook)。通知(Notification):支持多种通知方式,如邮件、Slack、PagerDuty、Webhook 等。

二、Alertmanager 配置文件详解

完整的配置文件,这个只是实例,生产环境需要根据实际配置:

复制
global: resolve_timeout: 5m smtp_smarthost: smtp.example.com:587 smtp_from: alert@example.com smtp_auth_username: user@example.com smtp_auth_password: yourpassword smtp_hello: "qq.com" smtp_require_tls: false route: receiver: default group_by: [alertname, cluster, service] # 分组依据 group_wait: 30s # 第一次分组告警发送前的等待时间 group_interval: 5m # 组内新告警的发送间隔 repeat_interval: 3h # 相同告警重复发送的间隔 routes: - match: severity: critical receiver: email continue: true# 继续匹配后续规则 - match: severity: warning receiver: slack - match: alertname: InstanceDown receiver: webhook receivers: - name: default webhook_configs: - url: http://webhook.example.com/alert# Webhook 地址 - name: email email_configs: - to: admin@example.com from: alert@example.com smarthost: smtp.example.com:587 auth_username: user@example.com auth_password: yourpassword send_resolved: true# 发送告警恢复通知 - name: slack slack_configs: - channel: #alerts send_resolved: true api_url: https://hooks.slack.com/services/XXX/YYY/ZZZ title: {{ .CommonAnnotations.summary }} text: {{ .CommonAnnotations.description }} - name: webhook webhook_configs: - url: http://alert-handler.local/notify inhibit_rules: - source_match: severity: critical target_match: severity: warning equal: [instance]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.

Alertmanager 的配置文件 alertmanager.yml 主要包括以下几个部分,每个部分的作用及参数如下。

1. global 配置

全局配置,Global块配置下的配置选项在本配置文件内的所有配置项下可见,但是文件内其他位置的子配置可以覆盖Global配置。

复制
global: resolve_timeout: 5m # 告警恢复后等待 5 分钟再标记为已解决 smtp_smarthost: smtp.example.com:587 # 邮件服务器地址 smtp_from: xxx@example.com # 发送邮件的地址 smtp_auth_username: xxx@example.com # 邮件服务器认证用户名 smtp_auth_password: yourpassword # 邮件服务器认证密码1.2.3.4.5.6.
resolve_timeout:在告警恢复后,等待多长时间才将其标记为“已解决”。smtp_smarthost:用于发送邮件告警的邮件服务器地址。smtp_from:用于发送告警邮件的发件人地址。smtp_auth_username 和 smtp_auth_password:用于认证 SMTP 服务器的用户名和授权码。smtp_require_tls:是否开启tls认证2. route 配置

告警路由配置,用于告警信息的分组路由,可以将不同分组的告警发送给不同的收件人。

复制
route: receiver: default group_by: [alertname, cluster, service] group_wait: 30s group_interval: 5m repeat_interval: 3h1.2.3.4.5.6.
receiver:默认的接收器(如果没有匹配到具体的路由规则,则使用此接收器)。group_by:定义告警分组的方式,确保相同服务的告警不会重复发送。group_wait:在发送第一个告警前等待的时间,避免瞬时波动导致告警风暴。group_interval:在同一分组内,新增告警的发送间隔。repeat_interval:相同告警重复发送的时间间隔,防止过度通知。3. routes 配置

路由子配置,优先级高于route,配置和route一样的:

复制
routes: - match: severity: critical receiver: email continue: true - match: severity: warning receiver: slack - match: alertname: InstanceDown receiver: webhook1.2.3.4.5.6.7.8.9.10.11.
match:定义匹配规则,例如 severity: critical 表示匹配所有严重告警。receiver:匹配该规则的告警将发送到指定接收器。continue:如果为 true,则匹配该规则后仍会继续匹配其他规则,默认是找到符合的规则后就不在往下继续匹配。4. receivers 配置

告警接收人配置,每个receiver都有一个名字,经过route分组并且路由后需要指定一个receiver,可以配置不同类型的接收者:

复制
receivers: - name: default webhook_configs: - url: http://webhook.example.com/alert1.2.3.4.
name:定义接收器的名称。webhook_configs:使用 Webhook 发送告警。
复制
- name: email email_configs: - to: admin@example.com send_resolved: true1.2.3.4.
to:邮件接收者。send_resolved:是否发送恢复通知。
复制
- name: slack slack_configs: - channel: #alerts api_url: https://hooks.slack.com/services/XXX/YYY/ZZZ1.2.3.4.
channel:告警发送的 Slack 频道。api_url:Slack Webhook URL。5. inhibit_rules 配置

告警抑制,主要用于减少告警的次数,防止“告警轰炸:

复制
inhibit_rules: - source_match: severity: critical target_match: severity: warning equal: [instance]1.2.3.4.5.6.
source_match:定义高优先级的告警。target_match:当 source_match 触发时,抑制 target_match。equal:只有在相同 instance 触发时才应用抑制规则。

当一个 severity 为 critical 的告警触发时,所有 severity 为 warning 且 alertname 和 instance 标签与 critical 告警相同的告警将被抑制,不会发送通知。

完整的配置文件应该还有个Templates配置:用于放置自定义模板的位置,这里不展开讲

三、Prometheus 告警规则详解

1. 告警规则文件

告警配置文件可以自定义名称,在prometheus配置文件同步就行。

alert-rules.yml:

复制
groups: - name: 主机状态监控 rules: - alert: 主机宕机 expr: up == 0 for: 1m labels: severity: critical annotations: summary: "实例 {{ $labels.instance }} 宕机" description: "实例 {{ $labels.instance }} 已经宕机超过 1 分钟。请检查服务状态。"1.2.3.4.5.6.7.8.9.10.11.
expr:告警触发条件。for:持续多长时间才触发告警。labels:告警标签,可用于匹配规则。annotations:告警的详细描述。

阅读剩余
THE END