2022-05-13 20:48:12 +08:00
|
|
|
//go:build linux || darwin
|
2020-06-09 22:03:58 +08:00
|
|
|
// +build linux darwin
|
|
|
|
|
|
|
|
package devices
|
|
|
|
|
|
|
|
import (
|
2022-06-07 03:18:59 +08:00
|
|
|
"log"
|
|
|
|
|
2022-06-14 06:28:37 +08:00
|
|
|
"github.com/anatol/smart.go"
|
2022-06-07 03:18:59 +08:00
|
|
|
"github.com/jaypipes/ghw"
|
2020-06-09 22:03:58 +08:00
|
|
|
"github.com/shirou/gopsutil/host"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
devs() // Populate the sensorMap
|
|
|
|
RegisterTemp(getTemps)
|
|
|
|
RegisterDeviceList(Temperatures, devs, defs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTemps(temps map[string]int) map[string]error {
|
|
|
|
sensors, err := host.SensorsTemperatures()
|
|
|
|
if err != nil {
|
2022-05-13 20:48:12 +08:00
|
|
|
if _, ok := err.(*host.Warnings); ok {
|
|
|
|
// ignore warnings
|
|
|
|
} else {
|
|
|
|
return map[string]error{"gopsutil host": err}
|
|
|
|
}
|
2020-06-09 22:03:58 +08:00
|
|
|
}
|
|
|
|
for _, sensor := range sensors {
|
|
|
|
label := sensorMap[sensor.SensorKey]
|
|
|
|
if _, ok := temps[label]; ok {
|
|
|
|
temps[label] = int(sensor.Temperature)
|
|
|
|
}
|
|
|
|
}
|
2022-06-07 03:18:59 +08:00
|
|
|
|
|
|
|
block, err := ghw.Block()
|
|
|
|
if err != nil {
|
|
|
|
log.Print("error getting block device info")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, disk := range block.Disks {
|
2022-06-08 02:31:53 +08:00
|
|
|
dev, err := smart.Open("/dev/" + disk.Name)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2022-06-09 00:37:41 +08:00
|
|
|
defer dev.Close()
|
|
|
|
|
2022-06-08 02:31:53 +08:00
|
|
|
switch sm := dev.(type) {
|
|
|
|
case *smart.SataDevice:
|
2022-06-16 05:01:53 +08:00
|
|
|
data, err := sm.ReadSMARTData()
|
2022-06-14 06:28:37 +08:00
|
|
|
if err != nil {
|
2022-06-16 05:01:53 +08:00
|
|
|
log.Print("error getting smart data for " + disk.Name + "_" + disk.Model)
|
2022-06-14 06:28:37 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if attr, ok := data.Attrs[194]; ok {
|
2022-06-16 05:01:53 +08:00
|
|
|
val, _, _, _, err := attr.ParseAsTemperature()
|
|
|
|
if err != nil {
|
|
|
|
log.Print("error parsing temperature smart data for " + disk.Name + "_" + disk.Model)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
temps[disk.Name+"_"+disk.Model] = int(val)
|
2022-06-07 03:18:59 +08:00
|
|
|
}
|
2022-06-08 02:31:53 +08:00
|
|
|
case *smart.NVMeDevice:
|
2022-06-16 05:01:53 +08:00
|
|
|
data, err := sm.ReadSMART()
|
2022-06-14 06:28:37 +08:00
|
|
|
if err != nil {
|
2022-06-16 05:01:53 +08:00
|
|
|
log.Print("error getting smart data for " + disk.Name + "_" + disk.Model)
|
2022-06-14 06:28:37 +08:00
|
|
|
return nil
|
|
|
|
}
|
2022-06-08 02:31:53 +08:00
|
|
|
temps[disk.Name+"_"+disk.Model] = int(data.Temperature)
|
2022-06-07 03:18:59 +08:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
2020-06-09 22:03:58 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Optimization to avoid string manipulation every update
|
|
|
|
var sensorMap map[string]string
|