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.
Go aialertsdashboarddockerdocker-monitoringgohomelabinfrastructure-monitoringkubernetesmonitoringmspproxmoxproxmox-backup-serverproxmox-mail-gatewayproxmox-veself-hostedtruenastypescriptvspherewebhooks
fix(alerts): scope orphaned backup detection per PVE instance to prevent false positives (#1286)
rcourtman62225e0c12f1
diff --git a/internal/alerts/alerts.go b/internal/alerts/alerts.goindex 93465e5efd..3d873272e6 100644--- a/internal/alerts/alerts.go+++ b/internal/alerts/alerts.go@@ -5662,21 +5662,18 @@ func (m *Manager) CheckBackups(return}- // Check whether the guest inventory contains any live entries (non-empty- // ResourceID). If every entry is persisted metadata for deleted guests,- // the inventory hasn't been populated yet (startup race, auth failure, etc.)- // and orphaned detection would produce false positives for every backup.- hasLiveInventory := false+ // Build a set of instances that have at least one live guest (non-empty+ // ResourceID). Orphan detection is only safe for instances whose guest+ // list has been populated — if an instance hasn't been polled yet+ // (startup race, auth failure, staggered polling), every backup from+ // that instance would look orphaned.+ instancesWithLiveGuests := make(map[string]bool)for _, guests := range guestsByVMID {for _, g := range guests {- if g.ResourceID != "" {- hasLiveInventory = true- break+ if g.ResourceID != "" && g.Instance != "" {+ instancesWithLiveGuests[g.Instance] = true}}- if hasLiveInventory {- break- }}validAlerts := make(map[string]struct{})@@ -5713,11 +5710,19 @@ func (m *Manager) CheckBackups(if backupIgnoreVMID(record.vmid, currentBackupCfg.IgnoreVMIDs) {continue}- if record.vmid != "" && record.lookup.ResourceID == "" && hasLiveInventory {+ // Determine whether we have enough inventory to safely run orphan+ // detection for this backup. For PVE storage backups the instance+ // guard is strict: only check when that specific PVE instance has+ // been polled. For PBS/PMG backups (which span instances) it's+ // enough that *any* instance has live guests.+ inventoryReady := false+ if record.source == "PVE storage" {+ inventoryReady = instancesWithLiveGuests[record.instance]+ } else {+ inventoryReady = len(instancesWithLiveGuests) > 0+ }+ if record.vmid != "" && record.lookup.ResourceID == "" && inventoryReady {// Backup has a VMID but no matching live guest in its lookup.- // Only run orphan detection when live inventory exists — if no- // live guests have been polled yet (startup race, auth failure,- // transient outage), every backup would look orphaned.//// Check whether the VMID exists anywhere in live inventory.// If it does, the backup is ambiguous (VMID collision) but not orphaned.@@ -6051,9 +6056,9 @@ func (m *Manager) CheckBackups(if _, ok := validAlerts[alertID]; ok {continue}- // When inventory has no live guests, preserve existing orphan alerts- // rather than clearing them — we can't confirm they're resolved.- if !hasLiveInventory && alert.Type == "backup-orphaned" {+ // When no instances have live inventory, preserve existing orphan+ // alerts rather than clearing them — we can't confirm they're resolved.+ if len(instancesWithLiveGuests) == 0 && alert.Type == "backup-orphaned" {continue}m.clearAlertNoLock(alertID)diff --git a/internal/alerts/alerts_test.go b/internal/alerts/alerts_test.goindex 3d50e273b5..740cab7edc 100644--- a/internal/alerts/alerts_test.go+++ b/internal/alerts/alerts_test.go@@ -1129,9 +1129,9 @@ func TestCheckBackupsHandlesPbsOnlyGuests(t *testing.T) {}// Include a live sentinel guest so hasLiveInventory is true and orphan detection runs.- sentinelKey := BuildGuestKey("sentinel", "snode", 9999)+ sentinelKey := BuildGuestKey("inst", "snode", 9999)guestsByKey := map[string]GuestLookup{- sentinelKey: {ResourceID: "qemu/9999", Name: "sentinel-vm", Instance: "sentinel", Node: "snode", Type: "qemu", VMID: 9999},+ sentinelKey: {ResourceID: "qemu/9999", Name: "sentinel-vm", Instance: "inst", Node: "snode", Type: "qemu", VMID: 9999},}guestsByVMID := map[string][]GuestLookup{"9999": {guestsByKey[sentinelKey]},@@ -1474,9 +1474,9 @@ func TestCheckBackupsSkipsOrphanedWhenDisabled(t *testing.T) {}// Include a live sentinel guest so hasLiveInventory is true and orphan detection runs.- sentinelKey := BuildGuestKey("sentinel", "snode", 9999)+ sentinelKey := BuildGuestKey("inst", "snode", 9999)guestsByKey := map[string]GuestLookup{- sentinelKey: {ResourceID: "qemu/9999", Name: "sentinel-vm", Instance: "sentinel", Node: "snode", Type: "qemu", VMID: 9999},+ sentinelKey: {ResourceID: "qemu/9999", Name: "sentinel-vm", Instance: "inst", Node: "snode", Type: "qemu", VMID: 9999},}guestsByVMID := map[string][]GuestLookup{"9999": {guestsByKey[sentinelKey]},@@ -1528,9 +1528,9 @@ func TestCheckBackupsCreatesOrphanedAlert(t *testing.T) {// Include a live sentinel guest so hasLiveInventory is true and orphan detection runs.// VMID 200 is still orphaned because it's not in the inventory.- sentinelKey := BuildGuestKey("sentinel", "snode", 9999)+ sentinelKey := BuildGuestKey("inst", "snode", 9999)guestsByKey := map[string]GuestLookup{- sentinelKey: {ResourceID: "qemu/9999", Name: "sentinel-vm", Instance: "sentinel", Node: "snode", Type: "qemu", VMID: 9999},+ sentinelKey: {ResourceID: "qemu/9999", Name: "sentinel-vm", Instance: "inst", Node: "snode", Type: "qemu", VMID: 9999},}guestsByVMID := map[string][]GuestLookup{"9999": {guestsByKey[sentinelKey]},@@ -1598,10 +1598,10 @@ func TestCheckBackupsOrphanedAlertClearsWhenGuestReappears(t *testing.T) {},}- // Include a live sentinel guest so hasLiveInventory is true and orphan detection runs.- sentinelKey := BuildGuestKey("sentinel", "snode", 9999)+ // Include a live sentinel guest on the same instance so orphan detection runs.+ sentinelKey := BuildGuestKey("inst", "snode", 9999)sentinelByKey := map[string]GuestLookup{- sentinelKey: {ResourceID: "qemu/9999", Name: "sentinel-vm", Instance: "sentinel", Node: "snode", Type: "qemu", VMID: 9999},+ sentinelKey: {ResourceID: "qemu/9999", Name: "sentinel-vm", Instance: "inst", Node: "snode", Type: "qemu", VMID: 9999},}sentinelByVMID := map[string][]GuestLookup{"9999": {sentinelByKey[sentinelKey]},@@ -1680,9 +1680,9 @@ func TestCheckBackupsOrphanedIgnoresVMIDs(t *testing.T) {}// Include a live sentinel guest so hasLiveInventory is true and orphan detection runs.- sentinelKey := BuildGuestKey("sentinel", "snode", 9999)+ sentinelKey := BuildGuestKey("inst", "snode", 9999)guestsByKey := map[string]GuestLookup{- sentinelKey: {ResourceID: "qemu/9999", Name: "sentinel-vm", Instance: "sentinel", Node: "snode", Type: "qemu", VMID: 9999},+ sentinelKey: {ResourceID: "qemu/9999", Name: "sentinel-vm", Instance: "inst", Node: "snode", Type: "qemu", VMID: 9999},}guestsByVMID := map[string][]GuestLookup{"9999": {guestsByKey[sentinelKey]},@@ -1739,9 +1739,9 @@ func TestCheckBackupsOrphanedWithZeroAgeThresholds(t *testing.T) {}// Include a live sentinel guest so hasLiveInventory is true and orphan detection runs.- sentinelKey := BuildGuestKey("sentinel", "snode", 9999)+ sentinelKey := BuildGuestKey("inst", "snode", 9999)guestsByKey := map[string]GuestLookup{- sentinelKey: {ResourceID: "qemu/9999", Name: "sentinel-vm", Instance: "sentinel", Node: "snode", Type: "qemu", VMID: 9999},+ sentinelKey: {ResourceID: "qemu/9999", Name: "sentinel-vm", Instance: "inst", Node: "snode", Type: "qemu", VMID: 9999},}guestsByVMID := map[string][]GuestLookup{"9999": {guestsByKey[sentinelKey]},@@ -1804,10 +1804,10 @@ func TestCheckBackupsOrphanedWithPersistedMetadata(t *testing.T) {// Simulate persisted metadata for deleted guest: entry exists in// guestsByVMID but with empty ResourceID (no live guest).- // Include a live sentinel guest so hasLiveInventory is true and orphan detection runs.- sentinelKey := BuildGuestKey("sentinel", "snode", 9999)+ // Include a live sentinel guest on the same instance so orphan detection runs.+ sentinelKey := BuildGuestKey("inst", "snode", 9999)guestsByKey := map[string]GuestLookup{- sentinelKey: {ResourceID: "qemu/9999", Name: "sentinel-vm", Instance: "sentinel", Node: "snode", Type: "qemu", VMID: 9999},+ sentinelKey: {ResourceID: "qemu/9999", Name: "sentinel-vm", Instance: "inst", Node: "snode", Type: "qemu", VMID: 9999},}guestsByVMID := map[string][]GuestLookup{"500": {{Name: "deleted-vm", Instance: "inst", Node: "node", Type: "qemu", VMID: 500}},@@ -1906,9 +1906,9 @@ func TestCheckBackupsOrphanedPreservedWhenNoLiveInventory(t *testing.T) {}// First cycle: with live inventory → orphan alert fires.- sentinelKey := BuildGuestKey("sentinel", "snode", 9999)+ sentinelKey := BuildGuestKey("inst", "snode", 9999)guestsByKey := map[string]GuestLookup{- sentinelKey: {ResourceID: "qemu/9999", Name: "sentinel-vm", Instance: "sentinel", Node: "snode", Type: "qemu", VMID: 9999},+ sentinelKey: {ResourceID: "qemu/9999", Name: "sentinel-vm", Instance: "inst", Node: "snode", Type: "qemu", VMID: 9999},}guestsByVMID := map[string][]GuestLookup{"9999": {guestsByKey[sentinelKey]},@@ -1977,12 +1977,16 @@ func TestCheckBackupsOrphanedCrossInstanceVMID(t *testing.T) {}// Instance B has a live guest with the same VMID.+ // Instance A also has a sentinel guest so its inventory is considered populated.keyB := BuildGuestKey("instB", "nodeB", 600)+ sentinelA := BuildGuestKey("instA", "nodeA", 9999)guestsByKey := map[string]GuestLookup{- keyB: {ResourceID: "qemu/600", Name: "vm-instB", Instance: "instB", Node: "nodeB", Type: "qemu", VMID: 600},+ keyB: {ResourceID: "qemu/600", Name: "vm-instB", Instance: "instB", Node: "nodeB", Type: "qemu", VMID: 600},+ sentinelA: {ResourceID: "qemu/9999", Name: "sentinel-vm", Instance: "instA", Node: "nodeA", Type: "qemu", VMID: 9999},}guestsByVMID := map[string][]GuestLookup{- "600": {guestsByKey[keyB]},+ "600": {guestsByKey[keyB]},+ "9999": {guestsByKey[sentinelA]},}m.CheckBackups(storageBackups, nil, nil, guestsByKey, guestsByVMID)