我们在文章 《使用 docker-compose 搭建 prometheus 监控系统》 的基础上,增加 prometheus 的告警功能。
Prometheus 指标的收集存储与告警是分开的,告警功能由 alertmanager 提供。我们需要在 prometheus 定义告警规则,这些规则可以触发事件,然后传播到 alertmanager。接下来,alertmanager 会决定如何处理相应的警报,并确定使用电子邮件,短信等发出警报。Prometheus 和 alertmanager 的关系如下图所示。
为了在 prometheus 增加 alertmanager 的使用,我们在 docker-compose.yml
增加 alertmanager 容器:
version: '3'
services:
centos1:
image: centos
container_name: centos1
restart: always
ports:
- "9101:9100"
volumes:
- ~/code/docker/prometheus/node_exporter:/root
command: /root/node_exporter
centos2:
image: centos
container_name: centos2
restart: always
ports:
- "9102:9100"
volumes:
- ~/code/docker/prometheus/node_exporter:/root
command: /root/node_exporter
prometheus:
image: prom/prometheus
container_name: prometheus
restart: always
ports:
- "9090:9090"
volumes:
- ~/code/docker/prometheus/prometheus:/etc/prometheus
- ~/code/docker/prometheus/prometheus_data:/prometheus
command:
- '--config.file=/etc/prometheus/prometheus.yml'
- '--storage.tsdb.path=/prometheus'
grafana:
image: grafana/grafana
container_name: grafana
restart: always
ports:
- "3000:3000"
volumes:
- ~/code/docker/prometheus/grafana_data:/var/lib/grafana
alertmanager:
image: prom/alertmanager
container_name: alertmanager
restart: always
ports:
- "9093:9093"
volumes:
- ~/code/docker/prometheus/alertmanager/alertmanager.yml:/etc/alertmanager/alertmanager.yml
command:
- '--config.file=/etc/alertmanager/alertmanager.yml'
在 docker-compose.yml
文件中,我们指定 alertmanager 的配置文件是 /etc/alertmanager/alertmanager.yml
,有关 alertmanager.yml
的配置,我们下面再说明。
我们先来看 prometheus 的配置 prometheus.yml
:
global:
scrape_interval: 5s
rule_files:
- "alert.rules"
alerting:
alertmanagers:
- static_configs:
- targets:
- 'alertmanager:9093'
scrape_configs:
- job_name: 'prometheus'
static_configs:
- targets: ['prometheus:9090']
- job_name: 'linux-exporter'
metrics_path: /metrics
static_configs:
- targets: ['centos1:9100', 'centos2:9100']
alerting
用于指定 alertmanager 的信息,alertmanager 启动端口为 9093,alertmanager 容器启动信息可以在 docker-compose.yml
文件中查看到。
rule_files
用于指定告警规则,这里我们指定 alert.rules
文件来用保存告警规则。
alert.rules
文件内容如下:
groups:
- name: example
rules:
# Alert for any instance that is unreachable for > 1 minutes.
- alert: InstanceDown
expr: up == 0
for: 1m
labels:
severity: page
annotations:
summary: "实例 {{ $labels.instance }} 宕机"
description: "{{ $labels.instance }} 任务 {{ $labels.job }} 已宕机 1 分钟"
这个告警的规则是指当实例宕机超过 1 分钟时触发告警。
接下来我们来看 alertmanager 的配置文件 alertmanager.yml
的内容:
global:
resolve_timeout: 2m
smtp_smarthost: 'smtpdm.aliyun.com:465'
smtp_from: 'xxx@xxx.leehao.me'
smtp_auth_username: 'xxx@xxx.leehao.me'
smtp_auth_password: 'xxxxxx' # 邮箱授权码,注意不是邮箱密码
smtp_require_tls: false
route:
group_by: ['alertname']
group_wait: 10s
group_interval: 10s
repeat_interval: 4m # 发送重复警报的周期
receiver: 'mail' # 发送警报的接收者的名称,以下 receivers name 的名称
receivers:
- name: 'mail' # 警报接收者,与上面的配置相对应
email_configs:
- to: 'xxxx@163.com' # 接收警报的email
headers: { Subject: "[WARN] alertmanager 报警邮件"}
其中,有关阿里云 SMTP 的设置需要先在阿里云邮件推送控制台提前配置好。
在阿里云邮件推送的控制台,根据文档提示,创建发信域名,创建发信地址,并设置 SMTP 授权码:
启动 docker-compose:
docker-compose up
在另一个终端关闭 centos1 容器:
docker-compose stop centos1
等待一段时间,打开 prometheus 的 alerts 页面(http://127.0.0.1:9090/alerts),可以看到告警提示:
再打开 alertmanager 页面(http://127.0.0.1:9093/#/alerts),可以看到 alertmanage 已正常接收到 prometheus 的告警:
登录接收告警邮件的邮箱,可以看到 alertmanager 发送的警报邮件:
参考资料
- https://github.com/prometheus/alertmanager
- Prometheus 监控实战,James Turnbull 著,史天等译,机械工业出版社
- https://blog.csdn.net/wshl1234567/article/details/100107167
- https://github.com/danguita/prometheus-monitoring-stack/blob/master/config/alertmanager.template.yml
- https://blog.csdn.net/aixiaoyang168/article/details/98474494
- https://blog.csdn.net/lihao21/article/details/104349219
- https://prometheus.io/docs/alerting/alertmanager/