Skip to content

Commit

Permalink
protect against emtpy wmi query result sets
Browse files Browse the repository at this point in the history
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 #240
  • Loading branch information
Steve Zook committed Aug 7, 2018
1 parent 832771b commit c156f2b
Show file tree
Hide file tree
Showing 9 changed files with 994 additions and 944 deletions.
5 changes: 5 additions & 0 deletions collector/ad.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package collector

import (
"errors"

"github.com/StackExchange/wmi"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions collector/cs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package collector

import (
"errors"

"github.com/StackExchange/wmi"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions collector/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package collector

import (
"errors"

"github.com/StackExchange/wmi"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/log"
Expand Down Expand Up @@ -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,
Expand Down
Loading

0 comments on commit c156f2b

Please sign in to comment.