2017-03-03 13:51:24 +00:00
|
|
|
// returns data points from Win32_Service
|
|
|
|
// https://msdn.microsoft.com/en-us/library/aa394418(v=vs.85).aspx - Win32_Service class
|
|
|
|
package collector
|
|
|
|
|
|
|
|
import (
|
2017-08-10 22:31:39 +00:00
|
|
|
"bytes"
|
2017-03-03 13:51:24 +00:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/StackExchange/wmi"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2018-04-05 05:27:26 +00:00
|
|
|
"github.com/prometheus/common/log"
|
2017-08-12 06:44:59 +00:00
|
|
|
"gopkg.in/alecthomas/kingpin.v2"
|
2017-03-03 13:51:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Factories["service"] = NewserviceCollector
|
|
|
|
}
|
|
|
|
|
2017-08-10 22:31:39 +00:00
|
|
|
var (
|
2017-08-12 06:44:59 +00:00
|
|
|
serviceWhereClause = kingpin.Flag(
|
|
|
|
"collector.service.services-where",
|
|
|
|
"WQL 'where' clause to use in WMI metrics query. Limits the response to the services you specify and reduces the size of the response.",
|
|
|
|
).Default("").String()
|
2017-08-10 22:31:39 +00:00
|
|
|
)
|
|
|
|
|
2017-03-03 13:51:24 +00:00
|
|
|
// A serviceCollector is a Prometheus collector for WMI Win32_Service metrics
|
|
|
|
type serviceCollector struct {
|
|
|
|
State *prometheus.Desc
|
|
|
|
StartMode *prometheus.Desc
|
2017-08-10 22:31:39 +00:00
|
|
|
|
|
|
|
queryWhereClause string
|
2017-03-03 13:51:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewserviceCollector ...
|
|
|
|
func NewserviceCollector() (Collector, error) {
|
|
|
|
const subsystem = "service"
|
2017-08-10 22:31:39 +00:00
|
|
|
|
|
|
|
var wc bytes.Buffer
|
|
|
|
if *serviceWhereClause != "" {
|
|
|
|
wc.WriteString("WHERE ")
|
|
|
|
wc.WriteString(*serviceWhereClause)
|
|
|
|
} else {
|
2018-04-05 05:27:26 +00:00
|
|
|
log.Warn("No where-clause specified for service collector. This will generate a very large number of metrics!")
|
2017-08-10 22:31:39 +00:00
|
|
|
}
|
|
|
|
|
2017-03-03 13:51:24 +00:00
|
|
|
return &serviceCollector{
|
|
|
|
State: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "state"),
|
|
|
|
"The state of the service (State)",
|
|
|
|
[]string{"name", "state"},
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
StartMode: prometheus.NewDesc(
|
|
|
|
prometheus.BuildFQName(Namespace, subsystem, "start_mode"),
|
|
|
|
"The start mode of the service (StartMode)",
|
|
|
|
[]string{"name", "start_mode"},
|
|
|
|
nil,
|
|
|
|
),
|
2017-08-10 22:31:39 +00:00
|
|
|
queryWhereClause: wc.String(),
|
2017-03-03 13:51:24 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect sends the metric values for each metric
|
|
|
|
// to the provided prometheus Metric channel.
|
|
|
|
func (c *serviceCollector) Collect(ch chan<- prometheus.Metric) error {
|
|
|
|
if desc, err := c.collect(ch); err != nil {
|
2018-04-05 05:27:26 +00:00
|
|
|
log.Error("failed collecting service metrics:", desc, err)
|
2017-03-03 13:51:24 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Win32_Service struct {
|
|
|
|
Name string
|
|
|
|
State string
|
|
|
|
StartMode string
|
|
|
|
}
|
|
|
|
|
2017-03-04 09:38:40 +00:00
|
|
|
var (
|
|
|
|
allStates = []string{
|
|
|
|
"stopped",
|
|
|
|
"start pending",
|
|
|
|
"stop pending",
|
|
|
|
"running",
|
|
|
|
"continue pending",
|
|
|
|
"pause pending",
|
|
|
|
"paused",
|
|
|
|
"unknown",
|
|
|
|
}
|
|
|
|
allStartModes = []string{
|
|
|
|
"boot",
|
|
|
|
"system",
|
|
|
|
"auto",
|
|
|
|
"manual",
|
|
|
|
"disabled",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2017-03-03 13:51:24 +00:00
|
|
|
func (c *serviceCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, error) {
|
|
|
|
var dst []Win32_Service
|
2017-08-10 22:31:39 +00:00
|
|
|
q := wmi.CreateQuery(&dst, c.queryWhereClause)
|
2017-03-03 13:51:24 +00:00
|
|
|
if err := wmi.Query(q, &dst); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, service := range dst {
|
2017-03-04 09:38:40 +00:00
|
|
|
for _, state := range allStates {
|
|
|
|
isCurrentState := 0.0
|
|
|
|
if state == strings.ToLower(service.State) {
|
|
|
|
isCurrentState = 1.0
|
|
|
|
}
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.State,
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
isCurrentState,
|
|
|
|
strings.ToLower(service.Name),
|
|
|
|
state,
|
|
|
|
)
|
|
|
|
}
|
2017-03-03 13:51:24 +00:00
|
|
|
|
2017-03-04 09:38:40 +00:00
|
|
|
for _, startMode := range allStartModes {
|
|
|
|
isCurrentStartMode := 0.0
|
|
|
|
if startMode == strings.ToLower(service.StartMode) {
|
|
|
|
isCurrentStartMode = 1.0
|
|
|
|
}
|
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
c.StartMode,
|
|
|
|
prometheus.GaugeValue,
|
|
|
|
isCurrentStartMode,
|
|
|
|
strings.ToLower(service.Name),
|
|
|
|
startMode,
|
|
|
|
)
|
|
|
|
}
|
2017-03-03 13:51:24 +00:00
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|