Prometheus 整合 AlertManager的教程详解

2025-05-27 0 48

简介

alertmanager 主要用于接收 prometheus 发送的告警信息,它很容易做到告警信息的去重,降噪,分组,策略路由,是一款前卫的告警通知系统。它支持丰富的告警通知渠道,可以将告警信息转发到邮箱、企业微信、钉钉等。这一节讲解利用alertmanager,把接受到的告警信息,转发到邮箱。

实验

准备

启动 http-simulator 度量模拟器:

?

1
docker run --name http-simulator -d -p 8080:8080 pierrevincent/prom-http-simulator:0.1

启动 prometheus,为了方便更新配置,使用挂载配置文件的方式:

?

1

2
docker run --name prometheus -d -p 9090:9090 -v /users/huanchu/documents/prometheus-data:/prometheus-data \\

prom/prometheus --web.enable-lifecycle --config.file=/prometheus-data/prometheus.yml

启动添加了参数 –web.enable-lifecycle,让prometheus支持通过web端点动态更新配置。

访问http://127.0.0.1:9090/targets ,prometheus 自身的 metrics 和 http-simulator 的 metrics 处于up 状态 ,那么准备工作就做好了。

Prometheus 整合 AlertManager的教程详解

实验

实验1

告警配置

在prometheus-data文件夹下,创建告警配置文件 simulator_alert_rules.yml:

?

1

2

3

4

5

6

7

8
groups:

- name: simulator-alert-rule

rules:

- alert: httpsimulatordown

expr: sum(up{job="http-simulator"}) == 0

for: 1m

labels:

severity: critical

配置文件的意思是 http-simulator 服务up状态为 0 ,并且持续1分钟时,产生告警 ,级别为 “严重的”。

修改prometheus.yml,引用simulator_alert_rules.yml文件,prometheus.yml 内容如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14
global:

scrape_interval: 5s

evaluation_interval: 5s

scrape_timeout: 5s

rule_files:

- "simulator_alert_rules.yml"

scrape_configs:

- job_name: 'prometheus'

static_configs:

- targets: ['localhost:9090']

- job_name: 'http-simulator'

metrics_path: /metrics

static_configs:

- targets: ['192.168.43.121:8080']

更新prometheus配置:

?

1
curl -x post http://localhost:9090/-/reload

访问http://127.0.0.1:9090/config,可以看到已经为更新了配置:

Prometheus 整合 AlertManager的教程详解

访问http://127.0.0.1:9090/rules,rules 下出现了新添加的告警规则:

Prometheus 整合 AlertManager的教程详解

验证

访问http://127.0.0.1:9090/alerts ,alerts 下 httpsimulatordown 为绿色,处于inactive 状态,表示什么都没有发生。

Prometheus 整合 AlertManager的教程详解

关闭 http-simulator 服务:

?

1
docker stop http-simulator

访问http://127.0.0.1:9090/alerts,httpsimulatordown 变成黄色,处于 pending 状态,表示报警即将被激活。

Prometheus 整合 AlertManager的教程详解

一分钟后,httpsimulatordown 变成红色,处于 firing 状态,表示报警已经被激活了。

Prometheus 整合 AlertManager的教程详解

实验2

告警配置

在simulator_alert_rules.yml文件中增加告警配置:

?

1

2

3

4

5

6

7

8
- alert: errorratehigh

expr: sum(rate(http_requests_total{job="http-simulator", status="500"}[5m])) / sum(rate(http_requests_total{job="http-simulator"}[5m])) > 0.02

for: 1m

labels:

severity: major

annotations:

summary: "high error rate detected"

description: "error rate is above 2% (current value is: {{ $value }}"

配置文件的意思是 http-simulator 请求的错误率对2% ,并且持续1分钟时,产生告警 ,级别为 “非常严重的”

更新prometheus配置:

?

1
curl -x post http://localhost:9090/-/reload

验证

访问http://127.0.0.1:9090/alerts,errorratehigh 为绿色的 inactive 状态。

Prometheus 整合 AlertManager的教程详解

把 http-simulator 的错误率调到 10%

?

1
curl -h 'content-type: application/json' -x put -d '{"error_rate": 10}' http://localhost:8080/error_rate

稍等一会后,访问http://127.0.0.1:9090/alerts, 可以看到错误率已经大2%,errorratehigh 为红色的 firing 状态,报警已经被激活了。

Prometheus 整合 AlertManager的教程详解

安装和配置alertmanager

通过docker 挂载文件的方式安装alertmanager,在本地创建文件夹 alertmanager-data 文件夹,在其中创建 alertmanager.yml,内容如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14
global:

smtp_smarthost: 'smtp.163.com:25'

smtp_from: 'xxxxx@163.com'

smtp_auth_username: 'xxxxx@163.com'

smtp_auth_password: 'xxxxx'

route:

group_interval: 1m #当第一个报警发送后,等待'group_interval'时间来发送新的一组报警信息

repeat_interval: 1m # 如果一个报警信息已经发送成功了,等待'repeat_interval'时间来重新发送他们

receiver: 'mail-receiver'

receivers:

- name: 'mail-receiver'

email_configs:

- to: 'xxxxxx@163.com'

启动 alertmanager:

?

1

2
docker run --name alertmanager -d -p 9093:9093 -v /users/huanchu/documents/alertmanager-data:/alertmanager-data \\

prom/alertmanager --config.file=/alertmanager-data/alertmanager.yml

在prometheus目录下,修改prometheus.yml配置alertmanager地址:

?

1

2

3

4

5

6
# alertmanager configuration

alerting:

alertmanagers:

- static_configs:

- targets:

- 192.168.43.121:9093

更新prometheus配置:

?

1
curl -x post http://localhost:9090/-/reload

访问http://127.0.0.1:9093,访问alertmanager ui界面,可以看到接收到errorratehigh告警:

Prometheus 整合 AlertManager的教程详解

邮箱会收到告警信息:

Prometheus 整合 AlertManager的教程详解

总结

以上所述是小编给大家介绍的prometheus 整合 alertmanager的教程详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对快网idc网站的支持!

如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

原文链接:https://www.cnblogs.com/huanchupkblog/p/11267477.htm

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 行业资讯 Prometheus 整合 AlertManager的教程详解 https://www.kuaiidc.com/66285.html

相关文章

发表评论
暂无评论