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.

Make backup orphan detection type-aware (vm vs ct VMID collision)

rcourtman9c8218ef0dab
diff --git a/internal/alerts/alerts_test.go b/internal/alerts/alerts_test.go
index 6208dad941..8a9cae0dba 100644
--- a/internal/alerts/alerts_test.go
+++ b/internal/alerts/alerts_test.go
@@ -1889,6 +1889,74 @@ func TestCheckBackupsVMIDCollisionNoNamespace(t *testing.T) {
}
}
+func TestCheckBackupsTypeMismatchDoesNotAttributeToWrongGuest(t *testing.T) {
+ m := newTestManager(t)
+ m.ClearActiveAlerts()
+
+ m.mu.Lock()
+ m.config.Enabled = true
+ m.config.BackupDefaults = BackupAlertConfig{
+ Enabled: true,
+ WarningDays: 3,
+ CriticalDays: 5,
+ }
+ m.mu.Unlock()
+
+ now := time.Now()
+
+ // Only an LXC container exists with vmid 101...
+ guestsByKey := map[string]GuestLookup{
+ "pve1-node1-101": {
+ ResourceID: "lxc/101",
+ Name: "ct-101",
+ Instance: "pve1",
+ Node: "node1",
+ Type: "lxc",
+ VMID: 101,
+ },
+ }
+ guestsByVMID := map[string][]GuestLookup{
+ "101": {guestsByKey["pve1-node1-101"]},
+ }
+
+ // ...but the backup is for a VM with the same vmid (no namespace -> VMID path).
+ rollups := []recovery.ProtectionRollup{
+ {
+ RollupID: "ext:pbs-typed-101",
+ SubjectRef: &recovery.ExternalRef{
+ Type: "proxmox-vm",
+ Name: "101",
+ ID: "101",
+ },
+ LastSuccessAt: ptrTime(now.Add(-30 * 24 * time.Hour)),
+ LastOutcome: recovery.OutcomeSuccess,
+ Providers: []recovery.Provider{recovery.ProviderProxmoxPBS},
+ },
+ }
+
+ m.CheckBackups(rollups, guestsByKey, guestsByVMID)
+
+ m.mu.RLock()
+ defer m.mu.RUnlock()
+
+ // The vm backup must not be attributed to the lxc container.
+ for storageKey, alert := range m.activeAlerts {
+ key := effectiveAlertID(alert, storageKey)
+ if strings.Contains(key, "pve1-node1-101") {
+ t.Errorf("vm backup must not be attributed to lxc guest, found key %q", key)
+ }
+ }
+
+ // It should still surface as the canonical rollup-backed (orphaned) alert.
+ if !testHasActiveAlert(t, m, buildCanonicalStateID("backup-subject:ext-pbs-typed-101", "backup-subject:ext-pbs-typed-101-backup-age")) {
+ var keys []string
+ for storageKey, active := range m.activeAlerts {
+ keys = append(keys, effectiveAlertID(active, storageKey))
+ }
+ t.Fatalf("expected canonical rollup-backed alert, found keys: %v", keys)
+ }
+}
+
func TestCheckBackupsHandlesPmgBackups(t *testing.T) {
m := newTestManager(t)
m.ClearActiveAlerts()
diff --git a/internal/alerts/backup_snapshot.go b/internal/alerts/backup_snapshot.go
index be0cd66a0a..0d4aa4c68e 100644
--- a/internal/alerts/backup_snapshot.go
+++ b/internal/alerts/backup_snapshot.go
@@ -171,6 +171,49 @@ func normalizeBackupGuestType(guestType string) string {
}
}
+// backupGuestTypeKnown reports whether the type resolves to a concrete guest
+// kind (qemu/lxc). Host/storage/unknown subjects return false so type filtering
+// is skipped for them.
+func backupGuestTypeKnown(guestType string) bool {
+ switch normalizeBackupGuestType(guestType) {
+ case "qemu", "lxc":
+ return true
+ default:
+ return false
+ }
+}
+
+// guestMatchesBackupType reports whether a candidate guest is the same kind as
+// the backup. A backup with an unknown/host type matches anything; a guest with
+// an unknown type never matches a typed backup. Prevents a vm backup with
+// vmid=101 from being attributed to an lxc container with the same vmid (and
+// vice-versa), which would otherwise suppress the orphaned-backup alert.
+func guestMatchesBackupType(guest GuestLookup, backupType string) bool {
+ if !backupGuestTypeKnown(backupType) {
+ return true
+ }
+ if !backupGuestTypeKnown(guest.Type) {
+ return false
+ }
+ return normalizeBackupGuestType(guest.Type) == normalizeBackupGuestType(backupType)
+}
+
+// filterGuestsByBackupType keeps only the candidates whose kind matches the
+// backup type. Returns the input unchanged when the backup type is not a
+// concrete guest kind.
+func filterGuestsByBackupType(guests []GuestLookup, backupType string) []GuestLookup {
+ if !backupGuestTypeKnown(backupType) {
+ return guests
+ }
+ filtered := make([]GuestLookup, 0, len(guests))
+ for _, guest := range guests {
+ if guestMatchesBackupType(guest, backupType) {
+ filtered = append(filtered, guest)
+ }
+ }
+ return filtered
+}
+
func backupOrphanInventoryReady(scope *BackupInventoryScope, record backupRecord) bool {
if scope == nil || scope.PVEOrphanInventoryReady == nil {
return true
@@ -594,6 +637,11 @@ func (m *Manager) CheckBackupsWithInventory(
if inst, nd, vmid, ok := parseGuestID(ref.ID); ok {
key = BuildGuestKey(inst, nd, vmid)
info = guestsByKey[key]
+ // A vm and a container can share instance:node:vmid; never
+ // attribute the backup to a guest of the wrong kind.
+ if !guestMatchesBackupType(info, subjectType) {
+ info = GuestLookup{}
+ }
instance = inst
node = nd
vmID = strconv.Itoa(vmid)
@@ -609,7 +657,9 @@ func (m *Manager) CheckBackupsWithInventory(
if vmidStr != "" {
if vmid, err := strconv.Atoi(vmidStr); err == nil && vmid > 0 {
vmID = vmidStr
- guests := guestsByVMID[vmidStr]
+ // Drop same-VMID guests of the wrong kind so a vm backup is
+ // not matched to an lxc container (or vice-versa).
+ guests := filterGuestsByBackupType(guestsByVMID[vmidStr], subjectType)
if len(guests) == 1 {
info = guests[0]
} else if len(guests) > 1 && strings.TrimSpace(ref.Namespace) != "" {