diff options
author | rare-magma <nun0@posteo.co> | 2022-06-14 00:28:37 +0200 |
---|---|---|
committer | rare-magma <nun0@posteo.co> | 2022-06-14 00:28:37 +0200 |
commit | 3c148a4d97d0f54f1d6f464ddc64b03fda1ad5fa (patch) | |
tree | 692ff03269f1f7df1a576b01eb43bf7182cc7edd | |
parent | 3704569b810211b7e31688d9b965736bdbba6155 (diff) |
add error handling, simplify attribute value assignment, remove redundant name for smart.go
-rw-r--r-- | devices/temp_nix.go | 17 |
1 files changed, 11 insertions, 6 deletions
diff --git a/devices/temp_nix.go b/devices/temp_nix.go index 7fa3613..dcb8ed4 100644 --- a/devices/temp_nix.go +++ b/devices/temp_nix.go @@ -6,7 +6,7 @@ package devices import ( "log" - smart "github.com/anatol/smart.go" + "github.com/anatol/smart.go" "github.com/jaypipes/ghw" "github.com/shirou/gopsutil/host" ) @@ -42,7 +42,6 @@ func getTemps(temps map[string]int) map[string]error { for _, disk := range block.Disks { dev, err := smart.Open("/dev/" + disk.Name) if err != nil { - log.Println(err) continue } defer dev.Close() @@ -50,13 +49,19 @@ func getTemps(temps map[string]int) map[string]error { switch sm := dev.(type) { case *smart.SataDevice: data, _ := sm.ReadSMARTData() - for _, attr := range data.Attrs { - if attr.Id == 194 { - temps[disk.Name+"_"+disk.Model] = int(attr.Value) - } + if err != nil { + log.Print("error getting smart data") + return nil + } + if attr, ok := data.Attrs[194]; ok { + temps[disk.Name+"_"+disk.Model] = int(attr.Value) } case *smart.NVMeDevice: data, _ := sm.ReadSMART() + if err != nil { + log.Print("error getting smart data") + return nil + } temps[disk.Name+"_"+disk.Model] = int(data.Temperature) default: } |