配置报警规则
Alertmanager部署
- 下载二进制包
https://prometheus.io/download/
安装alertmanager
- 安装步骤
[root@prometheus ~]# tar xf alertmanager-0.20.0-rc.0.linux-amd64.tar.gz -C /usr/local/
[root@prometheus ~]# ln -sv /usr/local/alertmanager-0.20.0-rc.0.linux-amd64/ /usr/local/Prometheus_alertmanager/
[root@prometheus ~]# cd /usr/local/Prometheus_alertmanager/
配置alertmanager邮件告警
- 修改alertmanager配置文件
[root@prometheus Prometheus_alertmanager]# cp alertmanager.yml alertmanager.yml.bak
[root@prometheus Prometheus_alertmanager]# vim alertmanager.yml
global:
resolve_timeout: 5m
smtp_smarthost: 'smtp.163.com:25' # smtp地址
smtp_from: 'xxx@163.com' # 谁发邮件
smtp_auth_username: 'xxx@163.com' # 邮箱用户
smtp_auth_password: 'xxxxx' # 邮箱客户端授权密码
smtp_require_tls: false
route: # route用来设置报警的分发策略
group_by: ["alertname"] # 分组名
group_wait: 30s # 当收到告警的时候,等待三十秒看是否还有告警,如果有就一起发出去
group_interval: 30s # 发送警告间隔时间
repeat_interval: 20m # 重复报警的间隔时间
receiver: Node_warning # 设置默认接收人,如果想分组接收,把下面这段的注释去掉
# routes: # 可以指定哪些组接收哪些消息
# - receiver: 'Node_warning'
# continue: true
# group_wait: 10s
# match_re:
# service: mysql|cassandra # 所有service=mysql或者service=cassandra的告警分配到数据库接收端
# - receiver: 'MySQL_warning'
# group_wait: 10s
# match_re: # 根据标签分组,匹配标签dest=szjf的为fping-receiver组
# serverity: warning
receivers: # 定义接收者,将告警发送给谁
- name: 'Node_warning'
email_configs:
- to: 'xxx@126.com'
#- name: 'MySQL_warning'
# email_configs:
# - to: 'xxx@qq.com'
测试
- 检查配置
[root@prometheus Prometheus_alertmanager]# ./amtool check-config alertmanager.yml
Checking 'alertmanager.yml' SUCCESS
Found:
- global config
- route
- 0 inhibit rules
- 1 receivers
- 0 templates
测试成功
- 配置systemd启动alertmanager
[root@prometheus ~]# vim /lib/systemd/system/alertmanager.service
[Unit]
Description=Alertmanager
After=network.target
[Service]
ExecStart=/usr/local/Prometheus_alertmanager/alertmanager --config.file='/usr/local/Prometheus_alertmanager/alertmanager.yml'
[Install]
WantedBy=multi-user.target
# 重载配置并设置开机自启
[root@prometheus ~]# systemctl daemon-reload
[root@prometheus ~]# systemctl start alertmanager
[root@prometheus ~]# systemctl enable alertmanager
在web中使用url:http://alertmanager_ip:9093,会显示alertmanager的界面
建立通信
- 修改prometheus配置文件,添加alertmanager通讯地址
[root@prometheus ~]# cd /usr/local/Prometheus
[root@prometheus Prometheus]# vim prometheus.yml
# 建立通信
alerting:
alertmanagers:
- static_configs:
- targets:
- 127.0.0.1:9093
# 规则读取路径(可配置多个)
rule_files:
- "rules/node_rules.yml"
# - "rules/mysql_rules.yml"
- 配置告警规则
[root@prometheus Prometheus]# mkdir rules
[root@prometheus Prometheus]# vim rules/node_rules.yml
groups:
- name: test
rules:
- alert: 内存使用率过高
expr: 100-(node_memory_Buffers_bytes+node_memory_Cached_bytes+node_memory_MemFree_bytes)/node_memory_MemTotal_bytes*100 > 90
for: 30s # 告警持续时间,超过这个时间才会发送给alertmanager
labels:
severity: warning
annotations:
summary: "Instance {{ $labels.instance }} 内存使用率过高"
description: "{{ $labels.instance }} of job {{$labels.job}}内存使用率超过80%,当前使用率[{{ $value }}]."
- alert: cpu使用率过高
expr: 100-avg(irate(node_cpu_seconds_total{mode="idle"}[5m])) by(instance)*100 > 90
for: 30s
labels:
severity: warning
annotations:
summary: "Instance {{ $labels.instance }} cpu使用率过高"
description: "{{ $labels.instance }} of job {{$labels.job}}cpu使用率超过80%,当前使用率[{{ $value }}]."
测试规则
- 检查告警规则,重载prometheus
[root@prometheus Prometheus]# curl -XPOST http://localhost:9090/-/reload
- 在prometheus界面的alert可以看到告警状态
- 绿色表示正常。
- 红色状态为PENDING表示alerts还没有发送至Alertmanager,因为rules里面配置了for: 30s。
- 30s后状态由PENDING变为FIRING,此时,prometheus才将告警发给alertmanager,在Alertmanager中可以看到有一个alert。