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
feat(alerts): add system-scoped alerts so Pulse can alert on itself
rcourtman9ba62be3033d
diff --git a/docs/release-control/v6/internal/subsystems/alerts.md b/docs/release-control/v6/internal/subsystems/alerts.mdindex 8193a17a3c..4baf4dc108 100644--- a/docs/release-control/v6/internal/subsystems/alerts.md+++ b/docs/release-control/v6/internal/subsystems/alerts.md@@ -242,6 +242,7 @@ default construction path still restores.74. `internal/operationaltrust/contracts.go`75. `internal/alerts/operational_contract.go`76. `internal/alerts/issue1497_test.go`+77. `internal/alerts/system_alert.go`## Shared Boundaries@@ -348,6 +349,21 @@ transition references recovery evidence separate from its trigger evidence.calls, and when the `relay` feature is absent it gates through the shared`FeatureGateSection` and upgrade-navigation contract, rendering no upgradecall-to-action when prompt suppression applies.+11. Add or change system-scoped alerts through+ `internal/alerts/system_alert.go`. A system-scoped alert reports on Pulse+ itself rather than on a monitored resource, for conditions the operator+ cannot observe from outside the product. Notification delivery is the+ founding case: the channel that would carry the warning is the thing that+ failed, so the alert list and navigation badge are the only escalation+ path that does not depend on delivery working. System alerts carry the+ stable `pulse-system-` identity prefix, set no `ResourceID` so+ resource-linked affordances are skipped rather than pointed at nothing,+ and stamp `systemAlert` metadata. Raising must stay idempotent for an+ unchanged condition so a timer-driven evaluator cannot become a+ notification storm; only a change of level or message re-notifies. New+ system alert types must be raised through this helper rather than by+ constructing bare alerts, so identity and the idempotence guarantee stay+ in one place.## Forbidden Pathsdiff --git a/internal/alerts/alerts_test.go b/internal/alerts/alerts_test.goindex 5cfc8820d5..1f4c24f2c6 100644--- a/internal/alerts/alerts_test.go+++ b/internal/alerts/alerts_test.go@@ -20336,3 +20336,121 @@ func TestFlappingDisabledIgnoresCooldown(t *testing.T) {t.Errorf("expected disabled flapping detection to suppress nothing, got suppress=%v justTransitioned=%v", suppress, justTransitioned)}}++// System-scoped alerts report on Pulse itself. The point of routing them+// through the alert pipeline is that they reach the alert list and badge even+// when notification delivery is the thing that is broken, so these pin the+// identity, the idempotence that keeps a timer-driven raise from storming, and+// the clear path.++func TestRaiseSystemAlertIsIdempotentForAnUnchangedCondition(t *testing.T) {+ m := newTestManager(t)++ input := SystemAlertInput{+ Type: NotificationDeliveryAlertType,+ Level: AlertLevelWarning,+ Message: "Notifications are not being delivered.",+ }++ if raised := m.RaiseSystemAlert(input); !raised {+ t.Fatal("expected the first raise to report a new system alert")+ }+ // A condition re-evaluated on a timer must not notify again.+ if raised := m.RaiseSystemAlert(input); raised {+ t.Error("expected an unchanged re-raise to report no change")+ }++ active := m.GetActiveAlerts()+ var found *Alert+ for i := range active {+ if active[i].ID == SystemAlertID(NotificationDeliveryAlertType) {+ if found != nil {+ t.Fatal("expected exactly one system alert, found a duplicate")+ }+ alert := active[i]+ found = &alert+ }+ }+ if found == nil {+ t.Fatal("expected the system alert to be standing")+ }+ if found.ResourceName != SystemAlertResourceName {+ t.Errorf("ResourceName = %q, want %q", found.ResourceName, SystemAlertResourceName)+ }+ if found.ResourceID != "" {+ t.Errorf("expected no resource id on a system alert, got %q", found.ResourceID)+ }+ if !IsSystemAlert(found) {+ t.Error("expected IsSystemAlert to recognise the alert")+ }+ if marker, _ := found.Metadata["systemAlert"].(bool); !marker {+ t.Error("expected the systemAlert metadata marker to be set")+ }+}++func TestRaiseSystemAlertReportsAChangedCondition(t *testing.T) {+ m := newTestManager(t)++ m.RaiseSystemAlert(SystemAlertInput{+ Type: NotificationDeliveryAlertType,+ Level: AlertLevelWarning,+ Message: "Notifications are not being delivered.",+ })++ changed := m.RaiseSystemAlert(SystemAlertInput{+ Type: NotificationDeliveryAlertType,+ Level: AlertLevelCritical,+ Message: "Notifications are not being delivered.",+ })+ if !changed {+ t.Error("expected an escalation in level to report a change")+ }++ active := m.GetActiveAlerts()+ count := 0+ for i := range active {+ if active[i].ID == SystemAlertID(NotificationDeliveryAlertType) {+ count+++ if active[i].Level != AlertLevelCritical {+ t.Errorf("Level = %q, want %q", active[i].Level, AlertLevelCritical)+ }+ }+ }+ if count != 1 {+ t.Errorf("expected the escalation to update one alert, found %d", count)+ }+}++func TestClearSystemAlertRemovesItAndIsSafeWhenAbsent(t *testing.T) {+ m := newTestManager(t)++ if cleared := m.ClearSystemAlert(NotificationDeliveryAlertType); cleared {+ t.Error("expected clearing an absent system alert to be a no-op")+ }++ m.RaiseSystemAlert(SystemAlertInput{+ Type: NotificationDeliveryAlertType,+ Level: AlertLevelWarning,+ Message: "Notifications are not being delivered.",+ })+ if cleared := m.ClearSystemAlert(NotificationDeliveryAlertType); !cleared {+ t.Fatal("expected the standing system alert to clear")+ }++ for _, alert := range m.GetActiveAlerts() {+ if alert.ID == SystemAlertID(NotificationDeliveryAlertType) {+ t.Fatal("expected the system alert to be gone from active alerts")+ }+ }+}++func TestSystemAlertIDRejectsAnEmptyType(t *testing.T) {+ m := newTestManager(t)++ if id := SystemAlertID(" "); id != "" {+ t.Errorf("SystemAlertID(blank) = %q, want empty", id)+ }+ if raised := m.RaiseSystemAlert(SystemAlertInput{Type: " "}); raised {+ t.Error("expected a blank system alert type to raise nothing")+ }+}diff --git a/internal/alerts/system_alert.go b/internal/alerts/system_alert.gonew file mode 100644index 0000000000..451cf936b3--- /dev/null+++ b/internal/alerts/system_alert.go@@ -0,0 +1,165 @@+package alerts++import (+ "strings"+ "time"++ "github.com/rs/zerolog/log"+)++// System-scoped alerts report on Pulse itself rather than on a monitored+// resource. They exist because some failures cannot be observed from the+// outside by the person running Pulse: the clearest case is notification+// delivery, where the channel that would have carried the warning is the thing+// that broke. Routing these through the ordinary alert pipeline puts them in+// the alert list and the navigation badge, which is the only escalation path+// that does not depend on delivery working.+const (+ // SystemAlertResourceName is what the alert list shows in place of a+ // monitored resource name.+ SystemAlertResourceName = "Pulse"++ // SystemAlertIDPrefix namespaces system alert IDs so they cannot collide+ // with a resource-derived alert ID.+ SystemAlertIDPrefix = "pulse-system-"++ // NotificationDeliveryAlertType is the first system alert: configured+ // notification destinations are not delivering.+ NotificationDeliveryAlertType = "notification-delivery"+)++// SystemAlertInput describes a system-scoped condition. Type is required and+// identifies the condition; the same Type always maps to the same alert, so+// repeated raises update one alert rather than accumulating.+type SystemAlertInput struct {+ Type string+ Level AlertLevel+ Message string+ Metadata map[string]interface{}+}++// SystemAlertID returns the stable alert ID for a system alert type.+func SystemAlertID(alertType string) string {+ alertType = strings.TrimSpace(alertType)+ if alertType == "" {+ return ""+ }+ return SystemAlertIDPrefix + alertType+}++// IsSystemAlert reports whether an alert is system-scoped.+func IsSystemAlert(alert *Alert) bool {+ if alert == nil {+ return false+ }+ return strings.HasPrefix(alert.ID, SystemAlertIDPrefix)+}++// RaiseSystemAlert raises a system-scoped alert, or refreshes one that is+// already standing. It is deliberately idempotent: re-raising an unchanged+// condition only advances LastSeen and does not notify again, so a condition+// that is re-evaluated on a timer cannot turn into a notification storm. A+// change of level or message is treated as new information and does notify.+//+// It reports whether this call raised the alert or changed it.+func (m *Manager) RaiseSystemAlert(input SystemAlertInput) bool {+ alertType := strings.TrimSpace(input.Type)+ if alertType == "" {+ return false+ }+ alertID := SystemAlertID(alertType)+ level := input.Level+ if level == "" {+ level = AlertLevelWarning+ }++ m.mu.Lock()++ now := time.Now()+ existing, exists := m.getActiveAlertNoLock(alertID)+ if exists && existing != nil {+ unchanged := existing.Level == level && existing.Message == input.Message+ existing.LastSeen = now+ if unchanged {+ m.setActiveAlertNoLock(alertID, existing)+ m.mu.Unlock()+ m.saveActiveAlertsAsync("system-alert-refresh")+ return false+ }++ existing.Level = level+ existing.Message = input.Message+ existing.Metadata = systemAlertMetadata(alertType, input.Metadata)+ m.setActiveAlertNoLock(alertID, existing)+ // dispatchAlert reads flapping and schedule state through the primary+ // lock, so it must run before the unlock rather than after.+ m.dispatchAlert(existing, true)+ m.mu.Unlock()++ m.saveActiveAlertsAsync("system-alert-update")+ log.Info().+ Str("alertID", alertID).+ Str("level", string(level)).+ Msg("System alert updated")+ return true+ }++ alert := &Alert{+ ID: alertID,+ Type: alertType,+ // A system alert has no monitored resource. ResourceID stays empty so+ // surfaces that link an alert back to a resource skip that affordance+ // rather than offering a link to nothing.+ ResourceID: "",+ ResourceName: SystemAlertResourceName,+ Level: level,+ Message: input.Message,+ StartTime: now,+ LastSeen: now,+ Metadata: systemAlertMetadata(alertType, input.Metadata),+ }++ m.setActiveAlertNoLock(alertID, alert)+ if m.historyManager != nil {+ m.historyManager.AddAlert(*alert)+ }+ m.dispatchAlert(alert, true)+ m.mu.Unlock()++ m.saveActiveAlertsAsync("system-alert-raise")+ log.Warn().+ Str("alertID", alertID).+ Str("level", string(level)).+ Str("message", input.Message).+ Msg("System alert raised")+ return true+}++// ClearSystemAlert clears a standing system alert and reports whether one was+// cleared. Clearing an alert that is not standing is a no-op.+func (m *Manager) ClearSystemAlert(alertType string) bool {+ alertID := SystemAlertID(alertType)+ if alertID == "" {+ return false+ }+ if !m.ClearAlert(alertID) {+ return false+ }+ log.Info().+ Str("alertID", alertID).+ Msg("System alert cleared")+ return true+}++// systemAlertMetadata stamps the marker that surfaces use to tell a+// system-scoped alert apart from a resource alert, without letting a caller+// overwrite it.+func systemAlertMetadata(alertType string, extra map[string]interface{}) map[string]interface{} {+ metadata := make(map[string]interface{}, len(extra)+2)+ for key, value := range extra {+ metadata[key] = value+ }+ metadata["systemAlert"] = true+ metadata["systemAlertType"] = alertType+ return metadata+}