File size: 2,413 Bytes
1e92f2d |
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 |
package remotemonitor
import (
"errors"
"fmt"
"net"
"net/url"
)
// Config contains all necessary values for remote monitoring.
type Config struct {
// Location is the unique location name of this monitor.
Location string
// PublicURL is the publicly-routable base URL for this monitor.
// It must match what is configured for twilio SMS.
PublicURL string
// ListenAddr is the address and port to bind to.
ListenAddr string
// CheckMinutes denotes the number of minutes between checks (for all instances).
CheckMinutes int
Twilio struct {
AccountSID string
AuthToken string
FromNumber string
MessageSID string
}
SMTP struct {
From string
// ServerAddr is the address of the SMTP server, including port.
ServerAddr string
User, Pass string
// Retries is the number of times to retry sending with an email integration key.
Retries int
}
// Instances determine what remote GoAlert instances will be monitored and send potential errors.
Instances []Instance
}
func (cfg Config) Validate() error {
if cfg.Location == "" {
return errors.New("location is required")
}
if cfg.PublicURL == "" {
return errors.New("public URL is required")
}
_, err := url.Parse(cfg.PublicURL)
if err != nil {
return fmt.Errorf("parse public URL: %v", err)
}
if cfg.ListenAddr == "" {
return errors.New("listen address is required")
}
if cfg.CheckMinutes < 1 {
return errors.New("check minutes is required")
}
if cfg.Twilio.AccountSID == "" {
return errors.New("twilio account SID is required")
}
if cfg.Twilio.AuthToken == "" {
return errors.New("twilio auth token is required")
}
if cfg.Twilio.FromNumber == "" {
return errors.New("twilio from number is required")
}
var hasEmail bool
for idx, i := range cfg.Instances {
if err := i.Validate(); err != nil {
return fmt.Errorf("instance[%d] %q: %v", idx, i.Location, err)
}
if i.EmailAPIKey != "" {
hasEmail = true
}
}
if !hasEmail {
return nil
}
if cfg.SMTP.ServerAddr == "" {
return errors.New("SMTP server address is required")
}
if _, _, err := net.SplitHostPort(cfg.SMTP.ServerAddr); err != nil {
return fmt.Errorf("parse SMTP server address: %v", err)
}
if cfg.SMTP.From == "" {
return errors.New("SMTP from address is required")
}
if cfg.SMTP.Retries > 5 || cfg.SMTP.Retries < 0 {
return errors.New("SMTP retries must be between 0 and 5")
}
return nil
}
|