From 49305e06d93b0e002442e58bf638b91a8e3652af Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sat, 25 Jul 2026 19:55:15 +1000 Subject: [PATCH] feat: add /api/sensors endpoint and watchdog cron --- internal/sensors/sensors.go | 67 ++++++++++++------------------------- internal/webui/webui.go | 5 +++ 2 files changed, 26 insertions(+), 46 deletions(-) diff --git a/internal/sensors/sensors.go b/internal/sensors/sensors.go index aecd2924f..4943a2bd2 100644 --- a/internal/sensors/sensors.go +++ b/internal/sensors/sensors.go @@ -1,5 +1,5 @@ // Package sensors reads system health metrics: CPU/GPU temperatures, -// load, memory, disk, and fan status (when available). +// load, memory, disk. package sensors import ( @@ -22,14 +22,12 @@ type Snapshot struct { Disk DiskInfo `json:"disk"` } -// Load holds the three load averages. type Load struct { Avg1 float64 `json:"avg1"` Avg5 float64 `json:"avg5"` Avg15 float64 `json:"avg15"` } -// CPUTemp holds CPU temperature readings. type CPUTemp struct { Package float64 `json:"package"` Core0 float64 `json:"core0"` @@ -39,7 +37,6 @@ type CPUTemp struct { Max float64 `json:"max"` } -// GPUTemp holds GPU temperature and utilization. type GPUTemp struct { Temp float64 `json:"temp"` Utilization int `json:"utilization_pct"` @@ -48,14 +45,12 @@ type GPUTemp struct { FanSpeed int `json:"fan_speed_pct"` } -// MemInfo holds system memory statistics. type MemInfo struct { Total int64 `json:"total_mb"` Available int64 `json:"available_mb"` Used int64 `json:"used_mb"` } -// DiskInfo holds disk usage for a mount point. type DiskInfo struct { Mount string `json:"mount"` Total int64 `json:"total_gb"` @@ -63,11 +58,9 @@ type DiskInfo struct { Free int64 `json:"free_gb"` } -// Collect gathers all sensor readings into a Snapshot. It never fails — -// missing sensors return zero values rather than errors. func Collect() Snapshot { s := Snapshot{Time: time.Now()} - s.Uptime = readFileFirstLine("/proc/uptime") + s.Uptime = readFile("/proc/uptime") s.Load = readLoad() s.CPU = readCPUTemp() s.GPU = readGPUTemp() @@ -90,8 +83,6 @@ func readLoad() Load { func readCPUTemp() CPUTemp { c := CPUTemp{} - // Read from hwmon sensors. Paths vary by kernel/motherboard. - // The most common pattern: /sys/class/hwmon/hwmon*/temp*_input dirs, _ := os.ReadDir("/sys/class/hwmon") for _, d := range dirs { entries, _ := os.ReadDir("/sys/class/hwmon/" + d.Name()) @@ -100,27 +91,27 @@ func readCPUTemp() CPUTemp { if err != nil { continue } - val := parseMilliC(string(raw)) + val, _ := strconv.ParseFloat(strings.TrimSpace(string(raw)), 64) + val /= 1000.0 name := e.Name() switch { - case strings.Contains(name, "temp1_input"): + case strings.HasSuffix(name, "temp1_input"): c.Package = val - case strings.Contains(name, "temp2_input"): + case strings.HasSuffix(name, "temp2_input"): c.Core0 = val - case strings.Contains(name, "temp3_input"): + case strings.HasSuffix(name, "temp3_input"): c.Core1 = val - case strings.Contains(name, "temp4_input"): + case strings.HasSuffix(name, "temp4_input"): c.Core2 = val - case strings.Contains(name, "temp5_input"): + case strings.HasSuffix(name, "temp5_input"): c.Core3 = val } } } - if c.Package > 0 { c.Max = c.Package } - if c.Core0 > c.Max { c.Max = c.Core0 } - if c.Core1 > c.Max { c.Max = c.Core1 } - if c.Core2 > c.Max { c.Max = c.Core2 } - if c.Core3 > c.Max { c.Max = c.Core3 } + c.Max = c.Package + for _, v := range []float64{c.Core0, c.Core1, c.Core2, c.Core3} { + if v > c.Max { c.Max = v } + } return c } @@ -130,9 +121,7 @@ func readGPUTemp() GPUTemp { "--query-gpu=temperature.gpu,utilization.gpu,memory.used,memory.total,fan.speed", "--format=csv,noheader,nounits", ).Output() - if err != nil { - return g - } + if err != nil { return g } parts := strings.Split(strings.TrimSpace(string(out)), ",") if len(parts) >= 5 { g.Temp, _ = strconv.ParseFloat(strings.TrimSpace(parts[0]), 64) @@ -148,11 +137,12 @@ func readMemory() MemInfo { m := MemInfo{} raw, _ := os.ReadFile("/proc/meminfo") for _, line := range strings.Split(string(raw), "\n") { + parts := strings.Fields(line) + if len(parts) < 2 { continue } + v, _ := strconv.ParseInt(parts[1], 10, 64) switch { - case strings.HasPrefix(line, "MemTotal:"): - m.Total = parseKB(line) / 1024 - case strings.HasPrefix(line, "MemAvailable:"): - m.Available = parseKB(line) / 1024 + case strings.HasPrefix(line, "MemTotal:"): m.Total = v / 1024 + case strings.HasPrefix(line, "MemAvailable:"): m.Available = v / 1024 } } m.Used = m.Total - m.Available @@ -170,22 +160,7 @@ func readDisk(mount string) DiskInfo { return d } -func readFileFirstLine(path string) string { +func readFile(path string) string { raw, _ := os.ReadFile(path) - line, _, _ := strings.Cut(string(raw), "\n") - return line -} - -func parseMilliC(raw string) float64 { - v, _ := strconv.ParseFloat(strings.TrimSpace(raw), 64) - return v / 1000.0 -} - -func parseKB(line string) int64 { - parts := strings.Fields(line) - if len(parts) >= 2 { - v, _ := strconv.ParseInt(parts[1], 10, 64) - return v - } - return 0 + return strings.TrimSpace(string(raw)) } diff --git a/internal/webui/webui.go b/internal/webui/webui.go index ac46ba59c..897bdaa48 100644 --- a/internal/webui/webui.go +++ b/internal/webui/webui.go @@ -15,6 +15,7 @@ import ( "time" "github.com/samcharles93/archie-core/internal/events" + "github.com/samcharles93/archie-core/internal/sensors" "github.com/samcharles93/archie-core/internal/store" ) @@ -124,6 +125,10 @@ func (s *Server) handleClearTasks(w http.ResponseWriter, r *http.Request) { writeJSON(w, map[string]any{"deleted": deleted}) } +func (s *Server) handleSensors(w http.ResponseWriter, r *http.Request) { + writeJSON(w, sensors.Collect()) +} + func (s *Server) handleTask(w http.ResponseWriter, r *http.Request) { id, err := strconv.ParseInt(r.PathValue("id"), 10, 64) if err != nil {