-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail_command_notifier.go
80 lines (73 loc) · 1.93 KB
/
mail_command_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
package happening
import (
"fmt"
"io"
"log"
"os"
"os/exec"
)
type MailCommandNotifier struct {
EnvironmentVariable string
DrilldownURL string
MailCommand string
ContactName string
ContactEmail string
}
func NewMailCommandNotifier(config ServerConfig) Notifier {
return &MailCommandNotifier{
EnvironmentVariable: config.NOTIFIER_ENVIRONMENT_VARIABLE,
DrilldownURL: config.HAPPENING_SERVER_URL,
MailCommand: config.NOTIFIER_MAIL_COMMAND,
ContactName: config.NOTIFIER_CONTACT_NAME,
ContactEmail: config.NOTIFIER_CONTACT_EMAIL,
}
}
func (notifier *MailCommandNotifier) sendMail(notifierMail NotifierMail) {
to := fmt.Sprintf("%s <%s>", notifier.ContactName, notifier.ContactEmail)
path, err := exec.LookPath(notifier.MailCommand)
if err != nil {
log.Printf("error: %v", err)
}
cmd := exec.Command(path, "-s", notifierMail.Subject(), to)
cmd.Env = append(os.Environ(), fmt.Sprintf(`REPLYTO="%s"`, to))
stdin, err := cmd.StdinPipe()
if err != nil {
log.Panic(err)
}
go func() {
defer stdin.Close()
io.WriteString(stdin, notifierMail.Text())
}()
if err = cmd.Run(); err != nil {
log.Panic(err)
}
}
func (notifier *MailCommandNotifier) Alert(check Check) {
go notifier.sendMail(
CheckNotifierMail{
Check: check,
EnvironmentVariable: notifier.EnvironmentVariable,
DrilldownURL: notifier.DrilldownURL,
},
)
}
func (notifier *MailCommandNotifier) Resolve(check Check) {
go notifier.sendMail(
CheckNotifierMail{
Check: check,
EnvironmentVariable: notifier.EnvironmentVariable,
DrilldownURL: notifier.DrilldownURL,
Resolved: true,
},
)
}
func (notifier *MailCommandNotifier) Mail(event Event) {
go notifier.sendMail(
EventNotifierMail{
Event: event,
EnvironmentVariable: notifier.EnvironmentVariable,
DrilldownURL: notifier.DrilldownURL,
},
)
return
}