You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

寻求可检查文件大小的Prometheus Exporter,实现0KB文件告警及清单功能

检测0KB文件的Prometheus Exporter方案与告警优化

一、适用的Prometheus Exporter选择

直接用node_exporter即可满足需求,它内置了node_filesystem_file_size_bytes指标,能监控指定文件的大小,无需额外部署第三方Exporter。

配置node_exporter监控目标文件

启动node_exporter时,通过参数指定要监控的文件/目录(支持正则匹配):

./node_exporter --collector.filesystem.include-files="/data/app/logs/.*|/data/backup/.*"
  • --collector.filesystem.include-files:指定需要监控的文件路径正则,多规则用|分隔
  • 如需排除特定文件,可搭配--collector.filesystem.exclude-files参数

二、0KB文件的告警规则优化

既然已能收到告警,建议在规则中包含文件路径标签,直接定位问题:

groups:
- name: zero_byte_files
  rules:
  - alert: ZeroByteFileDetected
    expr: node_filesystem_file_size_bytes{path=~"/data/app/logs/.*|/data/backup/.*"} == 0
    for: 1m
    labels:
      severity: warning
    annotations:
      summary: "检测到0KB文件: {{ $labels.path }}"
      description: "文件 {{ $labels.path }} 大小为0KB,请检查"

告警通知会直接显示具体文件路径,省去额外排查步骤。

三、生成0KB文件清单

通过Prometheus API编写脚本,定期拉取所有0KB文件路径并生成清单:

#!/bin/bash
# 替换为你的Prometheus地址
PROM_ADDR="http://127.0.0.1:9090"
# 替换为你的监控路径正则
TARGET_PATH_REGEX="/data/app/logs/.*|/data/backup/.*"
# 查询并生成带时间戳的清单文件
curl -s "${PROM_ADDR}/api/v1/query?query=node_filesystem_file_size_bytes{path=~\"${TARGET_PATH_REGEX}\"}%20%3D%3D%200" | \
jq -r '.data.result[] | .metric.path' > zero_byte_files_$(date +%Y%m%d_%H%M%S).txt
  • 依赖jq工具解析JSON结果,可通过apt install jq或yum install jq安装
  • 可将脚本加入crontab,定期自动生成清单(比如每小时执行一次)

备选方案:自定义textfile指标

如果需要更复杂的扫描逻辑(如按文件类型过滤、递归扫描子目录),可使用node_exporter的textfile collector:

  1. 编写脚本扫描目标目录,生成指标文件:
#!/bin/bash
OUTPUT_FILE="/var/lib/node_exporter/textfile_collector/file_sizes.prom"
TARGET_DIR="/data/app/logs"

# 清空旧指标
> $OUTPUT_FILE

# 扫描目录下所有文件,生成指标
find $TARGET_DIR -type f -print0 | while IFS= read -r -d '' file; do
  size=$(stat -c%s "$file")
  echo "custom_file_size_bytes{path=\"$file\"} $size" >> $OUTPUT_FILE
done
  1. 启动node_exporter时开启textfile collector:
./node_exporter --collector.textfile.directory="/var/lib/node_exporter/textfile_collector"
  1. 用PromQL查询custom_file_size_bytes == 0触发告警和生成清单

内容的提问来源于stack exchange,提问作者Akshat Jain

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 20:24:32