master
go 108 lines 2.02 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 //go:build linux
4
5 package ap
6
7 import (
8 "context"
9 _ "embed"
10 "errors"
11 "fmt"
12 "time"
13
14 "github.com/netdata/netdata/go/plugins/pkg/confopt"
15 "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi"
16 )
17
18 //go:embed "config_schema.json"
19 var configSchema string
20
21 func init() {
22 collectorapi.Register("ap", collectorapi.Creator{
23 JobConfigSchema: configSchema,
24 Defaults: collectorapi.Defaults{
25 UpdateEvery: 10,
26 },
27 Create: func() collectorapi.CollectorV1 { return New() },
28 Config: func() any { return &Config{} },
29 })
30 }
31
32 func New() *Collector {
33 return &Collector{
34 Config: Config{
35 BinaryPath: "/usr/sbin/iw",
36 Timeout: confopt.Duration(time.Second * 2),
37 },
38 charts: &collectorapi.Charts{},
39 seenIfaces: make(map[string]*iwInterface),
40 }
41 }
42
43 type Config struct {
44 UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"`
45 Timeout confopt.Duration `yaml:"timeout,omitempty" json:"timeout"`
46 BinaryPath string `yaml:"binary_path,omitempty" json:"binary_path"`
47 }
48
49 type Collector struct {
50 collectorapi.Base
51 Config `yaml:",inline" json:""`
52
53 charts *collectorapi.Charts
54
55 exec iwBinary
56
57 seenIfaces map[string]*iwInterface
58 }
59
60 func (c *Collector) Configuration() any {
61 return c.Config
62 }
63
64 func (c *Collector) Init(context.Context) error {
65 if err := c.validateConfig(); err != nil {
66 return fmt.Errorf("config validation: %s", err)
67 }
68
69 iw, err := c.initIwExec()
70 if err != nil {
71 return fmt.Errorf("iw exec initialization: %v", err)
72 }
73 c.exec = iw
74
75 return nil
76 }
77
78 func (c *Collector) Check(context.Context) error {
79 mx, err := c.collect()
80 if err != nil {
81 return err
82 }
83
84 if len(mx) == 0 {
85 return errors.New("no metrics collected")
86 }
87
88 return nil
89 }
90
91 func (c *Collector) Charts() *collectorapi.Charts {
92 return c.charts
93 }
94
95 func (c *Collector) Collect(context.Context) map[string]int64 {
96 mx, err := c.collect()
97 if err != nil {
98 c.Error(err)
99 }
100
101 if len(mx) == 0 {
102 return nil
103 }
104
105 return mx
106 }
107
108 func (c *Collector) Cleanup(context.Context) {}