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 {
ch <- prometheus.MustNewConstMetric( for _, state := range allStates {
c.State, isCurrentState := 0.0
prometheus.GaugeValue, if state == strings.ToLower(service.State) {
1.0, isCurrentState = 1.0
strings.ToLower(service.Name), }
strings.ToLower(service.State), ch <- prometheus.MustNewConstMetric(
) c.State,
prometheus.GaugeValue,
isCurrentState,
strings.ToLower(service.Name),
state,
)
}
ch <- prometheus.MustNewConstMetric( for _, startMode := range allStartModes {
c.StartMode, isCurrentStartMode := 0.0
prometheus.GaugeValue, if startMode == strings.ToLower(service.StartMode) {
1.0, isCurrentStartMode = 1.0
strings.ToLower(service.Name), }
strings.ToLower(service.StartMode), ch <- prometheus.MustNewConstMetric(
) c.StartMode,
prometheus.GaugeValue,
isCurrentStartMode,
strings.ToLower(service.Name),
startMode,
)
}
} }
return nil, nil return nil, nil
} }