From 3c148a4d97d0f54f1d6f464ddc64b03fda1ad5fa Mon Sep 17 00:00:00 2001 From: rare-magma Date: Tue, 14 Jun 2022 00:28:37 +0200 Subject: add error handling, simplify attribute value assignment, remove redundant name for smart.go --- devices/temp_nix.go | 17 +++++++++++------ 1 file 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: } -- cgit v1.2.3