From 157c93b967514f8c2708554f825729d132f1cd02 Mon Sep 17 00:00:00 2001 From: Jason Kulatunga Date: Mon, 20 Jun 2022 12:09:56 -0700 Subject: [PATCH] provide a mechanism to specify the absolute path to the smartctl binary used by metrics collector. - fixes #304 --- collector/pkg/collector/metrics.go | 6 +++--- collector/pkg/config/config.go | 1 + collector/pkg/detect/detect.go | 4 ++-- example.collector.yaml | 1 + 4 files changed, 7 insertions(+), 5 deletions(-) diff --git a/collector/pkg/collector/metrics.go b/collector/pkg/collector/metrics.go index ed1b777..415516b 100644 --- a/collector/pkg/collector/metrics.go +++ b/collector/pkg/collector/metrics.go @@ -98,10 +98,10 @@ func (mc *MetricsCollector) Run() error { func (mc *MetricsCollector) Validate() error { mc.logger.Infoln("Verifying required tools") - _, lookErr := exec.LookPath("smartctl") + _, lookErr := exec.LookPath(mc.config.GetString("commands.metrics_smartctl_bin")) if lookErr != nil { - return errors.DependencyMissingError("smartctl is missing") + return errors.DependencyMissingError(fmt.Sprintf("%s binary is missing", mc.config.GetString("commands.metrics_smartctl_bin"))) } return nil @@ -124,7 +124,7 @@ func (mc *MetricsCollector) Collect(deviceWWN string, deviceName string, deviceT } args = append(args, fullDeviceName) - result, err := mc.shell.Command(mc.logger, "smartctl", args, "", os.Environ()) + result, err := mc.shell.Command(mc.logger, mc.config.GetString("commands.metrics_smartctl_bin"), args, "", os.Environ()) resultBytes := []byte(result) if err != nil { if exitError, ok := err.(*exec.ExitError); ok { diff --git a/collector/pkg/config/config.go b/collector/pkg/config/config.go index 025082e..0a0d156 100644 --- a/collector/pkg/config/config.go +++ b/collector/pkg/config/config.go @@ -43,6 +43,7 @@ func (c *configuration) Init() error { c.SetDefault("api.endpoint", "http://localhost:8080") + c.SetDefault("commands.metrics_smartctl_bin", "smartctl") c.SetDefault("commands.metrics_scan_args", "--scan --json") c.SetDefault("commands.metrics_info_args", "--info --json") c.SetDefault("commands.metrics_smart_args", "--xall --json") diff --git a/collector/pkg/detect/detect.go b/collector/pkg/detect/detect.go index 5f2fa55..6f6e5d8 100644 --- a/collector/pkg/detect/detect.go +++ b/collector/pkg/detect/detect.go @@ -29,7 +29,7 @@ type Detect struct { func (d *Detect) SmartctlScan() ([]models.Device, error) { //we use smartctl to detect all the drives available. args := strings.Split(d.Config.GetString("commands.metrics_scan_args"), " ") - detectedDeviceConnJson, err := d.Shell.Command(d.Logger, "smartctl", args, "", os.Environ()) + detectedDeviceConnJson, err := d.Shell.Command(d.Logger, d.Config.GetString("commands.metrics_smartctl_bin"), args, "", os.Environ()) if err != nil { d.Logger.Errorf("Error scanning for devices: %v", err) return nil, err @@ -60,7 +60,7 @@ func (d *Detect) SmartCtlInfo(device *models.Device) error { } args = append(args, fullDeviceName) - availableDeviceInfoJson, err := d.Shell.Command(d.Logger, "smartctl", args, "", os.Environ()) + availableDeviceInfoJson, err := d.Shell.Command(d.Logger, d.Config.GetString("commands.metrics_smartctl_bin"), args, "", os.Environ()) if err != nil { d.Logger.Errorf("Could not retrieve device information for %s: %v", device.DeviceName, err) return err diff --git a/example.collector.yaml b/example.collector.yaml index 60ab408..26260bc 100644 --- a/example.collector.yaml +++ b/example.collector.yaml @@ -73,6 +73,7 @@ devices: # example to show how to override the smartctl command args globally #commands: +# metrics_smartctl_bin: 'smartctl' # change to provide custom `smartctl` binary path, eg. `/usr/sbin/smartctl` # metrics_scan_args: '--scan --json' # used to detect devices # metrics_info_args: '--info --json' # used to determine device unique ID & register device with Scrutiny # metrics_smart_args: '--xall --json' # used to retrieve smart data for each device.