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"
|
2023-04-22 10:17:51 +00:00
|
|
|
"fmt"
|
2017-07-15 12:56:09 +00:00
|
|
|
"reflect"
|
|
|
|
|
2023-04-22 10:17:51 +00:00
|
|
|
"github.com/go-kit/log"
|
|
|
|
"github.com/go-kit/log/level"
|
2017-07-15 12:56:09 +00:00
|
|
|
)
|
2016-09-01 14:04:43 +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()
|
|
|
|
}
|
|
|
|
|
2023-04-22 10:17:51 +00:00
|
|
|
func queryAll(src interface{}, logger log.Logger) string {
|
2018-06-06 08:31:50 +00:00
|
|
|
var b bytes.Buffer
|
|
|
|
b.WriteString("SELECT * FROM ")
|
|
|
|
b.WriteString(className(src))
|
|
|
|
|
2023-06-08 00:29:50 +00:00
|
|
|
_ = level.Debug(logger).Log("msg", fmt.Sprintf("Generated WMI query %s", b.String()))
|
2018-06-06 08:31:50 +00:00
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
2023-04-22 10:17:51 +00:00
|
|
|
func queryAllForClass(src interface{}, class string, logger log.Logger) string {
|
2018-06-06 08:31:50 +00:00
|
|
|
var b bytes.Buffer
|
|
|
|
b.WriteString("SELECT * FROM ")
|
|
|
|
b.WriteString(class)
|
|
|
|
|
2023-06-08 00:29:50 +00:00
|
|
|
_ = level.Debug(logger).Log("msg", fmt.Sprintf("Generated WMI query %s", b.String()))
|
2018-06-06 08:31:50 +00:00
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
2023-04-22 10:17:51 +00:00
|
|
|
func queryAllWhere(src interface{}, where string, logger log.Logger) string {
|
2018-06-06 08:31:50 +00:00
|
|
|
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
|
|
|
|
2023-06-08 00:29:50 +00:00
|
|
|
_ = level.Debug(logger).Log("msg", fmt.Sprintf("Generated WMI query %s", b.String()))
|
2018-06-06 08:31:50 +00:00
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
2023-04-22 10:17:51 +00:00
|
|
|
func queryAllForClassWhere(src interface{}, class string, where string, logger log.Logger) string {
|
2018-06-06 08:31:50 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-06-08 00:29:50 +00:00
|
|
|
_ = level.Debug(logger).Log("msg", fmt.Sprintf("Generated WMI query %s", b.String()))
|
2017-07-15 12:56:09 +00:00
|
|
|
return b.String()
|
|
|
|
}
|