diff --git a/server/internal/notify/smtp.go b/server/internal/notify/smtp.go index 98bfa41..e3f2135 100644 --- a/server/internal/notify/smtp.go +++ b/server/internal/notify/smtp.go @@ -27,7 +27,7 @@ func dispatchSMTP(ch models.NotificationChannel, ev Event) error { } return sender.SendMonitorAlert(to, mail.MonitorEvent{ - MonitorName: ev.MonitorName, + MonitorName: smtpAlertName(ev), Type: ev.Type, OldStatus: ev.OldStatus, NewStatus: ev.NewStatus, @@ -36,3 +36,13 @@ func dispatchSMTP(ch models.NotificationChannel, ev Event) error { Down: ev.NewStatus == models.StatusDown, }) } + +// smtpAlertName is the name shown in the alert email. mail.MonitorEvent (in +// vantage-shared) has no ServerName field, so the server is folded into the +// name the same way title() folds it into the notification title. +func smtpAlertName(ev Event) string { + if ev.ServerName == "" { + return ev.MonitorName + } + return fmt.Sprintf("%s on %s", ev.MonitorName, ev.ServerName) +} diff --git a/server/internal/notify/smtp_test.go b/server/internal/notify/smtp_test.go new file mode 100644 index 0000000..dd44c74 --- /dev/null +++ b/server/internal/notify/smtp_test.go @@ -0,0 +1,17 @@ +package notify + +import "testing" + +func TestSMTPAlertNameIncludesServerName(t *testing.T) { + ev := Event{MonitorName: "Disk full", ServerName: "web-01"} + if got, want := smtpAlertName(ev), "Disk full on web-01"; got != want { + t.Fatalf("smtpAlertName = %q, want %q", got, want) + } +} + +func TestSMTPAlertNameWithoutServerNameUnchanged(t *testing.T) { + ev := Event{MonitorName: "Site"} + if got, want := smtpAlertName(ev), "Site"; got != want { + t.Fatalf("smtpAlertName = %q, want %q", got, want) + } +}