master
go 106 lines 2.13 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package hpssa
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("hpssa", 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 seenControllers: make(map[string]*hpssaController),
37 seenArrays: make(map[string]*hpssaArray),
38 seenLDrives: make(map[string]*hpssaLogicalDrive),
39 seenPDrives: make(map[string]*hpssaPhysicalDrive),
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 }
47
48 type Collector struct {
49 collectorapi.Base
50 Config `yaml:",inline" json:""`
51
52 charts *collectorapi.Charts
53
54 exec ssacliBinary
55
56 seenControllers map[string]*hpssaController
57 seenArrays map[string]*hpssaArray
58 seenLDrives map[string]*hpssaLogicalDrive
59 seenPDrives map[string]*hpssaPhysicalDrive
60 }
61
62 func (c *Collector) Configuration() any {
63 return c.Config
64 }
65
66 func (c *Collector) Init(context.Context) error {
67 ssacli, err := c.initSsacliBinary()
68 if err != nil {
69 return fmt.Errorf("ssacli exec initialization: %v", err)
70 }
71 c.exec = ssacli
72
73 return nil
74 }
75
76 func (c *Collector) Check(context.Context) error {
77 mx, err := c.collect()
78 if err != nil {
79 return err
80 }
81
82 if len(mx) == 0 {
83 return errors.New("no metrics collected")
84 }
85
86 return nil
87 }
88
89 func (c *Collector) Charts() *collectorapi.Charts {
90 return c.charts
91 }
92
93 func (c *Collector) Collect(context.Context) map[string]int64 {
94 mx, err := c.collect()
95 if err != nil {
96 c.Error(err)
97 }
98
99 if len(mx) == 0 {
100 return nil
101 }
102
103 return mx
104 }
105
106 func (c *Collector) Cleanup(context.Context) {}