Skip to content

Commit

Permalink
Merge pull request prometheus-community#971 from andbanman/ft_hyperv_…
Browse files Browse the repository at this point in the history
…vm_memory

Add Hyper-V VM Memory metrics
  • Loading branch information
breed808 authored May 12, 2022
2 parents 2d888eb + 4a1a7d2 commit 7852734
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 0 deletions.
181 changes: 181 additions & 0 deletions collector/hyperv.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,18 @@ type HyperVCollector struct {
VMNetworkDroppedPacketsOutgoing *prometheus.Desc
VMNetworkPacketsReceived *prometheus.Desc
VMNetworkPacketsSent *prometheus.Desc

// Win32_PerfRawData_BalancerStats_HyperVDynamicMemoryVM
VMMemoryAddedMemory *prometheus.Desc
VMMemoryAveragePressure *prometheus.Desc
VMMemoryCurrentPressure *prometheus.Desc
VMMemoryGuestVisiblePhysicalMemory *prometheus.Desc
VMMemoryMaximumPressure *prometheus.Desc
VMMemoryMemoryAddOperations *prometheus.Desc
VMMemoryMemoryRemoveOperations *prometheus.Desc
VMMemoryMinimumPressure *prometheus.Desc
VMMemoryPhysicalMemory *prometheus.Desc
VMMemoryRemovedMemory *prometheus.Desc
}

// NewHyperVCollector ...
Expand Down Expand Up @@ -593,6 +605,69 @@ func NewHyperVCollector() (Collector, error) {
[]string{"vm_interface"},
nil,
),

//

VMMemoryAddedMemory: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, buildSubsystemName("vm_memory"), "added_total"),
"This counter represents memory in MB added to the VM",
[]string{"vm"},
nil,
),
VMMemoryAveragePressure: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, buildSubsystemName("vm_memory"), "pressure_average"),
"This gauge represents the average pressure in the VM.",
[]string{"vm"},
nil,
),
VMMemoryCurrentPressure: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, buildSubsystemName("vm_memory"), "pressure_current"),
"This gauge represents the current pressure in the VM.",
[]string{"vm"},
nil,
),
VMMemoryGuestVisiblePhysicalMemory: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, buildSubsystemName("vm_memory"), "physical_guest_visible"),
"'This gauge represents the amount of memory in MB visible to the VM guest.'",
[]string{"vm"},
nil,
),
VMMemoryMaximumPressure: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, buildSubsystemName("vm_memory"), "pressure_maximum"),
"This gauge represents the maximum pressure band in the VM.",
[]string{"vm"},
nil,
),
VMMemoryMemoryAddOperations: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, buildSubsystemName("vm_memory"), "add_operations_total"),
"This counter represents the number of operations adding memory to the VM.",
[]string{"vm"},
nil,
),
VMMemoryMemoryRemoveOperations: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, buildSubsystemName("vm_memory"), "remove_operations_total"),
"This counter represents the number of operations removing memory from the VM.",
[]string{"vm"},
nil,
),
VMMemoryMinimumPressure: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, buildSubsystemName("vm_memory"), "pressure_minimum"),
"This gauge represents the minimum pressure band in the VM.",
[]string{"vm"},
nil,
),
VMMemoryPhysicalMemory: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, buildSubsystemName("vm_memory"), "physical"),
"This gauge represents the current amount of memory in MB assigned to the VM.",
[]string{"vm"},
nil,
),
VMMemoryRemovedMemory: prometheus.NewDesc(
prometheus.BuildFQName(Namespace, buildSubsystemName("vm_memory"), "removed_total"),
"This counter represents memory in MB removed from the VM",
[]string{"vm"},
nil,
),
}, nil
}

Expand Down Expand Up @@ -649,6 +724,11 @@ func (c *HyperVCollector) Collect(ctx *ScrapeContext, ch chan<- prometheus.Metri
return err
}

if desc, err := c.collectVmMemory(ch); err != nil {
log.Error("failed collecting hyperV virtual memory metrics:", desc, err)
return err
}

return nil
}

Expand Down Expand Up @@ -1431,3 +1511,104 @@ func (c *HyperVCollector) collectVmNetwork(ch chan<- prometheus.Metric) (*promet

return nil, nil
}

// Win32_PerfRawData_BalancerStats_HyperVDynamicMemoryVM ...
type Win32_PerfRawData_BalancerStats_HyperVDynamicMemoryVM struct {
Name string
AddedMemory uint64
AveragePressure uint64
CurrentPressure uint64
GuestVisiblePhysicalMemory uint64
MaximumPressure uint64
MemoryAddOperations uint64
MemoryRemoveOperations uint64
MinimumPressure uint64
PhysicalMemory uint64
RemovedMemory uint64
}

