如何在Grafana告警描述中展示node_filesystem_avail_bytes实际数值?
解决Grafana告警中显示node_filesystem_avail_bytes人性化数值的问题
问题原因
Prometheus Alertmanager支持的动态查询类模板函数(如query)在Grafana告警系统中不兼容,导致原规则中的模板无法被解析,直接以原始文本形式输出。
解决方案
利用Grafana告警规则的多查询特性,直接在规则中定义可用空间字节数的查询,再通过Grafana内置模板函数格式化输出:
配置告警规则的双查询
在Grafana告警规则中添加两个关联查询:- 查询A(剩余百分比,触发告警):
(Ref ID保持默认(node_filesystem_avail_bytes * 100) / node_filesystem_size_bytes > 0 and ON(instance, device, mountpoint) node_filesystem_readonly == 0 and {instance=~"10.22.22.18.*"}A) - 查询B(可用空间字节数,获取原始值):
(Ref ID设为node_filesystem_avail_bytes and ON(instance, device, mountpoint) node_filesystem_readonly == 0 and {instance=~"10.22.22.18.*"}B)
- 查询A(剩余百分比,触发告警):
编写Annotations模板
在告警的Annotations中,使用Grafana支持的模板语法引用查询结果:summary: "Host {{ $labels.instance }} out of disk space\n- Device: {{ $labels.device }}\n- Mountpoint: {{ $labels.mountpoint }}" description: "Node disk space is filling up. Value = {{ $values.A.Value | printf \"%.2f\" }}% ({{ $values.B.Value | humanize1024 }})"{{ $values.A.Value | printf "%.2f" }}:引用查询A的百分比结果,保留两位小数{{ $values.B.Value | humanize1024 }}:引用查询B的字节数,自动转换为Mi/Gi等易读单位
关键注意事项
- 修正原规则笔误:将summary中的"out of memory"改为"out of disk space",避免语义混淆
- Grafana不支持动态查询函数,仅能引用当前告警规则内定义的查询结果
- 确保两个查询的标签(instance、device、mountpoint)完全匹配,保证指标值与实例正确关联
最终通知效果
🔥 [PROBLEM] Test_grafana_rules_filesystem Severity: high Summary: Host 10.11.11.100:9100 out of disk space - Device: /dev/sda1 - Mountpoint: /boot Description: Node disk space is filling up. Value = 72.61% (736.4Mi) Starts at: 2023-08-31 13:22:49.12 +0300 EAT
内容的提问来源于stack exchange,提问作者Stanislav_iz
相关产品推荐
相关产品推荐

