Skip to content

Commit

Permalink
Add support for autoscan device types and predictable device paths
Browse files Browse the repository at this point in the history
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/...)

Relevant change to device name parsing regular expression is included
now, so predictable device paths are now also usable when directly
specified.

Signed-off-by: Piotr Dobrowolski <[email protected]>
  • Loading branch information
Informatic committed Jul 14, 2024
1 parent 1c9c694 commit 4c5f721
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 5 additions & 1 deletion readjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion smartctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type SMARTctl struct {
}

func extractDiskName(input string) string {
re := regexp.MustCompile(`^(?:/dev/(?P<bus_name>\S+)/(?P<bus_num>\S+)\s\[|/dev/|\[)(?:\s\[|)(?P<disk>[a-z0-9_]+)(?:\].*|)$`)
re := regexp.MustCompile(`^(?:/dev/(?P<bus_name>\S+)/(?P<bus_num>\S+)\s\[|/dev/(?:disk\/by-id\/|disk\/by-path\/|)|\[)(?:\s\[|)(?P<disk>[A-Za-z0-9_\-]+)(?:\].*|)$`)
match := re.FindStringSubmatch(input)

if len(match) > 0 {
Expand Down

0 comments on commit 4c5f721

Please sign in to comment.