From bc1b40c6796f84aca4a54b199eefd13862565198 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Otto=20Kr=C3=B6pke?= Date: Fri, 14 Mar 2025 19:28:48 +0100 Subject: [PATCH] hyperv: fix Windows Server 2016 compatibility (#1925) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jan-Otto Kröpke --- internal/collector/hyperv/hyperv.go | 14 +++++++++----- .../hyperv/hyperv_dynamic_memory_balancer.go | 17 ++++++++++------- .../hyperv/hyperv_dynamic_memory_vm.go | 17 ++++++++++------- .../hyperv_hypervisor_virtual_processor.go | 10 +++++----- 4 files changed, 34 insertions(+), 24 deletions(-) diff --git a/internal/collector/hyperv/hyperv.go b/internal/collector/hyperv/hyperv.go index ea4aeb8c..eb52d0e0 100644 --- a/internal/collector/hyperv/hyperv.go +++ b/internal/collector/hyperv/hyperv.go @@ -148,7 +148,7 @@ func (c *Collector) Close() error { return nil } -func (c *Collector) Build(_ *slog.Logger, _ *mi.Session) error { +func (c *Collector) Build(logger *slog.Logger, _ *mi.Session) error { c.collectorFns = make([]func(ch chan<- prometheus.Metric) error, 0, len(c.config.CollectorsEnabled)) c.closeFns = make([]func(), 0, len(c.config.CollectorsEnabled)) @@ -224,9 +224,10 @@ func (c *Collector) Build(_ *slog.Logger, _ *mi.Session) error { close: c.perfDataCollectorVirtualNetworkAdapterDropReasons.Close, }, subCollectorVirtualSMB: { - build: c.buildVirtualSMB, - collect: c.collectVirtualSMB, - close: c.perfDataCollectorVirtualSMB.Close, + build: c.buildVirtualSMB, + collect: c.collectVirtualSMB, + close: c.perfDataCollectorVirtualSMB.Close, + minBuildNumber: osversion.LTSC2022, }, subCollectorVirtualStorageDevice: { build: c.buildVirtualStorageDevice, @@ -253,7 +254,10 @@ func (c *Collector) Build(_ *slog.Logger, _ *mi.Session) error { } if buildNumber < subCollectors[name].minBuildNumber { - errs = append(errs, fmt.Errorf("collector %s requires Windows Server 2022 or newer", name)) + logger.Warn(fmt.Sprintf( + "collector %s requires windows build version %d. Current build version: %d", + name, subCollectors[name].minBuildNumber, buildNumber, + ), slog.String("collector", name)) continue } diff --git a/internal/collector/hyperv/hyperv_dynamic_memory_balancer.go b/internal/collector/hyperv/hyperv_dynamic_memory_balancer.go index 97a26b38..6bf49b32 100644 --- a/internal/collector/hyperv/hyperv_dynamic_memory_balancer.go +++ b/internal/collector/hyperv/hyperv_dynamic_memory_balancer.go @@ -18,6 +18,7 @@ package hyperv import ( "fmt" + "github.com/Microsoft/hcsshim/osversion" "github.com/prometheus-community/windows_exporter/internal/pdh" "github.com/prometheus-community/windows_exporter/internal/types" "github.com/prometheus-community/windows_exporter/internal/utils" @@ -40,7 +41,7 @@ type perfDataCounterValuesDynamicMemoryBalancer struct { // Hyper-V Dynamic Memory Balancer metrics VmDynamicMemoryBalancerAvailableMemory float64 `perfdata:"Available Memory"` - VmDynamicMemoryBalancerAvailableMemoryForBalancing float64 `perfdata:"Available Memory For Balancing"` + VmDynamicMemoryBalancerAvailableMemoryForBalancing float64 `perfdata:"Available Memory For Balancing" perfdata_min_build:"17763"` VmDynamicMemoryBalancerAveragePressure float64 `perfdata:"Average Pressure"` VmDynamicMemoryBalancerSystemCurrentPressure float64 `perfdata:"System Current Pressure"` } @@ -96,12 +97,14 @@ func (c *Collector) collectDynamicMemoryBalancer(ch chan<- prometheus.Metric) er data.Name, ) - ch <- prometheus.MustNewConstMetric( - c.vmDynamicMemoryBalancerAvailableMemoryForBalancing, - prometheus.GaugeValue, - utils.MBToBytes(data.VmDynamicMemoryBalancerAvailableMemoryForBalancing), - data.Name, - ) + if osversion.Build() >= osversion.LTSC2019 { + ch <- prometheus.MustNewConstMetric( + c.vmDynamicMemoryBalancerAvailableMemoryForBalancing, + prometheus.GaugeValue, + utils.MBToBytes(data.VmDynamicMemoryBalancerAvailableMemoryForBalancing), + data.Name, + ) + } ch <- prometheus.MustNewConstMetric( c.vmDynamicMemoryBalancerAveragePressure, diff --git a/internal/collector/hyperv/hyperv_dynamic_memory_vm.go b/internal/collector/hyperv/hyperv_dynamic_memory_vm.go index 86162f88..64511553 100644 --- a/internal/collector/hyperv/hyperv_dynamic_memory_vm.go +++ b/internal/collector/hyperv/hyperv_dynamic_memory_vm.go @@ -18,6 +18,7 @@ package hyperv import ( "fmt" + "github.com/Microsoft/hcsshim/osversion" "github.com/prometheus-community/windows_exporter/internal/pdh" "github.com/prometheus-community/windows_exporter/internal/types" "github.com/prometheus-community/windows_exporter/internal/utils" @@ -47,7 +48,7 @@ type perfDataCounterValuesDynamicMemoryVM struct { // Hyper-V Dynamic Memory VM metrics VmMemoryAddedMemory float64 `perfdata:"Added Memory"` VmMemoryCurrentPressure float64 `perfdata:"Current Pressure"` - VmMemoryGuestAvailableMemory float64 `perfdata:"Guest Available Memory"` + VmMemoryGuestAvailableMemory float64 `perfdata:"Guest Available Memory" perfdata_min_build:"17763"` VmMemoryGuestVisiblePhysicalMemory float64 `perfdata:"Guest Visible Physical Memory"` VmMemoryMaximumPressure float64 `perfdata:"Maximum Pressure"` VmMemoryMemoryAddOperations float64 `perfdata:"Memory Add Operations"` @@ -150,12 +151,14 @@ func (c *Collector) collectDynamicMemoryVM(ch chan<- prometheus.Metric) error { data.Name, ) - ch <- prometheus.MustNewConstMetric( - c.vmMemoryGuestAvailableMemory, - prometheus.GaugeValue, - utils.MBToBytes(data.VmMemoryGuestAvailableMemory), - data.Name, - ) + if osversion.Build() >= osversion.LTSC2019 { + ch <- prometheus.MustNewConstMetric( + c.vmMemoryGuestAvailableMemory, + prometheus.GaugeValue, + utils.MBToBytes(data.VmMemoryGuestAvailableMemory), + data.Name, + ) + } ch <- prometheus.MustNewConstMetric( c.vmMemoryGuestVisiblePhysicalMemory, diff --git a/internal/collector/hyperv/hyperv_hypervisor_virtual_processor.go b/internal/collector/hyperv/hyperv_hypervisor_virtual_processor.go index 78ef1561..32f74d19 100644 --- a/internal/collector/hyperv/hyperv_hypervisor_virtual_processor.go +++ b/internal/collector/hyperv/hyperv_hypervisor_virtual_processor.go @@ -40,7 +40,7 @@ type collectorHypervisorVirtualProcessor struct { type perfDataCounterValuesHypervisorVirtualProcessor struct { Name string - HypervisorVirtualProcessorGuestIdleTimePercent float64 `perfdata:"% Guest Idle Time"` + HypervisorVirtualProcessorGuestRunTimePercent float64 `perfdata:"% Guest Run Time"` HypervisorVirtualProcessorHypervisorRunTimePercent float64 `perfdata:"% Hypervisor Run Time"` HypervisorVirtualProcessorTotalRunTimePercent float64 `perfdata:"% Total Run Time"` HypervisorVirtualProcessorRemoteRunTimePercent float64 `perfdata:"% Remote Run Time"` @@ -108,15 +108,15 @@ func (c *Collector) collectHypervisorVirtualProcessor(ch chan<- prometheus.Metri ch <- prometheus.MustNewConstMetric( c.hypervisorVirtualProcessorTimeTotal, prometheus.CounterValue, - data.HypervisorVirtualProcessorGuestIdleTimePercent, - vmName, coreID, "guest_idle", + data.HypervisorVirtualProcessorGuestRunTimePercent, + vmName, coreID, "guest", ) ch <- prometheus.MustNewConstMetric( c.hypervisorVirtualProcessorTimeTotal, prometheus.CounterValue, - data.HypervisorVirtualProcessorGuestIdleTimePercent, - vmName, coreID, "guest_idle", + data.HypervisorVirtualProcessorRemoteRunTimePercent, + vmName, coreID, "remote", ) ch <- prometheus.MustNewConstMetric(