2023-11-04 19:51:35 +00:00
|
|
|
package wmi
|
2016-08-26 06:59:27 +00:00
|
|
|
|
2017-07-15 12:56:09 +00:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"reflect"
|
|
|
|
|
2023-04-22 10:17:51 +00:00
|
|
|
"github.com/go-kit/log"
|
|
|
|
"github.com/go-kit/log/level"
|
2023-11-04 19:51:35 +00:00
|
|
|
"github.com/yusufpapurcu/wmi"
|
2017-07-15 12:56:09 +00:00
|
|
|
)
|
2016-09-01 14:04:43 +00:00
|
|
|
|
2023-11-04 19:51:35 +00:00
|
|
|
func InitWbem(logger log.Logger) error {
|
|
|
|
// This initialization prevents a memory leak on WMF 5+. See
|
|
|
|
// https://github.com/prometheus-community/windows_exporter/issues/77 and
|
|
|
|
// linked issues for details.
|
|
|
|
_ = level.Debug(logger).Log("msg", "Initializing SWbemServices")
|
|
|
|
s, err := wmi.InitializeSWbemServices(wmi.DefaultClient)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
wmi.DefaultClient.AllowMissingFields = true
|
|
|
|
wmi.DefaultClient.SWbemServicesClient = s
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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-11-04 19:51:35 +00:00
|
|
|
func Query(query string, dst interface{}, connectServerArgs ...interface{}) error {
|
|
|
|
return wmi.Query(query, dst, connectServerArgs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func QueryNamespace(query string, dst interface{}, namespace string) error {
|
|
|
|
return wmi.QueryNamespace(query, dst, namespace)
|
|
|
|
}
|
|
|
|
|
|
|
|
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))
|
|
|
|
|
2024-05-12 23:18:14 +00:00
|
|
|
_ = level.Debug(logger).Log("msg", "Generated WMI query "+b.String())
|
2018-06-06 08:31:50 +00:00
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
2023-11-04 19:51:35 +00:00
|
|
|
func QueryAllForClass(_ 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)
|
|
|
|
|
2024-05-12 23:18:14 +00:00
|
|
|
_ = level.Debug(logger).Log("msg", "Generated WMI query "+b.String())
|
2018-06-06 08:31:50 +00:00
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
2023-11-04 19:51:35 +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
|
|
|
|
2024-05-12 23:18:14 +00:00
|
|
|
_ = level.Debug(logger).Log("msg", "Generated WMI query "+b.String())
|
2018-06-06 08:31:50 +00:00
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
2023-11-04 19:51:35 +00:00
|
|
|
func QueryAllForClassWhere(_ 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)
|
|
|
|
}
|
|
|
|
|
2024-05-12 23:18:14 +00:00
|
|
|
_ = level.Debug(logger).Log("msg", "Generated WMI query "+b.String())
|
2017-07-15 12:56:09 +00:00
|
|
|
return b.String()
|
|
|
|
}
|