2016-09-01 12:55:35 +00:00
|
|
|
package collector
|
2016-08-26 06:59:27 +00:00
|
|
|
|
2017-07-15 12:56:09 +00:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"reflect"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2018-06-06 08:31:50 +00:00
|
|
|
"github.com/prometheus/common/log"
|
2017-07-15 12:56:09 +00:00
|
|
|
)
|
2016-09-01 14:04:43 +00:00
|
|
|
|
|
|
|
// ...
|
2016-08-26 06:59:27 +00:00
|
|
|
const (
|
2016-09-01 14:04:43 +00:00
|
|
|
Namespace = "wmi"
|
2016-09-17 15:11:35 +00:00
|
|
|
|
|
|
|
// Conversion factors
|
|
|
|
ticksToSecondsScaleFactor = 1 / 1e7
|
2016-08-26 06:59:27 +00:00
|
|
|
)
|
2016-09-01 14:04:43 +00:00
|
|
|
|
|
|
|
// Factories ...
|
|
|
|
var Factories = make(map[string]func() (Collector, error))
|
|
|
|
|
|
|
|
// Collector is the interface a collector has to implement.
|
|
|
|
type Collector interface {
|
|
|
|
// Get new metrics and expose them via prometheus registry.
|
|
|
|
Collect(ch chan<- prometheus.Metric) (err error)
|
|
|
|
}
|
2017-07-15 12:56:09 +00:00
|
|
|
|
2018-06-06 08:31:50 +00:00
|
|
|
func className(src interface{}) string {
|
2017-07-15 12:56:09 +00:00
|
|
|
s := reflect.Indirect(reflect.ValueOf(src))
|
|
|
|
t := s.Type()
|
|
|
|
if s.Kind() == reflect.Slice {
|
|
|
|
t = t.Elem()
|
|
|
|
}
|
2018-06-06 08:31:50 +00:00
|
|
|
return t.Name()
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryAll(src interface{}) string {
|
|
|
|
var b bytes.Buffer
|
|
|
|
b.WriteString("SELECT * FROM ")
|
|
|
|
b.WriteString(className(src))
|
|
|
|
|
|
|
|
log.Debugf("Generated WMI query %s", b.String())
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryAllForClass(src interface{}, class string) string {
|
|
|
|
var b bytes.Buffer
|
|
|
|
b.WriteString("SELECT * FROM ")
|
|
|
|
b.WriteString(class)
|
|
|
|
|
|
|
|
log.Debugf("Generated WMI query %s", b.String())
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryAllWhere(src interface{}, where string) string {
|
|
|
|
var b bytes.Buffer
|
|
|
|
b.WriteString("SELECT * FROM ")
|
|
|
|
b.WriteString(className(src))
|
|
|
|
|
|
|
|
if where != "" {
|
|
|
|
b.WriteString(" WHERE ")
|
|
|
|
b.WriteString(where)
|
2017-07-15 12:56:09 +00:00
|
|
|
}
|
2018-06-06 08:31:50 +00:00
|
|
|
|
|
|
|
log.Debugf("Generated WMI query %s", b.String())
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func queryAllForClassWhere(src interface{}, class string, where string) string {
|
|
|
|
var b bytes.Buffer
|
|
|
|
b.WriteString("SELECT * FROM ")
|
2017-07-15 12:56:09 +00:00
|
|
|
b.WriteString(class)
|
2018-06-06 08:31:50 +00:00
|
|
|
|
|
|
|
if where != "" {
|
|
|
|
b.WriteString(" WHERE ")
|
|
|
|
b.WriteString(where)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debugf("Generated WMI query %s", b.String())
|
2017-07-15 12:56:09 +00:00
|
|
|
return b.String()
|
|
|
|
}
|