Added subject and message email template options

This commit is contained in:
Christopher Ramey 2021-01-23 08:27:03 -09:00
parent 8c7822be97
commit 3fa0c393fe
3 changed files with 157 additions and 53 deletions

View File

@ -5,6 +5,8 @@ import (
"net/smtp" "net/smtp"
"os" "os"
"strings" "strings"
"text/template"
"time"
) )
const ( const (
@ -12,77 +14,73 @@ const (
TK_TO TK_TO
TK_SMTP TK_SMTP
TK_FROM TK_FROM
TK_USER
TK_PASS
TK_SUBJECT
TK_MESSAGE
) )
type AlarmEmail struct { type AlarmEmail struct {
Type string Type string
Name string Name string
To []string
From string From string
SMTP string SMTP string
To []string User string
Pass string
Subject string
subTemplate *template.Template
Message string
msgTemplate *template.Template
state int state int
} }
type EmailDetail struct {
Timestamp string
Group string
Host string
Check string
Error error
}
func NewAlarmEmail(name string) *AlarmEmail { func NewAlarmEmail(name string) *AlarmEmail {
host, _ := os.Hostname() host, _ := os.Hostname()
if host == "" { if host == "" {
host = "localhost" host = "localhost"
} }
return &AlarmEmail{ al := &AlarmEmail{
Type: "email", Name: name, Type: "email",
From: "alrm@" + host, SMTP: "localhost:25", Name: name,
From: "alrm@" + host,
SMTP: "localhost:25",
Subject: "{{.Host}} failure",
Message: "Check {{.Check}} failed at {{.Timestamp}}: {{.Error}}",
} }
al.updateSubject()
al.updateMessage()
return al
} }
func (a *AlarmEmail) Alarm(grp string, host string, chk string, cerr error) error { func (a *AlarmEmail) updateSubject() error {
c, err := smtp.Dial(a.SMTP) t := template.New("email subject")
_, err := t.Parse(a.Subject)
if err != nil { if err != nil {
fmt.Print(err)
return err return err
} }
a.subTemplate = t
return nil
}
helo := "localhost" func (a *AlarmEmail) updateMessage() error {
tspl := strings.Split(a.From, "@") t := template.New("email message")
if len(tspl) > 1 { _, err := t.Parse(a.Message)
helo = tspl[1]
}
err = c.Hello(helo)
if err != nil {
return err
}
err = c.Mail(a.From)
if err != nil {
return err
}
for _, to := range a.To {
err = c.Rcpt(to)
if err != nil {
return err
}
}
m, err := c.Data()
if err != nil {
return err
}
msg := fmt.Sprintf("From: %s\r\n", a.From)
msg += fmt.Sprintf("To: %s\r\n", strings.Join(a.To, ";"))
msg += fmt.Sprintf("Subject: %s\r\n\r\n", "test subject")
msg += fmt.Sprintf("%s", cerr.Error())
_, err = fmt.Fprintf(m, "%s", msg)
if err != nil {
return err
}
err = m.Close()
if err != nil {
return err
}
err = c.Quit()
if err != nil { if err != nil {
fmt.Print(err)
return err return err
} }
a.msgTemplate = t
return nil return nil
} }
@ -96,6 +94,14 @@ func (a *AlarmEmail) Parse(tk string) (bool, error) {
a.state = TK_FROM a.state = TK_FROM
case "smtp": case "smtp":
a.state = TK_SMTP a.state = TK_SMTP
case "user":
a.state = TK_USER
case "pass":
a.state = TK_PASS
case "subject":
a.state = TK_SUBJECT
case "message":
a.state = TK_MESSAGE
default: default:
if len(a.To) < 1 { if len(a.To) < 1 {
return false, fmt.Errorf("email alarm requires to address") return false, fmt.Errorf("email alarm requires to address")
@ -128,8 +134,108 @@ func (a *AlarmEmail) Parse(tk string) (bool, error) {
a.To = append(a.To, tk) a.To = append(a.To, tk)
a.state = TK_NONE a.state = TK_NONE
case TK_USER:
if strings.TrimSpace(tk) == "" {
return false, fmt.Errorf("user cannot be empty")
}
a.User = tk
a.state = TK_NONE
case TK_PASS:
if strings.TrimSpace(tk) == "" {
return false, fmt.Errorf("pass cannot be empty")
}
a.Pass = tk
a.state = TK_NONE
case TK_SUBJECT:
if strings.TrimSpace(tk) == "" {
return false, fmt.Errorf("subject cannot be empty")
}
a.Subject = tk
err := a.updateSubject()
if err != nil {
return false, err
}
a.state = TK_NONE
case TK_MESSAGE:
if strings.TrimSpace(tk) == "" {
return false, fmt.Errorf("message cannot be empty")
}
a.Message = tk
err := a.updateMessage()
if err != nil {
return false, err
}
a.state = TK_NONE
default: default:
return false, fmt.Errorf("invalid state in alarm_email") return false, fmt.Errorf("invalid state in alarm_email")
} }
return true, nil return true, nil
} }
func (a *AlarmEmail) Alarm(grp string, hst string, chk string, alerr error) error {
dt := EmailDetail{
Timestamp: time.Now().Format(time.RFC1123),
Group: grp,
Host: hst,
Check: chk,
Error: alerr,
}
c, err := smtp.Dial(a.SMTP)
if err != nil {
return err
}
helo := "localhost"
tspl := strings.Split(a.From, "@")
if len(tspl) > 1 {
helo = tspl[1]
}
err = c.Hello(helo)
if err != nil {
return err
}
err = c.Mail(a.From)
if err != nil {
return err
}
for _, to := range a.To {
err = c.Rcpt(to)
if err != nil {
return err
}
}
m, err := c.Data()
if err != nil {
return err
}
fmt.Fprintf(m, "From: %s\r\n", a.From)
fmt.Fprintf(m, "To: %s\r\n", strings.Join(a.To, ";"))
fmt.Fprintf(m, "Subject: ")
err = a.subTemplate.Execute(m, dt)
if err != nil {
return err
}
fmt.Fprintf(m, "\r\n\r\n")
err = a.msgTemplate.Execute(m, dt)
if err != nil {
return err
}
err = m.Close()
if err != nil {
return err
}
err = c.Quit()
if err != nil {
return err
}
return nil
}

View File

@ -16,7 +16,6 @@ func main() {
flag.Usage = printUsage flag.Usage = printUsage
flag.Parse() flag.Parse()
if *cfgPath == "" { if *cfgPath == "" {
searchpaths := []string{"/etc/alrmrc", "./alrmrc"} searchpaths := []string{"/etc/alrmrc", "./alrmrc"}
for _, sp := range searchpaths { for _, sp := range searchpaths {
@ -44,7 +43,6 @@ func main() {
os.Exit(1) os.Exit(1)
} }
if *debuglvl > 0 { if *debuglvl > 0 {
o, err := json.MarshalIndent(cfg, "", " ") o, err := json.MarshalIndent(cfg, "", " ")
if err != nil { if err != nil {