整体架构图
1.Prometheus配置
要使得Prometheus可以连接到alertmanager
alerting:
alertmanagers:
- static_configs:
- targets: ['alertmanager:9093']
2.alertmanager存储说明
alertmanager本身只接收由Prometheus发过来Prometheus rule并分组,它不会去存储告警数据和zabbix不同。
它主要在内存中处理告警,分组,路由,静默等,所以它本身不需要做持久化(除了一些模板相关)
3.配置
默认的容器配置是使用的webhooks,这里的webhooks可以是很多不同实现,如接入钉钉,企业微信,slack等,这里先接入最简单的SMTP,做一个简单的测试
global:
resolve_timeout: 5m
smtp_from: '12785024@qq.com' #发送人的邮箱,自定义
smtp_smarthost: 'smtp.qq.com:465'
smtp_auth_username: '12785024@qq.com' #你自己的邮箱
smtp_auth_password: 'xxx' #这个是上面获取安全码的值
smtp_require_tls: false
smtp_hello: 'qq.com'
route:
group_by: ['alertname']
group_wait: 30s
group_interval: 5m
repeat_interval: 1h
receiver: 'email'
receivers: #定义谁接收告警,(接收路由的告警)
- name: 'email' #名称对应route中的receiver参数
email_configs: #采用邮箱机制
- to: '12785024@qq.com'
send_resolved: true #是否发送状态恢复的邮件
inhibit_rules:
- source_match:
severity: 'critical'
target_match:
severity: 'warning'
equal: ['alertname', 'dev', 'instance']
alertmanager启动后续还会自动进行一些部分进行一个标准化的填充,如补充默认模板
global:
resolve_timeout: 5m
http_config:
follow_redirects: true
enable_http2: true
smtp_from: 12785024@qq.com
smtp_hello: qq.com
smtp_smarthost: smtp.qq.com:465
smtp_auth_username: 12785024@qq.com
smtp_auth_password: <secret>
smtp_require_tls: false
pagerduty_url: https://events.pagerduty.com/v2/enqueue
opsgenie_api_url: https://api.opsgenie.com/
wechat_api_url: https://qyapi.weixin.qq.com/cgi-bin/
victorops_api_url: https://alert.victorops.com/integrations/generic/20131114/alert/
telegram_api_url: https://api.telegram.org
webex_api_url: https://webexapis.com/v1/messages
route:
receiver: email
group_by:
- alertname
continue: false
group_wait: 30s
group_interval: 5m
repeat_interval: 1h
inhibit_rules:
- source_match:
severity: critical
target_match:
severity: warning
equal:
- alertname
- dev
- instance
receivers:
- name: email
email_configs:
- send_resolved: true
to: 12785024@qq.com
from: 12785024@qq.com
hello: qq.com
smarthost: smtp.qq.com:465
auth_username: 12785024@qq.com
auth_password: <secret>
headers:
From: 12785024@qq.com
Subject: '{{ template "email.default.subject" . }}'
To: 12785024@qq.com
html: '{{ template "email.default.html" . }}'
require_tls: false
templates: []
笔者采用了个人的qq邮箱进行测试,一般的邮箱如果想开启SMTP支持,是需要到邮箱的设置里进行开启和获得许可密钥的,和邮箱直接使用密码登录不同
以笔者使用的QQ邮箱为例,简单展示一下配置
登录到邮箱之后找到设置
根据提示开启SMTP,POP3等拓展后获得一个授权码,将填在上面alertmanager的xxx中
效果
在Prometheus rule触发后,alertmanager收到告警
然后邮箱收到告警的短信
但由于没有专门设置模板,可能比较丑
4.后续
自此笔者调通了最简单的一个示例,后续将对下面大模型提到的功能做更多的探索,读者也可以到官网进行查看
Alerting overview | Prometheus
- 警报汇聚(Silence & Inhibition):
- Alertmanager能够对重复或相关的警报进行汇聚处理,避免发送过多的相同警报通知。
- 支持定义静默规则(Silences),在特定时间段内对指定的警报进行临时抑制,不发送通知。
- 路由与分发(Routing & Grouping):
- 提供灵活的路由配置,可以根据标签将警报路由到不同的接收器(receiver),如邮件、PagerDuty、Slack、Webhook等。
- 对接收到的警报进行分组,合并成一个通知以减少噪音,并且支持按严重程度、环境或其他标签属性分组。
- 去重(Duplication):
- 自动识别并去除重复的警报,在一定时间内只发送一次相同的警报通知。
- 通知延迟与抑制策略:
- 可以设置通知间隔和阈值,比如第一次触发警报立即通知,后续类似警报则在一定时间间隔后再次通知。
- 实现告警抑制链,当较高优先级的警报处于活动状态时,可以抑制较低优先级的相关警报。
- 高可用性:
- Alertmanager可以集群部署以实现高可用性和负载均衡,确保警报管理服务的稳定性。