master
go 102 lines 1.86 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package adaptecraid
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("adaptec_raid", 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 lds: make(map[string]bool),
37 pds: make(map[string]bool),
38 }
39 }
40
41 type Config struct {
42 UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"`
43 Timeout confopt.Duration `yaml:"timeout,omitempty" json:"timeout"`
44 }
45
46 type Collector struct {
47 collectorapi.Base
48 Config `yaml:",inline" json:""`
49
50 charts *collectorapi.Charts
51
52 exec arcconfCli
53
54 lds map[string]bool
55 pds map[string]bool
56 }
57
58 func (c *Collector) Configuration() any {
59 return c.Config
60 }
61
62 func (c *Collector) Init(context.Context) error {
63 arcconf, err := c.initArcconfCliExec()
64 if err != nil {
65 return fmt.Errorf("arcconf exec initialization: %v", err)
66 }
67 c.exec = arcconf
68
69 return nil
70 }
71
72 func (c *Collector) Check(context.Context) error {
73 mx, err := c.collect()
74 if err != nil {
75 return err
76 }
77
78 if len(mx) == 0 {
79 return errors.New("no metrics collected")
80 }
81
82 return nil
83 }
84
85 func (c *Collector) Charts() *collectorapi.Charts {
86 return c.charts
87 }
88
89 func (c *Collector) Collect(context.Context) map[string]int64 {
90 mx, err := c.collect()
91 if err != nil {
92 c.Error(err)
93 }
94
95 if len(mx) == 0 {
96 return nil
97 }
98
99 return mx
100 }
101
102 func (c *Collector) Cleanup(context.Context) {}