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.
告警路由配置,用于告警信息的分组路由,可以将不同分组的告警发送给不同的收件人。
复制
route:
receiver: default
group_by: [alertname, cluster, service]
group_wait: 30s
group_interval: 5m
repeat_interval: 3h1.2.3.4.5.6.
路由子配置,优先级高于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.
告警接收人配置,每个receiver都有一个名字,经过route分组并且路由后需要指定一个receiver,可以配置不同类型的接收者:
复制
receivers:
- name: default
webhook_configs:
- url: http://webhook.example.com/alert1.2.3.4.
复制
- name: email
email_configs:
- to: admin@example.com
send_resolved: true1.2.3.4.
复制
- name: slack
slack_configs:
- channel: #alerts
api_url: https://hooks.slack.com/services/XXX/YYY/ZZZ1.2.3.4.
告警抑制,主要用于减少告警的次数,防止“告警轰炸:
复制
inhibit_rules:
- source_match:
severity: critical
target_match:
severity: warning
equal: [instance]1.2.3.4.5.6.
当一个 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.
阅读剩余
THE END