@cryptotaxi247 / netdata / commits / 788fb66d0

improve(go.d/smartctl): enable direct smartctl execution on non-Linux (#20567)

Ilya Mashchenko committed Jun 24, 2025 at 19:24 UTC 788fb66d091ffa8d76e6754993fa4ea806f78a8c
8 files changed +97 -36
src/go/plugin/go.d/collector/smartctl/charts.go
+2 -4
@@ -1,7 +1,5 @@
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 -//go:build linux || freebsd || openbsd || netbsd || dragonfly
4 -
3 package smartctl
4
5 import (
@@ -128,7 +126,7 @@ var (
126 Priority: prioDeviceScsiWriteErrors,
127 Dims: module.Dims{
128 {ID: "device_%s_type_%s_scsi_error_log_write_total_errors_corrected", Name: "corrected", Algo: module.Incremental},
131 - {ID: "device_%s_type_%s_scsi_error_log_read_total_uncorrected_errors", Name: "uncorrected", Algo: module.Incremental},
129 + {ID: "device_%s_type_%s_scsi_error_log_write_total_uncorrected_errors", Name: "uncorrected", Algo: module.Incremental},
130 },
131 }
132 deviceScsiVerifyErrorsChartTmpl = module.Chart{
@@ -219,7 +217,7 @@ func (c *Collector) newDeviceCharts(dev *smartDevice) *module.Charts {
217 _ = charts.Remove(deviceTemperatureChartTmpl.ID)
218 }
219 if _, ok := dev.powerCycleCount(); !ok {
222 - _ = charts.Remove(devicePowerOnTimeChartTmpl.ID)
220 + _ = charts.Remove(devicePowerCycleCountChartTmpl.ID)
221 }
222 if _, ok := dev.smartStatusPassed(); !ok {
223 _ = charts.Remove(deviceSmartStatusChartTmpl.ID)
src/go/plugin/go.d/collector/smartctl/collect.go
-2
@@ -1,7 +1,5 @@
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 -//go:build linux || freebsd || openbsd || netbsd || dragonfly
4 -
3 package smartctl
4
5 import (
src/go/plugin/go.d/collector/smartctl/collector.go
-2
@@ -1,7 +1,5 @@
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 -//go:build linux || freebsd || openbsd || netbsd || dragonfly
4 -
3 package smartctl
4
5 import (
src/go/plugin/go.d/collector/smartctl/collector_test.go
-2
@@ -1,7 +1,5 @@
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 -//go:build linux || freebsd || openbsd || netbsd || dragonfly
4 -
3 package smartctl
4
5 import (
src/go/plugin/go.d/collector/smartctl/exec.go
+74 -19
@@ -1,7 +1,5 @@
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 -//go:build linux || freebsd || openbsd || netbsd || dragonfly
4 -
3 package smartctl
4
5 import (
@@ -24,29 +22,30 @@ type smartctlCli interface {
22 deviceInfo(deviceName, deviceType, powerMode string) (*gjson.Result, error)
23 }
24
27 -func newSmartctlCliExec(ndsudoPath string, timeout time.Duration, log *logger.Logger) *smartctlCliExec {
28 - return &smartctlCliExec{
29 - Logger: log,
30 - ndsudoPath: ndsudoPath,
31 - timeout: timeout,
32 - }
33 -}
34 -
35 -type smartctlCliExec struct {
25 +// ndsudoSmartctlCli executes smartctl via ndsudo (Linux)
26 +type ndsudoSmartctlCli struct {
27 *logger.Logger
28
29 ndsudoPath string
30 timeout time.Duration
31 }
32
42 -func (e *smartctlCliExec) scan(open bool) (*gjson.Result, error) {
33 +func newNdsudoSmartctlCli(ndsudoPath string, timeout time.Duration, log *logger.Logger) *ndsudoSmartctlCli {
34 + return &ndsudoSmartctlCli{
35 + Logger: log,
36 + ndsudoPath: ndsudoPath,
37 + timeout: timeout,
38 + }
39 +}
40 +
41 +func (e *ndsudoSmartctlCli) scan(open bool) (*gjson.Result, error) {
42 if open {
43 return e.execute("smartctl-json-scan-open")
44 }
45 return e.execute("smartctl-json-scan")
46 }
47
49 -func (e *smartctlCliExec) deviceInfo(deviceName, deviceType, powerMode string) (*gjson.Result, error) {
48 +func (e *ndsudoSmartctlCli) deviceInfo(deviceName, deviceType, powerMode string) (*gjson.Result, error) {
49 return e.execute("smartctl-json-device-info",
50 "--deviceName", deviceName,
51 "--deviceType", deviceType,
@@ -54,7 +53,7 @@ func (e *smartctlCliExec) deviceInfo(deviceName, deviceType, powerMode string) (
53 )
54 }
55
57 -func (e *smartctlCliExec) execute(args ...string) (*gjson.Result, error) {
56 +func (e *ndsudoSmartctlCli) execute(args ...string) (*gjson.Result, error) {
57 ctx, cancel := context.WithTimeout(context.Background(), e.timeout)
58 defer cancel()
59
@@ -67,29 +66,85 @@ func (e *smartctlCliExec) execute(args ...string) (*gjson.Result, error) {
66 return nil, fmt.Errorf("'%s' execution failed: %v", cmd, err)
67 }
68 }
69 +
70 + return parseOutput(cmd.String(), bs, args, e.Logger)
71 +}
72 +
73 +// directSmartctlCli executes smartctl directly (Windows, macOS, etc.)
74 +type directSmartctlCli struct {
75 + *logger.Logger
76 +
77 + timeout time.Duration
78 +}
79 +
80 +func newDirectSmartctlCli(timeout time.Duration, log *logger.Logger) *directSmartctlCli {
81 + return &directSmartctlCli{
82 + Logger: log,
83 + timeout: timeout,
84 + }
85 +}
86 +
87 +func (e *directSmartctlCli) scan(open bool) (*gjson.Result, error) {
88 + args := []string{"--json", "--scan"}
89 + if open {
90 + args = append(args, "--scan-open")
91 + }
92 + return e.execute(args...)
93 +}
94 +
95 +func (e *directSmartctlCli) deviceInfo(deviceName, deviceType, powerMode string) (*gjson.Result, error) {
96 + args := []string{
97 + "--json",
98 + "--xall",
99 + "--device", deviceType,
100 + "--nocheck", powerMode,
101 + deviceName,
102 + }
103 + return e.execute(args...)
104 +}
105 +
106 +func (e *directSmartctlCli) execute(args ...string) (*gjson.Result, error) {
107 + ctx, cancel := context.WithTimeout(context.Background(), e.timeout)
108 + defer cancel()
109 +
110 + cmd := exec.CommandContext(ctx, "smartctl", args...)
111 + e.Debugf("executing '%s'", cmd)
112 +
113 + bs, err := cmd.Output()
114 + if err != nil {
115 + if errors.Is(err, context.DeadlineExceeded) || isExecExitCode(err, 1) || len(bs) == 0 {
116 + return nil, fmt.Errorf("'%s' execution failed: %v", cmd, err)
117 + }
118 + }
119 +
120 + return parseOutput(cmd.String(), bs, args, e.Logger)
121 +}
122 +
123 +// Common output parsing function
124 +func parseOutput(cmdStr string, bs []byte, args []string, log *logger.Logger) (*gjson.Result, error) {
125 if len(bs) == 0 {
71 - return nil, fmt.Errorf("'%s' returned no output", cmd)
126 + return nil, fmt.Errorf("'%s' returned no output", cmdStr)
127 }
128
129 if logger.Level.Enabled(slog.LevelDebug) {
130 var buf bytes.Buffer
131 if err := json.Compact(&buf, bs); err == nil {
77 - e.Debugf("exec: %v, resp: %s", args, buf.String())
132 + log.Debugf("exec: %v, resp: %s", args, buf.String())
133 }
134 }
135
136 if !gjson.ValidBytes(bs) {
82 - return nil, fmt.Errorf("'%s' returned invalid JSON output", cmd)
137 + return nil, fmt.Errorf("'%s' returned invalid JSON output", cmdStr)
138 }
139
140 res := gjson.ParseBytes(bs)
141 if !res.Get("smartctl.exit_status").Exists() {
87 - return nil, fmt.Errorf("'%s' returned unexpected data", cmd)
142 + return nil, fmt.Errorf("'%s' returned unexpected data", cmdStr)
143 }
144
145 for _, msg := range res.Get("smartctl.messages").Array() {
146 if msg.Get("severity").String() == "error" {
92 - return &res, fmt.Errorf("'%s' reported an error: %s", cmd, msg.Get("string"))
147 + return &res, fmt.Errorf("'%s' reported an error: %s", cmdStr, msg.Get("string"))
148 }
149 }
150
src/go/plugin/go.d/collector/smartctl/init.go
+21 -3
@@ -1,13 +1,13 @@
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 -//go:build linux || freebsd || openbsd || netbsd || dragonfly
4 -
3 package smartctl
4
5 import (
6 "fmt"
7 "os"
8 + "os/exec"
9 "path/filepath"
10 + "runtime"
11
12 "github.com/netdata/netdata/go/plugins/pkg/executable"
13 "github.com/netdata/netdata/go/plugins/pkg/matcher"
@@ -43,13 +43,31 @@ func (c *Collector) initDeviceSelector() (matcher.Matcher, error) {
43 }
44
45 func (c *Collector) initSmartctlCli() (smartctlCli, error) {
46 + if runtime.GOOS == "linux" {
47 + return c.initNdsudoSmartctlCli()
48 + }
49 + return c.initDirectSmartctlCli()
50 +}
51 +
52 +func (c *Collector) initNdsudoSmartctlCli() (smartctlCli, error) {
53 ndsudoPath := filepath.Join(executable.Directory, "ndsudo")
54 if _, err := os.Stat(ndsudoPath); err != nil {
55 return nil, fmt.Errorf("ndsudo executable not found: %v", err)
56 + }
57
58 + smartctlExec := newNdsudoSmartctlCli(ndsudoPath, c.Timeout.Duration(), c.Logger)
59 + return smartctlExec, nil
60 +}
61 +
62 +func (c *Collector) initDirectSmartctlCli() (smartctlCli, error) {
63 + // Check if smartctl is available in PATH
64 + smartctlPath, err := exec.LookPath("smartctl")
65 + if err != nil {
66 + return nil, fmt.Errorf("smartctl executable not found in PATH: %v", err)
67 }
68
52 - smartctlExec := newSmartctlCliExec(ndsudoPath, c.Timeout.Duration(), c.Logger)
69 + c.Debugf("found smartctl at: %s", smartctlPath)
70
71 + smartctlExec := newDirectSmartctlCli(c.Timeout.Duration(), c.Logger)
72 return smartctlExec, nil
73 }
src/go/plugin/go.d/collector/smartctl/scan.go
-2
@@ -1,7 +1,5 @@
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 -//go:build linux || freebsd || openbsd || netbsd || dragonfly
4 -
3 package smartctl
4
5 import (
src/go/plugin/go.d/collector/smartctl/smart_device.go
-2
@@ -1,7 +1,5 @@
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 -//go:build linux || freebsd || openbsd || netbsd || dragonfly
4 -
3 package smartctl
4
5 import (