Always expose all possible service states/start modes

This commit is contained in:
Calle Pettersson 2017-03-04 10:38:40 +01:00
parent a05febe069
commit f5365c96f6
1 changed files with 46 additions and 14 deletions

View File

@ -55,6 +55,26 @@ type Win32_Service struct {
StartMode string StartMode string
} }
var (
allStates = []string{
"stopped",
"start pending",
"stop pending",
"running",
"continue pending",
"pause pending",
"paused",
"unknown",
}
allStartModes = []string{
"boot",
"system",
"auto",
"manual",
"disabled",
}
)
func (c *serviceCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, error) { func (c *serviceCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, error) {
var dst []Win32_Service var dst []Win32_Service
q := wmi.CreateQuery(&dst, "") q := wmi.CreateQuery(&dst, "")
@ -63,21 +83,33 @@ func (c *serviceCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Des
} }
for _, service := range dst { for _, service := range dst {
for _, state := range allStates {
isCurrentState := 0.0
if state == strings.ToLower(service.State) {
isCurrentState = 1.0
}
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.State, c.State,
prometheus.GaugeValue, prometheus.GaugeValue,
1.0, isCurrentState,
strings.ToLower(service.Name), strings.ToLower(service.Name),
strings.ToLower(service.State), state,
) )
}
for _, startMode := range allStartModes {
isCurrentStartMode := 0.0
if startMode == strings.ToLower(service.StartMode) {
isCurrentStartMode = 1.0
}
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
c.StartMode, c.StartMode,
prometheus.GaugeValue, prometheus.GaugeValue,
1.0, isCurrentStartMode,
strings.ToLower(service.Name), strings.ToLower(service.Name),
strings.ToLower(service.StartMode), startMode,
) )
} }
}
return nil, nil return nil, nil
} }