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:
Steve Zook 2018-08-07 15:42:16 -04:00
parent 832771b4a2
commit c156f2bcbe
9 changed files with 1110 additions and 1060 deletions

View File

@ -3,6 +3,8 @@
package collector
import (
"errors"
"github.com/StackExchange/wmi"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
@ -616,6 +618,9 @@ func (c *ADCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, er
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.AddressBookOperationsTotal,

View File

@ -4,6 +4,8 @@
package collector
import (
"errors"
"github.com/StackExchange/wmi"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
@ -60,6 +62,9 @@ func (c *CSCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, er
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.LogicalProcessors,

View File

@ -4,6 +4,8 @@
package collector
import (
"errors"
"github.com/StackExchange/wmi"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
@ -237,6 +239,9 @@ func (c *DNSCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, e
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.ZoneTransferRequestsReceived,

View File

@ -7,6 +7,7 @@
package collector
import (
"errors"
"fmt"
"regexp"
@ -1738,216 +1739,218 @@ func (c *IISCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, e
return nil, err
}
if len(dst_cache) > 0 {
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_ActiveFlushedEntries,
prometheus.GaugeValue,
float64(dst_cache[0].ActiveFlushedEntries),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_FileCacheMemoryUsage,
prometheus.GaugeValue,
float64(dst_cache[0].CurrentFileCacheMemoryUsage),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_MaximumFileCacheMemoryUsage,
prometheus.CounterValue,
float64(dst_cache[0].MaximumFileCacheMemoryUsage),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_FileCacheFlushesTotal,
prometheus.CounterValue,
float64(dst_cache[0].TotalFlushedFiles),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_FileCacheQueriesTotal,
prometheus.CounterValue,
float64(dst_cache[0].FileCacheHits+dst_cache[0].FileCacheMisses),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_FileCacheHitsTotal,
prometheus.CounterValue,
float64(dst_cache[0].FileCacheHits),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_FilesCached,
prometheus.GaugeValue,
float64(dst_cache[0].CurrentFilesCached),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_FilesCachedTotal,
prometheus.CounterValue,
float64(dst_cache[0].TotalFilesCached),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_FilesFlushedTotal,
prometheus.CounterValue,
float64(dst_cache[0].TotalFlushedFiles),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_URICacheFlushesTotal,
prometheus.CounterValue,
float64(dst_cache[0].TotalFlushedURIs),
"user",
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_URICacheFlushesTotal,
prometheus.CounterValue,
float64(dst_cache[0].KernelTotalFlushedURIs),
"kernel",
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_URICacheQueriesTotal,
prometheus.CounterValue,
float64(dst_cache[0].URICacheHits+dst_cache[0].URICacheMisses),
"user",
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_URICacheQueriesTotal,
prometheus.CounterValue,
float64(dst_cache[0].KernelURICacheHits+dst_cache[0].KernelURICacheMisses),
"kernel",
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_URICacheHitsTotal,
prometheus.CounterValue,
float64(dst_cache[0].URICacheHits),
"user",
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_URICacheHitsTotal,
prometheus.CounterValue,
float64(dst_cache[0].KernelURICacheHits),
"kernel",
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_URIsCached,
prometheus.GaugeValue,
float64(dst_cache[0].CurrentURIsCached),
"user",
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_URIsCached,
prometheus.GaugeValue,
float64(dst_cache[0].KernelCurrentURIsCached),
"kernel",
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_URIsCachedTotal,
prometheus.CounterValue,
float64(dst_cache[0].TotalURIsCached),
"user",
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_URIsCachedTotal,
prometheus.CounterValue,
float64(dst_cache[0].KernelTotalURIsCached),
"kernel",
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_URIsFlushedTotal,
prometheus.CounterValue,
float64(dst_cache[0].TotalFlushedURIs),
"user",
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_URIsFlushedTotal,
prometheus.CounterValue,
float64(dst_cache[0].KernelTotalFlushedURIs),
"kernel",
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_MetadataCached,
prometheus.GaugeValue,
float64(dst_cache[0].CurrentMetadataCached),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_MetadataCacheFlushes,
prometheus.CounterValue,
float64(dst_cache[0].TotalFlushedMetadata),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_MetadataCacheQueriesTotal,
prometheus.CounterValue,
float64(dst_cache[0].MetadataCacheHits+dst_cache[0].MetadataCacheMisses),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_MetadataCacheHitsTotal,
prometheus.CounterValue,
float64(dst_cache[0].MetadataCacheHits),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_MetadataCachedTotal,
prometheus.CounterValue,
float64(dst_cache[0].TotalMetadataCached),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_MetadataFlushedTotal,
prometheus.CounterValue,
float64(dst_cache[0].TotalFlushedMetadata),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_OutputCacheActiveFlushedItems,
prometheus.CounterValue,
float64(dst_cache[0].OutputCacheCurrentFlushedItems),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_OutputCacheItems,
prometheus.CounterValue,
float64(dst_cache[0].OutputCacheCurrentItems),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_OutputCacheMemoryUsage,
prometheus.CounterValue,
float64(dst_cache[0].OutputCacheCurrentMemoryUsage),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_OutputCacheQueriesTotal,
prometheus.CounterValue,
float64(dst_cache[0].OutputCacheTotalHits+dst_cache[0].OutputCacheTotalMisses),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_OutputCacheHitsTotal,
prometheus.CounterValue,
float64(dst_cache[0].OutputCacheTotalHits),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_OutputCacheFlushedItemsTotal,
prometheus.CounterValue,
float64(dst_cache[0].OutputCacheTotalFlushedItems),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_OutputCacheFlushesTotal,
prometheus.CounterValue,
float64(dst_cache[0].OutputCacheTotalFlushes),
)
if len(dst_cache) == 0 {
return nil, errors.New("WMI query returned empty result set")
}
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_ActiveFlushedEntries,
prometheus.GaugeValue,
float64(dst_cache[0].ActiveFlushedEntries),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_FileCacheMemoryUsage,
prometheus.GaugeValue,
float64(dst_cache[0].CurrentFileCacheMemoryUsage),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_MaximumFileCacheMemoryUsage,
prometheus.CounterValue,
float64(dst_cache[0].MaximumFileCacheMemoryUsage),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_FileCacheFlushesTotal,
prometheus.CounterValue,
float64(dst_cache[0].TotalFlushedFiles),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_FileCacheQueriesTotal,
prometheus.CounterValue,
float64(dst_cache[0].FileCacheHits+dst_cache[0].FileCacheMisses),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_FileCacheHitsTotal,
prometheus.CounterValue,
float64(dst_cache[0].FileCacheHits),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_FilesCached,
prometheus.GaugeValue,
float64(dst_cache[0].CurrentFilesCached),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_FilesCachedTotal,
prometheus.CounterValue,
float64(dst_cache[0].TotalFilesCached),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_FilesFlushedTotal,
prometheus.CounterValue,
float64(dst_cache[0].TotalFlushedFiles),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_URICacheFlushesTotal,
prometheus.CounterValue,
float64(dst_cache[0].TotalFlushedURIs),
"user",
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_URICacheFlushesTotal,
prometheus.CounterValue,
float64(dst_cache[0].KernelTotalFlushedURIs),
"kernel",
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_URICacheQueriesTotal,
prometheus.CounterValue,
float64(dst_cache[0].URICacheHits+dst_cache[0].URICacheMisses),
"user",
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_URICacheQueriesTotal,
prometheus.CounterValue,
float64(dst_cache[0].KernelURICacheHits+dst_cache[0].KernelURICacheMisses),
"kernel",
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_URICacheHitsTotal,
prometheus.CounterValue,
float64(dst_cache[0].URICacheHits),
"user",
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_URICacheHitsTotal,
prometheus.CounterValue,
float64(dst_cache[0].KernelURICacheHits),
"kernel",
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_URIsCached,
prometheus.GaugeValue,
float64(dst_cache[0].CurrentURIsCached),
"user",
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_URIsCached,
prometheus.GaugeValue,
float64(dst_cache[0].KernelCurrentURIsCached),
"kernel",
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_URIsCachedTotal,
prometheus.CounterValue,
float64(dst_cache[0].TotalURIsCached),
"user",
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_URIsCachedTotal,
prometheus.CounterValue,
float64(dst_cache[0].KernelTotalURIsCached),
"kernel",
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_URIsFlushedTotal,
prometheus.CounterValue,
float64(dst_cache[0].TotalFlushedURIs),
"user",
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_URIsFlushedTotal,
prometheus.CounterValue,
float64(dst_cache[0].KernelTotalFlushedURIs),
"kernel",
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_MetadataCached,
prometheus.GaugeValue,
float64(dst_cache[0].CurrentMetadataCached),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_MetadataCacheFlushes,
prometheus.CounterValue,
float64(dst_cache[0].TotalFlushedMetadata),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_MetadataCacheQueriesTotal,
prometheus.CounterValue,
float64(dst_cache[0].MetadataCacheHits+dst_cache[0].MetadataCacheMisses),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_MetadataCacheHitsTotal,
prometheus.CounterValue,
float64(dst_cache[0].MetadataCacheHits),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_MetadataCachedTotal,
prometheus.CounterValue,
float64(dst_cache[0].TotalMetadataCached),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_MetadataFlushedTotal,
prometheus.CounterValue,
float64(dst_cache[0].TotalFlushedMetadata),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_OutputCacheActiveFlushedItems,
prometheus.CounterValue,
float64(dst_cache[0].OutputCacheCurrentFlushedItems),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_OutputCacheItems,
prometheus.CounterValue,
float64(dst_cache[0].OutputCacheCurrentItems),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_OutputCacheMemoryUsage,
prometheus.CounterValue,
float64(dst_cache[0].OutputCacheCurrentMemoryUsage),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_OutputCacheQueriesTotal,
prometheus.CounterValue,
float64(dst_cache[0].OutputCacheTotalHits+dst_cache[0].OutputCacheTotalMisses),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_OutputCacheHitsTotal,
prometheus.CounterValue,
float64(dst_cache[0].OutputCacheTotalHits),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_OutputCacheFlushedItemsTotal,
prometheus.CounterValue,
float64(dst_cache[0].OutputCacheTotalFlushedItems),
)
ch <- prometheus.MustNewConstMetric(
c.ServiceCache_OutputCacheFlushesTotal,
prometheus.CounterValue,
float64(dst_cache[0].OutputCacheTotalFlushes),
)
return nil, nil
}

File diff suppressed because it is too large Load Diff

View File

@ -4,6 +4,7 @@
package collector
import (
"errors"
"time"
"github.com/StackExchange/wmi"
@ -142,6 +143,10 @@ func (c *OSCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, er
return nil, err
}
if len(dst) == 0 {
return nil, errors.New("WMI query returned empty result set")
}
ch <- prometheus.MustNewConstMetric(
c.PhysicalMemoryFreeBytes,
prometheus.GaugeValue,

View File

@ -4,6 +4,7 @@
package collector
import (
"errors"
"github.com/StackExchange/wmi"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
@ -94,6 +95,9 @@ func (c *SystemCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc
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.ContextSwitchesTotal,

View File

@ -5,6 +5,7 @@
package collector
import (
"errors"
"github.com/StackExchange/wmi"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
@ -118,6 +119,9 @@ func (c *TCPCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, e
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")
}
// Counters
ch <- prometheus.MustNewConstMetric(

View File

@ -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,