Skip to content

Commit

Permalink
Added flag to filter services (like the one for processes). Added exa…
Browse files Browse the repository at this point in the history
…mple for said flag in README.md (#109)
  • Loading branch information
vbeausoleil authored and martinlindhe committed Aug 10, 2017
1 parent 96faedf commit 44c3940
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 19 additions & 1 deletion collector/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package collector

import (
"bytes"
"flag"
"log"
"strings"

Expand All @@ -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"),
Expand All @@ -36,6 +53,7 @@ func NewserviceCollector() (Collector, error) {
[]string{"name", "start_mode"},
nil,
),
queryWhereClause: wc.String(),
}, nil
}

Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 44c3940

Please sign in to comment.