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.

Show Patrol findings in alert emails

[email protected]66d8e90c0c93
diff --git a/docs/release-control/v6/internal/subsystems/notifications.md b/docs/release-control/v6/internal/subsystems/notifications.md
index 5aac4bd0c1..bf9cc0e2b7 100644
--- a/docs/release-control/v6/internal/subsystems/notifications.md
+++ b/docs/release-control/v6/internal/subsystems/notifications.md
@@ -28,6 +28,10 @@ window delivers each alert independently, and disabling grouping flushes any
pending alerts as individual deliveries. Grouped provider payloads must retain
every alert, while live ntfy firing deliveries and webhook tests share the same
severity-derived title, priority, and tags.
+Email rendering treats `patrol_finding` as a finding rather than a numeric
+threshold alert. Single and grouped HTML/text bodies show the finding message
+and optional category, omit meaningless zero-value metrics, preserve mixed
+group headings, and HTML-escape every resource, message, and category field.
## Canonical Files
diff --git a/internal/notifications/email_template.go b/internal/notifications/email_template.go
index 01e04315e9..9d7ef40ad5 100644
--- a/internal/notifications/email_template.go
+++ b/internal/notifications/email_template.go
@@ -37,6 +37,35 @@ func alertNodeDisplay(alert *alerts.Alert) string {
return alert.Node
}
+func isPatrolFindingAlert(alert *alerts.Alert) bool {
+ return alert != nil && strings.EqualFold(strings.TrimSpace(alert.Type), "patrol_finding")
+}
+
+func alertTypeDisplay(alertType string) string {
+ switch strings.ToLower(strings.TrimSpace(alertType)) {
+ case "cpu":
+ return "CPU"
+ case "memory":
+ return "Memory"
+ case "disk":
+ return "Disk"
+ case "io":
+ return "I/O"
+ case "patrol_finding":
+ return "Patrol Finding"
+ default:
+ return titleCase(alertType)
+ }
+}
+
+func patrolFindingCategory(alert *alerts.Alert) string {
+ if alert == nil || alert.Metadata == nil {
+ return ""
+ }
+ category, _ := alert.Metadata["category"].(string)
+ return titleCase(strings.TrimSpace(category))
+}
+
// EmailTemplate generates a professional HTML email template for alerts
func EmailTemplate(alertList []*alerts.Alert, isSingle bool) (subject, htmlBody, textBody string) {
if isSingle && len(alertList) == 1 {
@@ -53,20 +82,7 @@ func singleAlertTemplate(alert *alerts.Alert) (subject, htmlBody, textBody strin
levelBg = "#fffaeb"
}
- // Properly format alert type (CPU, Memory, etc.)
- alertType := alert.Type
- switch strings.ToLower(alertType) {
- case "cpu":
- alertType = "CPU"
- case "memory":
- alertType = "Memory"
- case "disk":
- alertType = "Disk"
- case "io":
- alertType = "I/O"
- default:
- alertType = titleCase(alertType)
- }
+ alertType := alertTypeDisplay(alert.Type)
subject = fmt.Sprintf("[Pulse Alert] %s: %s on %s",
titleCase(string(alert.Level)), alertType, alert.ResourceName)
@@ -83,6 +99,24 @@ func singleAlertTemplate(alert *alerts.Alert) (subject, htmlBody, textBody strin
escapedStarted := html.EscapeString(formatAlertStartTime(alert.StartTime))
escapedDuration := html.EscapeString(formatDuration(time.Since(alert.StartTime)))
+ metricsHTML := fmt.Sprintf(`
+ <div class="metrics">
+ <div class="metric">
+ <div class="metric-label">Current Value</div>
+ <div class="metric-value">%s</div>
+ </div>
+ <div class="metric">
+ <div class="metric-label">Threshold</div>
+ <div class="metric-value">%s</div>
+ </div>
+ </div>`,
+ escapedCurrentValue,
+ escapedThresholdValue,
+ )
+ if isPatrolFindingAlert(alert) {
+ metricsHTML = ""
+ }
+
htmlBody = fmt.Sprintf(`<!DOCTYPE html>
<html>
<head>
@@ -130,17 +164,7 @@ func singleAlertTemplate(alert *alerts.Alert) (subject, htmlBody, textBody strin
<div class="alert-resource">%s</div>
<div>%s</div>
</div>
-
- <div class="metrics">
- <div class="metric">
- <div class="metric-label">Current Value</div>
- <div class="metric-value">%s</div>
- </div>
- <div class="metric">
- <div class="metric-label">Threshold</div>
- <div class="metric-value">%s</div>
- </div>
- </div>
+ %s
<div class="details">
<div class="detail-row">
@@ -180,8 +204,7 @@ func singleAlertTemplate(alert *alerts.Alert) (subject, htmlBody, textBody strin
escapedLevel,
escapedResourceName,
escapedMessage,
- escapedCurrentValue,
- escapedThresholdValue,
+ metricsHTML,
escapedResourceID,
escapedAlertType,
escapedNode,
@@ -190,7 +213,37 @@ func singleAlertTemplate(alert *alerts.Alert) (subject, htmlBody, textBody strin
escapedDuration,
)
- // Plain text version
+ if isPatrolFindingAlert(alert) {
+ textBody = fmt.Sprintf(`PULSE MONITORING ALERT
+
+%s ALERT: %s
+
+Resource: %s (%s)
+Type: %s
+Message: %s
+
+Details:
+- Node: %s
+- Instance: %s
+- Started: %s
+- Duration: %s
+
+This is an automated notification from Pulse Monitoring.
+View alerts and configure settings in your Pulse dashboard.`,
+ strings.ToUpper(string(alert.Level)),
+ alert.ResourceName,
+ alert.ResourceName,
+ alert.ResourceID,
+ alertType,
+ alert.Message,
+ alertNodeDisplay(alert),
+ alert.Instance,
+ formatAlertStartTime(alert.StartTime),
+ formatDuration(time.Since(alert.StartTime)),
+ )
+ return subject, htmlBody, textBody
+ }
+
textBody = fmt.Sprintf(`PULSE MONITORING ALERT
%s ALERT: %s
@@ -228,12 +281,16 @@ View alerts and configure settings in your Pulse dashboard.`,
func groupedAlertTemplate(alertList []*alerts.Alert) (subject, htmlBody, textBody string) {
critical := 0
warning := 0
+ patrolFindings := 0
for _, alert := range alertList {
if alert.Level == "critical" {
critical++
} else {
warning++
}
+ if isPatrolFindingAlert(alert) {
+ patrolFindings++
+ }
}
// Subject line
@@ -260,6 +317,18 @@ func groupedAlertTemplate(alertList []*alerts.Alert) (subject, htmlBody, textBod
escapedValue := html.EscapeString(formatMetricValue(alert.Type, alert.Value))
escapedThreshold := html.EscapeString(formatMetricThreshold(alert.Type, alert.Threshold))
escapedDuration := html.EscapeString(formatDuration(time.Since(alert.StartTime)))
+ escapedDetail := escapedType + " on " + escapedNode
+ value := fmt.Sprintf(`<div style="font-weight: 500;">%s</div>
+ <div style="font-size: 12px; color: #666;">of %s</div>`, escapedValue, escapedThreshold)
+ if isPatrolFindingAlert(alert) {
+ escapedDetail = html.EscapeString(alert.Message)
+ category := html.EscapeString(patrolFindingCategory(alert))
+ value = `<div style="font-weight: 500;">Patrol</div>`
+ if category != "" {
+ value += fmt.Sprintf(`
+ <div style="font-size: 12px; color: #666;">%s</div>`, category)
+ }
+ }
alertRows.WriteString(fmt.Sprintf(`
<tr>
@@ -268,7 +337,7 @@ func groupedAlertTemplate(alertList []*alerts.Alert) (subject, htmlBody, textBod
<span style="display: inline-block; width: 8px; height: 8px; background: %s; border-radius: 50%%; margin-right: 10px;"></span>
<div>
<div style="font-weight: 500; color: #1a1a1a;">%s</div>
- <div style="font-size: 12px; color: #666; margin-top: 2px;">%s on %s</div>
+ <div style="font-size: 12px; color: #666; margin-top: 2px;">%s</div>
</div>
</div>
</td>
@@ -276,8 +345,7 @@ func groupedAlertTemplate(alertList []*alerts.Alert) (subject, htmlBody, textBod
<span style="color: %s; font-weight: 500; text-transform: uppercase; font-size: 12px;">%s</span>
</td>
<td style="padding: 12px; border-bottom: 1px solid #e9ecef; text-align: right;">
- <div style="font-weight: 500;">%s</div>
- <div style="font-size: 12px; color: #666;">of %s</div>
+ %s
</td>
<td style="padding: 12px; border-bottom: 1px solid #e9ecef; text-align: right; color: #666; font-size: 12px;">
%s ago
@@ -285,13 +353,20 @@ func groupedAlertTemplate(alertList []*alerts.Alert) (subject, htmlBody, textBod
</tr>`,
levelColor,
escapedResourceName,
- escapedType, escapedNode,
+ escapedDetail,
levelColor, escapedLevel,
- escapedValue, escapedThreshold,
+ value,
escapedDuration,
))
}
+ valueHeading := "Value"
+ if patrolFindings == len(alertList) {
+ valueHeading = "Finding"
+ } else if patrolFindings > 0 {
+ valueHeading = "Value / Finding"
+ }
+
htmlBody = fmt.Sprintf(`<!DOCTYPE html>
<html>
<head>
@@ -361,7 +436,7 @@ func groupedAlertTemplate(alertList []*alerts.Alert) (subject, htmlBody, textBod
<tr>
<th>Resource</th>
<th style="text-align: center;">Level</th>
- <th style="text-align: right;">Value</th>
+ <th style="text-align: right;">%s</th>
<th style="text-align: right;">Duration</th>
</tr>
</thead>
@@ -375,7 +450,7 @@ func groupedAlertTemplate(alertList []*alerts.Alert) (subject, htmlBody, textBod
</div>
</div>
</body>
-</html>`, alertRows.String())
+</html>`, valueHeading, alertRows.String())
// Plain text version
var textBuilder strings.Builder
@@ -393,7 +468,9 @@ func groupedAlertTemplate(alertList []*alerts.Alert) (subject, htmlBody, textBod
for i, alert := range alertList {
textBuilder.WriteString(fmt.Sprintf("\n%d. %s (%s)\n", i+1, alert.ResourceName, alert.ResourceID))
textBuilder.WriteString(fmt.Sprintf(" Level: %s | Type: %s\n", strings.ToUpper(string(alert.Level)), alert.Type))
- textBuilder.WriteString(fmt.Sprintf(" Value: %s (Threshold: %s)\n", formatMetricValue(alert.Type, alert.Value), formatMetricThreshold(alert.Type, alert.Threshold)))
+ if !isPatrolFindingAlert(alert) {
+ textBuilder.WriteString(fmt.Sprintf(" Value: %s (Threshold: %s)\n", formatMetricValue(alert.Type, alert.Value), formatMetricThreshold(alert.Type, alert.Threshold)))
+ }
textBuilder.WriteString(fmt.Sprintf(" Node: %s | Started: %s ago\n", alertNodeDisplay(alert), formatDuration(time.Since(alert.StartTime))))
textBuilder.WriteString(fmt.Sprintf(" Message: %s\n", alert.Message))
}
diff --git a/internal/notifications/email_template_test.go b/internal/notifications/email_template_test.go
index 116b8b9441..17df28dc44 100644
--- a/internal/notifications/email_template_test.go
+++ b/internal/notifications/email_template_test.go
@@ -737,3 +737,78 @@ func TestEmailTemplateEscapesHTMLContent(t *testing.T) {
t.Fatalf("plain text body should preserve original message, got %q", textBody)
}
}
+
+func TestEmailTemplatePatrolFindingShowsFindingInsteadOfMetrics(t *testing.T) {
+ t.Parallel()
+
+ alert := &alerts.Alert{
+ ID: "patrol-finding-apt",
+ Level: "warning",
+ Type: "patrol_finding",
+ ResourceName: "docker-host-gcp-01",
+ ResourceID: "agent:docker-host-gcp-01",
+ Message: "1 operating system update(s) are ready on docker-host-gcp-01",
+ Value: 0,
+ Threshold: 0,
+ StartTime: time.Now().Add(-5 * time.Minute),
+ Metadata: map[string]interface{}{"category": "reliability"},
+ }
+
+ t.Run("grouped", func(t *testing.T) {
+ _, htmlBody, textBody := EmailTemplate([]*alerts.Alert{alert}, false)
+
+ if !strings.Contains(htmlBody, alert.Message) {
+ t.Fatalf("grouped HTML omitted finding title: %q", htmlBody)
+ }
+ if strings.Contains(htmlBody, "patrol_finding on") {
+ t.Fatalf("grouped HTML used generic alert fallback: %q", htmlBody)
+ }
+ if strings.Contains(htmlBody, ">0.0<") || strings.Contains(htmlBody, ">of 0<") {
+ t.Fatalf("grouped HTML rendered meaningless Patrol metrics: %q", htmlBody)
+ }
+ if !strings.Contains(htmlBody, ">Finding<") || !strings.Contains(htmlBody, ">Reliability<") {
+ t.Fatalf("grouped HTML omitted finding labels: %q", htmlBody)
+ }
+ if !strings.Contains(textBody, alert.Message) || strings.Contains(textBody, "Value: 0.0") {
+ t.Fatalf("grouped text did not render Patrol finding cleanly: %q", textBody)
+ }
+ })
+
+ t.Run("single", func(t *testing.T) {
+ subject, htmlBody, textBody := EmailTemplate([]*alerts.Alert{alert}, true)
+
+ if !strings.Contains(subject, "Patrol Finding") {
+ t.Fatalf("single subject = %q, want Patrol Finding", subject)
+ }
+ if !strings.Contains(htmlBody, alert.Message) {
+ t.Fatalf("single HTML omitted finding title: %q", htmlBody)
+ }
+ if strings.Contains(htmlBody, "Current Value") || strings.Contains(htmlBody, "Threshold") {
+ t.Fatalf("single HTML rendered meaningless Patrol metrics: %q", htmlBody)
+ }
+ if strings.Contains(textBody, "Current Value:") || strings.Contains(textBody, "Threshold:") {
+ t.Fatalf("single text rendered meaningless Patrol metrics: %q", textBody)
+ }
+ })
+}
+
+func TestEmailTemplateGroupedPatrolFindingEscapesFindingContent(t *testing.T) {
+ t.Parallel()
+
+ alert := &alerts.Alert{
+ Level: "warning",
+ Type: "patrol_finding",
+ ResourceName: `<img src=x onerror="alert(1)">`,
+ Message: `Update <script>alert("xss")</script>`,
+ StartTime: time.Now(),
+ Metadata: map[string]interface{}{"category": `<script>alert("category")</script>`},
+ }
+
+ _, htmlBody, _ := EmailTemplate([]*alerts.Alert{alert}, false)
+ if strings.Contains(htmlBody, "<script>") || strings.Contains(htmlBody, `<img src=x onerror="alert(1)">`) {
+ t.Fatalf("grouped Patrol HTML did not escape finding content: %q", htmlBody)
+ }
+ if !strings.Contains(htmlBody, `Update &lt;script&gt;alert(&#34;xss&#34;)&lt;/script&gt;`) {
+ t.Fatalf("grouped Patrol HTML omitted escaped finding title: %q", htmlBody)
+ }
+}