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): respect per-guest backup and snapshot overrides (fixes #961)
rcourtman065a59316f7b
diff --git a/internal/alerts/alerts.go b/internal/alerts/alerts.goindex 19a74ff10b..e727c2d876 100644--- a/internal/alerts/alerts.go+++ b/internal/alerts/alerts.go@@ -205,8 +205,10 @@ type ThresholdConfig struct {NetworkOut *HysteresisThreshold `json:"networkOut,omitempty"`Usage *HysteresisThreshold `json:"usage,omitempty"` // For storage devicesTemperature *HysteresisThreshold `json:"temperature,omitempty"` // For node CPU temperature+ Backup *BackupAlertConfig `json:"backup,omitempty"`+ Snapshot *SnapshotAlertConfig `json:"snapshot,omitempty"`Note *string `json:"note,omitempty"`- // Legacy fields for backward compatibility+ // Legacy thresholds for backwards compatibilityCPULegacy *float64 `json:"cpuLegacy,omitempty"`MemoryLegacy *float64 `json:"memoryLegacy,omitempty"`DiskLegacy *float64 `json:"diskLegacy,omitempty"`@@ -359,11 +361,12 @@ type BackupAlertConfig struct {// GuestLookup describes a guest identity used for snapshot/backup evaluations.type GuestLookup struct {- Name string- Instance string- Node string- Type string- VMID int+ ResourceID string+ Name string+ Instance string+ Node string+ Type string+ VMID int}// AlertConfig represents the complete alert configuration@@ -4716,32 +4719,49 @@ func (m *Manager) CheckSnapshotsForInstance(instanceName string, snapshots []modsizeGiB = float64(snapshot.SizeBytes) / gib}- var (- ageLevel AlertLevel- ageThreshold int- sizeLevel AlertLevel- sizeThreshold float64- triggeredStats []string- )+ // Determine thresholds for this snapshot+ resourceID := fmt.Sprintf("%s:%s:%d", snapshot.Instance, snapshot.Node, snapshot.VMID)+ m.mu.RLock()+ gh := m.getGuestThresholds(nil, resourceID)+ m.mu.RUnlock()++ if gh.Disabled {+ continue+ }++ currentSnapshotCfg := snapshotCfg+ if gh.Snapshot != nil {+ currentSnapshotCfg = *gh.Snapshot+ }++ if !currentSnapshotCfg.Enabled {+ continue+ }- if snapshotCfg.CriticalDays > 0 && ageDays >= float64(snapshotCfg.CriticalDays) {+ var ageLevel AlertLevel+ var ageThreshold int+ var sizeLevel AlertLevel+ var sizeThreshold float64+ var triggeredStats []string++ if currentSnapshotCfg.CriticalDays > 0 && ageDays >= float64(currentSnapshotCfg.CriticalDays) {ageLevel = AlertLevelCritical- ageThreshold = snapshotCfg.CriticalDays+ ageThreshold = currentSnapshotCfg.CriticalDaystriggeredStats = append(triggeredStats, "age")- } else if snapshotCfg.WarningDays > 0 && ageDays >= float64(snapshotCfg.WarningDays) {+ } else if currentSnapshotCfg.WarningDays > 0 && ageDays >= float64(currentSnapshotCfg.WarningDays) {ageLevel = AlertLevelWarning- ageThreshold = snapshotCfg.WarningDays+ ageThreshold = currentSnapshotCfg.WarningDaystriggeredStats = append(triggeredStats, "age")}if snapshot.SizeBytes > 0 {- if snapshotCfg.CriticalSizeGiB > 0 && sizeGiB >= snapshotCfg.CriticalSizeGiB {+ if currentSnapshotCfg.CriticalSizeGiB > 0 && sizeGiB >= currentSnapshotCfg.CriticalSizeGiB {sizeLevel = AlertLevelCritical- sizeThreshold = snapshotCfg.CriticalSizeGiB+ sizeThreshold = currentSnapshotCfg.CriticalSizeGiBtriggeredStats = append(triggeredStats, "size")- } else if snapshotCfg.WarningSizeGiB > 0 && sizeGiB >= snapshotCfg.WarningSizeGiB {+ } else if currentSnapshotCfg.WarningSizeGiB > 0 && sizeGiB >= currentSnapshotCfg.WarningSizeGiB {sizeLevel = AlertLevelWarning- sizeThreshold = snapshotCfg.WarningSizeGiB+ sizeThreshold = currentSnapshotCfg.WarningSizeGiBtriggeredStats = append(triggeredStats, "size")}}@@ -5141,15 +5161,33 @@ func (m *Manager) CheckBackups(}ageDaysRounded := math.Round(ageDays*10) / 10+ // Determine thresholds for this backup+ currentBackupCfg := backupCfg+ if record.lookup.ResourceID != "" {+ m.mu.RLock()+ gh := m.getGuestThresholds(nil, record.lookup.ResourceID)+ m.mu.RUnlock()+ if gh.Disabled {+ continue+ }+ if gh.Backup != nil {+ currentBackupCfg = *gh.Backup+ }+ }++ if !currentBackupCfg.Enabled {+ continue+ }+var level AlertLevelvar threshold intswitch {- case backupCfg.CriticalDays > 0 && ageDays >= float64(backupCfg.CriticalDays):+ case currentBackupCfg.CriticalDays > 0 && ageDays >= float64(currentBackupCfg.CriticalDays):level = AlertLevelCritical- threshold = backupCfg.CriticalDays- case backupCfg.WarningDays > 0 && ageDays >= float64(backupCfg.WarningDays):+ threshold = currentBackupCfg.CriticalDays+ case currentBackupCfg.WarningDays > 0 && ageDays >= float64(currentBackupCfg.WarningDays):level = AlertLevelWarning- threshold = backupCfg.WarningDays+ threshold = currentBackupCfg.WarningDaysdefault:continue}diff --git a/internal/alerts/alerts_test.go b/internal/alerts/alerts_test.goindex d0b04fdd73..feb99a10da 100644--- a/internal/alerts/alerts_test.go+++ b/internal/alerts/alerts_test.go@@ -660,6 +660,70 @@ func TestCheckSnapshotsForInstanceCreatesAndClearsAlerts(t *testing.T) {}}+func TestCheckSnapshotsRespectsOverrides(t *testing.T) {+ m := newTestManager(t)+ m.ClearActiveAlerts()++ cfg := AlertConfig{+ Enabled: true,+ SnapshotDefaults: SnapshotAlertConfig{+ Enabled: true,+ WarningDays: 7,+ CriticalDays: 14,+ },+ }+ m.UpdateConfig(cfg)+ m.mu.Lock()+ m.config.TimeThreshold = 0+ m.mu.Unlock()++ now := time.Now()+ snapshots := []models.GuestSnapshot{+ {+ ID: "inst:node:100:weekly",+ Name: "weekly",+ Node: "node",+ Instance: "inst",+ Type: "qemu",+ VMID: 100,+ Time: now.Add(-10 * 24 * time.Hour), // Triggers Warning (10 > 7)+ },+ }+ resourceKey := "inst:node:100"+ guestNames := map[string]string{+ resourceKey: "app-server",+ }++ // 1. Verify warning alert is created+ m.CheckSnapshotsForInstance("inst", snapshots, guestNames)+ m.mu.RLock()+ alert, exists := m.activeAlerts["snapshot-age-inst:node:100:weekly"]+ m.mu.RUnlock()+ if !exists {+ t.Fatalf("expected snapshot warning alert")+ }+ if alert.Level != AlertLevelWarning {+ t.Fatalf("expected warning alert, got %s", alert.Level)+ }++ // 2. Disable via override+ cfg = m.GetConfig()+ cfg.Overrides = map[string]ThresholdConfig{+ "inst:node:100": {+ Snapshot: &SnapshotAlertConfig{Enabled: false},+ },+ }+ m.UpdateConfig(cfg)+ m.CheckSnapshotsForInstance("inst", snapshots, guestNames)+ m.mu.RLock()+ _, exists = m.activeAlerts["snapshot-age-inst:node:100:weekly"]+ m.mu.RUnlock()+ if exists {+ t.Fatalf("expected snapshot alert to be suppressed by override")+ }+}++func TestCheckSnapshotsForInstanceTriggersOnSnapshotSize(t *testing.T) {m := newTestManager(t)m.ClearActiveAlerts()@@ -875,6 +939,111 @@ func TestCheckBackupsCreatesAndClearsAlerts(t *testing.T) {}}+func TestCheckBackupsRespectsOverrides(t *testing.T) {+ m := newTestManager(t)+ m.ClearActiveAlerts()++ m.mu.Lock()+ m.config.Enabled = true+ m.config.BackupDefaults = BackupAlertConfig{+ Enabled: true,+ WarningDays: 7,+ CriticalDays: 14,+ }+ m.config.TimeThreshold = 0+ m.mu.Unlock()++ now := time.Now()+ storageBackups := []models.StorageBackup{+ {+ ID: "inst-node-100-backup",+ Storage: "local",+ Node: "node",+ Instance: "inst",+ Type: "qemu",+ VMID: 100,+ Time: now.Add(-10 * 24 * time.Hour), // Triggers Warning (10 > 7)+ },+ }++ key := BuildGuestKey("inst", "node", 100)+ resourceID := "inst:node:100"+ guestsByKey := map[string]GuestLookup{+ key: {+ ResourceID: resourceID,+ Name: "app-server",+ Instance: "inst",+ Node: "node",+ Type: "qemu",+ VMID: 100,+ },+ }+ guestsByVMID := map[string][]GuestLookup{+ "100": {guestsByKey[key]},+ }++ // 1. Verify warning alert is created with defaults+ m.CheckBackups(storageBackups, nil, nil, guestsByKey, guestsByVMID)+ m.mu.RLock()+ alert, exists := m.activeAlerts["backup-age-"+sanitizeAlertKey(key)]+ m.mu.RUnlock()+ if !exists {+ t.Fatalf("expected backup warning alert")+ }+ if alert.Level != AlertLevelWarning {+ t.Fatalf("expected warning alert, got %s", alert.Level)+ }++ // 2. Apply override to disable backup alerts for this guest+ cfg := m.GetConfig()+ cfg.Overrides = map[string]ThresholdConfig{+ resourceID: {+ Backup: &BackupAlertConfig{Enabled: false},+ },+ }+ m.UpdateConfig(cfg)++ m.CheckBackups(storageBackups, nil, nil, guestsByKey, guestsByVMID)+ m.mu.RLock()+ _, exists = m.activeAlerts["backup-age-"+sanitizeAlertKey(key)]+ m.mu.RUnlock()+ if exists {+ t.Fatalf("expected backup alert to be cleared/suppressed by override")+ }++ // 3. Apply override to change thresholds+ cfg.Overrides[resourceID] = ThresholdConfig{+ Backup: &BackupAlertConfig{+ Enabled: true,+ WarningDays: 15, // 10 < 15, so no alert+ CriticalDays: 20,+ },+ }+ m.UpdateConfig(cfg)+ m.CheckBackups(storageBackups, nil, nil, guestsByKey, guestsByVMID)+ m.mu.RLock()+ _, exists = m.activeAlerts["backup-age-"+sanitizeAlertKey(key)]+ m.mu.RUnlock()+ if exists {+ t.Fatalf("expected no backup alert with increased thresholds in override")+ }++ // 4. Test global guest disable+ cfg.Overrides[resourceID] = ThresholdConfig{+ Disabled: true,+ }+ m.UpdateConfig(cfg)+ storageBackups[0].Time = now.Add(-30 * 24 * time.Hour) // Way past defaults+ m.CheckBackups(storageBackups, nil, nil, guestsByKey, guestsByVMID)+ m.mu.RLock()+ _, exists = m.activeAlerts["backup-age-"+sanitizeAlertKey(key)]+ m.mu.RUnlock()+ if exists {+ t.Fatalf("expected no backup alert for globally disabled guest")+ }+}++func TestCheckBackupsHandlesPbsOnlyGuests(t *testing.T) {m := newTestManager(t)m.ClearActiveAlerts()diff --git a/internal/alerts/filter_evaluation.go b/internal/alerts/filter_evaluation.goindex 73a875e597..1ea9ebc19d 100644--- a/internal/alerts/filter_evaluation.go+++ b/internal/alerts/filter_evaluation.go@@ -253,6 +253,12 @@ func (m *Manager) getGuestThresholds(guest interface{}, guestID string) Thresholif applicableRule.Thresholds.DisableConnectivity {thresholds.DisableConnectivity = true}+ if applicableRule.Thresholds.Backup != nil {+ thresholds.Backup = applicableRule.Thresholds.Backup+ }+ if applicableRule.Thresholds.Snapshot != nil {+ thresholds.Snapshot = applicableRule.Thresholds.Snapshot+ }log.Debug().Str("guest", guestID).@@ -314,6 +320,12 @@ func (m *Manager) getGuestThresholds(guest interface{}, guestID string) Threshol} else if override.NetworkOutLegacy != nil {thresholds.NetworkOut = m.convertLegacyThreshold(override.NetworkOutLegacy)}+ if override.Backup != nil {+ thresholds.Backup = override.Backup+ }+ if override.Snapshot != nil {+ thresholds.Snapshot = override.Snapshot+ }}return thresholdsdiff --git a/internal/monitoring/monitor.go b/internal/monitoring/monitor.goindex 3dbdb6a14a..40f20a7c94 100644--- a/internal/monitoring/monitor.go+++ b/internal/monitoring/monitor.go@@ -8608,11 +8608,12 @@ func buildGuestLookups(snapshot models.StateSnapshot, metadataStore *config.Guesfor _, vm := range snapshot.VMs {info := alerts.GuestLookup{- Name: vm.Name,- Instance: vm.Instance,- Node: vm.Node,- Type: vm.Type,- VMID: vm.VMID,+ ResourceID: makeGuestID(vm.Instance, vm.Node, vm.VMID),+ Name: vm.Name,+ Instance: vm.Instance,+ Node: vm.Node,+ Type: vm.Type,+ VMID: vm.VMID,}key := alerts.BuildGuestKey(vm.Instance, vm.Node, vm.VMID)byKey[key] = info@@ -8628,11 +8629,12 @@ func buildGuestLookups(snapshot models.StateSnapshot, metadataStore *config.Guesfor _, ct := range snapshot.Containers {info := alerts.GuestLookup{- Name: ct.Name,- Instance: ct.Instance,- Node: ct.Node,- Type: ct.Type,- VMID: ct.VMID,+ ResourceID: makeGuestID(ct.Instance, ct.Node, ct.VMID),+ Name: ct.Name,+ Instance: ct.Instance,+ Node: ct.Node,+ Type: ct.Type,+ VMID: ct.VMID,}key := alerts.BuildGuestKey(ct.Instance, ct.Node, ct.VMID)if _, exists := byKey[key]; !exists {