Skip to content

Commit

Permalink
Tweaking out a couple bugs and regrouping options.
Browse files Browse the repository at this point in the history
  • Loading branch information
fusiondog committed Jul 27, 2016
1 parent 781939f commit 80aa0c6
Showing 1 changed file with 55 additions and 36 deletions.
91 changes: 55 additions & 36 deletions consul-alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ Options:
--alert-addr=<addr> The address for the consul-alert api [default: localhost:9000].
--consul-addr=<consuladdr> The consul api address [default: localhost:8500].
--consul-dc=<dc> The consul datacenter [default: dc1].
--log-level=<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=<level> Set the logging level - valid values are "debug", "info", "warn", and "err" [default: warn].
--help Show this screen.
--version Show version.
--config-file=<file> Path to the configuration file in JSON format
Expand All @@ -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 != "" {
Expand All @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit 80aa0c6

Please sign in to comment.