master
go 103 lines 1.93 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package nvme
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("nvme", 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
36 charts: &collectorapi.Charts{},
37 devicePaths: make(map[string]bool),
38 listDevicesEvery: time.Minute * 10,
39 }
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 nvmeCli
55
56 devicePaths map[string]bool
57 listDevicesTime time.Time
58 listDevicesEvery time.Duration
59 forceListDevices bool
60 }
61
62 func (c *Collector) Configuration() any {
63 return c.Config
64 }
65
66 func (c *Collector) Init(context.Context) error {
67 nvmeExec, err := c.initNVMeCLIExec()
68 if err != nil {
69 return fmt.Errorf("init nvme-cli exec: %v", err)
70 }
71 c.exec = nvmeExec
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 if len(mx) == 0 {
82 return errors.New("no metrics collected")
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 return mx
101 }
102
103 func (c *Collector) Cleanup(context.Context) {}