Alertmanager 告警配置文件和告警規(guī)則詳解
一、什么是 Alertmanager?
Alertmanager 是 Prometheus 生態(tài)中的一個重要組件,用于處理 Prometheus 發(fā)送的告警(Alerts)。它提供了告警分組、抑制、去重、路由以及告警通知等功能。它可以在發(fā)現(xiàn)問題時立即通知相關(guān)負(fù)責(zé)人,使運維人員能快速響應(yīng)并采取措施。
下面是告警是流程圖:
Alertmanager 的主要功能:
- 告警分組(Grouping):將相似的告警合并,減少告警數(shù)量。
- 去重(Deduplication):如果同一告警在短時間內(nèi)重復(fù)觸發(fā),Alertmanager 只會發(fā)送一次。
- 抑制(Inhibition):當(dāng)某個高優(yōu)先級告警觸發(fā)時,屏蔽低優(yōu)先級的相關(guān)告警。
- 路由(Routing):將不同類型的告警發(fā)送到不同的接收者(如郵件、Slack、Webhook)。
- 通知(Notification):支持多種通知方式,如郵件、Slack、PagerDuty、Webhook 等。
二、Alertmanager 配置文件詳解
完整的配置文件,這個只是實例,生產(chǎn)環(huán)境需要根據(jù)實際配置:
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'] # 分組依據(jù)
group_wait: 30s # 第一次分組告警發(fā)送前的等待時間
group_interval: 5m # 組內(nèi)新告警的發(fā)送間隔
repeat_interval: 3h # 相同告警重復(fù)發(fā)送的間隔
routes:
- match:
severity: critical
receiver: 'email'
continue: true# 繼續(xù)匹配后續(xù)規(guī)則
- 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# 發(fā)送告警恢復(fù)通知
- 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']
Alertmanager 的配置文件 alertmanager.yml 主要包括以下幾個部分,每個部分的作用及參數(shù)如下。
1. global 配置
全局配置,Global塊配置下的配置選項在本配置文件內(nèi)的所有配置項下可見,但是文件內(nèi)其他位置的子配置可以覆蓋Global配置。
global:
resolve_timeout: 5m # 告警恢復(fù)后等待 5 分鐘再標(biāo)記為已解決
smtp_smarthost: 'smtp.example.com:587' # 郵件服務(wù)器地址
smtp_from: 'xxx@example.com' # 發(fā)送郵件的地址
smtp_auth_username: 'xxx@example.com' # 郵件服務(wù)器認(rèn)證用戶名
smtp_auth_password: 'yourpassword' # 郵件服務(wù)器認(rèn)證密碼
- resolve_timeout:在告警恢復(fù)后,等待多長時間才將其標(biāo)記為“已解決”。
- smtp_smarthost:用于發(fā)送郵件告警的郵件服務(wù)器地址。
- smtp_from:用于發(fā)送告警郵件的發(fā)件人地址。
- smtp_auth_username 和 smtp_auth_password:用于認(rèn)證 SMTP 服務(wù)器的用戶名和授權(quán)碼。
- smtp_require_tls:是否開啟tls認(rèn)證
2. route 配置
告警路由配置,用于告警信息的分組路由,可以將不同分組的告警發(fā)送給不同的收件人。
route:
receiver: 'default'
group_by: ['alertname', 'cluster', 'service']
group_wait: 30s
group_interval: 5m
repeat_interval: 3h
- receiver:默認(rèn)的接收器(如果沒有匹配到具體的路由規(guī)則,則使用此接收器)。
- group_by:定義告警分組的方式,確保相同服務(wù)的告警不會重復(fù)發(fā)送。
- group_wait:在發(fā)送第一個告警前等待的時間,避免瞬時波動導(dǎo)致告警風(fēng)暴。
- group_interval:在同一分組內(nèi),新增告警的發(fā)送間隔。
- repeat_interval:相同告警重復(fù)發(fā)送的時間間隔,防止過度通知。
3. routes 配置
路由子配置,優(yōu)先級高于route,配置和route一樣的:
routes:
- match:
severity: critical
receiver: 'email'
continue: true
- match:
severity: warning
receiver: 'slack'
- match:
alertname: InstanceDown
receiver: 'webhook'
- match:定義匹配規(guī)則,例如 severity: critical 表示匹配所有嚴(yán)重告警。
- receiver:匹配該規(guī)則的告警將發(fā)送到指定接收器。
- continue:如果為 true,則匹配該規(guī)則后仍會繼續(xù)匹配其他規(guī)則,默認(rèn)是找到符合的規(guī)則后就不在往下繼續(xù)匹配。
4. receivers 配置
告警接收人配置,每個receiver都有一個名字,經(jīng)過route分組并且路由后需要指定一個receiver,可以配置不同類型的接收者:
receivers:
- name: 'default'
webhook_configs:
- url: 'http://webhook.example.com/alert'
- name:定義接收器的名稱。
- webhook_configs:使用 Webhook 發(fā)送告警。
- name: 'email'
email_configs:
- to: 'admin@example.com'
send_resolved: true
- to:郵件接收者。
- send_resolved:是否發(fā)送恢復(fù)通知。
- name: 'slack'
slack_configs:
- channel: '#alerts'
api_url: 'https://hooks.slack.com/services/XXX/YYY/ZZZ'
- channel:告警發(fā)送的 Slack 頻道。
- api_url:Slack Webhook URL。
5. inhibit_rules 配置
告警抑制,主要用于減少告警的次數(shù),防止“告警轟炸:
inhibit_rules:
- source_match:
severity: 'critical'
target_match:
severity: 'warning'
equal: ['instance']
- source_match:定義高優(yōu)先級的告警。
- target_match:當(dāng) source_match 觸發(fā)時,抑制 target_match。
- equal:只有在相同 instance 觸發(fā)時才應(yīng)用抑制規(guī)則。
當(dāng)一個 severity 為 critical 的告警觸發(fā)時,所有 severity 為 warning 且 alertname 和 instance 標(biāo)簽與 critical 告警相同的告警將被抑制,不會發(fā)送通知。
完整的配置文件應(yīng)該還有個Templates配置:用于放置自定義模板的位置,這里不展開講
三、Prometheus 告警規(guī)則詳解
1. 告警規(guī)則文件
告警配置文件可以自定義名稱,在prometheus配置文件同步就行。
alert-rules.yml:
groups:
- name: 主機狀態(tài)監(jiān)控
rules:
- alert: 主機宕機
expr: up == 0
for: 1m
labels:
severity: critical
annotations:
summary: "實例 {{ $labels.instance }} 宕機"
description: "實例 {{ $labels.instance }} 已經(jīng)宕機超過 1 分鐘。請檢查服務(wù)狀態(tài)。"
- expr:告警觸發(fā)條件。
- for:持續(xù)多長時間才觸發(fā)告警。
- labels:告警標(biāo)簽,可用于匹配規(guī)則。
- annotations:告警的詳細(xì)描述。