master
go 140 lines 3.28 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package smartctl
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/pkg/matcher"
14 "github.com/netdata/netdata/go/plugins/plugin/framework/collectorapi"
15 )
16
17 //go:embed "config_schema.json"
18 var configSchema string
19
20 func init() {
21 collectorapi.Register("smartctl", collectorapi.Creator{
22 JobConfigSchema: configSchema,
23 Defaults: collectorapi.Defaults{
24 UpdateEvery: 10,
25 },
26 Create: func() collectorapi.CollectorV1 { return New() },
27 Config: func() any { return &Config{} },
28 })
29 }
30
31 func New() *Collector {
32 return &Collector{
33 Config: Config{
34 Timeout: confopt.Duration(time.Second * 5),
35 ScanEvery: confopt.Duration(time.Minute * 15),
36 PollDevicesEvery: confopt.Duration(time.Minute * 5),
37 NoCheckPowerMode: "standby",
38 DeviceSelector: "*",
39 ConcurrentScans: 0, // Default to sequential
40 },
41 charts: &collectorapi.Charts{},
42 forceScan: true,
43 deviceSr: matcher.TRUE(),
44 seenDevices: make(map[string]bool),
45 }
46 }
47
48 type (
49 Config struct {
50 UpdateEvery int `yaml:"update_every,omitempty" json:"update_every"`
51 Timeout confopt.Duration `yaml:"timeout,omitempty" json:"timeout"`
52 ScanEvery confopt.Duration `yaml:"scan_every,omitempty" json:"scan_every"`
53 PollDevicesEvery confopt.Duration `yaml:"poll_devices_every,omitempty" json:"poll_devices_every"`
54 NoCheckPowerMode string `yaml:"no_check_power_mode,omitempty" json:"no_check_power_mode"`
55 DeviceSelector string `yaml:"device_selector,omitempty" json:"device_selector"`
56 ExtraDevices []ConfigExtraDevice `yaml:"extra_devices,omitempty" json:"extra_devices"`
57 ConcurrentScans int `yaml:"concurrent_scans,omitempty" json:"concurrent_scans"`
58 }
59 ConfigExtraDevice struct {
60 Name string `yaml:"name" json:"name"`
61 Type string `yaml:"type" json:"type"`
62 }
63 )
64
65 type Collector struct {
66 collectorapi.Base
67 Config `yaml:",inline" data:""`
68
69 charts *collectorapi.Charts
70
71 exec smartctlCli
72
73 deviceSr matcher.Matcher
74
75 lastScanTime time.Time
76 forceScan bool
77 scannedDevices map[string]*scanDevice
78
79 lastDevicePollTime time.Time
80 forceDevicePoll bool
81
82 seenDevices map[string]bool
83 mx map[string]int64
84 }
85
86 func (c *Collector) Configuration() any {
87 return c.Config
88 }
89
90 func (c *Collector) Init(context.Context) error {
91 if err := c.validateConfig(); err != nil {
92 return fmt.Errorf("config validation: %s", err)
93 }
94
95 sr, err := c.initDeviceSelector()
96 if err != nil {
97 return fmt.Errorf("device selector initialization: %v", err)
98 }
99 c.deviceSr = sr
100
101 smartctlExec, err := c.initSmartctlCli()
102 if err != nil {
103 return fmt.Errorf("smartctl exec initialization: %v", err)
104 }
105 c.exec = smartctlExec
106
107 return nil
108 }
109
110 func (c *Collector) Check(context.Context) error {
111 mx, err := c.collect()
112 if err != nil {
113 return err
114 }
115
116 if len(mx) == 0 {
117 return errors.New("no metrics collected")
118 }
119
120 return nil
121 }
122
123 func (c *Collector) Charts() *collectorapi.Charts {
124 return c.charts
125 }
126
127 func (c *Collector) Collect(context.Context) map[string]int64 {
128 mx, err := c.collect()
129 if err != nil {
130 c.Error(err)
131 }
132
133 if len(mx) == 0 {
134 return nil
135 }
136
137 return mx
138 }
139
140 func (c *Collector) Cleanup(context.Context) {}