Alertmanager
在prometheus 中告警分为两部分:
- prometheus 服务根据所设置的告警规则将告警信息发送给 Alertmanager
- Alertmanager 对收到的告警信息进行处理,包括:忽略,禁止,聚合或者通过邮件等方式发送告警消息
使用告警服务主要的步骤如下:
- 下载配置 Alertmanager
- 通过设置
-alertmanager.url
让prometheus服务与Alertmanager进行通信 - 在prometheus服务中设置告警规则
安装
cd /op/monitor
wget https://github.com/prometheus/alertmanager/releases/download/\
v0.12.0/alertmanager-0.12.0.linux-amd64.tar.gz
tar xf alertmanager-0.12.0.linux-amd64.tar.gz
cd alertmanager-0.12.0.linux-amd64
# 启动alertmanager
nohup ./alertmanager --config.file=simple.yml >> /var/log/alertmanage.log 2>&1 &
# 重启 prometheus
nohup prometheus --web.enable-lifecycle --config.file=/etc/prometheus/prometheus.yml \
--storage.tsdb.path=/opt/monitor-data >> /var/log/monitor.log 2>&1 &
告警配置
####通过 Email 接收告警
本章将通过一个简单的实验介绍如何通过Email接受告警。
相关信息说明:
- prometheus版本: prometheus-2.0.darwin-amd64
- alertmanager版本: alertmanager-0.12.0.darwin-amd64
- 发送告警邮件的邮箱: qq email
- 假设该实验运行在本地机器上。prometheus默认端口为9090,Alertmanager默认端口为 9093.
step 1: 配置AlertManager下的simple.yml
其中一些关键配置如下:
global:
smtp_smarthost: 'smtp.qq.com:587'
smtp_from: 'xxx@qq.com'
smtp_auth_username: 'xxx@qq.com'
smtp_auth_password: 'your_email_password'
route:
# If an alert has successfully been sent, wait 'repeat_interval' to resend them.
repeat_interval: 10s
# A default receiver
receiver: team-X-mails
receivers:
- name: 'team-X-mails'
email_configs:
- to: 'team-X+alerts@example.org'
step 2: 在prometheus下添加 alert.rules 文件
文件中写入以下简单规则作为示例。
groups:
- name: all.yml
rules:
- alert: Network_traffic_to Hight
expr: rate(node_network_receive_bytes[30s])/1000 > 500000
for: 2m
labels:
service: system
severity: warning
annotations:
description: '工作组{{ $labels.job }} 中{{ $labels.instance }}({{ $labels.ip }})服务器流量持续两分钟超过50Mb/s,当前值{{ $value }}Kb/s'
summary: Instance {{ $labels.instance }} traffic warning
step 3: 修改prometheus.yml 文件
添加以下规则:
rule_files:
- "alert.rules"
step 4: 启动AlertManager服务
./alertmanager -config.file=simple.yml
step 5: 启动prometheus服务
./prometheus -alertmanager.url=http://localhost:9093
根据以上步骤设置,此时 “team-X+alerts@example.org” 应该就可以收到 “xxx@qq.com” 发送的告警邮件了。