章工运维 章工运维
首页
  • linux
  • windows
  • 中间件
  • 监控
  • 网络
  • 存储
  • 安全
  • 防火墙
  • 数据库
  • 系统
  • docker
  • 运维工具
  • other
  • elk
  • K8S
  • ansible
  • Jenkins
  • GitLabCI_CD
  • 随笔
  • 面试
  • 工具
  • 收藏夹
  • Shell
  • python
  • golang
友链
  • 索引

    • 分类
    • 标签
    • 归档
    • 首页 (opens new window)
    • 关于我 (opens new window)
    • 图床 (opens new window)
    • 评论 (opens new window)
    • 导航栏 (opens new window)
周刊
GitHub (opens new window)

章工运维

业精于勤,荒于嬉
首页
  • linux
  • windows
  • 中间件
  • 监控
  • 网络
  • 存储
  • 安全
  • 防火墙
  • 数据库
  • 系统
  • docker
  • 运维工具
  • other
  • elk
  • K8S
  • ansible
  • Jenkins
  • GitLabCI_CD
  • 随笔
  • 面试
  • 工具
  • 收藏夹
  • Shell
  • python
  • golang
友链
  • 索引

    • 分类
    • 标签
    • 归档
    • 首页 (opens new window)
    • 关于我 (opens new window)
    • 图床 (opens new window)
    • 评论 (opens new window)
    • 导航栏 (opens new window)
周刊
GitHub (opens new window)
  • linux

  • windows

  • 中间件

  • 网络

  • 安全

  • 存储

  • 防火墙

  • 数据库

  • 系统

  • docker

  • other

  • 监控

    • zabbix

    • prometheus

      • prometheus监控、告警与存储
      • 使用docker-compose搭建promethes+grafana监控系统
      • prometheus添加钉钉消息告警
      • alertmanager实现某个时间段静默某些告警项
      • prometheus设置表达式触发告警
      • 监控MySQL运行状态:MySQLD Exporter
      • alertmanager设置定时静默告警脚本
      • 构建高大上的黑盒监控平台
      • prometheus使用一个redis_exporter监控所有redis实例
      • alertmanager-webhook与API
  • 运维
  • 监控
  • prometheus
章工运维
2023-04-15

prometheus设置表达式触发告警

Prometheus 是一个开源监控系统,通过设置警报规则来实现对指标变化的监控。要在 Prometheus 中设置一个表达式值变动 50% 触发告警,你需要创建一个 alert 规则。

假设你要监控的指标是 example_metric,可以使用以下方法创建告警规则:

  1. 首先,在 Prometheus 的配置文件(通常是 prometheus.yml)中,为 alert 规则创建一个规则文件的路径。例如:
rule_files:
  - "alert_rules.yml"

1
2
3
  1. 接下来,在 alert_rules.yml 文件中定义告警规则。这里我们需要用到 abs 和 rate 函数,以便计算指标值的变动百分比:
groups:
- name: example_alerts
  rules:
  - alert: ExampleMetricChange
    expr: |
      abs(
        (rate(example_metric[5m]) - rate(example_metric[10m])) / rate(example_metric[10m])
      ) > 0.5
    for: 5m
    labels:
      severity: critical
    annotations:
      summary: "Example metric changed more than 50% in the last 5 minutes"
      description: "Example metric has changed by more than 50% in the last 5 minutes (current value: {{ $value }})."

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

在这个示例中,我们使用了 rate 函数来计算 example_metric 在 5 分钟和 10 分钟滚动窗口内的变化速率。然后我们计算了这两个速率之间的差值,并将其除以 10 分钟内的速率来计算百分比变化。最后,我们使用 abs 函数确保结果是一个正数。

如果表达式值变动超过 50%,则告警将在 5 分钟内持续触发。告警的标签和注释可以根据需要进行修改。

  1. 保存并重新加载 Prometheus 配置,以使更改生效。

现在,当 example_metric 的值在 5 分钟内变动超过 50% 时,Prometheus 将触发一个告警。你可以根据需要配置告警的接收器,例如使用 Alertmanager。

微信 支付宝
上次更新: 2023/04/21, 08:57:47

← alertmanager实现某个时间段静默某些告警项 监控MySQL运行状态:MySQLD Exporter→

最近更新
01
shell脚本模块集合
05-13
02
生活小技巧(认知版)
04-29
03
生活小技巧(防骗版)
04-29
更多文章>
Theme by Vdoing | Copyright © 2019-2025 | 点击查看十年之约 | 鄂ICP备2024072800号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式