diff --git a/internal/sensors/sensors.go b/internal/sensors/sensors.go index 4943a2bd2..aecd2924f 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. +// load, memory, disk, and fan status (when available). package sensors import ( @@ -22,12 +22,14 @@ 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"` @@ -37,6 +39,7 @@ 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"` @@ -45,12 +48,14 @@ 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"` @@ -58,9 +63,11 @@ 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 = readFile("/proc/uptime") + s.Uptime = readFileFirstLine("/proc/uptime") s.Load = readLoad() s.CPU = readCPUTemp() s.GPU = readGPUTemp() @@ -83,6 +90,8 @@ 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()) @@ -91,27 +100,27 @@ func readCPUTemp() CPUTemp { if err != nil { continue } - val, _ := strconv.ParseFloat(strings.TrimSpace(string(raw)), 64) - val /= 1000.0 + val := parseMilliC(string(raw)) name := e.Name() switch { - case strings.HasSuffix(name, "temp1_input"): + case strings.Contains(name, "temp1_input"): c.Package = val - case strings.HasSuffix(name, "temp2_input"): + case strings.Contains(name, "temp2_input"): c.Core0 = val - case strings.HasSuffix(name, "temp3_input"): + case strings.Contains(name, "temp3_input"): c.Core1 = val - case strings.HasSuffix(name, "temp4_input"): + case strings.Contains(name, "temp4_input"): c.Core2 = val - case strings.HasSuffix(name, "temp5_input"): + case strings.Contains(name, "temp5_input"): c.Core3 = val } } } - c.Max = c.Package - for _, v := range []float64{c.Core0, c.Core1, c.Core2, c.Core3} { - if v > c.Max { c.Max = v } - } + 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 } return c } @@ -121,7 +130,9 @@ 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) @@ -137,12 +148,11 @@ 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 = v / 1024 - case strings.HasPrefix(line, "MemAvailable:"): m.Available = v / 1024 + case strings.HasPrefix(line, "MemTotal:"): + m.Total = parseKB(line) / 1024 + case strings.HasPrefix(line, "MemAvailable:"): + m.Available = parseKB(line) / 1024 } } m.Used = m.Total - m.Available @@ -160,7 +170,22 @@ func readDisk(mount string) DiskInfo { return d } -func readFile(path string) string { +func readFileFirstLine(path string) string { raw, _ := os.ReadFile(path) - return strings.TrimSpace(string(raw)) + 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 } diff --git a/internal/webui/webui.go b/internal/webui/webui.go index 897bdaa48..ac46ba59c 100644 --- a/internal/webui/webui.go +++ b/internal/webui/webui.go @@ -15,7 +15,6 @@ import ( "time" "github.com/samcharles93/archie-core/internal/events" - "github.com/samcharles93/archie-core/internal/sensors" "github.com/samcharles93/archie-core/internal/store" ) @@ -125,10 +124,6 @@ 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 {