add error handling, simplify attribute value assignment, remove redundant name for smart.go

This commit is contained in:
rare-magma 2022-06-14 00:28:37 +02:00
parent 3704569b81
commit 3c148a4d97

View File

@ -6,7 +6,7 @@ package devices
import ( import (
"log" "log"
smart "github.com/anatol/smart.go" "github.com/anatol/smart.go"
"github.com/jaypipes/ghw" "github.com/jaypipes/ghw"
"github.com/shirou/gopsutil/host" "github.com/shirou/gopsutil/host"
) )
@ -42,7 +42,6 @@ func getTemps(temps map[string]int) map[string]error {
for _, disk := range block.Disks { for _, disk := range block.Disks {
dev, err := smart.Open("/dev/" + disk.Name) dev, err := smart.Open("/dev/" + disk.Name)
if err != nil { if err != nil {
log.Println(err)
continue continue
} }
defer dev.Close() defer dev.Close()
@ -50,13 +49,19 @@ func getTemps(temps map[string]int) map[string]error {
switch sm := dev.(type) { switch sm := dev.(type) {
case *smart.SataDevice: case *smart.SataDevice:
data, _ := sm.ReadSMARTData() data, _ := sm.ReadSMARTData()
for _, attr := range data.Attrs { if err != nil {
if attr.Id == 194 { log.Print("error getting smart data")
temps[disk.Name+"_"+disk.Model] = int(attr.Value) return nil
} }
if attr, ok := data.Attrs[194]; ok {
temps[disk.Name+"_"+disk.Model] = int(attr.Value)
} }
case *smart.NVMeDevice: case *smart.NVMeDevice:
data, _ := sm.ReadSMART() data, _ := sm.ReadSMART()
if err != nil {
log.Print("error getting smart data")
return nil
}
temps[disk.Name+"_"+disk.Model] = int(data.Temperature) temps[disk.Name+"_"+disk.Model] = int(data.Temperature)
default: default:
} }