forked from AcalephStorage/consul-alerts
/
email-notifier.go
265 lines (223 loc) · 6.92 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package notifier
import (
"bytes"
"fmt"
"html/template"
"net/smtp"
log "github.com/AcalephStorage/consul-alerts/Godeps/_workspace/src/github.com/Sirupsen/logrus"
"strings"
)
var sendMail = smtp.SendMail
type EmailNotifier struct {
ClusterName string
Template string
Url string
Port int
Username string
Password string
SenderAlias string
SenderEmail string
Receivers []string
NotifName string
OnePerAlert bool
OnePerNode bool
}
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
}
// NotifierName provides name for notifier selection
func (emailNotifier *EmailNotifier) NotifierName() string {
return emailNotifier.NotifName
}
//Notify sends messages to the endpoint notifier
func (emailNotifier *EmailNotifier) Notify(alerts Messages) bool {
overAllStatus, pass, warn, fail := alerts.Summary()
nodeMap := mapByNodes(alerts)
var emailDataList []EmailData
if emailNotifier.OnePerAlert {
log.Println("Going to send one email per alert")
emailDataList = []EmailData{}
for _, check := range alerts {
singleAlertChecks := make(Messages, 0)
singleAlertChecks = append(singleAlertChecks, check)
singleAlertMap := mapByNodes(singleAlertChecks)
alertStatus, alertPassing, alertWarnings, alertFailures := singleAlertChecks.Summary()
alertClusterName := emailNotifier.ClusterName + " " + check.Node + " - " + check.CheckId
e := EmailData{
ClusterName: alertClusterName,
SystemStatus: alertStatus,
FailCount: alertFailures,
WarnCount: alertWarnings,
PassCount: alertPassing,
Nodes: singleAlertMap,
}
emailDataList = append(emailDataList, e)
}
} else if emailNotifier.OnePerNode {
log.Println("Going to send one email per node")
emailDataList = []EmailData{}
for nodeName, checks := range nodeMap {
singleNodeMap := mapByNodes(checks)
nodeStatus, nodePassing, nodeWarnings, nodeFailures := checks.Summary()
nodeClusterName := emailNotifier.ClusterName + " " + nodeName
e := EmailData{
ClusterName: nodeClusterName,
SystemStatus: nodeStatus,
FailCount: nodeFailures,
WarnCount: nodeWarnings,
PassCount: nodePassing,
Nodes: singleNodeMap,
}
emailDataList = append(emailDataList, e)
}
} else {
log.Println("Going to send one email for many alerts")
e := EmailData{
ClusterName: emailNotifier.ClusterName,
SystemStatus: overAllStatus,
FailCount: fail,
WarnCount: warn,
PassCount: pass,
Nodes: nodeMap,
}
emailDataList = []EmailData{e}
}
success := true
for _, e := range emailDataList {
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)
success = false
continue
}
var body bytes.Buffer
if err := tmpl.Execute(&body, e); err != nil {
log.Println("Template error, unable to send email notification: ", err)
success = false
continue
}
msg := fmt.Sprintf(`From: "%s" <%s>
To: %s
Subject: %s is %s
MIME-version: 1.0;
Content-Type: text/html; charset="UTF-8";
%s
`,
emailNotifier.SenderAlias,
emailNotifier.SenderEmail,
strings.Join(emailNotifier.Receivers, ", "),
emailNotifier.ClusterName,
e.SystemStatus,
body.String())
addr := fmt.Sprintf("%s:%d", emailNotifier.Url, emailNotifier.Port)
auth := smtp.PlainAuth("", emailNotifier.Username, emailNotifier.Password, emailNotifier.Url)
if err := sendMail(addr, auth, emailNotifier.SenderEmail, emailNotifier.Receivers, []byte(msg)); err != nil {
log.Println("Unable to send notification:", err)
continue
}
log.Println("Email notification sent.")
success = success && true
}
return success
}
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>
`