rmm2/agent/collector/memory.go
cynfo3000 6120d0cffa Initial commit: RMM Agent + Backend
- Go Agent (FreeBSD/amd64) fuer OPNsense
- Go Backend (Linux/amd64) mit REST API + SQLite
- Collector: Hardware, CPU, Memory, Disks, Network, Services,
  WireGuard, DHCP (KEA/ISC/dnsmasq), Routes, Gateways,
  Certificates, Plugins, Updates
- Persistente Agent-ID
- TLS + API-Key Auth
- WebSocket-Infrastruktur (WIP): bidirektionaler Command-Kanal,
  TCP-Tunnel fuer Remote-Zugriff auf Firewall-WebUI
- Lastenheft und README
2026-02-28 07:38:14 +01:00

43 lines
986 B
Go

package collector
import (
"strconv"
)
type MemoryInfo struct {
TotalBytes int64 `json:"total_bytes"`
UsedBytes int64 `json:"used_bytes"`
FreeBytes int64 `json:"free_bytes"`
}
func CollectMemory() MemoryInfo {
info := MemoryInfo{}
// Gesamter physischer Speicher
if v, err := strconv.ParseInt(sysctlGet("hw.physmem"), 10, 64); err == nil {
info.TotalBytes = v
}
// Freier Speicher berechnen via vm.stats.vm
pageSize := int64(4096)
if v, err := strconv.ParseInt(sysctlGet("hw.pagesize"), 10, 64); err == nil && v > 0 {
pageSize = v
}
// Freie + inactive + cache Pages
freePages, _ := strconv.ParseInt(sysctlGet("vm.stats.vm.v_free_count"), 10, 64)
inactivePages, _ := strconv.ParseInt(sysctlGet("vm.stats.vm.v_inactive_count"), 10, 64)
info.FreeBytes = (freePages + inactivePages) * pageSize
info.UsedBytes = info.TotalBytes - info.FreeBytes
if info.UsedBytes < 0 {
info.UsedBytes = 0
}
if info.FreeBytes < 0 {
info.FreeBytes = 0
}
return info
}