From 8331cff3ca3af9ca00518f20d2780d5efdef0541 Mon Sep 17 00:00:00 2001 From: Piotr Dobrowolski Date: Sun, 14 Jul 2024 20:31:46 +0200 Subject: [PATCH] Add support for autoscan device types and predictable device paths This adds a new command line option allowing for customization of autodetected device types and enables use of special "by-id" device type that forces use of predictable device paths (/dev/disk/by-id/...) Signed-off-by: Piotr Dobrowolski --- main.go | 4 ++++ readjson.go | 6 +++++- smartctl.go | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 7f55518..07df636 100644 --- a/main.go +++ b/main.go @@ -108,6 +108,10 @@ var ( "smartctl.device-include", "Regexp of devices to exclude from automatic scanning. (mutually exclusive to device-exclude)", ).Default("").String() + smartctlDeviceTypes = kingpin.Flag( + "smartctl.device-type", + "Device type to use during automatic scan. Special by-id value forces predictable device names. (repeatable)", + ).Strings() smartctlFakeData = kingpin.Flag("smartctl.fake-data", "The device to monitor (repeatable)", ).Default("false").Hidden().Bool() diff --git a/readjson.go b/readjson.go index 67efefb..4f4571d 100644 --- a/readjson.go +++ b/readjson.go @@ -77,7 +77,11 @@ func readSMARTctl(logger log.Logger, device Device) (gjson.Result, bool) { func readSMARTctlDevices(logger log.Logger) gjson.Result { level.Debug(logger).Log("msg", "Scanning for devices") - out, err := exec.Command(*smartctlPath, "--json", "--scan").Output() + var scanArgs []string = []string{"--json", "--scan"} + for _, d := range *smartctlDeviceTypes { + scanArgs = append(scanArgs, "--device", d) + } + out, err := exec.Command(*smartctlPath, scanArgs...).Output() if exiterr, ok := err.(*exec.ExitError); ok { level.Debug(logger).Log("msg", "Exit Status", "exit_code", exiterr.ExitCode()) // The smartctl command returns 2 if devices are sleeping, ignore this error. diff --git a/smartctl.go b/smartctl.go index 0fa96db..2a549d9 100644 --- a/smartctl.go +++ b/smartctl.go @@ -44,7 +44,7 @@ type SMARTctl struct { } func extractDiskName(input string) string { - re := regexp.MustCompile(`^(?:/dev/(?P\S+)/(?P\S+)\s\[|/dev/|\[)(?:\s\[|)(?P[a-z0-9_]+)(?:\].*|)$`) + re := regexp.MustCompile(`^(?:/dev/(?P\S+)/(?P\S+)\s\[|/dev/(?:disk\/by-id\/|disk\/by-path\/|)|\[)(?:\s\[|)(?P[A-Za-z0-9_\-]+)(?:\].*|)$`) match := re.FindStringSubmatch(input) if len(match) > 0 {