Linux hardware sensor monitoring in Go.
Uses the lm-sensors (linux monitoring sensors) pacakge, on top of the hwmon kernel feature.
- Install lm-sensors
- Ubuntu:
sudo apt install lm-sensors libsensors-dev - Arch:
pacman -S lm_sensors
- Ubuntu:
- Configure lm-sensors
- Run
sensors-detect - Make any necessary adjustments to the configuration in
/etc/sensors3.conf, using/etc/sensors.d/*
- Run
go get github.com/mt-inside/go-lmsensors
This module links against the C-language libsensors and calls it to get sensor readings from the hwmon kernel subsystem (which it reads from sysfs).
My original version ran and parsed sensors -j, as all the information is in that JSON if you really squint and know how to read it. However, using the library direct seemed faster, avoids a fork(), and doesn't require lm-sensors to be installed, just libsensors5 (some package managers have them separately). (The instructions say to install lm-sensors, becuase you almost certainly want to run sensors-detect.)
The hwmon data are exposed through sysfs, but those are raw values - libsensors isn't just a convenience binding; it scales raw values according to a big built-in database, and lets the user rename sensors.
package main import ( "fmt" "log" "github.com/mt-inside/go-lmsensors" ) func main() { sensors, err := golmsensors.Get(true, true) if err != nil { log.Fatalf("Can't get sensor readings: %v", err) } for _, chip := range sensors.ChipsList { fmt.Println(chip.ID) for _, reading := range chip.SensorsList { fmt.Printf(" [%s] %s: %s\n", reading.SensorType, reading.Name, reading.Value) } } }it8792-isa-0a60 [In] PM_CLDO12: 1.504000 [Fan] SYS_FAN4: 0.000000 [In] VIN0: 1.788000 [In] DDR VTT: 0.665000 [In] Chipset Core: 1.090000 [In] six: 2.780000 [Temp] PCIEX4_1: 37.000000 [Temp] System2: 34.000000 ...