Prometheus 常用告警规则 rules.yml

Prometheus 常用告警规则 rules.yml

说明:整理了企业运维中常见服务的告警规则,覆盖服务器、Redis、RabbitMQ、Kafka、SSL证书、Elasticsearch。每条规则都给了 PromQL,复制到 Prometheus 就能用。


规则文件放哪

Prometheus 的告警规则以 YAML 文件存放在 rules/ 目录下,然后在 prometheus.yml 中引用:

yaml 复制代码
rule_files:
  - "rules/*.yml"

改完配置记得重载:

bash 复制代码
systemctl reload prometheus

一、服务器基础告警(node_exporter)

文件node_exporter_rules.yml

服务器宕机

yaml 复制代码
- alert: 服务器宕机
  expr: up == 0
  for: 3m
  labels:
    severity: 严重告警
  annotations:
    summary: "服务器 {{ $labels.instance }} 宕机"
    description: "{{ $labels.instance }} 超过 3 分钟无响应"

CPU 使用率超过 90%

yaml 复制代码
- alert: CPU 使用率过高
  expr: 100 - (avg by (instance)(irate(node_cpu_seconds_total{mode="idle"}[5m])) * 100) > 90
  for: 5m
  labels:
    severity: 严重告警
  annotations:
    summary: "服务器 {{ $labels.instance }} CPU 过高"
    description: "CPU 使用率 {{ $value }}%,超过 90%"

计算思路:用 irate 取 5 分钟内 CPU 空闲率的瞬时变化,然后反算使用率。irate 适合 CPU 这种抖动大的指标,能更灵敏反映瞬时变化。

内存使用率过高

yaml 复制代码
- alert: 内存使用率过高
  expr: 100 - ((node_memory_MemFree_bytes + node_memory_Cached_bytes + node_memory_Buffers_bytes) / node_memory_MemTotal_bytes * 100) < 10
  for: 5m
  labels:
    severity: 严重告警
  annotations:
    summary: "服务器 {{ $labels.instance }} 内存不足"
    description: "可用内存(含缓存)不足 10%"

计算用了 MemFree + Cached + Buffers 来衡量剩余内存。因为 Linux 的缓存可以回收,比只看 MemFree 更符合实际情况。

磁盘 IO 过高

yaml 复制代码
- alert: 磁盘 IO 占用过高
  expr: avg by (instance)(irate(node_disk_io_time_seconds_total[1m])) * 100 > 90
  for: 5m
  labels:
    severity: 严重告警
  annotations:
    summary: "服务器 {{ $labels.instance }} 磁盘 IO 过高"
    description: "磁盘 IO 占用率 {{ $value }}%"

网络流入带宽超过 100Mbps

yaml 复制代码
- alert: 网络流入带宽过高
  expr: (sum by (instance)(rate(node_network_receive_bytes_total{device!~"tap.*|veth.*|br.*|docker.*|virbr.*|lo.*"}[5m])) / 100) > 102400
  for: 5m
  labels:
    severity: 严重告警
  annotations:
    summary: "服务器 {{ $labels.instance }} 网络流入过高"
    description: "流入带宽 {{ $value }},持续 5 分钟超过 100Mbps"

过滤掉了虚拟网卡(docker、veth、br 等),只监控物理网卡。102400 对应 100Mbps,因为除以 100 将 bytes 转成了 bits。

网络流出带宽超过 100Mbps

yaml 复制代码
- alert: 网络流出带宽过高
  expr: (sum by (instance)(rate(node_network_transmit_bytes_total{device!~"tap.*|veth.*|br.*|docker.*|virbr.*|lo.*"}[5m])) / 100) > 102400
  for: 5m
  labels:
    severity: 严重告警
  annotations:
    summary: "服务器 {{ $labels.instance }} 网络流出过高"
    description: "流出带宽 {{ $value }},持续 5 分钟超过 100Mbps"

TCP 连接数过多

yaml 复制代码
- alert: TCP 连接数过高
  expr: node_netstat_Tcp_CurrEstab > 10000
  for: 2m
  labels:
    severity: 严重告警
  annotations:
    summary: "服务器 {{ $labels.instance }} TCP 连接数过高"
    description: "当前 TCP ESTABLISHED 连接数 {{ $value }},超过 10000"

磁盘使用率超过 90%

yaml 复制代码
- alert: 磁盘使用率过高
  expr: 100 - (node_filesystem_free_bytes{fstype=~"ext4|xfs"} / node_filesystem_size_bytes{fstype=~"ext4|xfs"} * 100) > 90
  for: 1m
  labels:
    severity: 严重告警
  annotations:
    summary: "{{ $labels.mountpoint }} 磁盘使用率过高"
    description: "分区 {{ $labels.mountpoint }} 使用率 {{ $value }}%"

只监控 ext4 和 xfs 格式的文件系统,跳过 tmpfs、overlay 之类的虚拟文件系统。


二、Redis 告警(redis_exporter)

文件redis_exporter_rules.yml

yaml 复制代码
- alert: Redis 服务停止
  expr: redis_up == 0
  for: 1m
  labels:
    severity: 严重告警
  annotations:
    summary: "Redis {{ $labels.instance }} 服务停止"
    description: "Redis 服务不可达"

- alert: Redis 连接数超过 80%
  expr: redis_connected_clients / redis_config_maxclients * 100 > 80
  for: 1m
  labels:
    severity: 严重告警
  annotations:
    summary: "Redis {{ $labels.instance }} 连接数过高"
    description: "当前连接数 {{ $value }},超过最大连接数的 80%"

