-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
Copy pathwin_wmi.go
78 lines (63 loc) · 1.58 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
//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
// S_FALSE is returned by CoInitializeEx if it was already called on this thread.
const sFalse = 0x00000001
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:"-"`
}
func (*Wmi) SampleConfig() string {
return sampleConfig
}
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
}
func (w *Wmi) Gather(acc telegraf.Accumulator) error {
var wg sync.WaitGroup
for _, q := range w.Queries {
wg.Add(1)
go func(q query) {
defer wg.Done()
acc.AddError(q.execute(acc))
}(q)
}
for _, m := range w.Methods {
wg.Add(1)
go func(m method) {
defer wg.Done()
acc.AddError(m.execute(acc))
}(m)
}
wg.Wait()
return nil
}
func init() {
inputs.Add("win_wmi", func() telegraf.Input { return &Wmi{} })
}