From ac8ab9ef43bbcb07e667f5ff6307c694188ab3b2 Mon Sep 17 00:00:00 2001 From: Ian Fijolek Date: Fri, 4 Oct 2019 16:05:25 -0700 Subject: [PATCH] Add debug flag to adjust log verbosity a bit Not as good as a proper logging system, but still provids a bit of control over log verbosity --- Makefile | 2 +- alert.go | 9 +++++++-- main.go | 12 +++++++++++- monitor.go | 24 +++++++++++++++--------- 4 files changed, 34 insertions(+), 13 deletions(-) diff --git a/Makefile b/Makefile index 73bd2e7..600fef1 100644 --- a/Makefile +++ b/Makefile @@ -9,7 +9,7 @@ minitor-go: .PHONY: run run: minitor-go - ./minitor-go + ./minitor-go -debug .PHONY: test test: diff --git a/alert.go b/alert.go index e6f4938..24e2b81 100644 --- a/alert.go +++ b/alert.go @@ -38,7 +38,9 @@ func (alert Alert) IsValid() bool { // BuildTemplates compiles command templates for the Alert func (alert *Alert) BuildTemplates() error { - log.Printf("DEBUG: Building template for alert %s", alert.Name) + if LogDebug { + log.Printf("DEBUG: Building template for alert %s", alert.Name) + } if alert.commandTemplate == nil && alert.Command != nil { alert.commandTemplate = []*template.Template{} for i, cmdPart := range alert.Command { @@ -59,6 +61,7 @@ func (alert *Alert) BuildTemplates() error { // Send will send an alert notice by executing the command template func (alert Alert) Send(notice AlertNotice) (output_str string, err error) { + log.Printf("INFO: Sending alert %s for %s", alert.Name, notice.MonitorName) var cmd *exec.Cmd if alert.commandTemplate != nil { command := []string{} @@ -93,7 +96,9 @@ func (alert Alert) Send(notice AlertNotice) (output_str string, err error) { var output []byte output, err = cmd.CombinedOutput() output_str = string(output) - // log.Printf("DEBUG: Alert output for: %s\n---\n%s\n---", alert.Name, output_str) + if LogDebug { + log.Printf("DEBUG: Alert output for: %s\n---\n%s\n---", alert.Name, output_str) + } return output_str, err } diff --git a/main.go b/main.go index 5b1916e..06f33eb 100644 --- a/main.go +++ b/main.go @@ -1,11 +1,15 @@ package main import ( + "flag" "fmt" "log" "time" ) +// LogDebug will control whether debug messsages should be logged +var LogDebug bool = false + func checkMonitors(config *Config) { for _, monitor := range config.Monitors { if monitor.ShouldCheck() { @@ -13,7 +17,9 @@ func checkMonitors(config *Config) { // Should probably consider refactoring everything below here if alertNotice != nil { - log.Printf("DEBUG: Recieved an alert notice from %s", alertNotice.MonitorName) + if LogDebug { + log.Printf("DEBUG: Recieved an alert notice from %s", alertNotice.MonitorName) + } alertNames := monitor.GetAlertNames(alertNotice.IsUp) if alertNames == nil { // TODO: Should this be a panic? Should this be validated against? Probably @@ -51,6 +57,10 @@ func checkMonitors(config *Config) { } func main() { + // Get debug flag + flag.BoolVar(&LogDebug, "debug", false, "Enables debug logs (default: false)") + flag.Parse() + config, err := LoadConfig("config.yml") if err != nil { log.Fatalf("Error loading config: %v", err) diff --git a/monitor.go b/monitor.go index 0577653..558bdc9 100644 --- a/monitor.go +++ b/monitor.go @@ -67,9 +67,13 @@ func (monitor *Monitor) Check() (bool, *AlertNotice) { alertNotice = monitor.failure() } - // log.Printf("DEBUG: Command output: %s", monitor.lastOutput) + if LogDebug { + log.Printf("DEBUG: Command output: %s", monitor.lastOutput) + } if err != nil { - log.Printf("DEBUG: Command result: %v", err) + if LogDebug { + log.Printf("DEBUG: Command result: %v", err) + } } log.Printf( @@ -102,13 +106,15 @@ func (monitor *Monitor) failure() (notice *AlertNotice) { monitor.failureCount++ // If we haven't hit the minimum failures, we can exit if monitor.failureCount < monitor.getAlertAfter() { - log.Printf( - "DEBUG: %s failed but did not hit minimum failures. "+ - "Count: %v alert after: %v", - monitor.Name, - monitor.failureCount, - monitor.getAlertAfter(), - ) + if LogDebug { + log.Printf( + "DEBUG: %s failed but did not hit minimum failures. "+ + "Count: %v alert after: %v", + monitor.Name, + monitor.failureCount, + monitor.getAlertAfter(), + ) + } return }