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): preserve docker update delay across host identity churn (#1226)
rcourtman1f74c12ef8ab
diff --git a/internal/alerts/alerts.go b/internal/alerts/alerts.goindex dffe1bb8cb..5d2540ce8d 100644--- a/internal/alerts/alerts.go+++ b/internal/alerts/alerts.go@@ -526,6 +526,8 @@ type Manager struct {dockerRestartTracking map[string]*dockerRestartRecord // Track restart counts and times for restart loop detectiondockerLastExitCode map[string]int // Track last exit code for OOM detectiondockerUpdateFirstSeen map[string]time.Time // Track when image updates were first detected for alert delay+ // Stable identity tracking prevents update-delay resets when host IDs churn.+ dockerUpdateFirstSeenByIdentity map[string]time.Time// PMG quarantine growth trackingpmgQuarantineHistory map[string][]pmgQuarantineSnapshot // Track quarantine snapshots for growth detection// PMG anomaly detection tracking@@ -578,29 +580,30 @@ func NewManagerWithDataDir(dataDir string) *Manager {alertsDir := filepath.Join(dataDir, "alerts")alertOrphaned := truem := &Manager{- activeAlerts: make(map[string]*Alert),- historyManager: NewHistoryManager(alertsDir),- escalationStop: make(chan struct{}),- alertRateLimit: make(map[string][]time.Time),- recentAlerts: make(map[string]*Alert),- suppressedUntil: make(map[string]time.Time),- recentlyResolved: make(map[string]*ResolvedAlert),- pendingAlerts: make(map[string]time.Time),- nodeOfflineCount: make(map[string]int),- offlineConfirmations: make(map[string]int),- dockerOfflineCount: make(map[string]int),- dockerStateConfirm: make(map[string]int),- dockerRestartTracking: make(map[string]*dockerRestartRecord),- dockerLastExitCode: make(map[string]int),- dockerUpdateFirstSeen: make(map[string]time.Time),- pmgQuarantineHistory: make(map[string][]pmgQuarantineSnapshot),- pmgAnomalyTrackers: make(map[string]*pmgAnomalyTracker),- ackState: make(map[string]ackRecord),- flappingHistory: make(map[string][]time.Time),- flappingActive: make(map[string]bool),- cleanupStop: make(chan struct{}),- hostAgentHostnames: make(map[string]struct{}),- nodeDisplayNames: make(map[string]string),+ activeAlerts: make(map[string]*Alert),+ historyManager: NewHistoryManager(alertsDir),+ escalationStop: make(chan struct{}),+ alertRateLimit: make(map[string][]time.Time),+ recentAlerts: make(map[string]*Alert),+ suppressedUntil: make(map[string]time.Time),+ recentlyResolved: make(map[string]*ResolvedAlert),+ pendingAlerts: make(map[string]time.Time),+ nodeOfflineCount: make(map[string]int),+ offlineConfirmations: make(map[string]int),+ dockerOfflineCount: make(map[string]int),+ dockerStateConfirm: make(map[string]int),+ dockerRestartTracking: make(map[string]*dockerRestartRecord),+ dockerLastExitCode: make(map[string]int),+ dockerUpdateFirstSeen: make(map[string]time.Time),+ dockerUpdateFirstSeenByIdentity: make(map[string]time.Time),+ pmgQuarantineHistory: make(map[string][]pmgQuarantineSnapshot),+ pmgAnomalyTrackers: make(map[string]*pmgAnomalyTracker),+ ackState: make(map[string]ackRecord),+ flappingHistory: make(map[string][]time.Time),+ flappingActive: make(map[string]bool),+ cleanupStop: make(chan struct{}),+ hostAgentHostnames: make(map[string]struct{}),+ nodeDisplayNames: make(map[string]string),config: AlertConfig{Enabled: true,ActivationState: ActivationPending,@@ -1687,6 +1690,8 @@ func (m *Manager) applyGlobalOfflineSettingsLocked() {m.dockerStateConfirm = make(map[string]int)m.dockerRestartTracking = make(map[string]*dockerRestartRecord)m.dockerLastExitCode = make(map[string]int)+ m.dockerUpdateFirstSeen = make(map[string]time.Time)+ m.dockerUpdateFirstSeenByIdentity = make(map[string]time.Time)}if m.config.DisableAllDockerServices {var serviceAlerts []string@@ -3737,6 +3742,56 @@ func dockerResourceID(hostID, containerID string) string {return fmt.Sprintf("docker:%s/%s", hostID, containerID)}+func normalizeDockerUpdateTrackingPart(part string) string {+ return strings.ToLower(strings.TrimSpace(part))+}++// dockerUpdateTrackingHostKey builds a stable host identity for Docker update timing.+func dockerUpdateTrackingHostKey(host models.DockerHost) string {+ switch {+ case normalizeDockerUpdateTrackingPart(host.AgentID) != "":+ return "agent:" + normalizeDockerUpdateTrackingPart(host.AgentID)+ case normalizeDockerUpdateTrackingPart(host.TokenID) != "":+ return "token:" + normalizeDockerUpdateTrackingPart(host.TokenID)+ case normalizeDockerUpdateTrackingPart(host.MachineID) != "":+ return "machine:" + normalizeDockerUpdateTrackingPart(host.MachineID)+ case normalizeDockerUpdateTrackingPart(host.Hostname) != "":+ return "hostname:" + normalizeDockerUpdateTrackingPart(host.Hostname)+ case normalizeDockerUpdateTrackingPart(host.ID) != "":+ return "id:" + normalizeDockerUpdateTrackingPart(host.ID)+ case normalizeDockerUpdateTrackingPart(host.DisplayName) != "":+ return "name:" + normalizeDockerUpdateTrackingPart(host.DisplayName)+ default:+ return "unknown-host"+ }+}++func dockerUpdateTrackingContainerKey(container models.DockerContainer) string {+ if id := normalizeDockerUpdateTrackingPart(container.ID); id != "" {+ return "id:" + id+ }++ name := normalizeDockerUpdateTrackingPart(container.Name)+ name = strings.TrimPrefix(name, "/")+ if name != "" {+ return "name:" + name+ }++ if image := normalizeDockerUpdateTrackingPart(container.Image); image != "" {+ return "image:" + image+ }++ return "unknown-container"+}++func dockerUpdateTrackingKey(host models.DockerHost, container models.DockerContainer) string {+ return fmt.Sprintf("docker-update:%s/%s", dockerUpdateTrackingHostKey(host), dockerUpdateTrackingContainerKey(container))+}++func dockerUpdateTrackingHostPrefix(host models.DockerHost) string {+ return fmt.Sprintf("docker-update:%s/", dockerUpdateTrackingHostKey(host))+}+// dockerServiceDisplayName normalizes the service name for alert readability.func dockerServiceDisplayName(service models.DockerService) string {name := strings.TrimSpace(service.Name)@@ -3834,9 +3889,11 @@ func (m *Manager) CheckDockerHost(host models.DockerHost) {}seen := make(map[string]struct{}, len(host.Containers)+len(host.Services))+ seenUpdateTracking := make(map[string]struct{}, len(host.Containers))for _, container := range host.Containers {containerName := dockerContainerDisplayName(container)resourceID := dockerResourceID(host.ID, container.ID)+ updateTrackingKey := dockerUpdateTrackingKey(host, container)if matchesDockerIgnoredPrefix(containerName, container.ID, ignoredPrefixes) {log.Debug().@@ -3853,10 +3910,12 @@ func (m *Manager) CheckDockerHost(host models.DockerHost) {delete(m.dockerRestartTracking, resourceID)delete(m.dockerLastExitCode, resourceID)m.mu.Unlock()+ m.clearDockerContainerUpdateTracking(resourceID, updateTrackingKey)continue}seen[resourceID] = struct{}{}+ seenUpdateTracking[updateTrackingKey] = struct{}{}m.evaluateDockerContainer(host, container, resourceID)}@@ -3866,7 +3925,7 @@ func (m *Manager) CheckDockerHost(host models.DockerHost) {m.evaluateDockerService(host, service, resourceID)}- m.cleanupDockerContainerAlerts(host, seen)+ m.cleanupDockerContainerAlertsWithTracking(host, seen, seenUpdateTracking)}func (m *Manager) evaluateDockerContainer(host models.DockerHost, container models.DockerContainer, resourceID string) {@@ -4201,7 +4260,7 @@ func (m *Manager) HandleDockerHostRemoved(host models.DockerHost) {// Reuse the online handler to clear offline alerts and tracking.m.HandleDockerHostOnline(host)// Drop any container alerts and host-scoped tracking entries.- m.clearDockerHostContainerAlerts(host.ID)+ m.clearDockerHostContainerAlerts(host)}// HandleDockerHostOffline raises an alert when a Docker host stops reporting.@@ -4326,7 +4385,7 @@ func (m *Manager) HandleDockerHostOffline(host models.DockerHost) {Str("hostname", host.Hostname).Msg("CRITICAL: Docker host is offline")- m.clearDockerHostContainerAlerts(host.ID)+ m.clearDockerHostContainerAlerts(host)}func (m *Manager) checkDockerContainerState(host models.DockerHost, container models.DockerContainer, resourceID, containerName, instanceName, nodeName string) {@@ -4770,9 +4829,19 @@ func (m *Manager) clearDockerContainerMetricAlerts(resourceID string, metrics ..}}+func (m *Manager) clearDockerContainerUpdateTracking(resourceID, trackingKey string) {+ m.mu.Lock()+ delete(m.dockerUpdateFirstSeen, resourceID)+ if trackingKey != "" {+ delete(m.dockerUpdateFirstSeenByIdentity, trackingKey)+ }+ m.mu.Unlock()+}+// checkDockerContainerImageUpdate checks if an image update has been pending for too longfunc (m *Manager) checkDockerContainerImageUpdate(host models.DockerHost, container models.DockerContainer, resourceID, containerName, instanceName, nodeName string) {alertID := fmt.Sprintf("docker-container-update-%s", resourceID)+ updateTrackingKey := dockerUpdateTrackingKey(host, container)// Check if update detection is enabledm.mu.RLock()@@ -4782,9 +4851,7 @@ func (m *Manager) checkDockerContainerImageUpdate(host models.DockerHost, contai// Negative value means disabledif delayHours < 0 {m.clearAlert(alertID)- m.mu.Lock()- delete(m.dockerUpdateFirstSeen, resourceID)- m.mu.Unlock()+ m.clearDockerContainerUpdateTracking(resourceID, updateTrackingKey)return}@@ -4792,9 +4859,7 @@ func (m *Manager) checkDockerContainerImageUpdate(host models.DockerHost, contaiif container.UpdateStatus == nil {// No update status - clear any tracking and alertsm.clearAlert(alertID)- m.mu.Lock()- delete(m.dockerUpdateFirstSeen, resourceID)- m.mu.Unlock()+ m.clearDockerContainerUpdateTracking(resourceID, updateTrackingKey)return}@@ -4809,19 +4874,21 @@ func (m *Manager) checkDockerContainerImageUpdate(host models.DockerHost, contaiif !container.UpdateStatus.UpdateAvailable {// No update available - clear tracking and alertm.clearAlert(alertID)- m.mu.Lock()- delete(m.dockerUpdateFirstSeen, resourceID)- m.mu.Unlock()+ m.clearDockerContainerUpdateTracking(resourceID, updateTrackingKey)return}// Update is available - track when we first saw itm.mu.Lock()- firstSeen, exists := m.dockerUpdateFirstSeen[resourceID]+ firstSeen, exists := m.dockerUpdateFirstSeenByIdentity[updateTrackingKey]+ if !exists {+ firstSeen, exists = m.dockerUpdateFirstSeen[resourceID]+ }if !exists {firstSeen = time.Now()- m.dockerUpdateFirstSeen[resourceID] = firstSeen}+ m.dockerUpdateFirstSeen[resourceID] = firstSeen+ m.dockerUpdateFirstSeenByIdentity[updateTrackingKey] = firstSeenm.mu.Unlock()// Check if we've exceeded the delay threshold@@ -4896,7 +4963,12 @@ func (m *Manager) checkDockerContainerImageUpdate(host models.DockerHost, contai}func (m *Manager) cleanupDockerContainerAlerts(host models.DockerHost, seen map[string]struct{}) {+ m.cleanupDockerContainerAlertsWithTracking(host, seen, nil)+}++func (m *Manager) cleanupDockerContainerAlertsWithTracking(host models.DockerHost, seen map[string]struct{}, seenUpdateTracking map[string]struct{}) {prefix := fmt.Sprintf("docker:%s/", strings.TrimSpace(host.ID))+ updateTrackingPrefix := dockerUpdateTrackingHostPrefix(host)m.mu.Lock()toClear := make([]string, 0)@@ -4924,6 +4996,16 @@ func (m *Manager) cleanupDockerContainerAlerts(host models.DockerHost, seen map[}}}+ if seenUpdateTracking != nil {+ for trackingKey := range m.dockerUpdateFirstSeenByIdentity {+ if !strings.HasPrefix(trackingKey, updateTrackingPrefix) {+ continue+ }+ if _, exists := seenUpdateTracking[trackingKey]; !exists {+ delete(m.dockerUpdateFirstSeenByIdentity, trackingKey)+ }+ }+ }m.mu.Unlock()for _, alertID := range toClear {@@ -4931,8 +5013,9 @@ func (m *Manager) cleanupDockerContainerAlerts(host models.DockerHost, seen map[}}-func (m *Manager) clearDockerHostContainerAlerts(hostID string) {- prefix := fmt.Sprintf("docker:%s/", strings.TrimSpace(hostID))+func (m *Manager) clearDockerHostContainerAlerts(host models.DockerHost) {+ prefix := fmt.Sprintf("docker:%s/", strings.TrimSpace(host.ID))+ updateTrackingPrefix := dockerUpdateTrackingHostPrefix(host)m.mu.Lock()toClear := make([]string, 0)@@ -4961,6 +5044,11 @@ func (m *Manager) clearDockerHostContainerAlerts(hostID string) {delete(m.dockerUpdateFirstSeen, resourceID)}}+ for trackingKey := range m.dockerUpdateFirstSeenByIdentity {+ if strings.HasPrefix(trackingKey, updateTrackingPrefix) {+ delete(m.dockerUpdateFirstSeenByIdentity, trackingKey)+ }+ }m.mu.Unlock()for _, alertID := range toClear {@@ -9404,6 +9492,10 @@ func (m *Manager) ClearActiveAlerts() {m.offlineConfirmations = make(map[string]int)m.dockerOfflineCount = make(map[string]int)m.dockerStateConfirm = make(map[string]int)+ m.dockerRestartTracking = make(map[string]*dockerRestartRecord)+ m.dockerLastExitCode = make(map[string]int)+ m.dockerUpdateFirstSeen = make(map[string]time.Time)+ m.dockerUpdateFirstSeenByIdentity = make(map[string]time.Time)m.ackState = make(map[string]ackRecord)m.mu.Unlock()@@ -9575,6 +9667,12 @@ func (m *Manager) cleanupStaleMaps() {cleaned++}}+ for containerID, firstSeen := range m.dockerUpdateFirstSeenByIdentity {+ if now.Sub(firstSeen) > staleThreshold {+ delete(m.dockerUpdateFirstSeenByIdentity, containerID)+ cleaned+++ }+ }// Clean up rate limit entries older than 1 hourrateLimitThreshold := RateLimitCleanupWindowdiff --git a/internal/alerts/update_alerts_test.go b/internal/alerts/update_alerts_test.goindex 2ff3784feb..07ad41a078 100644--- a/internal/alerts/update_alerts_test.go+++ b/internal/alerts/update_alerts_test.go@@ -117,10 +117,15 @@ func TestCheckDockerContainerImageUpdate(t *testing.T) {// Verify tracking is setm.mu.RLock()_, tracked := m.dockerUpdateFirstSeen[resourceID]+ trackingKey := dockerUpdateTrackingKey(host, container)+ _, trackedByIdentity := m.dockerUpdateFirstSeenByIdentity[trackingKey]m.mu.RUnlock()if !tracked {t.Error("Expected update to be tracked in dockerUpdateFirstSeen")}+ if !trackedByIdentity {+ t.Error("Expected update to be tracked in dockerUpdateFirstSeenByIdentity")+ }})t.Run("update available and past threshold - creates alert", func(t *testing.T) {@@ -199,10 +204,15 @@ func TestCheckDockerContainerImageUpdate(t *testing.T) {// Verify tracking is removedm.mu.RLock()_, tracked := m.dockerUpdateFirstSeen[testResourceID]+ trackingKey := dockerUpdateTrackingKey(host, container)+ _, trackedByIdentity := m.dockerUpdateFirstSeenByIdentity[trackingKey]m.mu.RUnlock()if tracked {t.Error("Expected tracking to be removed when update is applied")}+ if trackedByIdentity {+ t.Error("Expected identity tracking to be removed when update is applied")+ }})t.Run("disabled by negative delay hours", func(t *testing.T) {@@ -238,6 +248,76 @@ func TestCheckDockerContainerImageUpdate(t *testing.T) {})}+func TestCheckDockerContainerImageUpdatePreservesDelayAcrossHostIDChange(t *testing.T) {+ m := NewManager()+ m.mu.Lock()+ m.config.DockerDefaults.UpdateAlertDelayHours = 24+ m.mu.Unlock()++ container := models.DockerContainer{+ ID: "container-abc123",+ Name: "web",+ Image: "nginx:latest",+ UpdateStatus: &models.DockerContainerUpdateStatus{+ UpdateAvailable: true,+ CurrentDigest: "sha256:old",+ LatestDigest: "sha256:new",+ LastChecked: time.Now(),+ },+ }++ oldHost := models.DockerHost{+ ID: "docker-host-old",+ AgentID: "agent-stable-1",+ DisplayName: "Old Docker Host",+ Hostname: "docker.local",+ }+ newHost := oldHost+ newHost.ID = "docker-host-new"+ newHost.DisplayName = "New Docker Host"++ oldResourceID := dockerResourceID(oldHost.ID, container.ID)+ m.checkDockerContainerImageUpdate(oldHost, container, oldResourceID, "web", "docker-instance", "docker.local")++ firstSeen := time.Now().Add(-25 * time.Hour)+ trackingKey := dockerUpdateTrackingKey(oldHost, container)+ m.mu.Lock()+ m.dockerUpdateFirstSeen[oldResourceID] = firstSeen+ m.dockerUpdateFirstSeenByIdentity[trackingKey] = firstSeen+ m.mu.Unlock()++ newResourceID := dockerResourceID(newHost.ID, container.ID)+ m.checkDockerContainerImageUpdate(newHost, container, newResourceID, "web", "docker-instance", "docker.local")++ alertID := "docker-container-update-" + newResourceID+ m.mu.RLock()+ alert, hasAlert := m.activeAlerts[alertID]+ resourceFirstSeen, hasResourceTracking := m.dockerUpdateFirstSeen[newResourceID]+ identityFirstSeen, hasIdentityTracking := m.dockerUpdateFirstSeenByIdentity[dockerUpdateTrackingKey(newHost, container)]+ m.mu.RUnlock()++ if !hasAlert {+ t.Fatalf("Expected update alert %q after host ID change", alertID)+ }+ if !hasResourceTracking {+ t.Fatalf("Expected resource tracking for new host resource ID")+ }+ if !hasIdentityTracking {+ t.Fatalf("Expected identity tracking to persist across host ID change")+ }++ const tolerance = 2 * time.Second+ if delta := alert.StartTime.Sub(firstSeen); delta < -tolerance || delta > tolerance {+ t.Fatalf("Expected alert StartTime near %s, got %s", firstSeen, alert.StartTime)+ }+ if delta := resourceFirstSeen.Sub(firstSeen); delta < -tolerance || delta > tolerance {+ t.Fatalf("Expected resource firstSeen near %s, got %s", firstSeen, resourceFirstSeen)+ }+ if delta := identityFirstSeen.Sub(firstSeen); delta < -tolerance || delta > tolerance {+ t.Fatalf("Expected identity firstSeen near %s, got %s", firstSeen, identityFirstSeen)+ }+}+// Note: Update alerts are now a free feature (no license gating).// The Pro license gating tests were removed when update alerts became free.@@ -246,6 +326,19 @@ func TestDockerUpdateTrackingCleanup(t *testing.T) {hostID := "docker-host-1"prefix := "docker:" + hostID + "/"+ host := models.DockerHost{+ ID: hostID,+ AgentID: "agent-1",+ Hostname: "docker-host-1.local",+ }+ otherHost := models.DockerHost{+ ID: "other-host",+ AgentID: "agent-other",+ }+ c1 := models.DockerContainer{ID: "container-1"}+ c2 := models.DockerContainer{ID: "container-2"}+ c3 := models.DockerContainer{ID: "container-3"}+ otherContainer := models.DockerContainer{ID: "container-x"}// Add some tracking entriesm.mu.Lock()@@ -253,18 +346,25 @@ func TestDockerUpdateTrackingCleanup(t *testing.T) {m.dockerUpdateFirstSeen[prefix+"container-2"] = time.Now()m.dockerUpdateFirstSeen[prefix+"container-3"] = time.Now()m.dockerUpdateFirstSeen["docker:other-host/container-x"] = time.Now()+ m.dockerUpdateFirstSeenByIdentity[dockerUpdateTrackingKey(host, c1)] = time.Now()+ m.dockerUpdateFirstSeenByIdentity[dockerUpdateTrackingKey(host, c2)] = time.Now()+ m.dockerUpdateFirstSeenByIdentity[dockerUpdateTrackingKey(host, c3)] = time.Now()+ m.dockerUpdateFirstSeenByIdentity[dockerUpdateTrackingKey(otherHost, otherContainer)] = time.Now()m.mu.Unlock()- // Create host and seen map with only container-1 and container-2- host := models.DockerHost{ID: hostID}+ // Create seen maps with only container-1 and container-2seen := map[string]struct{}{prefix + "container-1": {},prefix + "container-2": {},// container-3 is removed}+ seenUpdateTracking := map[string]struct{}{+ dockerUpdateTrackingKey(host, c1): {},+ dockerUpdateTrackingKey(host, c2): {},+ }// Run cleanup- m.cleanupDockerContainerAlerts(host, seen)+ m.cleanupDockerContainerAlertsWithTracking(host, seen, seenUpdateTracking)// Verify container-3 tracking is removedm.mu.RLock()@@ -272,6 +372,10 @@ func TestDockerUpdateTrackingCleanup(t *testing.T) {_, hasC2 := m.dockerUpdateFirstSeen[prefix+"container-2"]_, hasC3 := m.dockerUpdateFirstSeen[prefix+"container-3"]_, hasOther := m.dockerUpdateFirstSeen["docker:other-host/container-x"]+ _, hasC1ByIdentity := m.dockerUpdateFirstSeenByIdentity[dockerUpdateTrackingKey(host, c1)]+ _, hasC2ByIdentity := m.dockerUpdateFirstSeenByIdentity[dockerUpdateTrackingKey(host, c2)]+ _, hasC3ByIdentity := m.dockerUpdateFirstSeenByIdentity[dockerUpdateTrackingKey(host, c3)]+ _, hasOtherByIdentity := m.dockerUpdateFirstSeenByIdentity[dockerUpdateTrackingKey(otherHost, otherContainer)]m.mu.RUnlock()if !hasC1 {@@ -286,4 +390,16 @@ func TestDockerUpdateTrackingCleanup(t *testing.T) {if !hasOther {t.Error("Expected other host tracking to remain")}+ if !hasC1ByIdentity {+ t.Error("Expected container-1 identity tracking to remain")+ }+ if !hasC2ByIdentity {+ t.Error("Expected container-2 identity tracking to remain")+ }+ if hasC3ByIdentity {+ t.Error("Expected container-3 identity tracking to be removed")+ }+ if !hasOtherByIdentity {+ t.Error("Expected other host identity tracking to remain")+ }}