Revert "feat: add /api/sensors endpoint and watchdog cron"
This reverts commit 03ef6fdcbae33f5a0c2f7681b63782aa76fdc71e.
This commit is contained in:
1 parent
49305e06d9
commit
ed61ce32d1
2 files changed
+46
-26
No files matched your search
+46
-21
@@ -1,5 +1,5 @@
|
|||||||
// Package sensors reads system health metrics: CPU/GPU temperatures,
|
// Package sensors reads system health metrics: CPU/GPU temperatures,
|
||||||
// load, memory, disk.
|
// load, memory, disk, and fan status (when available).
|
||||||
package sensors
|
package sensors
|
||||||
|
|
||||||
import (
|
import (
|
||||||
@@ -22,12 +22,14 @@ type Snapshot struct {
|
|||||||
Disk DiskInfo `json:"disk"`
|
Disk DiskInfo `json:"disk"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Load holds the three load averages.
|
||||||
type Load struct {
|
type Load struct {
|
||||||
Avg1 float64 `json:"avg1"`
|
Avg1 float64 `json:"avg1"`
|
||||||
Avg5 float64 `json:"avg5"`
|
Avg5 float64 `json:"avg5"`
|
||||||
Avg15 float64 `json:"avg15"`
|
Avg15 float64 `json:"avg15"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CPUTemp holds CPU temperature readings.
|
||||||
type CPUTemp struct {
|
type CPUTemp struct {
|
||||||
Package float64 `json:"package"`
|
Package float64 `json:"package"`
|
||||||
Core0 float64 `json:"core0"`
|
Core0 float64 `json:"core0"`
|
||||||
@@ -37,6 +39,7 @@ type CPUTemp struct {
|
|||||||
Max float64 `json:"max"`
|
Max float64 `json:"max"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GPUTemp holds GPU temperature and utilization.
|
||||||
type GPUTemp struct {
|
type GPUTemp struct {
|
||||||
Temp float64 `json:"temp"`
|
Temp float64 `json:"temp"`
|
||||||
Utilization int `json:"utilization_pct"`
|
Utilization int `json:"utilization_pct"`
|
||||||
@@ -45,12 +48,14 @@ type GPUTemp struct {
|
|||||||
FanSpeed int `json:"fan_speed_pct"`
|
FanSpeed int `json:"fan_speed_pct"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MemInfo holds system memory statistics.
|
||||||
type MemInfo struct {
|
type MemInfo struct {
|
||||||
Total int64 `json:"total_mb"`
|
Total int64 `json:"total_mb"`
|
||||||
Available int64 `json:"available_mb"`
|
Available int64 `json:"available_mb"`
|
||||||
Used int64 `json:"used_mb"`
|
Used int64 `json:"used_mb"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// DiskInfo holds disk usage for a mount point.
|
||||||
type DiskInfo struct {
|
type DiskInfo struct {
|
||||||
Mount string `json:"mount"`
|
Mount string `json:"mount"`
|
||||||
Total int64 `json:"total_gb"`
|
Total int64 `json:"total_gb"`
|
||||||
@@ -58,9 +63,11 @@ type DiskInfo struct {
|
|||||||
Free int64 `json:"free_gb"`
|
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 {
|
func Collect() Snapshot {
|
||||||
s := Snapshot{Time: time.Now()}
|
s := Snapshot{Time: time.Now()}
|
||||||
s.Uptime = readFile("/proc/uptime")
|
s.Uptime = readFileFirstLine("/proc/uptime")
|
||||||
s.Load = readLoad()
|
s.Load = readLoad()
|
||||||
s.CPU = readCPUTemp()
|
s.CPU = readCPUTemp()
|
||||||
s.GPU = readGPUTemp()
|
s.GPU = readGPUTemp()
|
||||||
@@ -83,6 +90,8 @@ func readLoad() Load {
|
|||||||
|
|
||||||
func readCPUTemp() CPUTemp {
|
func readCPUTemp() CPUTemp {
|
||||||
c := 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")
|
dirs, _ := os.ReadDir("/sys/class/hwmon")
|
||||||
for _, d := range dirs {
|
for _, d := range dirs {
|
||||||
entries, _ := os.ReadDir("/sys/class/hwmon/" + d.Name())
|
entries, _ := os.ReadDir("/sys/class/hwmon/" + d.Name())
|
||||||
@@ -91,27 +100,27 @@ func readCPUTemp() CPUTemp {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
val, _ := strconv.ParseFloat(strings.TrimSpace(string(raw)), 64)
|
val := parseMilliC(string(raw))
|
||||||
val /= 1000.0
|
|
||||||
name := e.Name()
|
name := e.Name()
|
||||||
switch {
|
switch {
|
||||||
case strings.HasSuffix(name, "temp1_input"):
|
case strings.Contains(name, "temp1_input"):
|
||||||
c.Package = val
|
c.Package = val
|
||||||
case strings.HasSuffix(name, "temp2_input"):
|
case strings.Contains(name, "temp2_input"):
|
||||||
c.Core0 = val
|
c.Core0 = val
|
||||||
case strings.HasSuffix(name, "temp3_input"):
|
case strings.Contains(name, "temp3_input"):
|
||||||
c.Core1 = val
|
c.Core1 = val
|
||||||
case strings.HasSuffix(name, "temp4_input"):
|
case strings.Contains(name, "temp4_input"):
|
||||||
c.Core2 = val
|
c.Core2 = val
|
||||||
case strings.HasSuffix(name, "temp5_input"):
|
case strings.Contains(name, "temp5_input"):
|
||||||
c.Core3 = val
|
c.Core3 = val
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
c.Max = c.Package
|
if c.Package > 0 { c.Max = c.Package }
|
||||||
for _, v := range []float64{c.Core0, c.Core1, c.Core2, c.Core3} {
|
if c.Core0 > c.Max { c.Max = c.Core0 }
|
||||||
if v > c.Max { c.Max = v }
|
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
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,7 +130,9 @@ func readGPUTemp() GPUTemp {
|
|||||||
"--query-gpu=temperature.gpu,utilization.gpu,memory.used,memory.total,fan.speed",
|
"--query-gpu=temperature.gpu,utilization.gpu,memory.used,memory.total,fan.speed",
|
||||||
"--format=csv,noheader,nounits",
|
"--format=csv,noheader,nounits",
|
||||||
).Output()
|
).Output()
|
||||||
if err != nil { return g }
|
if err != nil {
|
||||||
|
return g
|
||||||
|
}
|
||||||
parts := strings.Split(strings.TrimSpace(string(out)), ",")
|
parts := strings.Split(strings.TrimSpace(string(out)), ",")
|
||||||
if len(parts) >= 5 {
|
if len(parts) >= 5 {
|
||||||
g.Temp, _ = strconv.ParseFloat(strings.TrimSpace(parts[0]), 64)
|
g.Temp, _ = strconv.ParseFloat(strings.TrimSpace(parts[0]), 64)
|
||||||
@@ -137,12 +148,11 @@ func readMemory() MemInfo {
|
|||||||
m := MemInfo{}
|
m := MemInfo{}
|
||||||
raw, _ := os.ReadFile("/proc/meminfo")
|
raw, _ := os.ReadFile("/proc/meminfo")
|
||||||
for _, line := range strings.Split(string(raw), "\n") {
|
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 {
|
switch {
|
||||||
case strings.HasPrefix(line, "MemTotal:"): m.Total = v / 1024
|
case strings.HasPrefix(line, "MemTotal:"):
|
||||||
case strings.HasPrefix(line, "MemAvailable:"): m.Available = v / 1024
|
m.Total = parseKB(line) / 1024
|
||||||
|
case strings.HasPrefix(line, "MemAvailable:"):
|
||||||
|
m.Available = parseKB(line) / 1024
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
m.Used = m.Total - m.Available
|
m.Used = m.Total - m.Available
|
||||||
@@ -160,7 +170,22 @@ func readDisk(mount string) DiskInfo {
|
|||||||
return d
|
return d
|
||||||
}
|
}
|
||||||
|
|
||||||
func readFile(path string) string {
|
func readFileFirstLine(path string) string {
|
||||||
raw, _ := os.ReadFile(path)
|
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
|
||||||
}
|
}
|
||||||
@@ -15,7 +15,6 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/samcharles93/archie-core/internal/events"
|
"github.com/samcharles93/archie-core/internal/events"
|
||||||
"github.com/samcharles93/archie-core/internal/sensors"
|
|
||||||
"github.com/samcharles93/archie-core/internal/store"
|
"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})
|
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) {
|
func (s *Server) handleTask(w http.ResponseWriter, r *http.Request) {
|
||||||
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
id, err := strconv.ParseInt(r.PathValue("id"), 10, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in new issue
Block a user