| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package smartctl |
| 4 | |
| 5 | import ( |
| 6 | "bytes" |
| 7 | "context" |
| 8 | "encoding/json" |
| 9 | "errors" |
| 10 | "fmt" |
| 11 | "log/slog" |
| 12 | "os/exec" |
| 13 | "time" |
| 14 | |
| 15 | "github.com/netdata/netdata/go/plugins/logger" |
| 16 | "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/ndexec" |
| 17 | |
| 18 | "github.com/tidwall/gjson" |
| 19 | ) |
| 20 | |
| 21 | type smartctlCli interface { |
| 22 | scan(open bool) (*gjson.Result, error) |
| 23 | deviceInfo(deviceName, deviceType, powerMode string) (*gjson.Result, error) |
| 24 | } |
| 25 | |
| 26 | // ndsudoSmartctlCli executes smartctl via ndsudo (Linux) |
| 27 | type ndsudoSmartctlCli struct { |
| 28 | *logger.Logger |
| 29 | |
| 30 | timeout time.Duration |
| 31 | } |
| 32 | |
| 33 | func newNdsudoSmartctlCli(timeout time.Duration, log *logger.Logger) *ndsudoSmartctlCli { |
| 34 | return &ndsudoSmartctlCli{ |
| 35 | Logger: log, |
| 36 | timeout: timeout, |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func (e *ndsudoSmartctlCli) scan(open bool) (*gjson.Result, error) { |
| 41 | if open { |
| 42 | return e.execute("smartctl-json-scan-open") |
| 43 | } |
| 44 | return e.execute("smartctl-json-scan") |
| 45 | } |
| 46 | |
| 47 | func (e *ndsudoSmartctlCli) deviceInfo(deviceName, deviceType, powerMode string) (*gjson.Result, error) { |
| 48 | return e.execute("smartctl-json-device-info", |
| 49 | "--deviceName", deviceName, |
| 50 | "--deviceType", deviceType, |
| 51 | "--powerMode", powerMode, |
| 52 | ) |
| 53 | } |
| 54 | |
| 55 | func (e *ndsudoSmartctlCli) execute(cmd string, args ...string) (*gjson.Result, error) { |
| 56 | bs, cmdStr, err := ndexec.RunNDSudoWithCmd(e.Logger, e.timeout, cmd, args...) |
| 57 | if err != nil { |
| 58 | if errors.Is(err, context.DeadlineExceeded) || isExecExitCode(err, 1) || len(bs) == 0 { |
| 59 | return nil, fmt.Errorf("'%s' execution failed: %v", cmdStr, err) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | return parseOutput(cmdStr, bs, e.Logger) |
| 64 | } |
| 65 | |
| 66 | // directSmartctlCli executes smartctl directly (Windows only) |
| 67 | type directSmartctlCli struct { |
| 68 | *logger.Logger |
| 69 | |
| 70 | smartctlPath string |
| 71 | timeout time.Duration |
| 72 | } |
| 73 | |
| 74 | func newDirectSmartctlCli(smartctlPath string, timeout time.Duration, log *logger.Logger) *directSmartctlCli { |
| 75 | return &directSmartctlCli{ |
| 76 | Logger: log, |
| 77 | smartctlPath: smartctlPath, |
| 78 | timeout: timeout, |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | func (e *directSmartctlCli) scan(open bool) (*gjson.Result, error) { |
| 83 | args := []string{"--json", "--scan"} |
| 84 | if open { |
| 85 | args = append(args, "--scan-open") |
| 86 | } |
| 87 | return e.execute(args...) |
| 88 | } |
| 89 | |
| 90 | func (e *directSmartctlCli) deviceInfo(deviceName, deviceType, powerMode string) (*gjson.Result, error) { |
| 91 | args := []string{ |
| 92 | "--json", |
| 93 | "--xall", |
| 94 | "--device", deviceType, |
| 95 | "--nocheck", powerMode, |
| 96 | deviceName, |
| 97 | } |
| 98 | return e.execute(args...) |
| 99 | } |
| 100 | |
| 101 | func (e *directSmartctlCli) execute(args ...string) (*gjson.Result, error) { |
| 102 | ctx, cancel := context.WithTimeout(context.Background(), e.timeout) |
| 103 | defer cancel() |
| 104 | |
| 105 | cmd := exec.CommandContext(ctx, e.smartctlPath, args...) |
| 106 | e.Debugf("executing '%s'", cmd) |
| 107 | |
| 108 | bs, err := cmd.Output() |
| 109 | if err != nil { |
| 110 | if errors.Is(err, context.DeadlineExceeded) || isExecExitCode(err, 1) || len(bs) == 0 { |
| 111 | return nil, fmt.Errorf("'%s' execution failed: %v", cmd, err) |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | return parseOutput(cmd.String(), bs, e.Logger) |
| 116 | } |
| 117 | |
| 118 | // Common output parsing function |
| 119 | func parseOutput(cmdStr string, bs []byte, log *logger.Logger) (*gjson.Result, error) { |
| 120 | if len(bs) == 0 { |
| 121 | return nil, fmt.Errorf("'%s' returned no output", cmdStr) |
| 122 | } |
| 123 | |
| 124 | if logger.Level.Enabled(slog.LevelDebug) { |
| 125 | var buf bytes.Buffer |
| 126 | if err := json.Compact(&buf, bs); err == nil { |
| 127 | log.Debugf("exec: %v, resp: %s", cmdStr, buf.String()) |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | if !gjson.ValidBytes(bs) { |
| 132 | return nil, fmt.Errorf("'%s' returned invalid JSON output", cmdStr) |
| 133 | } |
| 134 | |
| 135 | res := gjson.ParseBytes(bs) |
| 136 | if !res.Get("smartctl.exit_status").Exists() { |
| 137 | return nil, fmt.Errorf("'%s' returned unexpected data", cmdStr) |
| 138 | } |
| 139 | |
| 140 | // https://manpages.debian.org/bullseye/smartmontools/smartctl.8.en.html#EXIT_STATUS |
| 141 | // Bits 0-1 indicate fatal conditions (command line error, device open failure). |
| 142 | // Bits 2-7 indicate disk health conditions but the output data is still valid. |
| 143 | if isExitStatusHasAnyBit(&res, 0, 1) { |
| 144 | for _, msg := range res.Get("smartctl.messages").Array() { |
| 145 | if msg.Get("severity").String() == "error" { |
| 146 | return &res, fmt.Errorf("'%s' reported an error: %s", cmdStr, msg.Get("string")) |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | return &res, nil |
| 152 | } |
| 153 | |
| 154 | func isExecExitCode(err error, exitCode int) bool { |
| 155 | var v *exec.ExitError |
| 156 | return errors.As(err, &v) && v.ExitCode() == exitCode |
| 157 | } |