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 resolved notifications after direct alert dispatch

rcourtman4721f3d1c0ea
diff --git a/docs/release-control/v6/internal/subsystems/alerts.md b/docs/release-control/v6/internal/subsystems/alerts.md
index f038bc08d1..115f3b1c82 100644
--- a/docs/release-control/v6/internal/subsystems/alerts.md
+++ b/docs/release-control/v6/internal/subsystems/alerts.md
@@ -312,6 +312,11 @@ suppression, monitor-only notification suppression, cooldown decisions, and
per-alert rate limiting; future notification-gating changes should extend that
policy owner rather than burying new checks inside metric or resource-specific
evaluators.
+The same dispatch policy owns firing-notification evidence on active alerts:
+any alert that passes notification suppression and enters the fired callback
+fan-out must carry `LastNotified` before the callback clone is emitted. Resolved
+notification gating depends on that field to distinguish alerts whose firing
+notification actually entered delivery from alerts that were never sent.
Alert read-side output now lives in `internal/alerts/read_model.go`. That file
owns active alert projection and sorting, metadata coercion helpers,
recently-resolved and history output wrappers, and notify-existing redispatch;
diff --git a/internal/alerts/alerts_test.go b/internal/alerts/alerts_test.go
index 8e098e4bb9..9ad05d72b3 100644
--- a/internal/alerts/alerts_test.go
+++ b/internal/alerts/alerts_test.go
@@ -17169,6 +17169,38 @@ func TestDispatchAlert(t *testing.T) {
}
})
+ t.Run("records last notified when dispatch succeeds", func(t *testing.T) {
+ m := newTestManager(t)
+
+ var receivedAlert *Alert
+ m.SetAlertCallback(func(a *Alert) {
+ receivedAlert = a
+ })
+
+ m.mu.Lock()
+ m.config.ActivationState = ActivationActive
+ m.mu.Unlock()
+
+ alert := &Alert{
+ ID: "test-alert",
+ Type: "powered-off",
+ ResourceName: "testvm",
+ }
+
+ if !m.dispatchAlert(alert, false) {
+ t.Fatal("expected dispatch to succeed")
+ }
+ if alert.LastNotified == nil {
+ t.Fatal("expected dispatched alert to record LastNotified")
+ }
+ if receivedAlert == nil || receivedAlert.LastNotified == nil {
+ t.Fatal("expected dispatched clone to include LastNotified")
+ }
+ if !receivedAlert.LastNotified.Equal(*alert.LastNotified) {
+ t.Fatalf("clone LastNotified = %v, want %v", receivedAlert.LastNotified, alert.LastNotified)
+ }
+ })
+
t.Run("dispatches asynchronously when async is true", func(t *testing.T) {
// t.Parallel()
m := newTestManager(t)
diff --git a/internal/alerts/notification_policy.go b/internal/alerts/notification_policy.go
index 0c503d1984..c5d8bbfd59 100644
--- a/internal/alerts/notification_policy.go
+++ b/internal/alerts/notification_policy.go
@@ -173,6 +173,9 @@ func (m *Manager) dispatchAlert(alert *Alert, async bool) bool {
recordAlertFired(alert)
}
+ notifiedAt := time.Now()
+ alert.LastNotified = &notifiedAt
+
alertCopy := cloneAlertForOutput(alert)
if async {
go func(a *Alert, fns []func(*Alert)) {
diff --git a/internal/monitoring/monitor_alert_handling_test.go b/internal/monitoring/monitor_alert_handling_test.go
index 73a80a65c1..a4733b63e1 100644
--- a/internal/monitoring/monitor_alert_handling_test.go
+++ b/internal/monitoring/monitor_alert_handling_test.go
@@ -1,6 +1,8 @@
package monitoring
import (
+ "encoding/json"
+ "io"
"net/http"
"net/http/httptest"
"sync/atomic"
@@ -279,6 +281,97 @@ func TestMonitor_HandleAlertResolved_NoQuietHoursSendsNotification(t *testing.T)
m.handleAlertResolved(alertID)
}
+func TestMonitor_HandleAlertResolved_SendsRecoveryForGuestPoweredOffState(t *testing.T) {
+ received := make(chan []byte, 2)
+ srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ defer r.Body.Close()
+ body, _ := io.ReadAll(r.Body)
+ select {
+ case received <- body:
+ default:
+ }
+ w.WriteHeader(http.StatusOK)
+ }))
+ t.Cleanup(srv.Close)
+
+ notifMgr := notifications.NewNotificationManagerWithDataDir("http://pulse.example", t.TempDir())
+ if err := notifMgr.UpdateAllowedPrivateCIDRs("127.0.0.1/32,::1/128"); err != nil {
+ t.Fatalf("UpdateAllowedPrivateCIDRs: %v", err)
+ }
+ notifMgr.AddWebhook(notifications.WebhookConfig{
+ ID: "test-webhook",
+ Name: "test-webhook",
+ URL: srv.URL,
+ Enabled: true,
+ Service: "generic",
+ })
+ notifMgr.SetNotifyOnResolve(true)
+ notifMgr.SetGroupingWindow(0)
+
+ alertMgr := alerts.NewManager()
+ cfg := alertMgr.GetConfig()
+ cfg.Enabled = true
+ cfg.ActivationState = alerts.ActivationActive
+ cfg.Schedule.QuietHours.Enabled = false
+ alertMgr.UpdateConfig(cfg)
+
+ m := &Monitor{
+ alertManager: alertMgr,
+ notificationMgr: notifMgr,
+ }
+ alertMgr.SetAlertCallback(m.handleAlertFired)
+ alertMgr.SetResolvedCallback(m.handleAlertResolved)
+
+ vm := models.VM{
+ ID: "vm-powered-off",
+ Name: "powered-off-vm",
+ Node: "node-1",
+ Instance: "inst-1",
+ Status: "stopped",
+ }
+
+ alertMgr.CheckGuest(vm, vm.Instance)
+ alertMgr.CheckGuest(vm, vm.Instance)
+
+ select {
+ case <-received:
+ case <-time.After(5 * time.Second):
+ t.Fatalf("timed out waiting for initial powered-off notification webhook")
+ }
+
+ activeAlerts := alertMgr.GetActiveAlerts()
+ if len(activeAlerts) != 1 {
+ t.Fatalf("expected one active powered-off alert, got %#v", activeAlerts)
+ }
+ alertID := activeAlerts[0].ID
+ if activeAlerts[0].LastNotified == nil {
+ t.Fatalf("expected powered-off alert %q to record firing notification time", alertID)
+ }
+
+ if resolved := alertMgr.GetResolvedAlert(alertID); resolved != nil {
+ t.Fatalf("did not expect powered-off alert %q to be resolved before the VM starts", alertID)
+ }
+
+ vm.Status = "running"
+ alertMgr.CheckGuest(vm, vm.Instance)
+
+ select {
+ case body := <-received:
+ var payload map[string]interface{}
+ if err := json.Unmarshal(body, &payload); err != nil {
+ t.Fatalf("failed to parse webhook payload: %v", err)
+ }
+ if payload["event"] != "resolved" {
+ t.Fatalf("expected webhook event=resolved, got %v", payload["event"])
+ }
+ if payload["alertIdentifier"] != alertID {
+ t.Fatalf("expected webhook alertIdentifier=%q, got %v", alertID, payload["alertIdentifier"])
+ }
+ case <-time.After(5 * time.Second):
+ t.Fatalf("timed out waiting for powered-off recovery notification webhook")
+ }
+}
+
func TestMonitor_HandleAlertEscalated_QuietHoursSuppressesNotification(t *testing.T) {
t.Setenv("PULSE_DATA_DIR", t.TempDir())