From e77bda4e9fc14d2088f7d9941705c55d29b79739 Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Fri, 16 Oct 2015 17:45:15 +0200 Subject: [PATCH] Fix inverted cleanup trigger, proper mail notifications --- config/notifies.go | 2 -- dispatch.go | 4 ++-- notify/impl.go | 30 ++++++++++++++++++------------ template/default.tmpl | 36 ++++++++++++++++++++++++++++-------- 4 files changed, 48 insertions(+), 24 deletions(-) diff --git a/config/notifies.go b/config/notifies.go index 209a5b42..b63f315a 100644 --- a/config/notifies.go +++ b/config/notifies.go @@ -41,7 +41,6 @@ var ( Templates: EmailTemplates{ Header: "email.default.header", HTML: "email.default.html", - Plain: "email.default.plain", }, } ) @@ -84,7 +83,6 @@ type EmailConfig struct { type EmailTemplates struct { Header string `yaml:"header"` HTML string `yaml:"html"` - Plain string `yaml:"plain"` } // UnmarshalYAML implements the yaml.Unmarshaler interface. diff --git a/dispatch.go b/dispatch.go index 37dd8839..ddb0de22 100644 --- a/dispatch.go +++ b/dispatch.go @@ -56,7 +56,7 @@ func (d *Dispatcher) Run() { } func (d *Dispatcher) run(it provider.AlertIterator) { - cleanup := time.NewTicker(5 * time.Minute) + cleanup := time.NewTicker(30 * time.Second) defer cleanup.Stop() defer it.Close() @@ -140,7 +140,7 @@ func (d *Dispatcher) processAlert(alert *types.Alert, opts *RouteOpts) { if err != nil { log.Errorf("Notify for %d alerts failed: %s %T", len(alerts), err, err) } - return err != nil + return err == nil }) } diff --git a/notify/impl.go b/notify/impl.go index fdf92998..fc1e5520 100644 --- a/notify/impl.go +++ b/notify/impl.go @@ -175,8 +175,6 @@ func (n *Email) Notify(ctx context.Context, as ...*types.Alert) error { return err } - log.Debugln("sending mail", n.conf.Email, c) - // Send the email body. wc, err := c.Data() if err != nil { @@ -184,18 +182,26 @@ func (n *Email) Notify(ctx context.Context, as ...*types.Alert) error { } defer wc.Close() - data := struct { - Alerts model.Alerts - From string - To string - Date string - }{ - Alerts: types.Alerts(as...), - From: n.conf.Sender, - To: n.conf.Email, - Date: time.Now().Format(time.RFC1123Z), + groupLabels, ok := GroupLabels(ctx) + if !ok { + log.Error("missing group labels") } + data := struct { + Alerts model.Alerts + GroupLabels model.LabelSet + From string + To string + Date string + }{ + Alerts: types.Alerts(as...), + GroupLabels: groupLabels, + From: n.conf.Sender, + To: n.conf.Email, + Date: time.Now().Format(time.RFC1123Z), + } + + // Expand the mail header first without HTML escaping if err := n.tmpl.ExecuteText(wc, n.conf.Templates.Header, &data); err != nil { return err } diff --git a/template/default.tmpl b/template/default.tmpl index 00246680..33ffa2d0 100644 --- a/template/default.tmpl +++ b/template/default.tmpl @@ -23,17 +23,37 @@ my text {{ define "email.default.header" }}From: "Prometheus Alertmanager" <{{ .From }}> To: {{ .To }} Date: {{ .Date }} -Subject: [{{ .Alerts.Status }}] -{{ end }} - -{{ define "email.default.plain" }} - {{ range .Alerts }} -{{ .String }} - {{ end }} +Subject: [{{ .Alerts.Status }}] {{ .GroupLabels }} +Content-Type: text/html; charset=UTF-8 {{ end }} {{ define "email.default.html" }} +

{{ len .Alerts }} alert(s) for {{ .GroupLabels }}

+ {{ range .Alerts }} -{{ .String }} +
+ {{ .Status }}
+
+ Labels
+
    +{{range $label, $value := .Labels}} +
  • {{$label}}: {{$value}}
  • +{{end}} +
+
+
+ Annotations
+
    +{{ range $label, $value := .Annotations }} +
  • {{ $label }}: {{ $value }}
  • +{{end}} +
{{ end }} +
+
+
{{ end }} \ No newline at end of file