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