示例 - 对主机进行监控告警
准备条件
在主机上安装好NodeExporter
定义告警规则
修改prometheus.yml文件,配置告警规则文件host.yml:
rule_files:
- /etc/prometheus/rule_files/host.yml
创建文件/usr/local/prometheus/rule_files/host.yml
,内容如下
groups:
- name: Host
rules:
- alert: HostCPU
expr: 100 * (1 - avg(irate(node_cpu_seconds_total{mode="idle"}[2m])) by(instance)) > 80
for: 5m
labels:
serverity: high
annotations:
summary: "{{$labels.instance}}: High CPU Usage Detected"
description: "{{$labels.instance}}: CPU usage is {{$value}}, above 80%"
- alert: HostMemory
expr: (node_memory_MemTotal_bytes - node_memory_MemAvailable_bytes) / node_memory_MemTotal_bytes * 100 > 80
for: 5m
labels:
serverity: middle
annotations:
summary: "{{$labels.instance}}: High Memory Usage Detected"
description: "{{$labels.instance}}: Memory Usage i{{ $value }}, above 80%"
- alert: HostDisk
expr: 100 * (node_filesystem_size_bytes{fstype=~"xfs|ext4"} - node_filesystem_available_bytes) / node_filesystem_size_bytes > 80
for: 5m
labels:
serverity: low
annotations:
summary: "{{$labels.instance}}: High Disk Usage Detected"
description: "{{$labels.instance}}, mountpoint {{$labels.mountpoint}}: Disk Usage is {{ $value }}, above 80%"
然后,重启Prometheus。
访问UI,查看当前告警的活动状态,如下,都为Inactive

此时,我们在主机上可以手动拉高系统的CPU使用率,验证Prometheus的告警流程,在主机上运行以下命令(可以执行命令pkill -9 dd
停止下面的进程):
$ for i in `seq 1 $(cat /proc/cpuinfo |grep "physical id" |wc -l)`; do dd if=/dev/zero of=/dev/null & done
15秒后(因为我们设置的prometheus采集周期为15s),我们就可以从UI上看到cpu使用率为100%

同时我们也可以在Alerts
页面可以看到Pending
状态中已经有了HostCPU
,

由于我们在告警规则中设置的持续时间为5分钟,所以HostCPU
的状态还不会变成Firing
。此时,如果我们执行命令pkill -9 dd
让CPU的使用率降下去,再去看一下,HostCPU
的状态又会变成了Inactive
。
五分钟后,我们再看,HostCPU
的状态就由Pending
变成了Firing

下一节,我们将介绍如果通过AlertManager将告警发送出去
Last updated
Was this helpful?