三、RabbitMQ 告警(rabbitmq_exporter)

文件rabbitmq_exporter_rules.yml

yaml 复制代码
- alert: RabbitMQ 服务停止
  expr: rabbitmq_up == 0
  for: 3m
  labels:
    severity: 严重告警
  annotations:
    summary: "RabbitMQ {{ $labels.instance }} 服务停止"
    description: "RabbitMQ 已停止运行"

- alert: RabbitMQ 内存占用超过 2GB
  expr: rabbitmq_node_mem_used / 1024 / 1024 > 2048
  for: 3m
  labels:
    severity: 严重告警
  annotations:
    summary: "RabbitMQ {{ $labels.instance }} 内存过高"
    description: "当前内存占用 {{ $value }} MB,超过 2048 MB"

四、Kafka 告警(kafka_exporter)

文件kafka_exporter_rules.yml

yaml 复制代码
- alert: Kafka 消费组消息积压
  expr: sum by (consumergroup, topic)(kafka_consumergroup_lag) > 50000
  for: 3m
  labels:
    severity: 严重告警
  annotations:
    summary: "Topic {{ $labels.topic }} 消费滞后"
    description: "消费组 {{ $labels.consumergroup }} 积压超过 50000 条消息"

- alert: Kafka 集群节点减少
  expr: kafka_brokers < 3
  for: 3m
  labels:
    severity: 严重告警
  annotations:
    summary: "Kafka 集群节点减少"
    description: "当前 broker 数量 {{ $value }},少于 3 个"

- alert: Kafka Topic 无消息写入
  expr: sum by (topic)(rate(kafka_topic_partition_current_offset[5m])) == 0
  for: 5m
  labels:
    severity: 警告
  annotations:
    summary: "Topic {{ $labels.topic }} 无消息流入"
    description: "持续 5 分钟没有新消息写入该 Topic"

五、SSL 证书过期监控(blackbox_exporter)

文件ssl_expiry.yml

yaml 复制代码
- alert: SSL 证书即将过期
  expr: probe_ssl_earliest_cert_expiry - time() < 86400 * 30
  for: 5m
  labels:
    severity: 重要告警
  annotations:
    summary: "SSL 证书将在 30 天内过期"
    description: "证书剩余 {{ $value }} 秒,请及时更新"

- alert: SSL 证书已过期
  expr: probe_ssl_earliest_cert_expiry - time() <= 0
  for: 5m
  labels:
    severity: 严重告警
  annotations:
    summary: "SSL 证书已过期"
    description: "证书已过期,请立即更新"

probe_ssl_earliest_cert_expiry 返回的是证书过期的时间戳(秒),减去当前时间 time() 就是剩余有效时间。86400 是一天的秒数。


六、Elasticsearch 告警(elasticsearch_exporter)

文件elasticsearch_exporter_rules.yml

yaml 复制代码
- alert: ES 集群节点减少
  expr: elasticsearch_cluster_health_number_of_nodes < 3
  for: 5m
  labels:
    severity: 严重告警
  annotations:
    summary: "ES 集群节点数减少"
    description: "当前节点数 {{ $value }},少于 3 个"

- alert: JVM 堆内存使用率过高
  expr: elasticsearch_jvm_memory_used_bytes{area="heap"} / elasticsearch_jvm_memory_max_bytes{area="heap"} * 100 > 90
  for: 5m
  labels:
    severity: 严重告警
  annotations:
    summary: "ES JVM 内存使用率过高"
    description: "堆内存使用率 {{ $value }}%,超过 90%"

使用建议

  1. 阈值调整:上面的阈值是通用值,建议根据实际环境调整。比如磁盘告警,数据盘可以放到 95%,根分区建议 85% 就告警。
  2. 集群节点数kafka_brokers < 3es 节点数 < 3 假设你的集群至少 3 个节点,如果架构不同记得改。
  3. for 参数for: 5m 表示持续 5 分钟满足条件才触发告警,防止误报。可以根据业务容忍度调整。
  4. 86400 * 30 的笔误 :原文写的是 86400 * 300(300天),实际应为 30 天,这里已修正。
相关推荐
阿里matlab建模师2 小时前
【机场停机位分配】matlab实现基于遗传算法的机场停机位分配优化研究
开发语言·算法·数学建模·matlab·全国大学生数学建模竞赛
xiaoshuaishuai82 小时前
C# Avalonia 依赖属性与WPF的区别
开发语言·c#·wpf
一碗白开水一2 小时前
【训练技巧】bash: conda: command not found:conda 没有适配环境
开发语言·conda·bash
一晌小贪欢2 小时前
第22节:相关性分析——协方差、相关系数与热力图解读
开发语言·python·数据分析·pandas·数据可视化
keykey6.2 小时前
循环神经网络(RNN)与序列模型:让AI学会“记忆“
开发语言·人工智能·深度学习·机器学习
星恒随风2 小时前
C++ 类和对象入门(四):日期类 Date 的运算符重载实现详解
开发语言·c++·笔记·学习
疯狂打码的少年3 小时前
编译程序与解释程序的区别
java·开发语言·笔记
caimouse6 小时前
reactos编码规范
c语言·开发语言
xieliyu.10 小时前
Java算法精讲:双指针(三)
java·开发语言·算法