package system import ( "bufio" "os" "runtime" "strconv" "strings" "sync" "time" ) // Memory reports memory usage in bytes. type Memory struct { Total uint64 `json:"total"` Used uint64 `json:"used"` Available uint64 `json:"available"` SwapTotal uint64 `json:"swap_total"` SwapUsed uint64 `json:"swap_used"` } // Disk reports filesystem usage in bytes. type Disk struct { Path string `json:"path"` Total uint64 `json:"total"` Used uint64 `json:"used"` Free uint64 `json:"free"` } // Snapshot is the dashboard payload. type Snapshot struct { Hostname string `json:"hostname"` Distro Distro `json:"distro"` Uptime int64 `json:"uptime"` Load1 float64 `json:"load1"` Load5 float64 `json:"load5"` Load15 float64 `json:"load15"` CPUCores int `json:"cpu_cores"` CPUUsage float64 `json:"cpu_usage"` Memory Memory `json:"memory"` Disk Disk `json:"disk"` NetRX uint64 `json:"net_rx"` NetTX uint64 `json:"net_tx"` Time int64 `json:"time"` } // Collect gathers the current host metrics. func Collect(diskPath string) Snapshot { host, _ := os.Hostname() if diskPath == "" { diskPath = "/" } s := Snapshot{ Hostname: host, Distro: Detect(), CPUCores: runtime.NumCPU(), CPUUsage: cpuUsage(), Memory: readMemory(), Disk: diskUsage(diskPath), Time: time.Now().Unix(), } s.Uptime = readUptime() s.Load1, s.Load5, s.Load15 = readLoad() s.NetRX, s.NetTX = readNet() return s } func readUptime() int64 { b, err := os.ReadFile("/proc/uptime") if err != nil { return 0 } fields := strings.Fields(string(b)) if len(fields) == 0 { return 0 } f, _ := strconv.ParseFloat(fields[0], 64) return int64(f) } func readLoad() (float64, float64, float64) { b, err := os.ReadFile("/proc/loadavg") if err != nil { return 0, 0, 0 } fields := strings.Fields(string(b)) if len(fields) < 3 { return 0, 0, 0 } a, _ := strconv.ParseFloat(fields[0], 64) c, _ := strconv.ParseFloat(fields[1], 64) d, _ := strconv.ParseFloat(fields[2], 64) return a, c, d } func readMemory() Memory { b, err := os.ReadFile("/proc/meminfo") if err != nil { return Memory{} } vals := map[string]uint64{} sc := bufio.NewScanner(strings.NewReader(string(b))) for sc.Scan() { key, rest, ok := strings.Cut(sc.Text(), ":") if !ok { continue } fields := strings.Fields(rest) if len(fields) == 0 { continue } v, _ := strconv.ParseUint(fields[0], 10, 64) vals[key] = v * 1024 } var m Memory m.Total = vals["MemTotal"] m.Available = vals["MemAvailable"] if m.Available == 0 { m.Available = vals["MemFree"] + vals["Buffers"] + vals["Cached"] } if m.Total > m.Available { m.Used = m.Total - m.Available } m.SwapTotal = vals["SwapTotal"] if m.SwapTotal > vals["SwapFree"] { m.SwapUsed = m.SwapTotal - vals["SwapFree"] } return m } func readNet() (uint64, uint64) { b, err := os.ReadFile("/proc/net/dev") if err != nil { return 0, 0 } var rx, tx uint64 sc := bufio.NewScanner(strings.NewReader(string(b))) for sc.Scan() { line := sc.Text() idx := strings.IndexByte(line, ':') if idx < 0 { continue } name := strings.TrimSpace(line[:idx]) if name == "lo" { continue } fields := strings.Fields(line[idx+1:]) if len(fields) < 9 { continue } r, _ := strconv.ParseUint(fields[0], 10, 64) t, _ := strconv.ParseUint(fields[8], 10, 64) rx += r tx += t } return rx, tx } var cpuSample struct { sync.Mutex lastTotal uint64 lastIdle uint64 lastAt time.Time lastValue float64 } // cpuUsage returns the busy percentage since the previous sample. The very // first call measures a short 200ms window so the dashboard never flashes 0%. func cpuUsage() float64 { data, err := os.ReadFile("/proc/stat") if err != nil { return 0 } total, idle := parseCPUStat(string(data)) if total == 0 { return 0 } cpuSample.Lock() defer cpuSample.Unlock() if cpuSample.lastAt.IsZero() { time.Sleep(200 * time.Millisecond) second, err := os.ReadFile("/proc/stat") if err != nil { cpuSample.lastTotal, cpuSample.lastIdle, cpuSample.lastAt = total, idle, time.Now() return 0 } t2, i2 := parseCPUStat(string(second)) cpuSample.lastTotal, cpuSample.lastIdle, cpuSample.lastAt = t2, i2, time.Now() dTotal := float64(t2) - float64(total) dIdle := float64(i2) - float64(idle) if dTotal > 0 { cpuSample.lastValue = clampPercent((dTotal - dIdle) / dTotal * 100) } return cpuSample.lastValue } dTotal := float64(total) - float64(cpuSample.lastTotal) dIdle := float64(idle) - float64(cpuSample.lastIdle) cpuSample.lastTotal, cpuSample.lastIdle, cpuSample.lastAt = total, idle, time.Now() if dTotal <= 0 { return cpuSample.lastValue } cpuSample.lastValue = clampPercent((dTotal - dIdle) / dTotal * 100) return cpuSample.lastValue } func parseCPUStat(data string) (total, idle uint64) { line := strings.SplitN(data, "\n", 2)[0] fields := strings.Fields(line) if len(fields) < 5 { return 0, 0 } for i, f := range fields[1:] { v, _ := strconv.ParseUint(f, 10, 64) total += v if i == 3 || i == 4 { // idle + iowait idle += v } } return total, idle } func clampPercent(v float64) float64 { if v < 0 { return 0 } if v > 100 { return 100 } return v }