Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

Harden Proxmox guest memory fallback selection

rcourtman1a0119723137
diff --git a/docs/release-control/v6/internal/subsystems/monitoring.md b/docs/release-control/v6/internal/subsystems/monitoring.md
index 2d04d16f7f..9c8ab585f8 100644
--- a/docs/release-control/v6/internal/subsystems/monitoring.md
+++ b/docs/release-control/v6/internal/subsystems/monitoring.md
@@ -379,6 +379,12 @@ not only at source selection time: node and guest diagnostic snapshots must
normalize memory-source aliases and backfill default fallback reasons before
logging or persistence, so later diagnostics/reporting cannot diverge just
because one poll path still emitted a compatibility label.
+That same guest-memory boundary also owns the low-trust Proxmox status-memory
+selector. When cache-aware availability is unavailable, the shared selector in
+`internal/monitoring/guest_memory_sources.go` must derive `status-freemem`
+against the effective balloon total and prefer that fallback over `status-mem`
+when Proxmox reports a saturated or materially inconsistent used figure, so
+Windows and ballooned guests do not get pinned to false 100% usage samples.
That compatibility boundary also applies to historical snapshot labels that may
still exist in tests, live in-memory state, or pre-canonical diagnostic paths:
legacy aliases such as `rrd-available`, `rrd-data`, `node-status-available`,
diff --git a/internal/monitoring/canonical_guardrails_test.go b/internal/monitoring/canonical_guardrails_test.go
index e91d00c1fa..7aa5b05b99 100644
--- a/internal/monitoring/canonical_guardrails_test.go
+++ b/internal/monitoring/canonical_guardrails_test.go
@@ -91,6 +91,25 @@ func TestGetStateRefreshesLiveAlertSnapshots(t *testing.T) {
}
}
+func TestGuestMemoryFallbackUsesCanonicalLowTrustSelector(t *testing.T) {
+ data, err := os.ReadFile("guest_memory_sources.go")
+ if err != nil {
+ t.Fatalf("failed to read guest_memory_sources.go: %v", err)
+ }
+ source := string(data)
+
+ for _, snippet := range []string{
+ "func effectiveGuestFreeMemTotal(memTotal uint64, status *proxmox.VMStatus) uint64 {",
+ "if status.Balloon > 0 && status.Balloon <= memTotal && status.FreeMem <= status.Balloon {",
+ "func selectGuestLowTrustUsedMemory(memTotal uint64, status *proxmox.VMStatus) (uint64, string) {",
+ "if statusMemPlusFree > freeMemTotal+guestStatusMemoryMismatchTolerance {",
+ } {
+ if !strings.Contains(source, snippet) {
+ t.Fatalf("guest_memory_sources.go must contain %q", snippet)
+ }
+ }
+}
+
func TestMonitoringRuntimeAvoidsLegacyMockPartialHelpers(t *testing.T) {
forbiddenSnippets := []string{
"mock.GetMockState(",
diff --git a/internal/monitoring/guest_memory_sources.go b/internal/monitoring/guest_memory_sources.go
index 6922b10d67..01d7e65b11 100644
--- a/internal/monitoring/guest_memory_sources.go
+++ b/internal/monitoring/guest_memory_sources.go
@@ -9,6 +9,8 @@ import (
"github.com/rs/zerolog/log"
)
+const guestStatusMemoryMismatchTolerance uint64 = 128 * 1024 * 1024 // 128 MiB
+
// deriveGuestMemInfoAvailable normalizes guest meminfo-based availability
// selection so the efficient VM builder and the node-by-node VM polling path
// do not drift apart on degraded Proxmox guest-agent payloads.
@@ -86,6 +88,55 @@ func deriveGuestMemInfoAvailable(memInfo *proxmox.VMMemInfo, guestRaw *VMMemoryR
}
}
+func saturatingAddUint64(lhs, rhs uint64) uint64 {
+ if math.MaxUint64-lhs < rhs {
+ return math.MaxUint64
+ }
+ return lhs + rhs
+}
+
+func effectiveGuestFreeMemTotal(memTotal uint64, status *proxmox.VMStatus) uint64 {
+ if status == nil {
+ return memTotal
+ }
+ if status.Balloon > 0 && status.Balloon <= memTotal && status.FreeMem <= status.Balloon {
+ return status.Balloon
+ }
+ return memTotal
+}
+
+func selectGuestLowTrustUsedMemory(memTotal uint64, status *proxmox.VMStatus) (uint64, string) {
+ if status == nil {
+ return 0, ""
+ }
+
+ freeMemTotal := effectiveGuestFreeMemTotal(memTotal, status)
+ hasFreeFallback := status.FreeMem > 0 && freeMemTotal >= status.FreeMem
+ freeDerivedUsed := uint64(0)
+ if hasFreeFallback {
+ freeDerivedUsed = freeMemTotal - status.FreeMem
+ }
+
+ if status.Mem > 0 {
+ if hasFreeFallback && freeDerivedUsed < status.Mem {
+ statusMemPlusFree := saturatingAddUint64(status.Mem, status.FreeMem)
+ if status.Mem >= freeMemTotal && freeDerivedUsed < freeMemTotal {
+ return freeDerivedUsed, "status-freemem"
+ }
+ if statusMemPlusFree > freeMemTotal+guestStatusMemoryMismatchTolerance {
+ return freeDerivedUsed, "status-freemem"
+ }
+ }
+ return status.Mem, "status-mem"
+ }
+
+ if hasFreeFallback {
+ return freeDerivedUsed, "status-freemem"
+ }
+
+ return 0, ""
+}
+
func guestMemoryFallbackReason(source string) string {
return MemorySourceFallbackReason(source)
}
@@ -193,14 +244,11 @@ func (m *Monitor) resolveGuestStatusMemory(
memAvailable = memTotal
}
memUsed = memTotal - memAvailable
- case status.Mem > 0:
- memUsed = status.Mem
- memorySource = "status-mem"
- case status.FreeMem > 0 && memTotal >= status.FreeMem:
- memUsed = memTotal - status.FreeMem
- memorySource = "status-freemem"
default:
- memorySource = "status-unavailable"
+ memUsed, memorySource = selectGuestLowTrustUsedMemory(memTotal, status)
+ if memorySource == "" {
+ memorySource = "status-unavailable"
+ }
}
if memUsed > memTotal {
memUsed = memTotal
diff --git a/internal/monitoring/guest_memory_sources_test.go b/internal/monitoring/guest_memory_sources_test.go
new file mode 100644
index 0000000000..1cf8ccd2dd
--- /dev/null
+++ b/internal/monitoring/guest_memory_sources_test.go
@@ -0,0 +1,109 @@
+package monitoring
+
+import (
+ "testing"
+
+ "github.com/rcourtman/pulse-go-rewrite/pkg/proxmox"
+)
+
+func TestSelectGuestLowTrustUsedMemory(t *testing.T) {
+ giB := uint64(1024 * 1024 * 1024)
+
+ tests := []struct {
+ name string
+ memTotal uint64
+ status *proxmox.VMStatus
+ wantUsed uint64
+ wantSource string
+ }{
+ {
+ name: "prefers status mem when it is plausible",
+ memTotal: 8 * giB,
+ status: &proxmox.VMStatus{
+ Mem: 3 * giB,
+ FreeMem: 5 * giB,
+ },
+ wantUsed: 3 * giB,
+ wantSource: "status-mem",
+ },
+ {
+ name: "falls back to status freemem when status mem is falsely saturated",
+ memTotal: 8 * giB,
+ status: &proxmox.VMStatus{
+ Mem: 8 * giB,
+ FreeMem: 5 * giB,
+ },
+ wantUsed: 3 * giB,
+ wantSource: "status-freemem",
+ },
+ {
+ name: "falls back to status freemem when status mem and freemem are materially inconsistent",
+ memTotal: 8 * giB,
+ status: &proxmox.VMStatus{
+ Mem: 7920 * 1024 * 1024,
+ FreeMem: 5 * giB,
+ },
+ wantUsed: 3 * giB,
+ wantSource: "status-freemem",
+ },
+ {
+ name: "uses status freemem when status mem is absent",
+ memTotal: 8 * giB,
+ status: &proxmox.VMStatus{
+ FreeMem: 6 * giB,
+ },
+ wantUsed: 2 * giB,
+ wantSource: "status-freemem",
+ },
+ {
+ name: "uses balloon total when deriving used from status freemem",
+ memTotal: 8 * giB,
+ status: &proxmox.VMStatus{
+ Balloon: 4 * giB,
+ FreeMem: 1 * giB,
+ },
+ wantUsed: 3 * giB,
+ wantSource: "status-freemem",
+ },
+ {
+ name: "uses balloon total when status mem is falsely saturated against balloon",
+ memTotal: 8 * giB,
+ status: &proxmox.VMStatus{
+ Mem: 4 * giB,
+ Balloon: 4 * giB,
+ FreeMem: 1 * giB,
+ },
+ wantUsed: 3 * giB,
+ wantSource: "status-freemem",
+ },
+ {
+ name: "returns empty selection without usable fields",
+ memTotal: 8 * giB,
+ status: &proxmox.VMStatus{},
+ wantUsed: 0,
+ wantSource: "",
+ },
+ {
+ name: "keeps status mem when freemem only shows tiny headroom",
+ memTotal: 8 * giB,
+ status: &proxmox.VMStatus{
+ Mem: 8*giB - (64 * 1024 * 1024),
+ FreeMem: 64 * 1024 * 1024,
+ },
+ wantUsed: 8*giB - (64 * 1024 * 1024),
+ wantSource: "status-mem",
+ },
+ }
+
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ gotUsed, gotSource := selectGuestLowTrustUsedMemory(tt.memTotal, tt.status)
+ if gotUsed != tt.wantUsed {
+ t.Fatalf("used = %d, want %d", gotUsed, tt.wantUsed)
+ }
+ if gotSource != tt.wantSource {
+ t.Fatalf("source = %q, want %q", gotSource, tt.wantSource)
+ }
+ })
+ }
+}
diff --git a/internal/monitoring/memory_trust_characterization_test.go b/internal/monitoring/memory_trust_characterization_test.go
index 144088501c..37a534d2a8 100644
--- a/internal/monitoring/memory_trust_characterization_test.go
+++ b/internal/monitoring/memory_trust_characterization_test.go
@@ -249,6 +249,29 @@ func TestHandleClusterVMResourceMemoryTrustCharacterization(t *testing.T) {
wantUsed: 5 * gib,
wantGap: 3 * gib,
},
+ {
+ name: "materially inconsistent status memory prefers freemem fallback",
+ status: &proxmox.VMStatus{
+ Status: "running",
+ MaxMem: 8 * gib,
+ Mem: 7920 * 1024 * 1024,
+ FreeMem: 5 * gib,
+ },
+ wantSource: "status-freemem",
+ wantUsed: 3 * gib,
+ },
+ {
+ name: "ballooned VM derives freemem fallback from balloon total",
+ status: &proxmox.VMStatus{
+ Status: "running",
+ MaxMem: 8 * gib,
+ Mem: 4 * gib,
+ Balloon: 4 * gib,
+ FreeMem: 1 * gib,
+ },
+ wantSource: "status-freemem",
+ wantUsed: 3 * gib,
+ },
}
for _, tt := range tests {
@@ -381,6 +404,29 @@ func TestPollVMsWithNodesMemoryTrustCharacterization(t *testing.T) {
wantSource: "derived-total-minus-used",
wantUsed: 5 * gib,
},
+ {
+ name: "materially inconsistent status memory prefers freemem fallback",
+ status: &proxmox.VMStatus{
+ Status: "running",
+ MaxMem: 8 * gib,
+ Mem: 7920 * 1024 * 1024,
+ FreeMem: 5 * gib,
+ },
+ wantSource: "status-freemem",
+ wantUsed: 3 * gib,
+ },
+ {
+ name: "ballooned VM derives freemem fallback from balloon total",
+ status: &proxmox.VMStatus{
+ Status: "running",
+ MaxMem: 8 * gib,
+ Mem: 4 * gib,
+ Balloon: 4 * gib,
+ FreeMem: 1 * gib,
+ },
+ wantSource: "status-freemem",
+ wantUsed: 3 * gib,
+ },
}
for _, tt := range tests {