mirror of
https://github.com/prometheus-community/windows_exporter.git
synced 2026-02-26 06:36:36 +00:00
protect against emtpy wmi query result sets
If an WMI query were to return an empty result set, trying to access the first element in the result set would result in a `panic: runtime error: index out of range` error. add logic to explicitly check if the result set size is 0 and return an error rather. Fixes https://github.com/martinlindhe/wmi_exporter/issues/240
This commit is contained in:
@@ -2,6 +2,8 @@
|
||||
package collector
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/StackExchange/wmi"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/common/log"
|
||||
@@ -201,6 +203,9 @@ func (c *VmwareCollector) collectMem(ch chan<- prometheus.Metric) (*prometheus.D
|
||||
if err := wmi.Query(q, &dst); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(dst) == 0 {
|
||||
return nil, errors.New("WMI query returned empty result set")
|
||||
}
|
||||
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.MemActive,
|
||||
@@ -287,6 +292,9 @@ func (c *VmwareCollector) collectCpu(ch chan<- prometheus.Metric) (*prometheus.D
|
||||
if err := wmi.Query(q, &dst); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(dst) == 0 {
|
||||
return nil, errors.New("WMI query returned empty result set")
|
||||
}
|
||||
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
c.CpuLimitMHz,
|
||||
|
||||
Reference in New Issue
Block a user