下载&启动
【下载】
https://prometheus.io/download/
【启动】
1. cd D:\Program Files\alertmanager-0.24.0.windows-amd64
2. 双击 alertmanager.exe
3.访问 http://localhost:9093
关联Prometheus
在Prometheus的架构中被划分成两个独立的部分。
Prometheus负责产生告警,而Alertmanager负责告警产生后的后续处理。
因此Alertmanager部署完成后,需要在Prometheus中设置Alertmanager相关的信息。
编辑Prometheus配置文件prometheus.yml,并添加以下内容
alerting:
alertmanagers:
- static_configs:
- targets:
- localhost:9093
定义告警规则
groups:
- name: example
rules:
- alert: HighErrorRate
expr: job:request_latency_seconds:mean5m{job="myjob"} > 0.5
for: 10m
labels:
severity: page
annotations:
summary: High request latency
description: description info
解析:
在告警规则文件中,我们可以将一组相关的规则设置定义在一个group下。
在每一个group中我们可以定义多个告警规则(rule)。
一条告警规则主要由以下几部分组成:
alert: 告警规则的名称。
expr: 基于PromQL表达式告警触发条件,用于计算是否有时间序列满足该条件。
for: 评估等待时间,可选参数。用于表示只有当触发条件持续一段时间后才发送告警。在等待期间新产生告警的状态为pending。
labels: 自定义标签,允许用户指定要附加到告警上的一组附加标签。
annotations:用于指定一组附加信息,比如用于描述告警详细信息的文字等,annotations的内容在告警产生时会一同作为参数发送到Alertmanager。
在告警规则文件的annotations中使用summary描述告警的概要信息,description用于描述告警的详细信息
接口
1.查询告警信息
curl http://localhost:9093/api/v2/alerts
2.查看静默告警信息
# 查看所有静默的告警列表
curl http://localhost:9093/api/v2/silences
# 查看某条静默的告警
curl http://localhost:9093/api/v2/silence/{silenceID}
3.添加静默
curl -XPOST http://localhost:9093/api/v2/silences \
> --header 'Content-Type: application/json' \
> --data '{"comment": "test", "status": {"state": "active"}, "endsAt": "2022-05-26T01:51:01.143796Z", "matchers": [{"isRegex": false, "name": "alertname", "value": "YellowFrameworkBuildJobCheck"}, {"isRegex": false, "name": "severity", "value": "critical"}], "createdBy": "userName", "startsAt": "2022-05-25T15:51:01.143796Z", "id": ""}'
4.删除静默
# 删除指定silenceID的告警
curl -X DELETE http://alertmanager.com:9093/api/v2/silence/{silenceID}
发送告警信息
curl -X POST 'http://localhost:9093/api/v2/alerts' -H 'Content-Type: application/json' -d '[{"labels":{"severity":"Warning","alertname":"内存使用过高","instance":"实例1","msgtype":"testing"},"annotations":{"summary":"node","description":"请检查实例1","value":"0.95"}},{"labels":{"severity":"Warning","alertname":"CPU使用过高","instance":"实例2","msgtype":"testing"},"annotations":{"summary":"node","description":"请检查实例2","value":"0.90"}}]'