1.将Prometheus监控主机与被监控项都配置成功,须无任何问题
2.将Alertmanager软件包准备好并放入监控主机下
3.将Alertmanager软件包解压并放入Linux软件目录(/usr/local)
# 1. 解压
[root@prometheus ~]# cd prometheus_soft/
[root@prometheus prometheus_soft]# tar xf alertmanager-0.25.0.linux-amd64.tar.gz [root@prometheus prometheus_soft]# mv alertmanager-0.25.0.linux-amd64 /usr/local/alertmanager
4.编写Alertmanager服务service配置文件
# 2. 编写服务文件并启动
[root@prometheus prometheus_soft]# vim /usr/lib/systemd/system/alertmanager.service
[Unit]
Description=alertmanager System
[Service]
ExecStart=/usr/local/alertmanager/alertmanager \
--config.file=/usr/local/alertmanager/alertmanager.yml
[Install]
WantedBy=multi-user.target
5.使用systemctl重新加载命令重新加载service文件并启动服务
[root@prometheus ~]# systemctl daemon-reload
[root@prometheus ~]# systemctl enable alertmanager.service --now
6.通过使用监控主机IP:9093访问Alertmanager-Ul页面检测是否配置成功
访问http://192.168.88.5:9093可以访问web配置页面
7.通过修改Prometheus配置文件将Prometheus与Alertmanager进行连接
编辑Prometheus配置文件,修改alerting配置
[root@prometheus ~]# vim /usr/local/prometheus/prometheus.yml
...略...
alerting:
alertmanagers:
- static_configs:
- targets:
- localhost:9093
...略...
[root@prometheus ~]# systemctl restart prometheus.service
8.通过使用监控主机IP:9093访问Configuration查看配置是否生效
访问:http://192.168.88.5:9090/config,查看配置是否生效
9.修改Alertmanager配置文件,添加邮件所需项目
# 1. 备份配置文件
[root@prometheus ~]# cp /usr/local/alertmanager/alertmanager.yml ~
# 2. 修改配置文件
[root@prometheus ~]# vim /usr/local/alertmanager/alertmanager.yml
global:
smtp_from: 'zzg@tedu.cn' # 发件人地址
smtp_smarthost: 'localhost:25' # 邮件服务器地址
smtp_require_tls: false # 是否使用TLS安全连接
route:
group_by: ['alertname']
group_wait: 30s
group_interval: 5m
repeat_interval: 1h
receiver: 'default-receiver' # 接收器
receivers:
- name: 'default-receiver' # 配置接收器为邮件
email_configs:
- to: 'root@localhost.localdomain'
inhibit_rules:
- source_match:
severity: 'critical'
target_match:
severity: 'warning'
equal: ['alertname', 'dev', 'instance']
10.创建Alertmanager邮箱告警规则目录与文件,并在Prometheus声明告警规则文件路径
定义告警规则
[root@prometheus ~]# mkdir /usr/local/prometheus/rules
[root@prometheus ~]# vim /usr/local/prometheus/rules/hoststats-alert.rules
groups:
- name: example
rules:
- alert: InstanceDown
expr: up == 0
for: 5m
labels:
severity: warn
annotations:
summary: "Instance {{ $labels.instance }} down"
description: "{{ $labels.instance }} of job {{ $labels.job }} has been down for more than 5 minutes."
- alert: hostMemUsageAlert
expr: (node_memory_MemTotal -
node_memory_MemAvailable)/node_memory_MemTotal > 0.85
for: 1m
labels:
severity: warn
annotations:
summary: "Instance {{ $labels.instance }} MEM usgae high"
在Prometheus中声明规则文件位置
[root@prometheus ~]# vim /usr/local/prometheus/prometheus.yml
...略...
15 rule_files:
16 - /usr/local/prometheus/rules/*.rules
...略...
11.重启Prometheus与Alertmanager服务
systemctl restart alertmanager.service
systemctl restart prometheus.service
12.在监控主机上安装邮件服务器软件包,并启动邮件服务
yum install -y postfix mailx
systemctl enable postfix --now