initial proof of concept

This commit is contained in:
alu
2026-08-04 20:12:47 +00:00
commit cc4698e311
10 changed files with 543 additions and 0 deletions
+85
View File
@@ -0,0 +1,85 @@
package proc
import (
"bufio"
"fmt"
"os"
"strings"
)
type Proc struct {
Info map[string]string
SensorReader CPUSensorReader
}
type CPUSensorReader interface {
ReadTemperatureSensors() ([]TempSensor, error)
}
type TempSensor struct {
Name string
TempC float64
}
const (
VendorIntel string = "GenuineIntel"
VendorAMD string = "AuthenticAMD"
VendorUnknown string = "Unknown"
)
func GetProc() (Proc, error) {
// open and parse cpuinfo
proc := Proc{}
procFile, err := os.Open("/proc/cpuinfo")
if err != nil {
return proc, err
}
defer procFile.Close()
proc.Info = map[string]string{}
scanner := bufio.NewScanner(procFile)
for scanner.Scan() {
line := scanner.Text()
line = strings.ReplaceAll(line, "\t", "")
if line != "" {
x := strings.Split(line, ": ")
if len(x) != 2 {
continue
}
key := x[0]
value := x[1]
proc.Info[key] = value
}
}
return proc, proc.getSensorReader()
}
func (proc *Proc) getSensorReader() error {
// get vendor string and initialize sensor reader
vendor := proc.Vendor()
if vendor == VendorIntel {
intelReader, err := NewIntelReader()
proc.SensorReader = intelReader
return err
}
if vendor == VendorAMD {
amdReader, err := NewAMDReader()
proc.SensorReader = amdReader
return err
}
return fmt.Errorf("proc type %s is not supported", vendor)
}
func (proc *Proc) Vendor() string {
return proc.Info["vendor_id"]
}
func (proc *Proc) Model() string {
return proc.Info["model name"]
}
+80
View File
@@ -0,0 +1,80 @@
package proc
import (
"fmt"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
type AMDReader struct {
hwmonDir string
driver string
}
type PowerState struct {
lastEnergy uint64
lastTime time.Time
}
func NewAMDReader() (*AMDReader, error) {
// Locate AMD k10temp or zenpower hwmon path
matches, err := filepath.Glob("/sys/class/hwmon/hwmon*")
if err != nil {
return nil, err
}
for _, dir := range matches {
b, err := os.ReadFile(filepath.Join(dir, "name"))
if err != nil {
continue
}
drv := strings.TrimSpace(string(b))
if drv == "k10temp" || drv == "zenpower" {
return &AMDReader{
hwmonDir: dir,
driver: drv,
}, nil
}
}
return nil, fmt.Errorf("k10temp/zenpower driver not found under /sys/class/hwmon/")
}
func (r *AMDReader) 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
}
+76
View File
@@ -0,0 +1,76 @@
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
}