forked from AcalephStorage/consul-alerts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
email-notifier.go
186 lines (158 loc) · 5.02 KB
/
email-notifier.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package notifier
import (
"bytes"
"fmt"
"html/template"
"net/smtp"
log "github.com/AcalephStorage/consul-alerts/Godeps/_workspace/src/github.com/Sirupsen/logrus"
)
type EmailNotifier struct {
ClusterName string
Template string
Url string
Port int
Username string
Password string
SenderAlias string
SenderEmail string
Receivers []string
}
type EmailData struct {
ClusterName string
SystemStatus string
FailCount int
WarnCount int
PassCount int
Nodes map[string]Messages
}
func (e EmailData) IsCritical() bool {
return e.SystemStatus == SYSTEM_CRITICAL
}
func (e EmailData) IsWarning() bool {
return e.SystemStatus == SYSTEM_UNSTABLE
}
func (e EmailData) IsPassing() bool {
return e.SystemStatus == SYSTEM_HEALTHY
}
func (emailNotifier *EmailNotifier) Notify(alerts Messages) bool {
overAllStatus, pass, warn, fail := alerts.Summary()
nodeMap := mapByNodes(alerts)
e := EmailData{
ClusterName: emailNotifier.ClusterName,
SystemStatus: overAllStatus,
FailCount: fail,
WarnCount: warn,
PassCount: pass,
Nodes: nodeMap,
}
var tmpl *template.Template
var err error
if emailNotifier.Template == "" {
tmpl, err = template.New("base").Parse(defaultTemplate)
} else {
tmpl, err = template.ParseFiles(emailNotifier.Template)
}
if err != nil {
log.Println("Template error, unable to send email notification: ", err)
return false
}
var body bytes.Buffer
if err := tmpl.Execute(&body, e); err != nil {
log.Println("Template error, unable to send email notification: ", err)
return false
}
msg := ""
msg += fmt.Sprintf("From: \"%s\" <%s>\n", emailNotifier.SenderAlias, emailNotifier.SenderEmail)
msg += fmt.Sprintf("Subject: %s is %s\n", emailNotifier.ClusterName, overAllStatus)
msg += "MIME-version: 1.0;\nContent-Type: text/html; charset=\"UTF-8\";\n\n"
msg += body.String()
addr := fmt.Sprintf("%s:%d", emailNotifier.Url, emailNotifier.Port)
auth := smtp.PlainAuth("", emailNotifier.Username, emailNotifier.Password, emailNotifier.Url)
if err := smtp.SendMail(addr, auth, emailNotifier.SenderEmail, emailNotifier.Receivers, []byte(msg)); err != nil {
log.Println("Unable to send notification:", err)
return false
}
log.Println("Email notification sent.")
return true
}
func mapByNodes(alerts Messages) map[string]Messages {
nodeMap := make(map[string]Messages)
for _, alert := range alerts {
nodeName := alert.Node
nodeChecks := nodeMap[nodeName]
if nodeChecks == nil {
nodeChecks = make(Messages, 0)
}
nodeChecks = append(nodeChecks, alert)
nodeMap[nodeName] = nodeChecks
}
return nodeMap
}
var defaultTemplate string = `
<!DOCTYPE html>
<html lang="en">
<head>
<title>{{ .ClusterName }}</title>
</head>
<body style="width:100% !important; min-width: 100%; -webkit-text-size-adjust:100%; -ms-text-size-adjust:100%; margin:0; padding:0; font-family: 'Helvetica', 'Arial', sans-serif; color: #000000;">
<div style="margin-left: auto; margin-right: auto; width: 36em; padding: 10dp; font-weight: bold; color: #ffffff; background-color: {{ if .IsCritical }}#e13329{{ else if .IsWarning }}#eebb00{{ else if .IsPassing }}#24c75a{{ end }};">
<div style="padding: 10px;">
{{ .ClusterName }}
</div>
</div>
<div style="margin-left: auto; margin-right: auto; width: 36em; margin-top: 10px; margin-bottom: 10px; padding: 10dp">
<p>
<span style="font-weight: bold; font-size: 1.05em;">System is {{ .SystemStatus }}</span>
<br/>
<span style="font-size: 0.9em;">The following nodes are currently experiencing issues:</span>
<div style="font-size: 0.85em;">
<div style="float: left; width: 33%;">
<strong>Failed: </strong>
<span>{{ .FailCount }}</span>
</div>
<div style="float: right; width: 33%;">
<strong>Warning: </strong>
<span>{{ .WarnCount }}</span>
</div>
<div style="display: inline-block; width: 33%;">
<strong>Passed: </strong>
<span>{{ .PassCount }}</span>
</div>
</div>
</p>
</div>
{{ range $name, $checks := .Nodes }}
<div style="margin-left: auto; margin-right: auto; width: 36em; padding-top: 5px; padding-bottom: 20px;">
<div style="font-size: 1.1em;">
<strong>Node: </strong>
<strong>{{ $name }}</strong>
</div>
{{ range $check := $checks }}
<div style="margin-top: 15px; padding: 10px; background-color: {{ if $check.IsCritical }}#e13329{{ else if $check.IsWarning }}#eebb00{{ else if $check.IsPassing }}#24c75a{{ end }};">
<div style="font-weight: bold; font-size: 1.1em;">
{{ with $check.Service }}
{{ $check.Service }}:
{{ end }}
{{ $check.Check }}
</div>
<div style="font-size: 0.85em;">
<strong>Since: </strong>
<span>{{ $check.Timestamp }}</span>
</div>
{{ with $check.Notes }}
<div style="padding-top: 15px;">
<strong>Notes: </strong>
<pre>{{ $check.Notes }}</pre>
</div>
{{end }}
<div style="padding-top: 15px;">
<strong>Output:</strong>
<pre>{{ $check.Output }}</pre>
</div>
</div>
{{ end }}
</div>
{{ end }}
</body>
</html>
`