prometheus
  • Introduction
  • (一)快速开始
    • 安装Prometheus
    • 使用NodeExporter采集数据
    • AlertManager进行告警
    • Grafana数据可视化
  • (二)探索PromQL
    • 理解时间序列
    • Metrics类型
    • 初识PromQL
    • PromQL操作符
    • PromQL内置函数
    • PromQL聚合函数
  • (三)Prometheus告警处理
    • 自定义告警规则
    • 示例 - 对主机进行监控告警
    • 部署AlertManager
    • 告警的路由与分组
    • 使用Receiver接收告警信息
      • 集成邮件系统
    • 屏蔽告警通知
    • 扩展阅读
      • AlertManager的API
      • Prometheus发送告警机制
      • 实践:接收Prometheus的告警
      • 实践:AlertManager
  • Prometheus
    • PromQL
      • 内置函数
        • avg
        • rate与irate
      • 常见指标的PromQL
        • 主机CPU
    • 配置
      • 告警规则
Powered by GitBook
On this page
  • 准备条件
  • 定义告警规则

Was this helpful?

  1. (三)Prometheus告警处理

示例 - 对主机进行监控告警

准备条件

在主机上安装好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将告警发送出去

Previous自定义告警规则Next部署AlertManager

Last updated 4 years ago

Was this helpful?