[TOC]
## **准备条件**
在主机上安装好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
![](https://img.kancloud.cn/fd/60/fd60073c7b7116a38a00324658d7ca37_811x456.png)
此时,我们在主机上可以手动拉高系统的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%
![](https://img.kancloud.cn/80/64/806420733e6f911b7b8bc51850b85160_1324x661.png)
同时我们也可以在Alerts页面可以看到Pending状态中已经有了HostCPU,
![](https://img.kancloud.cn/a4/81/a4819f6e75d5a5319aa8cb6b7b5543b8_1344x668.png)
由于我们在告警规则中设置的持续时间为5分钟,所以HostCPU的状态还不会变成Firing。此时,如果我们执行命令pkill -9 dd让CPU的使用率降下去,再去看一下,HostCPU的状态又会变成了Inactive。
五分钟后,我们再看,HostCPU的状态就由Pending变成了Firing
![](https://img.kancloud.cn/08/a7/08a7c4f275ece939c334631e21708106_1321x680.png)
下一节,我们将介绍如果通过AlertManager将告警发送出去
- (一)快速开始
- 安装Prometheus
- 使用NodeExporter采集数据
- AlertManager进行告警
- Grafana数据可视化
- (二)探索PromQL
- 理解时间序列
- Metrics类型
- 初识PromQL
- PromQL操作符
- PromQL内置函数
- rate和irate
- 常见指标的PromQL
- 主机CPU使用率
- 主机内存使用率
- 主机磁盘使用率
- 主机磁盘IO
- 主机网络IO
- API的响应时间
- (三)Promtheus告警处理
- 自定义告警规则
- 示例-对主机进行监控告警
- 部署AlertManager
- 告警的路由与分组
- 使用Receiver接收告警信息
- 集成邮件系统
- 屏蔽告警通知
- 扩展阅读
- AlertManager的API
- Prometheus发送告警机制
- 实践:接收Prometheus的告警
- 实践:AlertManager
- (四)监控Kubernetes集群
- 部署Prometheus
- Kubernetes下的服务发现
- 监控Kubernetes集群
- 监控Kubelet的运行状态
- 监控Pod的资源(cadvisor)
- 监控K8s主机的资源
- KubeStateMetrics
- K8S及ETCD常见监控指标
- ETCD监控指标
- Kube-apiserver监控指标
- (五)其他
- Prometheus的relabel-config
- Target的Endpoint
- Prometheus的其他配置
- (六)BlackboxExporter
- 安装
- BlackboxExporter的应用场景
- 在Promtheus中使用BlackboxExporter
- 参考