func (c *HyperVCollector) collectVmMemory(ch chan<- prometheus.Metric) (*prometheus.Desc, error) {
var dst []Win32_PerfRawData_BalancerStats_HyperVDynamicMemoryVM
q := queryAll(&dst)
if err := wmi.Query(q, &dst); err != nil {
return nil, err
}

for _, obj := range dst {
if strings.Contains(obj.Name, "_Total") {
continue
}

ch <- prometheus.MustNewConstMetric(
c.VMMemoryAddedMemory,
prometheus.CounterValue,
float64(obj.AddedMemory),
obj.Name,
)

ch <- prometheus.MustNewConstMetric(
c.VMMemoryAveragePressure,
prometheus.GaugeValue,
float64(obj.AveragePressure),
obj.Name,
)

ch <- prometheus.MustNewConstMetric(
c.VMMemoryCurrentPressure,
prometheus.GaugeValue,
float64(obj.CurrentPressure),
obj.Name,
)

ch <- prometheus.MustNewConstMetric(
c.VMMemoryGuestVisiblePhysicalMemory,
prometheus.GaugeValue,
float64(obj.GuestVisiblePhysicalMemory),
obj.Name,
)

ch <- prometheus.MustNewConstMetric(
c.VMMemoryMaximumPressure,
prometheus.GaugeValue,
float64(obj.MaximumPressure),
obj.Name,
)

ch <- prometheus.MustNewConstMetric(
c.VMMemoryMemoryAddOperations,
prometheus.CounterValue,
float64(obj.MemoryAddOperations),
obj.Name,
)

ch <- prometheus.MustNewConstMetric(
c.VMMemoryMemoryRemoveOperations,
prometheus.CounterValue,
float64(obj.MemoryRemoveOperations),
obj.Name,
)

ch <- prometheus.MustNewConstMetric(
c.VMMemoryMinimumPressure,
prometheus.GaugeValue,
float64(obj.MinimumPressure),
obj.Name,
)

ch <- prometheus.MustNewConstMetric(
c.VMMemoryPhysicalMemory,
prometheus.GaugeValue,
float64(obj.PhysicalMemory),
obj.Name,
)

ch <- prometheus.MustNewConstMetric(
c.VMMemoryRemovedMemory,
prometheus.CounterValue,
float64(obj.RemovedMemory),
obj.Name,
)
}

return nil, nil
}
10 changes: 10 additions & 0 deletions docs/collector.hyperv.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ Name | Description | Type | Labels
`windows_hyperv_vm_cpu_guest_run_time` | _Not yet documented_ | counter | `vm`, `core`
`windows_hyperv_vm_cpu_hypervisor_run_time` | _Not yet documented_ | counter | `vm`, `core`
`windows_hyperv_vm_cpu_remote_run_time` | _Not yet documented_ | counter | `vm`, `core`
`windows_hyperv_vm_memory_added_total` | _Not yet documented_ | counter | `vm`
`windows_hyperv_vm_memory_pressure_average` | _Not yet documented_ | gauge | `vm`
`windows_hyperv_vm_memory_pressure_current` | _Not yet documented_ | counter | `vm`
`windows_hyperv_vm_memory_physical_guest_visible` | _Not yet documented_ | gauge | `vm`
`windows_hyperv_vm_memory_pressure_maximum` | _Not yet documented_ | gauge | `vm`
`windows_hyperv_vm_memory_add_operations_total` | _Not yet documented_ | counter | `vm`
`windows_hyperv_vm_memory_remove_operations_total` | _Not yet documented_ | counter | `vm`
`windows_hyperv_vm_memory_pressure_minumim` | _Not yet documented_ | gauge | `vm`
`windows_hyperv_vm_memory_physical` | _Not yet documented_ | gauge | `vm`
`windows_hyperv_vm_memory_removed_total` | _Not yet documented_ | counter | `vm`
`windows_hyperv_vm_cpu_total_run_time` | _Not yet documented_ | counter | `vm`, `core`
`windows_hyperv_vswitch_broadcast_packets_received_total` | _Not yet documented_ | counter | `vswitch`
`windows_hyperv_vswitch_broadcast_packets_sent_total` | _Not yet documented_ | counter | `vswitch`
Expand Down

0 comments on commit 7852734

Please sign in to comment.