Prometheus 监控 Windows 深度实战:从 CPU 到 IIS 的 Windows Exporter 全栈可观测性
在异构数据中心里,Windows Server 承载着 Active Directory、IIS、SQL Server、.NET 应用等关键服务。传统运维依赖性能计数器与事件日志,但想要统一告警与可视化,就必须将 Windows 节点纳入 Prometheus 生态。Windows Exporter(原 wmi_exporter)正是那把钥匙——它以极低的资源消耗,将 Windows 的性能计数器、WMI 查询、服务状态、IIS 站点等转化为 Prometheus 标准指标,让你像监控 Linux 一样监控 Windows。
1. Windows Exporter 是什么?
Windows Exporter 是一个在 Windows 上运行的 Prometheus 导出器,基于 WMI(Windows Management Instrumentation)和性能计数器 API,能够暴露:
- CPU、内存、磁盘、网络等基础资源
- 服务状态(services)
- IIS 站点与应用程序池
- .NET CLR 内存与 GC
- SQL Server 性能计数器(需额外 collector)
- 计划任务、MSMQ、终端服务等
目前社区维护的官方项目为 prometheus-community/windows_exporter,完全替代了早期的 wmi_exporter。
2. 部署 Windows Exporter
2.1 下载与安装
从 GitHub Releases 获取最新的 .msi 安装包。
双击安装,安装过程中会提示配置监听端口(默认 9182)和启用的采集器。建议至少勾选:
- CPU
- Memory
- Logical Disk
- Network
- Service
- IIS(如果运行 IIS)
- System
- .NET CLR(如果有 .NET 应用)
也可使用命令行静默安装并指定参数:
msiexec /i windows_exporter.msi ENABLED_COLLECTORS="cpu,memory,logical_disk,net,service,iis" LISTEN_PORT=9182
2.2 直接使用二进制
下载 .exe 后直接运行:
windows_exporter.exe –collectors.enabled="cpu,memory,logical_disk,net,service"
默认监听 0.0.0.0:9182,使用 –web.listen-address=:9182 调整。
2.3 防火墙配置
确保 Prometheus 服务器能够访问 Windows 主机的 9182 端口。在管理员 PowerShell 中添加规则:
New-NetFirewallRule –DisplayName "Prometheus Windows Exporter" –Direction Inbound –Protocol TCP –LocalPort 9182 –Action Allow
3. 配置 Prometheus 抓取
在 prometheus.yml 中添加:
scrape_configs:
– job_name: 'windows'
scrape_interval: 30s
static_configs:
– targets:
– 'win-server1:9182'
– 'win-server2:9182'
labels:
env: 'production'
os: 'windows'
如果有大量 Windows 机器,使用文件服务发现或 Consul 会更方便。
4. 核心监控指标与 PromQL
Windows Exporter 指标前缀为 windows_,标签丰富(如 volume、instance、name 等)。
4.1 CPU
| windows_cpu_time_total{mode="idle"} | CPU 空闲时间累计 |
| windows_cpu_time_total{mode="user"} | 用户态时间 |
| windows_cpu_time_total{mode="privileged"} | 内核态时间 |
| windows_cpu_info | CPU 核数、型号等信息 |
PromQL:
- 总 CPU 使用率(百分比):100 – (avg(rate(windows_cpu_time_total{mode="idle"}[5m])) * 100)
- 每核心使用率:按 instance 和核心 core 标签聚合。
4.2 内存
| windows_memory_available_bytes | 可用内存(含缓存) |
| windows_memory_total_bytes | 物理内存总量 |
| windows_memory_cache_bytes | 缓存内存 |
| windows_memory_committed_bytes | 已提交内存 |
| windows_memory_pool_paged_bytes | 分页池大小 |
PromQL:
- 内存使用率:100 – (windows_memory_available_bytes / windows_memory_total_bytes * 100)
- 已提交内存占比:windows_memory_committed_bytes / windows_memory_total_bytes
4.3 磁盘
| windows_logical_disk_free_bytes{volume="C:"} | 空闲空间 |
| windows_logical_disk_size_bytes{volume="C:"} | 总大小 |
| windows_logical_disk_read_bytes_total | 读取字节累计 |
| windows_logical_disk_write_bytes_total | 写入字节累计 |
| windows_logical_disk_idle_seconds_total | 磁盘空闲时间 |
PromQL:
- 磁盘使用率:(1 – windows_logical_disk_free_bytes{volume="C:"} / windows_logical_disk_size_bytes{volume="C:"}) * 100
- 磁盘繁忙率:100 – (rate(windows_logical_disk_idle_seconds_total[5m]) * 100 / (rate(windows_logical_disk_idle_seconds_total[5m]) + rate(windows_logical_disk_read_seconds_total[5m]) + rate(windows_logical_disk_write_seconds_total[5m])))
简化可用 windows_logical_disk_current_queue_length 来判断压力。
4.4 网络
| windows_net_bytes_received_total{nic="Ethernet0"} | 接收字节 |
| windows_net_bytes_sent_total{nic="Ethernet0"} | 发送字节 |
| windows_net_packets_received_discarded_total | 入站丢包 |
| windows_net_packets_outbound_discarded_total | 出站丢包 |
PromQL:
- 入站流量 Mbps:rate(windows_net_bytes_received_total{nic="Ethernet0"}[1m]) * 8 / 1e6
4.5 服务状态
| windows_service_state{name="W3SVC"} | 服务状态:1=stopped, 2=start pending, 3=stop pending, 4=running |
| windows_service_info{name="W3SVC"} | 服务的元数据 |
告警: windows_service_state{name="W3SVC"} != 4 表示 IIS 服务未运行。
4.6 IIS 站点与应用程序池
需要启用 iis 收集器,会暴露:
- windows_iis_site_state{site="Default Web Site"} — 站点状态(1=启动,2=停止)
- windows_iis_app_pool_state{app_pool="DefaultAppPool"} — 应用程序池状态
- windows_iis_connections_active — 活动连接数
- windows_iis_requests_total — 请求总数
告警: windows_iis_site_state{site="Default Web Site"} != 1 表示站点停止。
5. Grafana 仪表盘推荐
- Windows Exporter Dashboard:ID 14510(社区精品),全面展示 CPU、内存、磁盘、网络、服务、IIS 等,完美匹配 windows_exporter 指标。
- Windows Node:ID 12462,备选。
- IIS Dashboard:ID 10442,如果依赖 IIS,可单独导入。
导入后选择数据源,将变量 instance 绑定到 Windows target。
6. 告警规则实战
groups:
– name: windows_alerts
rules:
– alert: WindowsNodeDown
expr: up{job="windows"} == 0
for: 1m
labels:
severity: critical
annotations:
summary: "Windows 主机 {{ $labels.instance }} 不可达"
– alert: WindowsHighCPU
expr: 100 – (avg by(instance) (rate(windows_cpu_time_total{mode="idle"}[5m])) * 100) > 90
for: 5m
labels:
severity: warning
annotations:
summary: "CPU 使用率超过 90%"
– alert: WindowsHighMemory
expr: (1 – windows_memory_available_bytes / windows_memory_total_bytes) * 100 > 90
for: 5m
labels:
severity: warning
annotations:
summary: "可用内存低于 10%"
– alert: WindowsDiskSpaceLow
expr: (1 – windows_logical_disk_free_bytes{volume="C:"} / windows_logical_disk_size_bytes{volume="C:"}) * 100 > 85
for: 5m
labels:
severity: critical
annotations:
summary: "C 盘使用率超过 85%"
– alert: WindowsServiceDown
expr: windows_service_state{name=~"W3SVC|Spooler"} != 4
for: 2m
labels:
severity: critical
annotations:
summary: "关键服务 {{ $labels.name }} 未运行"
– alert: WindowsIISSiteDown
expr: windows_iis_site_state{site="Default Web Site"} != 1
for: 1m
labels:
severity: critical
annotations:
summary: "IIS 站点 {{ $labels.site }} 已停止"
– alert: WindowsNetworkErrors
expr: rate(windows_net_packets_received_discarded_total[5m]) > 0
for: 5m
labels:
severity: warning
annotations:
summary: "网卡出现丢弃包"
7. 进阶:自定义收集器与多实例
7.1 添加自定义性能计数器
Windows Exporter 支持通过配置文件添加额外的 Windows 性能计数器。创建 collector.yml:
collectors:
perfcounter:
– object: "\\\\.NET CLR Exceptions(*)\\"
counters:
– "# of Exceps Thrown / sec"
instance: "*"
启动时指定 –config.file=collector.yml,这些指标会以 windows_perf_ 前缀暴露。
7.2 监控多台 Windows 主机
使用 Ansible、GPO 或 SCCM 批量部署 .msi,统一配置 LISTEN_PORT 和启用收集器。在 Prometheus 侧通过文件发现动态添加目标。
7.3 安全加固
- 监听端口绑定内网 IP(–web.listen-address 10.0.0.5:9182)。
- 通过 Windows 防火墙限制来源 IP(仅允许 Prometheus 服务器)。
- 若需加密,可在前端部署 Nginx 反向代理并添加 HTTPS 与 Basic Auth。
8. 总结
借助 Windows Exporter,Windows 服务器的 CPU 负荷、内存压力、磁盘空间、IIS 健康、服务状态全部融入 Prometheus 的统一监控语言。无论你的基础设施是 99% Linux 还是混合环境,现在都能在同一套 Grafana 看板中透视所有节点的脉搏,真正消除监控盲区。从此刻起,Windows 运维不再是孤岛,而是整个可观测性地块中坚实的一块拼图。


