From 5a1771be63fd671edc737ce858c1308561f9ff96 Mon Sep 17 00:00:00 2001 From: Julius Volz Date: Mon, 16 Feb 2015 20:35:45 +0100 Subject: [PATCH] Add From/To (non-envelope) headers to email notifications. --- manager/notifier.go | 33 ++++++++++++++++++++------------- manager/notifier_test.go | 6 ++++-- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/manager/notifier.go b/manager/notifier.go index 1b79ab35..0023cdc3 100644 --- a/manager/notifier.go +++ b/manager/notifier.go @@ -32,16 +32,18 @@ import ( const contentTypeJson = "application/json" -var bodyTmpl = template.Must(template.New("message").Parse(`Subject: [ALERT] {{.Labels.alertname}}: {{.Summary}} +var bodyTmpl = template.Must(template.New("message").Parse(`From: Prometheus Alertmanager <{{.From}}> +To: {{.To}} +Subject: [ALERT] {{.Alert.Labels.alertname}}: {{.Alert.Summary}} -{{.Description}} +{{.Alert.Description}} Grouping labels: -{{range $label, $value := .Labels}} +{{range $label, $value := .Alert.Labels}} {{$label}} = "{{$value}}"{{end}} Payload labels: -{{range $label, $value := .Payload}} +{{range $label, $value := .Alert.Payload}} {{$label}} = "{{$value}}"{{end}}`)) var ( @@ -157,18 +159,23 @@ func (n *notifier) sendPagerDutyNotification(serviceKey string, a *Alert) error return nil } -func writeEmailBody(w io.Writer, a *Alert) error { - if err := bodyTmpl.Execute(w, a); err != nil { - return err - } - buf := &bytes.Buffer{} - if err := bodyTmpl.Execute(buf, a); err != nil { +func writeEmailBody(w io.Writer, from string, to string, a *Alert) error { + err := bodyTmpl.Execute(w, struct { + From string + To string + Alert *Alert + }{ + From: from, + To: to, + Alert: a, + }) + if err != nil { return err } return nil } -func (n *notifier) sendEmailNotification(email string, a *Alert) error { +func (n *notifier) sendEmailNotification(to string, a *Alert) error { // Connect to the SMTP smarthost. c, err := smtp.Dial(*smtpSmartHost) if err != nil { @@ -178,7 +185,7 @@ func (n *notifier) sendEmailNotification(email string, a *Alert) error { // Set the sender and recipient. c.Mail(*smtpSender) - c.Rcpt(email) + c.Rcpt(to) // Send the email body. wc, err := c.Data() @@ -187,7 +194,7 @@ func (n *notifier) sendEmailNotification(email string, a *Alert) error { } defer wc.Close() - return writeEmailBody(wc, a) + return writeEmailBody(wc, *smtpSender, to, a) } func (n *notifier) handleNotification(a *Alert, config *pb.NotificationConfig) { diff --git a/manager/notifier_test.go b/manager/notifier_test.go index a1990f98..532d30f7 100644 --- a/manager/notifier_test.go +++ b/manager/notifier_test.go @@ -33,9 +33,11 @@ func TestWriteEmailBody(t *testing.T) { }, } buf := &bytes.Buffer{} - writeEmailBody(buf, event) + writeEmailBody(buf, "from@prometheus.io", "to@prometheus.io", event) - expected := `Subject: [ALERT] TestAlert: Testsummary + expected := `From: Prometheus Alertmanager +To: to@prometheus.io +Subject: [ALERT] TestAlert: Testsummary Test alert description, something went wrong here.