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.

fix: prevent false PBS backup indicators when VMIDs collide across PVE instances (#1177)

rcourtmandcfa8cf0baba
diff --git a/internal/alerts/alerts.go b/internal/alerts/alerts.go
index e4c6c3bf3d..2b63ee7951 100644
--- a/internal/alerts/alerts.go
+++ b/internal/alerts/alerts.go
@@ -5353,22 +5353,16 @@ func (m *Manager) CheckBackups(
info = guests[0]
} else if backup.Namespace != "" {
// Try to match namespace to instance name
- matched := false
for _, g := range guests {
if namespaceMatchesInstance(backup.Namespace, g.Instance) {
info = g
- matched = true
break
}
}
- if !matched {
- // No namespace match found, fall back to first guest
- info = guests[0]
- }
- } else {
- // No namespace available, fall back to first guest
- info = guests[0]
+ // If no namespace match found, info stays zero-value.
+ // The VMID is ambiguous across instances so we must not guess.
}
+ // else: multiple guests, no namespace — info stays zero-value (ambiguous)
if info.Instance != "" && info.Node != "" {
key = BuildGuestKey(info.Instance, info.Node, info.VMID)
displayName = info.Name
diff --git a/internal/alerts/alerts_test.go b/internal/alerts/alerts_test.go
index e3ebf79264..d87f673b2c 100644
--- a/internal/alerts/alerts_test.go
+++ b/internal/alerts/alerts_test.go
@@ -1231,6 +1231,165 @@ func TestCheckBackupsDisambiguatesWithNamespace(t *testing.T) {
}
}
+// TestCheckBackupsVMIDCollisionNonMatchingNamespace verifies that when multiple guests
+// share a VMID and the PBS backup namespace matches none of them, the alert uses the
+// generic PBS key rather than falsely attributing to a specific guest.
+func TestCheckBackupsVMIDCollisionNonMatchingNamespace(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()
+
+ guestsByKey := map[string]GuestLookup{
+ "pve1-node1-100": {
+ ResourceID: "qemu/100",
+ Name: "vm-pve1",
+ Instance: "pve1",
+ Node: "node1",
+ Type: "qemu",
+ VMID: 100,
+ },
+ "pve2-node2-100": {
+ ResourceID: "qemu/100",
+ Name: "vm-pve2",
+ Instance: "pve2",
+ Node: "node2",
+ Type: "qemu",
+ VMID: 100,
+ },
+ }
+
+ guestsByVMID := map[string][]GuestLookup{
+ "100": {
+ guestsByKey["pve1-node1-100"],
+ guestsByKey["pve2-node2-100"],
+ },
+ }
+
+ // PBS backup with namespace "staging" — matches neither pve1 nor pve2
+ pbsBackups := []models.PBSBackup{
+ {
+ ID: "pbs-100",
+ Instance: "pbs-main",
+ Datastore: "backup-store",
+ Namespace: "staging",
+ BackupType: "qemu",
+ VMID: "100",
+ BackupTime: now.Add(-6 * 24 * time.Hour),
+ },
+ }
+
+ m.CheckBackups(nil, pbsBackups, nil, guestsByKey, guestsByVMID)
+
+ m.mu.RLock()
+ defer m.mu.RUnlock()
+
+ // Should NOT have a guest-specific alert key
+ for key := range m.activeAlerts {
+ if key == "backup-age-pve1-node1-100" || key == "backup-age-pve2-node2-100" {
+ t.Errorf("should not attribute ambiguous backup to a specific guest, but found key %q", key)
+ }
+ }
+
+ // Should have a generic PBS alert key
+ expectedKey := "backup-age-pbs-pbs-main-qemu-100"
+ if _, exists := m.activeAlerts[expectedKey]; !exists {
+ var keys []string
+ for k := range m.activeAlerts {
+ keys = append(keys, k)
+ }
+ t.Errorf("expected generic PBS alert key %q, found keys: %v", expectedKey, keys)
+ }
+}
+
+// TestCheckBackupsVMIDCollisionNoNamespace verifies that when multiple guests
+// share a VMID and the PBS backup has no namespace, the alert uses the generic PBS key.
+func TestCheckBackupsVMIDCollisionNoNamespace(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()
+
+ guestsByKey := map[string]GuestLookup{
+ "pve1-node1-100": {
+ ResourceID: "qemu/100",
+ Name: "vm-pve1",
+ Instance: "pve1",
+ Node: "node1",
+ Type: "qemu",
+ VMID: 100,
+ },
+ "pve2-node2-100": {
+ ResourceID: "qemu/100",
+ Name: "vm-pve2",
+ Instance: "pve2",
+ Node: "node2",
+ Type: "qemu",
+ VMID: 100,
+ },
+ }
+
+ guestsByVMID := map[string][]GuestLookup{
+ "100": {
+ guestsByKey["pve1-node1-100"],
+ guestsByKey["pve2-node2-100"],
+ },
+ }
+
+ // PBS backup with NO namespace
+ pbsBackups := []models.PBSBackup{
+ {
+ ID: "pbs-100",
+ Instance: "pbs-main",
+ Datastore: "backup-store",
+ Namespace: "",
+ BackupType: "qemu",
+ VMID: "100",
+ BackupTime: now.Add(-6 * 24 * time.Hour),
+ },
+ }
+
+ m.CheckBackups(nil, pbsBackups, nil, guestsByKey, guestsByVMID)
+
+ m.mu.RLock()
+ defer m.mu.RUnlock()
+
+ // Should NOT have a guest-specific alert key
+ for key := range m.activeAlerts {
+ if key == "backup-age-pve1-node1-100" || key == "backup-age-pve2-node2-100" {
+ t.Errorf("should not attribute ambiguous backup to a specific guest, but found key %q", key)
+ }
+ }
+
+ // Should have a generic PBS alert key
+ expectedKey := "backup-age-pbs-pbs-main-qemu-100"
+ if _, exists := m.activeAlerts[expectedKey]; !exists {
+ var keys []string
+ for k := range m.activeAlerts {
+ keys = append(keys, k)
+ }
+ t.Errorf("expected generic PBS alert key %q, found keys: %v", expectedKey, keys)
+ }
+}
+
func TestCheckBackupsHandlesPmgBackups(t *testing.T) {
m := newTestManager(t)
m.ClearActiveAlerts()
diff --git a/internal/models/models.go b/internal/models/models.go
index 23f6a0d383..b8c53b0ae6 100644
--- a/internal/models/models.go
+++ b/internal/models/models.go
@@ -1522,6 +1522,33 @@ func (s *State) SyncGuestBackupTimes() {
pbsBackupsByVMID[vmid] = append(pbsBackupsByVMID[vmid], backup)
}
+ // Build a set of VMIDs that appear on more than one PVE instance.
+ // When a VMID is ambiguous, we must not fall back to VMID-only matching
+ // because we can't tell which guest the backup belongs to.
+ vmidInstances := make(map[int]map[string]struct{})
+ for i := range s.VMs {
+ m, ok := vmidInstances[s.VMs[i].VMID]
+ if !ok {
+ m = make(map[string]struct{})
+ vmidInstances[s.VMs[i].VMID] = m
+ }
+ m[s.VMs[i].Instance] = struct{}{}
+ }
+ for i := range s.Containers {
+ m, ok := vmidInstances[s.Containers[i].VMID]
+ if !ok {
+ m = make(map[string]struct{})
+ vmidInstances[s.Containers[i].VMID] = m
+ }
+ m[s.Containers[i].Instance] = struct{}{}
+ }
+ vmidIsAmbiguous := make(map[int]bool)
+ for vmid, instances := range vmidInstances {
+ if len(instances) > 1 {
+ vmidIsAmbiguous[vmid] = true
+ }
+ }
+
// findBestPBSBackup finds the most recent PBS backup for a given VMID and instance.
// If the backup has a namespace that matches the instance, it's preferred.
// Returns zero time if no suitable backup found.
@@ -1552,8 +1579,12 @@ func (s *State) SyncGuestBackupTimes() {
return bestMatchTime
}
- // Fall back to any backup with this VMID (original behavior for backwards compat)
- // This handles cases where namespaces aren't used or don't match
+ // Fall back to any backup with this VMID, but only when the VMID is
+ // unique across PVE instances. If the VMID exists on multiple instances,
+ // the match is ambiguous and we must not guess.
+ if vmidIsAmbiguous[vmid] {
+ return time.Time{}
+ }
return bestTime
}
diff --git a/internal/models/state_host_test.go b/internal/models/state_host_test.go
index 4ef27d9172..d6ba30b7ce 100644
--- a/internal/models/state_host_test.go
+++ b/internal/models/state_host_test.go
@@ -938,6 +938,176 @@ func TestSyncGuestBackupTimesNamespaceDisambiguation(t *testing.T) {
}
}
+// TestSyncGuestBackupTimesVMIDCollisionNonMatchingNamespace verifies that when the same VMID
+// exists on multiple PVE instances and a PBS backup namespace matches neither, both guests
+// get zero LastBackup instead of a false positive.
+func TestSyncGuestBackupTimesVMIDCollisionNonMatchingNamespace(t *testing.T) {
+ state := NewState()
+
+ now := time.Now()
+ backupTime := now.Add(-1 * time.Hour)
+
+ // Two VMs with the same VMID on different instances
+ state.UpdateVMs([]VM{
+ {VMID: 100, Name: "vm-pve1", Instance: "pve1", Node: "node1"},
+ {VMID: 100, Name: "vm-pve2", Instance: "pve2", Node: "node2"},
+ })
+
+ // PBS backup with a namespace that matches neither instance
+ state.mu.Lock()
+ state.PBSBackups = []PBSBackup{
+ {
+ ID: "pbs-100",
+ VMID: "100",
+ Namespace: "staging",
+ BackupType: "vm",
+ BackupTime: backupTime,
+ Instance: "pbs-main",
+ },
+ }
+ state.mu.Unlock()
+
+ state.SyncGuestBackupTimes()
+ snapshot := state.GetSnapshot()
+
+ for _, vm := range snapshot.VMs {
+ if vm.VMID == 100 && !vm.LastBackup.IsZero() {
+ t.Errorf("VM %q on %s should have zero LastBackup (ambiguous VMID, non-matching namespace), got %v",
+ vm.Name, vm.Instance, vm.LastBackup)
+ }
+ }
+}
+
+// TestSyncGuestBackupTimesVMIDCollisionEmptyNamespace verifies that when the same VMID
+// exists on multiple PVE instances and a PBS backup has no namespace, both guests
+// get zero LastBackup.
+func TestSyncGuestBackupTimesVMIDCollisionEmptyNamespace(t *testing.T) {
+ state := NewState()
+
+ now := time.Now()
+ backupTime := now.Add(-1 * time.Hour)
+
+ state.UpdateVMs([]VM{
+ {VMID: 100, Name: "vm-pve1", Instance: "pve1", Node: "node1"},
+ {VMID: 100, Name: "vm-pve2", Instance: "pve2", Node: "node2"},
+ })
+
+ // PBS backup with empty namespace
+ state.mu.Lock()
+ state.PBSBackups = []PBSBackup{
+ {
+ ID: "pbs-100",
+ VMID: "100",
+ Namespace: "",
+ BackupType: "vm",
+ BackupTime: backupTime,
+ Instance: "pbs-main",
+ },
+ }
+ state.mu.Unlock()
+
+ state.SyncGuestBackupTimes()
+ snapshot := state.GetSnapshot()
+
+ for _, vm := range snapshot.VMs {
+ if vm.VMID == 100 && !vm.LastBackup.IsZero() {
+ t.Errorf("VM %q on %s should have zero LastBackup (ambiguous VMID, empty namespace), got %v",
+ vm.Name, vm.Instance, vm.LastBackup)
+ }
+ }
+}
+
+// TestSyncGuestBackupTimesUniqueVMIDFallback verifies that a unique VMID still gets
+// the PBS backup via fallback even when the namespace doesn't match.
+func TestSyncGuestBackupTimesUniqueVMIDFallback(t *testing.T) {
+ state := NewState()
+
+ now := time.Now()
+ backupTime := now.Add(-1 * time.Hour)
+
+ // Single VM — VMID is unique
+ state.UpdateVMs([]VM{
+ {VMID: 100, Name: "my-vm", Instance: "pve1", Node: "node1"},
+ })
+
+ // PBS backup with a namespace that does NOT match the instance
+ state.mu.Lock()
+ state.PBSBackups = []PBSBackup{
+ {
+ ID: "pbs-100",
+ VMID: "100",
+ Namespace: "daily",
+ BackupType: "vm",
+ BackupTime: backupTime,
+ Instance: "pbs-main",
+ },
+ }
+ state.mu.Unlock()
+
+ state.SyncGuestBackupTimes()
+ snapshot := state.GetSnapshot()
+
+ var found *VM
+ for i := range snapshot.VMs {
+ if snapshot.VMs[i].VMID == 100 {
+ found = &snapshot.VMs[i]
+ }
+ }
+ if found == nil {
+ t.Fatal("VM not found")
+ }
+ if !found.LastBackup.Equal(backupTime) {
+ t.Errorf("unique VMID should fall back to PBS backup; got LastBackup = %v, want %v",
+ found.LastBackup, backupTime)
+ }
+}
+
+// TestSyncGuestBackupTimesVMContainerCollision verifies that when a VM and Container
+// share the same VMID on different instances, neither gets an unmatched PBS backup.
+func TestSyncGuestBackupTimesVMContainerCollision(t *testing.T) {
+ state := NewState()
+
+ now := time.Now()
+ backupTime := now.Add(-1 * time.Hour)
+
+ state.UpdateVMs([]VM{
+ {VMID: 100, Name: "vm-pve1", Instance: "pve1", Node: "node1"},
+ })
+ state.UpdateContainers([]Container{
+ {VMID: 100, Name: "ct-pve2", Instance: "pve2", Node: "node2"},
+ })
+
+ // PBS backup with namespace matching neither
+ state.mu.Lock()
+ state.PBSBackups = []PBSBackup{
+ {
+ ID: "pbs-100",
+ VMID: "100",
+ Namespace: "other",
+ BackupType: "vm",
+ BackupTime: backupTime,
+ Instance: "pbs-main",
+ },
+ }
+ state.mu.Unlock()
+
+ state.SyncGuestBackupTimes()
+ snapshot := state.GetSnapshot()
+
+ for _, vm := range snapshot.VMs {
+ if vm.VMID == 100 && !vm.LastBackup.IsZero() {
+ t.Errorf("VM %q should have zero LastBackup (ambiguous VMID with container), got %v",
+ vm.Name, vm.LastBackup)
+ }
+ }
+ for _, ct := range snapshot.Containers {
+ if ct.VMID == 100 && !ct.LastBackup.IsZero() {
+ t.Errorf("Container %q should have zero LastBackup (ambiguous VMID with VM), got %v",
+ ct.Name, ct.LastBackup)
+ }
+ }
+}
+
func TestUpdateStorageBackupsForInstance(t *testing.T) {
state := NewState()