Add flag to specify "where" in process WMI query

This flag can be used to limit the Win32_PerfRawData_PerfProc_Process
response and pull metrics for only the desired processes.
This commit is contained in:
Andrew Mostello 2017-05-28 22:04:31 -04:00
parent 2c84c5ad8a
commit 95b04ec0a1
1 changed files with 19 additions and 1 deletions

View File

@ -3,6 +3,8 @@
package collector
import (
"bytes"
"flag"
"log"
"strconv"
@ -14,6 +16,10 @@ func init() {
Factories["process"] = NewProcessCollector
}
var (
processWhereClause = flag.String("collector.process.processes-where", "", "WQL 'where' clause to use in WMI metrics query. Limits the response to the processes you specify and reduces the size of the response.")
)
// A ProcessCollector is a Prometheus collector for WMI Win32_PerfRawData_PerfProc_Process metrics
type ProcessCollector struct {
StartTime *prometheus.Desc
@ -29,6 +35,8 @@ type ProcessCollector struct {
ThreadCount *prometheus.Desc
VirtualBytes *prometheus.Desc
WorkingSet *prometheus.Desc
queryWhereClause string
}
// NewProcessCollector ...
@ -113,6 +121,7 @@ func NewProcessCollector() (Collector, error) {
[]string{"process", "process_id", "creating_process_id"},
nil,
),
queryWhereClause: *processWhereClause,
}, nil
}
@ -162,7 +171,16 @@ type Win32_PerfRawData_PerfProc_Process struct {
func (c *ProcessCollector) collect(ch chan<- prometheus.Metric) (*prometheus.Desc, error) {
var dst []Win32_PerfRawData_PerfProc_Process
q := wmi.CreateQuery(&dst, "")
var wc bytes.Buffer
if c.queryWhereClause != "" {
wc.WriteString("WHERE ")
wc.WriteString(c.queryWhereClause)
}
q := wmi.CreateQuery(&dst, wc.String())
if err := wmi.Query(q, &dst); err != nil {
return nil, err
}