Skip to content

Commit

Permalink
Add bus name & bus number to disk name, example: bus_0_megaraid_disk_01
Browse files Browse the repository at this point in the history
Signed-off-by: Denys <[email protected]>
  • Loading branch information
zxzharmlesszxz committed Mar 15, 2024
1 parent 3a012b5 commit 7f7c652
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions smartctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,25 @@ type SMARTctl struct {
}

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

if len(match) > 0 {
return match[re.SubexpIndex("disk")]
busNameIndex := re.SubexpIndex("bus_name")
busNumIndex := re.SubexpIndex("bus_num")
diskIndex := re.SubexpIndex("disk")
var name []string
if busNameIndex != -1 && match[busNameIndex] != "" {
name = append(name, match[busNameIndex])
}
if busNumIndex != -1 && match[busNumIndex] != "" {
name = append(name, match[busNumIndex])
}
if diskIndex != -1 && match[diskIndex] != "" {
name = append(name, match[diskIndex])
}

return strings.Join(name, "_")
}
return ""
}
Expand Down

0 comments on commit 7f7c652

Please sign in to comment.