
可观测性指标监控【免费下载链接】windows_exporterPrometheus exporter for Windows machines项目地址https://gitcode.com/gh_mirrors/wi/windows_exporter点击查看免费下载本文围绕 windows_exporter 的net采集器展开它默认启用从 Performance Counter 对象Network Interface与 Win32 APIGetAdaptersAddresses两个数据源为每台 Windows 主机的每个网络适配器暴露收发包计数、错误/丢弃计数、当前带宽以及 IP 地址与操作状态等指标。读完本文你将掌握nic标签的命名规则、--collector.net.nic-include等三个命令行参数的行为边界、全部 16 项指标的语义以及用于计算接口利用率、配置高水位告警的 PromQL 写法并能对照源码理解采集链路PDH 计数器读取、适配器地址枚举、标签缓存与指标缓存的实现细节。windows_exporter 网络监控仪表盘一、采集器概览net采集器用于暴露网络接口network interface相关的指标其基本信息如下与 docs/collector.net.md 中的定义一致属性值指标名前缀net完整指标以windows_开头如windows_net_bytes_received_total数据源Performance countersNetwork Interface对象与 Win32 APIGetAdaptersAddresses是否默认启用是从源码结构看采集器由 internal/collector/net/net.go 中的Collector实现它内部分为两个子采集器sub collectormetrics通过 PDHPerformance Data Helper读取Network Interface性能计数器对象产出流量、包、队列与带宽类指标nic_info遍历GetAdaptersAddresses返回的适配器链表产出接口描述、MAC 地址、IP 地址与操作状态类指标。这种拆分由--collector.net.enabled参数控制见下文并在 pkg/collector/config.go 中以Net net.Config字段接入整体配置结构。二、配置参数Flags--collector.net.nic-include若给定只有接口名匹配该包含正则的适配器其指标才会被上报。默认值.匹配所有非空接口名。--collector.net.nic-exclude若给定接口名不匹配该排除正则的适配器才会被上报。即一个接口必须同时“匹配 include 且不匹配 exclude”才进入输出。默认值空正则不排除任何接口。--collector.net.enabled逗号分隔的子采集器列表可选取值为metrics、nic_info。默认值metrics,nic_info两者都启用。在 internal/collector/net/net.go 的NewWithFlags中可以看到这三个 flag 的注册与解析逻辑有几个值得注意的实现细节正则是全量锚定匹配的。用户输入的正则会被包成^(?:%s)$再编译net.go#L178-L186。这意味着--collector.net.nic-includeIntel并不会匹配所有包含 Intel 的接口而是要求整个接口名恰好等于 Intel要做子串匹配应写成.*Intel.*。非法子采集器会启动失败。Build阶段会对CollectorsEnabled做白名单校验出现未知取值时返回错误unknown sub collector: ...net.go#L207-L214这一行为在 internal/collector/lifecycle_test.go 中有对应测试覆盖。nic_info属于实验特性。启用nic_info时采集器构建阶段会输出一条日志提示“nic/addresses collector is in an experimental state”其配置与指标未来可能变化net.go#L324-L328。配置结构体本身定义在 net.go#L48-L62其中NicInclude/NicExclude以*regexp.Regexp形式存储CollectorsEnabled为字符串切片均可通过命令行 flag 或配置注入。三、接口命名规则nic标签这是使用 include/exclude 正则之前必须先理解的一点原文档说明如下nic标签的值是适配器在Network Interface性能计数器对象中的实例名。它由适配器的 Description 字符串转换而来(与)替换为[与]#、/与\替换为_。例如转换后可能得到Intel[R] PRO_1000 MT Network Connection _2。include 与 exclude 表达式匹配的就是这个转换后的名字而不是设备管理器里看到的原始描述。这一规则在源码中由一个固定的替换器实现internal/collector/net/net.go#L64-L68var nicNameReplacer strings.NewReplacer((, [, ), ], #, _, /, _, \\, _)collectNICInfo中对每个适配器执行nicNameReplacer.Replace(...Description)net.go#L480保证性能计数器路径与GetAdaptersAddresses路径产生一致的nic值。另外windows_net_nic_info的mac标签对于没有物理地址的适配器如 loopback为空字符串。从源码看formatPhysicalAddress 按PhysicalAddressLength截取物理地址字节以冒号分隔的大写十六进制输出长度为 0 时直接返回空串。四、指标全表下表完整继承自 docs/collector.net.md并在“对应计数器”一列补充了源码中 internal/collector/net/types.go#L39-L55perfDataCounterValues结构体的perfdata标签所声明的Network Interface计数器名称指标名描述类型标签对应计数器perfdata 标签windows_net_bytes_received_total接口接收的总字节数counternicBytes Received/secwindows_net_bytes_sent_total接口发送的总字节数counternicBytes Sent/secwindows_net_bytes_total接口收发合计的总字节数counternicBytes Total/secwindows_net_output_queue_length_packets输出队列长度以包计。超过 2 即可能出现延迟gaugenicOutput Queue Lengthwindows_net_packets_outbound_discarded_total未检测到错误却被丢弃的出站包总数counternicPackets Outbound Discardedwindows_net_packets_outbound_errors_total因错误而无法发送的包总数counternicPackets Outbound Errorswindows_net_packets_received_discarded_total未检测到错误却被丢弃的入站包总数counternicPackets Received Discardedwindows_net_packets_received_errors_total因错误而无法接收的包总数counternicPackets Received Errorswindows_net_packets_received_total接口接收的总包数counternicPackets Received/secwindows_net_packets_received_unknown_total因未知或不支持协议而被丢弃的入站包总数counternicPackets Received Unknownwindows_net_packets_total接口收发合计的总包数counternicPackets/secwindows_net_packets_sent_total接口发送的总包数counternicPackets Sent/secwindows_net_current_bandwidth_bytes接口当前带宽的估计值字节/秒gaugenicCurrent Bandwidthwindows_net_nic_address_info常量值 1标签携带接口地址信息gaugenic,address,family—GetAdaptersAddresseswindows_net_nic_info常量值 1标签携带接口基本信息gaugenic,friendly_name,mac—GetAdaptersAddresseswindows_net_nic_operation_status按 RFC 2863 的 IfOperStatus 定义的接口操作状态gaugenic,status—GetAdaptersAddresses关于计数器类型有一点容易被计数器名误导虽然 PDH 侧计数器叫 Bytes Received/sec 等每秒速率名采集器却以pdh.CounterTypeRaw方式读取net.go#L319-L322即以 Raw 100NS 计数器暴露为累计值counter 类型因此查询速率时必须用rate()或increase()。windows_net_current_bandwidth_bytes有一个单位换算细节Current Bandwidth计数器原始值单位是bit/秒源码中除以 8 后再上报为 byte/秒net.go#L452-L457这正是文档将其描述为“字节/秒”的原因。windows_net_nic_operation_status的status标签取值映射定义在 internal/collector/net/types.go#L28-L36与 RFC 2863 一致up、down、testing、unknown、dormant、not present、lower layer down。实现上对每个取值都输出一条 series当前状态对应的取值为 1其余为 0net.go#L558-L571这样可以直接用windows_net_nic_operation_status{statusup} 1判断链路是否可用。windows_net_nic_address_info的family标签取值为ipv4/ipv6由 types.go#L24-L27 的映射表给出。五、采集链路实现剖析5.1 性能计数器路径metrics子采集器metrics子采集器在Build阶段创建一个泛型 PDH 采集器c.perfDataCollector, err pdh.NewCollectorperfDataCounterValues), pdh.CounterTypeRaw, Network Interface, pdh.InstancesAll)net.go#L319-L322它订阅Network Interface对象的全部实例pdh.InstancesAll每次 scrape 时collect依次执行读取计数器值 → 按 include/exclude 正则过滤实例名 → 通过labelcache的 scrape 上下文批量生成 const metricnet.go#L353-L461。其中过滤逻辑为“排除匹配 或 包含不匹配 即跳过”net.go#L362-L365与第二节描述的语义一致。labelcache的作用是复用同一 scrape 内相同的标签组合降低重复创建标签对的开销这一缓存机制在 internal/labelcache 中实现并有配套测试。5.2GetAdaptersAddresses路径nic_info子采集器collectNICInfonet.go#L463-L514的核心流程带缓冲复用的系统调用。adapterAddresses函数net.go#L642-L667调用windows.GetAdaptersAddressesflags 为GAA_FLAG_SKIP_MULTICAST | GAA_FLAG_SKIP_DNS_SERVER即跳过组播地址与 DNS 服务器地址首次调用以推荐的 15000 字节为初始缓冲遇到ERROR_BUFFER_OVERFLOW时按系统要求的长度重试。缓冲在Collector结构体中跨 scrape 复用adapterAddressesBuf字段既避免重复分配也为下一次调用提供了尺寸提示。逐适配器过滤与取值。遍历链表时对Description做nicNameReplacer转换并与计数器路径共用同一套 include/exclude 过滤friendly_name取FriendlyName字段原文mac由formatPhysicalAddress格式化。仅当接口 Up 时收集地址。只有OperStatus IfOperStatusUp的适配器才会枚举FirstUnicastAddress与FirstAnycastAddress链表且只保留全局单播global unicast地址链路本地地址如 IPv6 的fe80::会被IsGlobalUnicast()过滤掉net.go#L516-L542、net.go#L577-L609。指标级缓存。nicMetricCache会对“自上次 scrape 以来信息未变化的适配器”直接复用上一轮构建好的nic_info、nic_operation_status与nic_address_info指标对象缓存键之外还比较nicValuesfriendly_name、MAC、operStatus 及编码后的地址串任一变化都会触发重建net.go#L104-L122、net.go#L502-L508。源码注释明确指出地址查询每次 scrape 仍会执行缓存省的是对象分配而非系统调用。测试方面internal/collector/net/net_test.go 使用testutils.TestCollector对默认配置做通用采集器测试internal/collector/net/net_internal_test.go 与 internal/collector/net/net_bench_test.go 分别补充包内测试与基准测试。六、PromQL 示例与实用查询查询发送流量速率rate(windows_net_bytes_sent_total{instancelocalhost}[2m])计算接口利用率百分比以文档中的 Hyper-V 适配器为例rate(windows_net_bytes_total{instancelocalhost, nicMicrosoft Hyper-V Network Adapter _2}[2m]) / windows_net_current_bandwidth_bytes{instancelocalhost, nicMicrosoft Hyper-V Network Adapter _2} * 100注意windows_net_current_bandwidth_bytes是“估计值”某些虚拟或无速率上报能力的适配器该值为 0 或长期不变仓库自带仪表盘在计算利用率时因此使用了 0守卫例如 contrib/mixin/dashboards/network.libsonnet 中的rate(windows_net_bytes_total{job~$job, instance$instance, nic~$nic}[$__rate_interval]) / (windows_net_current_bandwidth_bytes{job~$job, instance$instance, nic~$nic} 0)即“没有上报链路速率的接口不显示”。同一文件还给出了吞吐、错误/丢弃包等面板的现成查询编译产物可直接查看 dashboard/windows-exporter-network.json。舰队Fleet级预计算规则如果管理大量主机仓库的 mixin 提供了 net 相关 record 规则定义在 contrib/mixin/rules/rules.libsonnet包括windows:net_received_bytes:rate/windows:net_sent_bytes:rate按 nic 选择器对windows_net_bytes_*_total做rate预计算windows:net_received_utilization:ratio/windows:net_sent_utilization:ratio收发方向各自的利用率windows:net_received_errors:rate、windows:net_received_discarded:rate、windows:net_outbound_errors:rate、windows:net_outbound_discarded:rate四类异常包速率供告警与 Top-N 面板复用。这些指标也驱动了 fleet 仪表盘的“Top 25 发送/接收主机”“利用率最高主机”“异常包最多主机”等面板contrib/mixin/dashboards/fleet.libsonnet并配套了规则与告警的测试用例contrib/mixin/tests/rules.test.yaml、contrib/mixin/tests/alerts.test.yaml 中以nicEthernet的windows_net_packets_*_errors_total序列为输入。七、告警配置示例原文档给出的告警示例可直接用于 Prometheus 的prometheus.rules- alert: NetInterfaceUsage expr: rate(windows_net_bytes_total[2m]) / windows_net_current_bandwidth_bytes * 100 95 for: 10m labels: severity: high annotations: summary: Network Interface Usage (instance {{ $labels.instance }}) description: Network traffic usage is greater than 95% for interface {{ $labels.nic }}\n VALUE {{ $value }}\n LABELS: {{ $labels }}使用提示表达式对每台主机、每个nic标签序列独立求值{{ $labels.nic }}可直接定位告警接口windows_net_current_bandwidth_bytes为 0 的接口会产生Inf若环境中存在此类接口建议像 mixin 那样加 0守卫或改用windows_net_output_queue_length_packets、错误包速率等指标做辅助告警结合第四节的状态指标可以进一步用windows_net_nic_operation_status{statusdown} 1表达“链路中断”类告警此写法为基于指标语义的建议仓库内未提供对应现成规则。八、已知限制不采集路由信息。原文档明确说明Route information is not collected此前文档中列出的windows_net_route_info指标实际上从未被上报现已从支持的指标列表中移除。nic_info子采集器含windows_net_nic_address_info目前处于实验状态启动时会有相应日志提示其配置与指标在未来版本可能变化。windows_net_current_bandwidth_bytes是接口当前带宽的估计值来源于性能计数器Current Bandwidthbit/秒源码中已换算为 byte/秒并非精确值。九、延伸阅读文档原文docs/collector.net.md核心实现internal/collector/net/net.go、internal/collector/net/types.go测试internal/collector/net/net_test.go、internal/collector/lifecycle_test.go配置接入pkg/collector/config.goPDH 采集框架internal/pdh仪表盘源与产物contrib/mixin/dashboards/network.libsonnet、dashboard/windows-exporter-network.json网络运行手册docs/runbooks/windowsnetworkerrors.md、docs/runbooks/windowsnetworkdiscards.md赞分享可观测性指标监控【免费下载链接】windows_exporterPrometheus exporter for Windows machines项目地址https://gitcode.com/gh_mirrors/wi/windows_exporter点击查看免费下载相关推荐windows_exporter license collector 详解监控 Windows 授权状态的指标、告警与 slc.dll 实现原理windows_exporter license collector 详解监控 Windows 授权状态的指标、告警与 slc.dll 实现原理 本文围绕 w可观测性指标监控windows_exporter 的 cpu 采集器详解基于 Windows 性能计数器的 CPU 监控指标、PromQL 查询与告警实践windows_exporter 的 cpu 采集器详解基于 Windows 性能计数器的 CPU 监控指标、PromQL 查询与告警实践 本文围绕 wind可观测性指标监控windows_exporter memory collector 深度解析Windows 内存指标采集原理、完整指标清单与监控告警实践windows_exporter memory collector 深度解析Windows 内存指标采集原理、完整指标清单与监控告警实践 本篇以 window可观测性指标监控创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考