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"
|
|
|
|
)
|
|
|
|
|
2022-06-20 04:11:56 +08:00
|
|
|
var smDevices map[string]smart.Device
|
2022-06-19 00:55:38 +08:00
|
|
|
|
2020-06-09 22:03:58 +08:00
|
|
|
func init() {
|
|
|
|
devs() // Populate the sensorMap
|
2022-06-19 00:55:38 +08:00
|
|
|
RegisterStartup(startBlock)
|
2020-06-09 22:03:58 +08:00
|
|
|
RegisterTemp(getTemps)
|
|
|
|
RegisterDeviceList(Temperatures, devs, defs)
|
2022-06-19 00:55:38 +08:00
|
|
|
RegisterShutdown(endBlock)
|
|
|
|
}
|
|
|
|
|
|
|
|
func startBlock(vars map[string]string) error {
|
2022-06-20 04:11:56 +08:00
|
|
|
smDevices = make(map[string]smart.Device)
|
|
|
|
|
2022-06-19 00:55:38 +08:00
|
|
|
block, err := ghw.Block()
|
|
|
|
if err != nil {
|
2022-06-20 04:11:56 +08:00
|
|
|
log.Printf("error getting block device info: %s", err)
|
2022-06-19 00:55:38 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, disk := range block.Disks {
|
|
|
|
dev, err := smart.Open("/dev/" + disk.Name)
|
2022-06-20 04:11:56 +08:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("error opening smart info for %s: %s", disk.Name, err)
|
|
|
|
continue
|
2022-06-19 00:55:38 +08:00
|
|
|
}
|
2022-06-20 04:11:56 +08:00
|
|
|
smDevices[disk.Name+"_"+disk.Model] = dev
|
2022-06-19 00:55:38 +08:00
|
|
|
}
|
2022-06-20 04:11:56 +08:00
|
|
|
return nil
|
2022-06-19 00:55:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func endBlock() error {
|
2022-06-20 04:11:56 +08:00
|
|
|
for name, dev := range smDevices {
|
2022-06-19 00:55:38 +08:00
|
|
|
err := dev.Close()
|
|
|
|
if err != nil {
|
2022-06-20 04:11:56 +08:00
|
|
|
log.Printf("error closing device %s: %s", name, err)
|
2022-06-19 00:55:38 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2020-06-09 22:03:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2022-06-20 04:11:56 +08:00
|
|
|
for name, dev := range smDevices {
|
2022-09-18 03:52:54 +08:00
|
|
|
attr, err := dev.ReadGenericAttributes()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error getting smart data for %s: %s", name, err)
|
|
|
|
continue
|
2022-06-07 03:18:59 +08:00
|
|
|
}
|
2022-09-18 03:52:54 +08:00
|
|
|
temps[name] = int(attr.Temperature)
|
2022-06-07 03:18:59 +08:00
|
|
|
}
|
2020-06-09 22:03:58 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Optimization to avoid string manipulation every update
|
|
|
|
var sensorMap map[string]string
|