Prometheus 如何采集 Windows 主机的性能指标

作者:袖梨 2026-08-23

Prometheus 依赖 Windows Exporter 采集 Windows 主机指标,后者通过 WMI/API 暴露 /metrics 接口;需安装服务、配置启用收集器(如 cpu、memory)、在 prometheus.yml 中添加 target,并可通过 Grafana 导入 ID 12907 面板可视化。

Prometheus 本身不直接采集 Windows 主机指标,必须借助 Windows Exporter(原 wmi_exporter)作为中间桥梁。它运行在 Windows 上,通过 WMI 或系统 API 获取 CPU、内存、磁盘、网络等数据,并以 Prometheus 兼容的文本格式(/metrics)通过 HTTP 暴露出来,再由 Prometheus 定期拉取。

安装并启动 Windows Exporter

这是第一步,也是最关键的环节:

  1. 前往 GitHub Releases 页面,下载最新版 windows_exporter-*.amd64.msi(适用于 Windows Server 2016+ 或 Win10/11 21H2+)
  2. 以管理员身份双击安装,全程默认下一步即可
  3. 安装后自动注册为 Windows 服务 windows_exporter,默认监听 :9182
  4. 验证是否正常:浏览器访问 http://localhost:9182/metrics,能看到大量以 # TYPE 开头的指标文本,说明服务已就绪

配置要采集的指标项

Windows Exporter 默认启用基础收集器,但建议按需精简,避免冗余指标影响性能和存储:

  1. 常用核心收集器包括:cpulogical_disknetossystemmemoryservice
  2. 禁用不需要的项,例如光驱监控:--collector.logical_disk.ignore-disk-types="CDROM"
  3. 可通过命令行参数启动(如 PowerShell 中):

    .windows_exporter.exe --collectors.enabled="cpu,logical_disk,net,os,system,memory" --web.listen-address=":9182"

  4. 也可创建 config.yaml 文件统一管理,更利于批量部署

让 Prometheus 抓取这些指标

修改 Prometheus 的配置文件 prometheus.yml,添加 Windows 主机目标:

  1. scrape_configs 下新增一个 job,例如:
- job_name: "windows"static_configs:- targets: ["192.168.1.100:9182"]# 替换为实际 Windows 主机 IPmetrics_path: /metrics
  1. 保存后重启 Prometheus,或发送 SIGHUP 重载配置
  2. 访问 http://localhost:9090/targets,确认该 target 状态为 UP
  3. http://localhost:9090/graph 输入 windows_cpu_usage_percentwindows_logical_disk_free_bytes 等指标名,可查到实时数据

搭配 Grafana 做可视化展示

有了指标数据,下一步就是直观呈现:

  1. 启动 Grafana(如解压后运行 bingrafana-server.exe),访问 http://localhost:3000
  2. 添加 Prometheus 数据源(URL 填 http://localhost:9090
  3. 导入社区成熟的 Windows 监控面板,例如 ID 为 12907Windows Exporter Full,它已预置 CPU、内存、磁盘、服务状态等关键视图
  4. 面板会自动识别你采集的指标,无需手动写太多 PromQL,开箱即用

相关文章

精彩推荐