Add Makefile
[vaultmon/.git] / main.go
1 package main
2
3 import (
4         "strings"
5
6         log "github.com/sirupsen/logrus"
7
8         "github.com/kelseyhightower/envconfig"
9 )
10
11 type Config struct {
12         VaultURL        string `envconfig:"VAULT_URL" required:"true"`
13         EtcdURL         string `envconfig:"ETCD_URL" required:"true"`
14         SlackWebHookURL string `envconfig:"SLACK_WEBHOOK_URL" required:"true"`
15         SlackChannel    string `envconfig:"SLACK_CHANNEL" required:"true"`
16         Verbose         bool   `envconfig:"VERBOSE"`
17 }
18
19 func main() {
20         var config Config
21         err := envconfig.Process("monitor", &config)
22         if err != nil {
23                 log.Fatalf("Error reading config: %s", err)
24         }
25
26         if config.Verbose {
27                 log.SetLevel(log.DebugLevel)
28         }
29
30         checks := []CheckProvider{
31                 NewVault(config.VaultURL),
32                 NewEtcd(config.EtcdURL),
33         }
34
35         var msgs []string
36         for _, check := range checks {
37                 success, msg := check.Test()
38                 log.Infof("%s check [success: %t, msg: %s]", check.Name(), success, msg)
39                 if !success {
40                         msgs = append(msgs, msg)
41                 }
42         }
43
44         if len(msgs) < 1 {
45                 log.Infof("All checks successful.")
46                 return
47         }
48
49         slack := DefaultSlackClient(config.SlackWebHookURL, config.SlackChannel)
50         err = slack.Send(strings.Join(msgs, "\n"))
51         if err != nil {
52                 log.Fatalf("Error sending to Slack: %s", err)
53         }
54
55         log.Info("Done.")
56 }