/
win_wmi.go
82 lines (67 loc) · 1.67 KB
/
win_wmi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//go:generate ../../../tools/readme_config_includer/generator
//go:build windows
package win_wmi
import (
_ "embed"
"fmt"
"sync"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/config"
"github.com/influxdata/telegraf/plugins/inputs"
)
//go:embed sample.conf
var sampleConfig string
// Wmi struct
type Wmi struct {
Host string `toml:"host"`
Username config.Secret `toml:"username"`
Password config.Secret `toml:"password"`
Queries []Query `toml:"query"`
Methods []Method `toml:"method"`
Log telegraf.Logger `toml:"-"`
}
// S_FALSE is returned by CoInitializeEx if it was already called on this thread.
const sFalse = 0x00000001
// Init function
func (w *Wmi) Init() error {
for i := range w.Queries {
q := &w.Queries[i]
if err := q.prepare(w.Host, w.Username, w.Password); err != nil {
return fmt.Errorf("preparing query %q failed: %w", q.ClassName, err)
}
}
for i := range w.Methods {
m := &w.Methods[i]
if err := m.prepare(w.Host, w.Username, w.Password); err != nil {
return fmt.Errorf("preparing method %q failed: %w", m.Method, err)
}
}
return nil
}
// SampleConfig function
func (*Wmi) SampleConfig() string {
return sampleConfig
}
// Gather function
func (w *Wmi) Gather(acc telegraf.Accumulator) error {
var wg sync.WaitGroup
for _, query := range w.Queries {
wg.Add(1)
go func(q Query) {
defer wg.Done()
acc.AddError(q.execute(acc))
}(query)
}
for _, method := range w.Methods {
wg.Add(1)
go func(m Method) {
defer wg.Done()
acc.AddError(m.execute(acc))
}(method)
}
wg.Wait()
return nil
}
func init() {
inputs.Add("win_wmi", func() telegraf.Input { return &Wmi{} })
}