DiskHealth/main.go

77 lines
1.5 KiB
Go

package main
import (
"bytes"
"flag"
"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)
}
tmpptr := flag.Bool("temp", false, "get temperatures")
flag.Parse()
disks := getDisks()
if *tmpptr {
fmt.Println("printing temperatures")
for _, dev := range disks.Blockdevices {
path := fmt.Sprintf("/dev/%s", dev.Name)
max, min, temp := getTemp(path)
fmt.Printf("%s temp: %d°C (max: %d°C, min: %d°C)\n", path, temp, max, min)
}
return
}
fmt.Printf("Running smart check on %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)
}
}