diff --git a/README.md b/README.md index b96e489..8ece566 100644 --- a/README.md +++ b/README.md @@ -260,6 +260,21 @@ Enable notifications in Slack with this Notifier configuration: Follow these instructions to [create a webhook](https://get.slack.help/hc/en-us/articles/115005265063-Incoming-WebHooks-for-Slack). +#### Mail notifier + +Enable notifications via Email with this Notifier configuration: +```js +{ + "name": "mail", + "emails": "emails list separated by colon", + "username": "smtp service username", + "password": "smtp service password", + "smtpServer": "smtp service url", + "smtpPort": 587 // smtp service port +} +``` + + ## Setting up storage on S3 The easiest way to do this is to give an IAM user these two privileges (keep the credentials secret): diff --git a/checkup.go b/checkup.go index 40d6b39..0762e77 100644 --- a/checkup.go +++ b/checkup.go @@ -229,6 +229,8 @@ func (c Checkup) MarshalJSON() ([]byte, error) { switch c.Notifier.(type) { case Slack: notifierName = "slack" + case MailNotifier: + notifierName = "mail" default: return result, fmt.Errorf("unknown Notifier type") } @@ -362,6 +364,13 @@ func (c *Checkup) UnmarshalJSON(b []byte) error { return err } c.Notifier = notifier + case "mail": + var notifier MailNotifier + err = json.Unmarshal(raw.Notifier, ¬ifier) + if err != nil { + return err + } + c.Notifier = notifier default: return fmt.Errorf("%s: unknown Notifier type", types.Notifier.Name) } diff --git a/cmd/checkup/main.go b/cmd/checkup/main.go index 4491ce3..d6b8014 100644 --- a/cmd/checkup/main.go +++ b/cmd/checkup/main.go @@ -1,6 +1,6 @@ package main -import "github.com/sourcegraph/checkup/cmd" +import "github.com/andersou/checkup/cmd" func main() { cmd.Execute() diff --git a/cmd/root.go b/cmd/root.go index 051d9a1..c1aa3c3 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -7,7 +7,7 @@ import ( "log" "os" - "github.com/sourcegraph/checkup" + "github.com/andersou/checkup" "github.com/spf13/cobra" ) diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..29943da --- /dev/null +++ b/go.mod @@ -0,0 +1,24 @@ +module github.com/andersou/checkup + +go 1.12 + +require ( + github.com/ashwanthkumar/slack-go-webhook v0.0.0-20181208062437-4a19b1a876b7 + github.com/aws/aws-sdk-go v1.23.12 + github.com/fatih/color v1.7.0 + github.com/google/go-github v17.0.0+incompatible + github.com/google/go-querystring v1.0.0 // indirect + github.com/jmoiron/sqlx v1.2.0 + github.com/lib/pq v1.2.0 + github.com/mattn/go-colorable v0.1.2 // indirect + github.com/mattn/go-isatty v0.0.9 // indirect + github.com/mattn/go-sqlite3 v1.11.0 + github.com/miekg/dns v1.1.16 + github.com/moul/http2curl v1.0.0 // indirect + github.com/parnurzeal/gorequest v0.2.15 // indirect + github.com/pkg/errors v0.8.1 // indirect + github.com/sourcegraph/checkup v0.2.0 + github.com/spf13/cobra v0.0.5 + golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45 + gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df +) diff --git a/mail.go b/mail.go new file mode 100644 index 0000000..0236c2f --- /dev/null +++ b/mail.go @@ -0,0 +1,49 @@ +package checkup + +import ( + "fmt" + "strings" + "log" + "gopkg.in/gomail.v2" +) + +type MailNotifier struct { + Emails string `json:"emails"` + SMTPServer string `json:"smtpServer"` + SMTPPort int `json:"smtpPort"` + Username string `json:"username"` + Password string `json:"password"` +} + +// Notify implements notifier interface +func (m MailNotifier) Notify(results []Result) error { + var servicesUnavailable []Result + for _, result := range results { + if !result.Healthy { + servicesUnavailable = append(servicesUnavailable, result) + } + } + if len(servicesUnavailable) == 0 { + return nil + } + mail := gomail.NewMessage() + mail.SetHeader("From", m.Username) + mail.SetHeader("To", strings.Split(m.Emails, ",")...) + mail.SetHeader("Subject", "Service Unavailable") + var respBody = "