From 87b233aa720815f80e767c97c7333bd93a01ddea Mon Sep 17 00:00:00 2001 From: Vincent Vu Date: Sun, 24 Jul 2016 18:54:39 -0400 Subject: [PATCH 1/2] Configure consul-alerts options by using JSON file --- consul-alerts.go | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/consul-alerts.go b/consul-alerts.go index 82487152..b612794b 100644 --- a/consul-alerts.go +++ b/consul-alerts.go @@ -10,6 +10,9 @@ import ( "net/http" "os/signal" + "encoding/json" + "io/ioutil" + "github.com/AcalephStorage/consul-alerts/consul" "github.com/AcalephStorage/consul-alerts/notifier" @@ -21,7 +24,7 @@ const version = "Consul Alerts 0.3.3" const usage = `Consul Alerts. Usage: - consul-alerts start [--alert-addr=] [--consul-addr=] [--consul-dc=] [--consul-acl-token=] [--watch-checks] [--watch-events] [--log-level=] + consul-alerts start [--alert-addr=] [--consul-addr=] [--consul-dc=] [--consul-acl-token=] [--watch-checks] [--watch-events] [--log-level=] [--config-file=] consul-alerts watch (checks|event) [--alert-addr=] [--log-level=] consul-alerts --help consul-alerts --version @@ -36,6 +39,7 @@ Options: --log-level= Set the logging level - valid values are "debug", "info", "warn", and "err" [default: warn]. --help Show this screen. --version Show version. + --config-file= Path to the configuration file in JSON format ` @@ -59,6 +63,22 @@ func main() { func daemonMode(arguments map[string]interface{}) { loglevelString, _ := arguments["--log-level"].(string) + configFile := arguments["--config-file"].(string) + + var confData map[string]interface{} + file, err := ioutil.ReadFile(configFile) + if err != nil { + log.Error(err) + } + err = json.Unmarshal(file, &confData) + if err != nil { + log.Error(err) + } + log.Debug("Config data: ", confData) + + if confData["consul-acl-token"] != nil { + loglevelString = confData["log-level"].(string) + } if loglevelString != "" { loglevel, err := log.ParseLevel(loglevelString) @@ -86,6 +106,22 @@ func daemonMode(arguments map[string]interface{}) { watchChecks := arguments["--watch-checks"].(bool) watchEvents := arguments["--watch-events"].(bool) + if confData["consul-acl-token"] != nil { + consulAclToken = confData["consul-acl-token"].(string) + } + if confData["consul-addr"] != nil { + consulAddr = confData["consul-addr"].(string) + } + if confData["consul-dc"] != nil { + consulDc = confData["consul-dc"].(string) + } + if confData["watch-checks"] != nil { + watchChecks = confData["watch-checks"].(bool) + } + if confData["watch-events"] != nil { + watchEvents = confData["watch-events"].(bool) + } + consulClient, err = consul.NewClient(consulAddr, consulDc, consulAclToken) if err != nil { log.Println("Cluster has no leader or is unreacheable.", err) From 80aa0c66aa9b729785a6448073c5f62422fdd66a Mon Sep 17 00:00:00 2001 From: fusiondog Date: Wed, 27 Jul 2016 13:25:27 +0000 Subject: [PATCH 2/2] Tweaking out a couple bugs and regrouping options. --- consul-alerts.go | 91 +++++++++++++++++++++++++++++------------------- 1 file changed, 55 insertions(+), 36 deletions(-) diff --git a/consul-alerts.go b/consul-alerts.go index b612794b..ec563b6d 100644 --- a/consul-alerts.go +++ b/consul-alerts.go @@ -34,9 +34,9 @@ Options: --alert-addr= The address for the consul-alert api [default: localhost:9000]. --consul-addr= The consul api address [default: localhost:8500]. --consul-dc= The consul datacenter [default: dc1]. + --log-level= Set the logging level - valid values are "debug", "info", "warn", and "err" [default: warn]. --watch-checks Run check watcher. --watch-events Run event watcher. - --log-level= Set the logging level - valid values are "debug", "info", "warn", and "err" [default: warn]. --help Show this screen. --version Show version. --config-file= Path to the configuration file in JSON format @@ -62,22 +62,65 @@ func main() { } func daemonMode(arguments map[string]interface{}) { - loglevelString, _ := arguments["--log-level"].(string) - configFile := arguments["--config-file"].(string) + // Define options before setting in either config file or on command line + loglevelString := "" + consulAclToken := "" + consulAddr := "" + consulDc := "" + watchChecks := false + watchEvents := false + addr := "" var confData map[string]interface{} - file, err := ioutil.ReadFile(configFile) - if err != nil { - log.Error(err) - } - err = json.Unmarshal(file, &confData) - if err != nil { - log.Error(err) + + // This exists check only works for arguments with no default. arguments with defaults will always exist. + // Because of this the current code overrides command line flags with config file options if set. + if configFile, exists := arguments["--config-file"].(string); exists { + file, err := ioutil.ReadFile(configFile) + if err != nil { + log.Error(err) + } + err = json.Unmarshal(file, &confData) + if err != nil { + log.Error(err) + } + log.Debug("Config data: ", confData) } - log.Debug("Config data: ", confData) - if confData["consul-acl-token"] != nil { + if confData["log-level"] != nil { loglevelString = confData["log-level"].(string) + } else { + loglevelString = arguments["--log-level"].(string) + } + if confData["consul-acl-token"] != nil { + consulAclToken = confData["consul-acl-token"].(string) + } else { + consulAclToken = arguments["--consul-acl-token"].(string) + } + if confData["consul-addr"] != nil { + consulAddr = confData["consul-addr"].(string) + } else { + consulAddr = arguments["--consul-addr"].(string) + } + if confData["consul-dc"] != nil { + consulDc = confData["consul-dc"].(string) + } else { + consulDc = arguments["--consul-dc"].(string) + } + if confData["alert-addr"] != nil { + addr = confData["alert-addr"].(string) + } else { + addr = arguments["--alert-addr"].(string) + } + if confData["watch-checks"] != nil { + watchChecks = confData["watch-checks"].(bool) + } else { + watchChecks = arguments["--watch-checks"].(bool) + } + if confData["watch-events"] != nil { + watchEvents = confData["watch-events"].(bool) + } else { + watchEvents = arguments["--watch-events"].(bool) } if loglevelString != "" { @@ -89,8 +132,6 @@ func daemonMode(arguments map[string]interface{}) { } } - addr := arguments["--alert-addr"].(string) - url := fmt.Sprintf("http://%s/v1/info", addr) resp, err := http.Get(url) if err == nil && resp.StatusCode == 201 { @@ -100,28 +141,6 @@ func daemonMode(arguments map[string]interface{}) { os.Exit(1) } - consulAclToken := arguments["--consul-acl-token"].(string) - consulAddr := arguments["--consul-addr"].(string) - consulDc := arguments["--consul-dc"].(string) - watchChecks := arguments["--watch-checks"].(bool) - watchEvents := arguments["--watch-events"].(bool) - - if confData["consul-acl-token"] != nil { - consulAclToken = confData["consul-acl-token"].(string) - } - if confData["consul-addr"] != nil { - consulAddr = confData["consul-addr"].(string) - } - if confData["consul-dc"] != nil { - consulDc = confData["consul-dc"].(string) - } - if confData["watch-checks"] != nil { - watchChecks = confData["watch-checks"].(bool) - } - if confData["watch-events"] != nil { - watchEvents = confData["watch-events"].(bool) - } - consulClient, err = consul.NewClient(consulAddr, consulDc, consulAclToken) if err != nil { log.Println("Cluster has no leader or is unreacheable.", err)