-
Notifications
You must be signed in to change notification settings - Fork 4
/
notification.go
69 lines (56 loc) · 1.99 KB
/
notification.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
package main
import "fmt"
import "log"
import "time"
import "gopkg.in/gomail.v2"
var message_templates map[string]MessageTemplate
type MessageTemplate struct {
Subject string
Body string
}
func NotifyAlertNew(alert Alert) {
subject := fmt.Sprintf(message_templates["new_alert"].Subject, alert.StateName)
body := fmt.Sprintf(message_templates["new_alert"].Body, alert.StateName, alert.ID)
go SendEmail(subject, body)
}
func NotifyAlertClosed(alert Alert) {
subject := fmt.Sprintf(message_templates["closed_alert"].Subject, alert.StateName)
duration := time.Duration(alert.Duration) * time.Second
body := fmt.Sprintf(message_templates["closed_alert"].Body, alert.StateName, duration.String(), alert.ID)
go SendEmail(subject, body)
}
func NotifyAlertOngoing(alert Alert) {
subject := fmt.Sprintf(message_templates["ongoing_alert"].Subject, alert.StateName)
body := fmt.Sprintf(message_templates["ongoing_alert"].Body, alert.StateName, alert.ID)
go SendEmail(subject, body)
}
func SendEmail(subject, body string) {
m := gomail.NewMessage()
m.SetHeader("From", Configs.EmailSender)
m.SetHeader("Subject", subject)
m.SetBody("text/plain", body)
for _, address := range Configs.EmailRecipients {
m.SetHeader("To", address)
}
d := gomail.Dialer{Host: Configs.EmailServer, Port: Configs.EmailServerPort}
if err := d.DialAndSend(m); err != nil {
log.Printf("Failed emailing: %s", err)
} else {
log.Printf("Emailed '%s' notification", subject)
}
}
func init() {
message_templates = map[string]MessageTemplate{}
message_templates["new_alert"] = MessageTemplate{
Subject: "New Alert \"%s\"!",
Body: "Hi,\n\nAlert \"%s\" has just started firing.\n\n%s\n\nRegards",
}
message_templates["ongoing_alert"] = MessageTemplate{
Subject: "Alert \"%s\" still firing",
Body: "Hi,\n\nAlert \"%s\" is still firing.\n\n%s\n\nRegards",
}
message_templates["closed_alert"] = MessageTemplate{
Subject: "Alert \"%s\" closed",
Body: "Hi,\n\nAlert \"%s\" closed after %s.\n\n%s\n\nRegards",
}
}