forked from linkedin/Burrow
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathemailer.go
132 lines (116 loc) · 3.46 KB
/
emailer.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
/* Copyright 2015 LinkedIn Corp. Licensed under the Apache License, Version
* 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*/
package main
import (
"bytes"
"fmt"
log "github.com/cihub/seelog"
"net/smtp"
"os"
"strings"
"text/template"
"time"
)
type Emailer struct {
app *ApplicationContext
template *template.Template
Tickers map[string]*time.Ticker
quitSends chan struct{}
auth smtp.Auth
}
func NewEmailer(app *ApplicationContext) (*Emailer, error) {
template, err := template.ParseFiles(app.Config.Smtp.Template)
if err != nil {
log.Critical("Cannot parse email template: %v", err)
os.Exit(1)
}
var auth smtp.Auth
switch app.Config.Smtp.AuthType {
case "plain":
auth = smtp.PlainAuth("", app.Config.Smtp.Username, app.Config.Smtp.Password, app.Config.Smtp.Server)
case "crammd5":
auth = smtp.CRAMMD5Auth(app.Config.Smtp.Username, app.Config.Smtp.Password)
}
return &Emailer{
app: app,
template: template,
Tickers: make(map[string]*time.Ticker),
quitSends: make(chan struct{}),
auth: auth,
}, nil
}
func (emailer *Emailer) Start() {
for email, cfg := range emailer.app.Config.Email {
emailer.Tickers[email] = time.NewTicker(time.Duration(cfg.Interval) * time.Second)
go emailer.sendEmailNotifications(email, cfg.Threshold, cfg.Groups, emailer.Tickers[email].C)
}
}
func (emailer *Emailer) Stop() {
close(emailer.quitSends)
}
func (emailer *Emailer) sendEmail(to string, results []*ConsumerGroupStatus) {
var bytesToSend bytes.Buffer
err := emailer.template.Execute(&bytesToSend, struct {
From string
To string
Results []*ConsumerGroupStatus
}{
From: emailer.app.Config.Smtp.From,
To: to,
Results: results,
})
if err != nil {
log.Error("Failed to assemble email:", err)
}
err = smtp.SendMail(fmt.Sprintf("%s:%v", emailer.app.Config.Smtp.Server, emailer.app.Config.Smtp.Port),
emailer.auth, emailer.app.Config.Smtp.From, []string{to}, bytesToSend.Bytes())
if err != nil {
log.Error("Failed to send email message:", err)
}
}
func (emailer *Emailer) sendEmailNotifications(email string, threshold string, groups []string, ticker <-chan time.Time) {
// Convert the config threshold string into a value
thresholdVal := StatusWarning
if threshold == "ERROR" {
thresholdVal = StatusError
}
OUTERLOOP:
for {
select {
case <-emailer.quitSends:
for _, ticker := range emailer.Tickers {
ticker.Stop()
}
break OUTERLOOP
case <-ticker:
results := make([]*ConsumerGroupStatus, 0, len(groups))
resultChannel := make(chan *ConsumerGroupStatus)
for _, group := range groups {
groupParts := strings.Split(group, ",")
storageRequest := &RequestConsumerStatus{Result: resultChannel, Cluster: groupParts[0], Group: groupParts[1]}
emailer.app.Storage.requestChannel <- storageRequest
}
for {
result := <-resultChannel
results = append(results, result)
if len(results) == len(groups) {
break
}
}
// Send an email if any of the results breaches the threshold
for _, result := range results {
if result.Status >= thresholdVal {
emailer.sendEmail(email, results)
break
}
}
}
}
}