From 44c39405c79fd753a710b9b8295df23f7aefa282 Mon Sep 17 00:00:00 2001 From: vbeausoleil Date: Thu, 10 Aug 2017 18:31:39 -0400 Subject: [PATCH] Added flag to filter services (like the one for processes). Added example for said flag in README.md (#109) --- README.md | 8 ++++++++ collector/service.go | 20 +++++++++++++++++++- 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c8b263871..3b3884b97 100644 --- a/README.md +++ b/README.md @@ -60,6 +60,14 @@ See [open issues](https://github.com/martinlindhe/wmi_exporter/issues) The prometheus metrics will be exposed on [localhost:9182](http://localhost:9182) +## Examples + +Please note: The quotes in the parameter names are required because of how Powershell parses command line arguments. + +### Enable only service collector and specify a custom query + + .\wmi_exporter.exe "-collectors.enabled" "service" "-collector.service.services-where" "Name='wmi_exporter'" + ## License diff --git a/collector/service.go b/collector/service.go index d4fcdc480..4f7317637 100644 --- a/collector/service.go +++ b/collector/service.go @@ -3,6 +3,8 @@ package collector import ( + "bytes" + "flag" "log" "strings" @@ -14,15 +16,30 @@ func init() { Factories["service"] = NewserviceCollector } +var ( + serviceWhereClause = flag.String("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.") +) + // A serviceCollector is a Prometheus collector for WMI Win32_Service metrics type serviceCollector struct { State *prometheus.Desc StartMode *prometheus.Desc + + queryWhereClause string } // NewserviceCollector ... func NewserviceCollector() (Collector, error) { const subsystem = "service" + + var wc bytes.Buffer + if *serviceWhereClause != "" { + wc.WriteString("WHERE ") + wc.WriteString(*serviceWhereClause) + } else { + log.Println("warning: No where-clause specified for process collector. This will generate a very large number of metrics!") + } + return &serviceCollector{ State: prometheus.NewDesc( prometheus.BuildFQName(Namespace, subsystem, "state"), @@ -36,6 +53,7 @@ func NewserviceCollector() (Collector, error) { []string{"name", "start_mode"}, nil, ), + queryWhereClause: wc.String(), }, nil } @@ -77,7 +95,7 @@ var ( func (c *serviceCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, error) { var dst []Win32_Service - q := wmi.CreateQuery(&dst, "") + q := wmi.CreateQuery(&dst, c.queryWhereClause) if err := wmi.Query(q, &dst); err != nil { return nil, err }