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

Prometheus windows exporter如何通过文本文件实现许可证过期监控

答复

Windows Exporter 原生不支持直接读取你自定义格式的纯文本许可证文件,但可以通过自带的textfile收集器零额外依赖实现需求,整体配置流程如下:

核心原理

textfile是Windows Exporter官方内置的收集器,专门用于采集用户自定义的批处理/脚本生成的指标,它只识别Prometheus标准文本指标格式,无法直接解析你当前写的软件名 : 到期日期结构的txt文件,只需要加一层简单的格式转换逻辑即可。

配置步骤

  • 第一步:启用textfile收集器
    给Windows Exporter添加启动参数,指定自定义指标存放目录,示例参数:
    windows_exporter.exe --collectors.enabled=textfile,[你原本启用的其他收集器名] --collector.textfile.directory="C:\Program Files\windows_exporter\custom_metrics"
    
    收集器启动后会自动扫描该目录下所有.prom后缀的文件,把文件内的指标纳入采集结果。
  • 第二步:编写格式转换脚本
    你完全可以继续用你习惯的license_example.txt存许可证信息,只需要写个简单的PowerShell脚本,定时读取这个文件,把日期转换成Unix时间戳,输出为Prometheus标准格式的.prom文件。
    示例脚本如下:
    # 路径配置按实际情况修改
    $sourceLicenseFile = "C:\your\path\license_example.txt"
    $promDir = "C:\Program Files\windows_exporter\custom_metrics"
    $tempPath = Join-Path $promDir "license.prom.tmp"
    $targetPath = Join-Path $promDir "license.prom"
    
    $metricContent = @(
        "# HELP software_license_expiry_timestamp 软件许可证到期Unix时间戳",
        "# TYPE software_license_expiry_timestamp gauge"
    )
    
    # 解析原始许可证文件
    Get-Content $sourceLicenseFile | ForEach-Object {
        if ($_ -match '^(.*?)\s*:\s*(\d{2}-\d{2}-\d{4})$') {
            $softName = $matches[1].Trim()
            $expireDate = [datetime]::ParseExact($matches[2], "dd-MM-yyyy", [System.Globalization.CultureInfo]::InvariantCulture)
            $unixTimestamp = [int64]($expireDate.ToUniversalTime() - [datetime]::UnixEpoch).TotalSeconds
            $metricContent += "software_license_expiry_timestamp{software_name=`"$softName`"} $unixTimestamp"
        }
    }
    
    # 先写临时文件再替换,避免采集到半写入的损坏文件
    $metricContent | Out-File -FilePath $tempPath -Encoding ascii
    Move-Item -Path $tempPath -Destination $targetPath -Force
    
    把这个脚本加到Windows任务计划程序,设置每天执行1次即可,后续你只需要按原有格式更新license_example.txt里的内容,不需要调整其他配置。
  • 第三步:配置临期告警规则
    在Prometheus中添加如下告警规则,即可实现到期前10天触发告警:
    groups:
    - name: license_alerts
      rules:
      - alert: SoftwareLicenseExpiringSoon
        expr: (software_license_expiry_timestamp - time()) < 86400 * 10 and (software_license_expiry_timestamp - time()) > 0
        for: 5m
        labels:
          severity: warning
        annotations:
          summary: "软件{{ $labels.software_name }}许可证即将到期"
          description: "许可证剩余{{ printf \"%.1f\" (div (sub $value 0) 86400) }}天到期,请及时续期"
    
    规则生效后,符合条件的告警会自动推送到Alertmanager,按你配置的通知渠道发送提醒。

注意事项

  • 生成.prom文件时必须使用原子写入逻辑(先写临时文件再移动替换),否则可能出现Windows Exporter采集到空文件/半写文件导致指标异常的问题
  • 日期转时间戳建议统一使用UTC时区,避免Windows主机和Prometheus服务器时区不一致导致的告警时间偏差
  • 不要把非.prom后缀的文件放到textfile收集器的扫描目录下,避免收集器报解析错误

内容的提问来源于stack exchange,提问作者Adrian Vulpeanu

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.03 08:39:29