77 lines
1.5 KiB
Go
77 lines
1.5 KiB
Go
package proc
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
)
|
|
|
|
type IntelReader struct {
|
|
hwmonDir string
|
|
driver string
|
|
}
|
|
|
|
func NewIntelReader() (*IntelReader, error) {
|
|
// Locate Intel coretemp hwmon path
|
|
dirs, err := filepath.Glob("/sys/class/hwmon/hwmon*")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
hwmonDir := ""
|
|
for _, d := range dirs {
|
|
nameData, err := os.ReadFile(filepath.Join(d, "name"))
|
|
if err == nil && strings.TrimSpace(string(nameData)) == "coretemp" {
|
|
hwmonDir = d
|
|
break
|
|
}
|
|
}
|
|
|
|
if hwmonDir == "" {
|
|
return nil, fmt.Errorf("coretemp driver not found under /sys/class/hwmon/")
|
|
}
|
|
|
|
return &IntelReader{
|
|
hwmonDir: hwmonDir,
|
|
driver: "coretemp",
|
|
}, nil
|
|
}
|
|
|
|
func (r *IntelReader) ReadTemperatureSensors() ([]TempSensor, error) {
|
|
inputs, err := filepath.Glob(filepath.Join(r.hwmonDir, "temp*_input"))
|
|
if err != nil || len(inputs) == 0 {
|
|
return nil, fmt.Errorf("no temperature inputs found in %s", r.hwmonDir)
|
|
}
|
|
|
|
temps := []TempSensor{}
|
|
|
|
for _, inputPath := range inputs {
|
|
valBytes, err := os.ReadFile(inputPath)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
|
|
milliC, err := strconv.ParseFloat(strings.TrimSpace(string(valBytes)), 64)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
|
|
labelPath := strings.TrimSuffix(inputPath, "_input") + "_label"
|
|
label := ""
|
|
if lBytes, err := os.ReadFile(labelPath); err == nil {
|
|
label = strings.TrimSpace(string(lBytes))
|
|
} else {
|
|
label = filepath.Base(strings.TrimSuffix(inputPath, "_input"))
|
|
}
|
|
|
|
temps = append(temps, TempSensor{
|
|
Name: label,
|
|
TempC: milliC / 1000.0,
|
|
})
|
|
}
|
|
|
|
return temps, nil
|
|
}
|