Added memory limits and system info for memory
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
package system
|
||||
|
||||
// SystemInfo holds system resource usage information.
|
||||
type SystemInfo struct {
|
||||
TotalMemMB uint64 `json:"total_mem_mb"`
|
||||
UsedMemMB uint64 `json:"used_mem_mb"`
|
||||
AvailMemMB uint64 `json:"avail_mem_mb"`
|
||||
MemPercent float64 `json:"mem_percent"`
|
||||
|
||||
DiskTotalGB float64 `json:"disk_total_gb"`
|
||||
DiskUsedGB float64 `json:"disk_used_gb"`
|
||||
DiskAvailGB float64 `json:"disk_avail_gb"`
|
||||
DiskPercent float64 `json:"disk_percent"`
|
||||
|
||||
HDDTotalGB float64 `json:"hdd_total_gb,omitempty"`
|
||||
HDDUsedGB float64 `json:"hdd_used_gb,omitempty"`
|
||||
HDDAvailGB float64 `json:"hdd_avail_gb,omitempty"`
|
||||
HDDPercent float64 `json:"hdd_percent,omitempty"`
|
||||
HDDConfigured bool `json:"hdd_configured"`
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
//go:build linux
|
||||
|
||||
package system
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"os"
|
||||
"strings"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
// GetInfo reads system memory and disk usage.
|
||||
// hddPath is the mount path for external HDD; if empty, HDD info is skipped.
|
||||
func GetInfo(hddPath string) SystemInfo {
|
||||
info := SystemInfo{}
|
||||
|
||||
// --- Memory from /proc/meminfo ---
|
||||
readMemInfo(&info)
|
||||
|
||||
// --- Root filesystem disk usage ---
|
||||
readDiskUsage("/", &info.DiskTotalGB, &info.DiskUsedGB, &info.DiskAvailGB, &info.DiskPercent)
|
||||
|
||||
// --- HDD disk usage (if configured) ---
|
||||
if hddPath != "" {
|
||||
info.HDDConfigured = true
|
||||
readDiskUsage(hddPath, &info.HDDTotalGB, &info.HDDUsedGB, &info.HDDAvailGB, &info.HDDPercent)
|
||||
}
|
||||
|
||||
return info
|
||||
}
|
||||
|
||||
func readMemInfo(info *SystemInfo) {
|
||||
f, err := os.Open("/proc/meminfo")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
var totalKB, availKB uint64
|
||||
scanner := bufio.NewScanner(f)
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
switch {
|
||||
case strings.HasPrefix(line, "MemTotal:"):
|
||||
totalKB = parseMemLine(line)
|
||||
case strings.HasPrefix(line, "MemAvailable:"):
|
||||
availKB = parseMemLine(line)
|
||||
}
|
||||
if totalKB > 0 && availKB > 0 {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if totalKB > 0 {
|
||||
info.TotalMemMB = totalKB / 1024
|
||||
info.AvailMemMB = availKB / 1024
|
||||
info.UsedMemMB = info.TotalMemMB - info.AvailMemMB
|
||||
info.MemPercent = float64(info.UsedMemMB) / float64(info.TotalMemMB) * 100
|
||||
}
|
||||
}
|
||||
|
||||
// parseMemLine extracts the kB value from a /proc/meminfo line like "MemTotal: 16384000 kB"
|
||||
func parseMemLine(line string) uint64 {
|
||||
// Remove label prefix up to ':'
|
||||
parts := strings.SplitN(line, ":", 2)
|
||||
if len(parts) < 2 {
|
||||
return 0
|
||||
}
|
||||
valStr := strings.TrimSpace(parts[1])
|
||||
valStr = strings.TrimSuffix(valStr, " kB")
|
||||
valStr = strings.TrimSpace(valStr)
|
||||
|
||||
var val uint64
|
||||
for _, c := range valStr {
|
||||
if c >= '0' && c <= '9' {
|
||||
val = val*10 + uint64(c-'0')
|
||||
}
|
||||
}
|
||||
return val
|
||||
}
|
||||
|
||||
func readDiskUsage(path string, totalGB, usedGB, availGB *float64, percent *float64) {
|
||||
var stat syscall.Statfs_t
|
||||
if err := syscall.Statfs(path, &stat); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
bsize := uint64(stat.Bsize)
|
||||
total := stat.Blocks * bsize
|
||||
avail := stat.Bavail * bsize
|
||||
used := total - (stat.Bfree * bsize) // Bfree includes reserved blocks
|
||||
|
||||
const gb = 1024 * 1024 * 1024
|
||||
*totalGB = float64(total) / gb
|
||||
*usedGB = float64(used) / gb
|
||||
*availGB = float64(avail) / gb
|
||||
if total > 0 {
|
||||
*percent = float64(used) / float64(total) * 100
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
//go:build !linux
|
||||
|
||||
package system
|
||||
|
||||
// GetInfo returns empty system info on non-Linux platforms.
|
||||
func GetInfo(_ string) SystemInfo {
|
||||
return SystemInfo{}
|
||||
}
|
||||
Reference in New Issue
Block a user