add root check and better info of current disk to check

This commit is contained in:
lukas-heiligenbrunner 2022-05-03 13:12:38 +02:00
parent c2f946f7ed
commit 017449a58b

27
main.go
View File

@ -4,7 +4,9 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"log" "log"
"os"
"os/exec" "os/exec"
"os/user"
"strings" "strings"
) )
@ -25,19 +27,32 @@ func execSystem(cmd string, args ...string) []byte {
return out.Bytes() return out.Bytes()
} }
func isRoot() bool {
currentUser, err := user.Current()
if err != nil {
log.Fatalf("[isRoot] Unable to get current user: %s", err)
}
return currentUser.Username == "root"
}
func main() { func main() {
fmt.Println("Running smart check on all disks") if !isRoot() {
fmt.Println("this program needs root permissions!")
os.Exit(-1)
}
disks := getDisks() disks := getDisks()
fmt.Printf("Running smart check on all %d disks.\n", len(disks.Blockdevices))
for _, blockdevice := range disks.Blockdevices { for _, blockdevice := range disks.Blockdevices {
fmt.Printf("\nChecking /dev/%s\n", blockdevice.Name)
path := fmt.Sprintf("/dev/%s", blockdevice.Name) path := fmt.Sprintf("/dev/%s", blockdevice.Name)
info := getDiskInfo(path) info := getDiskInfo(path)
fmt.Printf("%s %s\n", info.ModelFamily, info.ModelName)
diskstring := "HDD"
if info.RotationRate == 0 {
diskstring = "SSD"
}
fmt.Printf("\nChecking %s /dev/%s - %s %s\n", diskstring, blockdevice.Name, info.ModelFamily, info.ModelName)
checkSmartAttributes(path, strings.Contains(strings.ToLower(info.ModelName+info.ModelFamily), strings.ToLower("Seagate")), info.RotationRate != 0) checkSmartAttributes(path, strings.Contains(strings.ToLower(info.ModelName+info.ModelFamily), strings.ToLower("Seagate")), info.RotationRate != 0)
} }
} }