fix(hosts): calculate Used memory from Total-Free for host agents in LXC

The previous fix (4090d981) addressed memory reporting for Docker agents
running in LXC containers, but the same issue also affects host agents.
When gopsutil runs inside an LXC, it can read Total and Free memory
from cgroup limits, but reports 0 for Used memory.

Added the same Total - Free fallback calculation to the host agent
processing path, which populates the Hosts tab.

Fixes #1075
This commit is contained in:
rcourtman
2026-01-12 21:14:26 +00:00
parent 0cc295066c
commit d389345153
+10
View File
@@ -2362,6 +2362,16 @@ func (m *Monitor) ApplyHostReport(report agentshost.Report, tokenRecord *config.
SwapTotal: report.Metrics.Memory.SwapTotal,
SwapUsed: report.Metrics.Memory.SwapUsed,
}
// Fallback for LXC environments: gopsutil may read Total and Free correctly
// from cgroup limits but return 0 for Used. Calculate Used from Total - Free.
if memory.Used <= 0 && memory.Total > 0 && memory.Free > 0 {
memory.Used = memory.Total - memory.Free
if memory.Used < 0 {
memory.Used = 0
}
}
if memory.Usage <= 0 && memory.Total > 0 {
memory.Usage = safePercentage(float64(memory.Used), float64(memory.Total))
}