43 lines
986 B
Go
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
|
|
}
|