Browse Source

Handle case where mail config isn't provided

Brendan Abolivier 7 years ago
parent
commit
298ad3e64c
Signed by: Brendan Abolivier <contact@brendanabolivier.com> GPG key ID: 8EF1500759F70623
1 changed files with 16 additions and 5 deletions
  1. 16
    5
      src/metrics-alerting/main.go

+ 16
- 5
src/metrics-alerting/main.go View File

@@ -27,16 +27,27 @@ func main() {
27 27
 		ExecEndpoint: cfg.Warp10Exec,
28 28
 		ReadToken:    cfg.ReadToken,
29 29
 	}
30
-	dialer := gomail.NewDialer(
31
-		cfg.Mail.SMTP.Host, cfg.Mail.SMTP.Port, cfg.Mail.SMTP.Username,
32
-		cfg.Mail.SMTP.Password,
33
-	)
34 30
 	alerter := alert.Alerter{
35
-		Dialer: dialer,
31
+		Dialer: nil,
36 32
 		Sender: cfg.Mail.Sender,
37 33
 	}
38 34
 
39 35
 	for _, script := range cfg.Scripts {
36
+		if script.Action == "email" && alerter.Dialer == nil {
37
+			if cfg.Mail == nil {
38
+				logrus.Errorf(
39
+					"no configuration set for sending emails, ignoring script %s",
40
+					script.Key,
41
+				)
42
+				continue
43
+			}
44
+
45
+			alerter.Dialer = gomail.NewDialer(
46
+				cfg.Mail.SMTP.Host, cfg.Mail.SMTP.Port, cfg.Mail.SMTP.Username,
47
+				cfg.Mail.SMTP.Password,
48
+			)
49
+		}
50
+
40 51
 		if err := process.Process(client, script, alerter); err != nil {
41 52
 			logrus.Error(err)
42 53
 		}