| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package smartctl |
| 4 | |
| 5 | import ( |
| 6 | "fmt" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "runtime" |
| 10 | |
| 11 | "github.com/netdata/netdata/go/plugins/pkg/matcher" |
| 12 | "github.com/netdata/netdata/go/plugins/plugin/go.d/pkg/ndexec" |
| 13 | ) |
| 14 | |
| 15 | func (c *Collector) validateConfig() error { |
| 16 | switch c.NoCheckPowerMode { |
| 17 | case "never", "sleep", "standby", "idle": |
| 18 | default: |
| 19 | return fmt.Errorf("invalid power mode '%s'", c.NoCheckPowerMode) |
| 20 | } |
| 21 | |
| 22 | for _, v := range c.ExtraDevices { |
| 23 | if v.Name == "" || v.Type == "" { |
| 24 | return fmt.Errorf("invalid extra device: name and type must both be provided, got name='%s' type='%s'", v.Name, v.Type) |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | return nil |
| 29 | } |
| 30 | |
| 31 | func (c *Collector) initDeviceSelector() (matcher.Matcher, error) { |
| 32 | if c.DeviceSelector == "" { |
| 33 | return matcher.TRUE(), nil |
| 34 | } |
| 35 | |
| 36 | m, err := matcher.NewSimplePatternsMatcher(c.DeviceSelector) |
| 37 | if err != nil { |
| 38 | return nil, err |
| 39 | } |
| 40 | |
| 41 | return m, nil |
| 42 | } |
| 43 | |
| 44 | func (c *Collector) initSmartctlCli() (smartctlCli, error) { |
| 45 | if runtime.GOOS == "windows" { |
| 46 | return c.initDirectSmartctlCli() |
| 47 | } |
| 48 | return c.initNdsudoSmartctlCli() |
| 49 | } |
| 50 | |
| 51 | func (c *Collector) initNdsudoSmartctlCli() (smartctlCli, error) { |
| 52 | smartctlExec := newNdsudoSmartctlCli(c.Timeout.Duration(), c.Logger) |
| 53 | return smartctlExec, nil |
| 54 | } |
| 55 | |
| 56 | func (c *Collector) initDirectSmartctlCli() (smartctlCli, error) { |
| 57 | path, err := ndexec.FindBinary( |
| 58 | []string{"smartctl"}, |
| 59 | []string{ |
| 60 | filepath.Join(os.Getenv("ProgramFiles"), "smartmontools", "bin", "smartctl.exe"), |
| 61 | }, |
| 62 | ) |
| 63 | if err != nil { |
| 64 | return nil, fmt.Errorf("smartctl: %w", err) |
| 65 | } |
| 66 | |
| 67 | c.Debugf("found smartctl at: %s", path) |
| 68 | |
| 69 | return newDirectSmartctlCli(path, c.Timeout.Duration(), c.Logger), nil |
| 70 | } |