-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
143 lines (124 loc) · 3.53 KB
/
main.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
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"github.com/sensu/sensu-go/types"
"github.com/spf13/cobra"
"io/ioutil"
"log"
"net/smtp"
"os"
"text/template"
)
var (
smtpHost string
smtpUsername string
smtpPassword string
smtpPort uint16
toEmail string
fromEmail string
subject string
eventJsonFile string
stdin *os.File
emailSubjectTemplate = "Sensu Alert for entity {{.Entity.System.Hostname}} - {{.Check.State}}"
emailBodyTemplate = "{{.Check.Output}}"
)
func main() {
cmd := &cobra.Command{
Use: "sensu-email-handler",
Short: "The Sensu Go Email handler for sending an email notification",
RunE: run,
}
cmd.Flags().StringVarP(&smtpHost, "smtpHost", "s", "", "The SMTP host to use to send to send email")
cmd.Flags().StringVarP(&smtpUsername, "smtpUsername", "u", "", "The SMTP username")
cmd.Flags().StringVarP(&smtpPassword, "smtpPassword", "p", "", "The SMTP password")
cmd.Flags().Uint16VarP(&smtpPort, "smtpPort", "P", 587, "The SMTP server port")
cmd.Flags().StringVarP(&toEmail, "toEmail", "t", "", "The 'to' email address")
cmd.Flags().StringVarP(&fromEmail, "fromEmail", "f", "", "The 'from' email address")
cmd.Flags().StringVarP(&eventJsonFile, "event", "e", "", "The JSON event file to process")
cmd.Execute()
}
func run(cmd *cobra.Command, args []string) error {
validationError := checkArgs()
if validationError != nil {
return validationError
}
log.Println("Executing with arguments:", args)
if stdin == nil {
stdin = os.Stdin
}
event := &types.Event{}
var eventJsonBytes []byte
var err error
if len(eventJsonFile) == 0 {
eventJsonBytes, err = ioutil.ReadAll(stdin)
log.Println("Event JSON:", eventJsonBytes)
} else {
//absoluteFilePath, _ := filepath.Abs(eventJsonFile)
eventJsonBytes, err = ioutil.ReadFile(eventJsonFile)
}
if err != nil {
return fmt.Errorf("Unexpected error: %s", err)
}
err = json.Unmarshal(eventJsonBytes, event)
if err != nil {
return fmt.Errorf("failed to unmarshal stdin data: %s", eventJsonBytes)
}
log.Println("Event", event)
sendMailError := sendEmail(event)
if sendMailError != nil {
return fmt.Errorf("failed to send email: %s", sendMailError)
}
return nil
}
func checkArgs() error {
if len(smtpHost) == 0 {
return errors.New("missing smtp host")
}
if len(toEmail) == 0 {
return errors.New("missing destination email address")
}
if len(smtpUsername) == 0 {
return errors.New("smtp username is empty")
}
if len(smtpPassword) == 0 {
return errors.New("smtp password is empty")
}
if len(smtpPassword) == 0 {
return errors.New("smtp password is empty")
}
if len(fromEmail) == 0 {
return errors.New("from email is empty")
}
return nil
}
func sendEmail(event *types.Event) error {
smtpAddress := fmt.Sprintf("%s:%d", smtpHost, smtpPort)
subject, subjectErr := resolveTemplate(emailSubjectTemplate, event)
if subjectErr != nil {
return subjectErr
}
body, bodyErr := resolveTemplate(emailBodyTemplate, event)
if bodyErr != nil {
return bodyErr
}
msg := []byte("To: " + toEmail + "\r\n" +
"Subject: " + subject + "\r\n" +
"\r\n" +
body + "\r\n")
return smtp.SendMail(smtpAddress, smtp.PlainAuth("", smtpUsername, smtpPassword, smtpHost), fromEmail, []string{toEmail}, msg)
}
func resolveTemplate(templateValue string, event *types.Event) (string, error) {
var resolved bytes.Buffer
tmpl, err := template.New("test").Parse(templateValue)
if err != nil {
panic(err)
}
err = tmpl.Execute(&resolved, *event)
if err != nil {
panic(err)
}
return resolved.String(), nil
}