59 lines
1.2 KiB
Go
59 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"os/exec"
|
|
"os/user"
|
|
"strings"
|
|
)
|
|
|
|
func execSystem(cmd string, args ...string) []byte {
|
|
com := exec.Command(cmd, args...)
|
|
var out bytes.Buffer
|
|
com.Stdout = &out
|
|
com.Stderr = &out
|
|
|
|
err := com.Run()
|
|
|
|
if out.Bytes() == nil {
|
|
fmt.Println(out.String())
|
|
log.Fatal(err)
|
|
return nil
|
|
}
|
|
|
|
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() {
|
|
if !isRoot() {
|
|
fmt.Println("this program needs root permissions!")
|
|
os.Exit(-1)
|
|
}
|
|
|
|
disks := getDisks()
|
|
fmt.Printf("Running smart check on all %d disks.\n", len(disks.Blockdevices))
|
|
for _, blockdevice := range disks.Blockdevices {
|
|
path := fmt.Sprintf("/dev/%s", blockdevice.Name)
|
|
info := getDiskInfo(path)
|
|
|
|
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)
|
|
}
|
|
}
|