1. 项目背景与核心需求
在企业的IT基础设施管理中,内网Windows服务器的监控一直是运维工作的重点和难点。传统方案如Zabbix或Nagios往往存在部署复杂、资源占用高的问题。而Prometheus作为云原生时代的监控标杆,其轻量级架构和强大的时序数据处理能力,为内网环境下的Windows监控提供了新的技术路径。
我最近为一个金融客户实施了基于Prometheus的内网Windows监控体系,整套方案在零外网依赖的情况下,实现了200+台Windows服务器的指标采集、存储和告警闭环。下面将分享具体实现方案中几个关键环节的实战经验。
2. 技术架构设计要点
2.1 整体架构拓扑
采用分层采集架构:
- 数据采集层:Windows Exporter + SNMP Exporter
- 传输层:Pushgateway做临时指标中转
- 存储计算层:Prometheus Server
- 展示层:Grafana
- 告警层:Alertmanager
内网环境需要特别注意:
- 所有组件需离线安装包
- 证书需提前自签名
- 防火墙策略要放行9100/9090等端口
2.2 Windows指标采集方案选型
对比三种采集方式:
- WMI Exporter:兼容性最好,但性能损耗大
- SNMP:需启用Windows SNMP服务
- Performance Counters:原生性能计数器
最终选择WMI Exporter+SNMP双通道方案:
# 典型启动参数 wmi_exporter --collectors.enabled="cpu,memory,net,service" --telemetry.addr=":9182"关键提示:Windows Server 2012 R2需打KB3060681补丁才能正常使用WMI查询
3. 详细部署实施
3.1 离线环境准备
依赖组件打包:
- vc_redist.x64.exe(Visual C++运行时)
- nssm-2.24.zip(服务封装工具)
- prometheus-2.45.0.windows-amd64.tar.gz
证书生成:
New-SelfSignedCertificate -DnsName "monitor.internal" -CertStoreLocation "cert:\LocalMachine\My"防火墙规则配置:
New-NetFirewallRule -DisplayName "Prometheus" -Direction Inbound -Protocol TCP -LocalPort 9090,9100,9182 -Action Allow
3.2 Exporter部署实战
服务化安装步骤:
- 解压wmi_exporter到C:\Program Files\
- 使用NSSM创建系统服务:
nssm install WMI_Exporter "C:\program files\wmi_exporter\wmi_exporter.exe" nssm set WMI_Exporter AppDirectory "C:\program files\wmi_exporter" - 配置自动重启策略:
Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\WMI_Exporter] "FailureActionsOnNonCrashFailures"=dword:00000001
性能优化参数:
collectors: enabled: cpu,memory,net,logical_disk,os,service,system service.services-where: "name LIKE '%sql%' OR name='spooler'" logical_disk.volume-whitelist: "C:;D:"4. Prometheus核心配置
4.1 抓取规则配置
scrape_configs: - job_name: 'windows' static_configs: - targets: ['win-server1:9182','win-server2:9182'] metrics_path: '/metrics' scheme: https tls_config: ca_file: C:\ssl\internal-ca.pem relabel_configs: - source_labels: [__address__] target_label: instance regex: '([^:]+).*' replacement: '$1'4.2 告警规则示例
groups: - name: windows-alerts rules: - alert: HighCPUUsage expr: 100 - (avg by(instance)(rate(wmi_cpu_time_total{mode="idle"}[2m])) * 100) > 85 for: 5m labels: severity: warning annotations: summary: "High CPU usage on {{ $labels.instance }}" value: "{{ $value }}%"5. 典型问题排查指南
5.1 WMI查询超时问题
现象: Exporter日志出现"WMI query timed out"
解决方案:
- 调整WMI超时参数:
Get-WmiObject -Namespace "root\cimv2" -Class "__ProviderHostQuotaConfiguration" | Set-WmiInstance -Arguments @{QueryTimeoutMS=30000} - 增加Exporter超时时间:
wmi_exporter --scrape.timeout-margin=1.5
5.2 证书验证失败
错误信息: x509: certificate signed by unknown authority
处理步骤:
- 将CA证书导入Windows信任库:
Import-Certificate -FilePath C:\ssl\internal-ca.pem -CertStoreLocation Cert:\LocalMachine\Root - 在Prometheus配置中添加:
tls_config: insecure_skip_verify: true # 临时方案
6. 监控指标优化建议
6.1 关键性能指标
必监控的Windows核心指标:
- 内存压力:
(wmi_os_physical_memory_free_bytes / wmi_os_physical_memory_total_bytes) * 100 < 10 - 磁盘队列长度:
rate(wmi_logical_disk_avg_disk_sec_per_read[5m]) > 0.1 - 服务状态:
wmi_service_status{status!="running"} == 1
6.2 采集频率建议
根据服务器角色调整:
- 普通应用服务器:30s间隔
- 数据库服务器:15s间隔
- 关键业务系统:10s间隔
对应Prometheus配置:
scrape_interval: 30s scrape_timeout: 25s7. 安全加固措施
7.1 认证鉴权方案
基本认证配置:
basic_auth: username: "prom_user" password: "S3cr3tP@ss"HTTPS强化配置:
tls_config: cert_file: /etc/ssl/server.crt key_file: /etc/ssl/server.key min_version: TLS1.2 cipher_suites: - TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA3847.2 网络隔离策略
- 采用双网卡方案:
- 管理网卡:仅开放9100/9090端口
- 业务网卡:正常业务通信
- 配置Windows防火墙入站规则:
Set-NetFirewallRule -DisplayName "Prometheus" -Profile Domain -Enabled True
8. 可视化仪表板配置
推荐使用Grafana官方仪表板:
- Windows主机监控(ID:10467)
- Windows Exporter Full(ID:14694)
自定义关键面板:
内存使用热力图:
SELECT $__timeEpoch(time), instance as metric, (1 - (mem_available / mem_total)) * 100 as value FROM windows_metrics服务状态矩阵:
{ "type": "state", "options": { "colorMode": "value", "thresholds": [ { "color": "red", "value": 0 }, { "color": "green", "value": 1 } ] } }
9. 性能压测数据
在32核/64GB内存的Windows Server 2019上测试:
| 指标类型 | 采集间隔 | CPU占用 | 内存增长 |
|---|---|---|---|
| 基础指标 | 30s | 0.8% | 45MB |
| 完整指标集 | 15s | 2.1% | 78MB |
| 带进程级监控 | 10s | 3.5% | 112MB |
经验值:每台服务器建议分配50MB内存缓冲区
10. 版本升级策略
采用蓝绿升级方案:
- 新版本Exporter部署到20%节点
- 观察48小时稳定性
- 全量滚动升级
回滚操作:
nssm stop WMI_Exporter Expand-Archive -Path wmi_exporter_old.zip -DestinationPath C:\program files\ nssm start WMI_Exporter这套方案在某城商行生产环境稳定运行17个月,累计发现硬件故障23次,预防服务中断8次。最关键的是要建立完整的指标基线,我们通过3个月的基线采集,最终设定的告警阈值比厂商推荐值更符合实际业务特征。