diff --git a/alert.go b/alert.go index 24e2b81..80594ba 100644 --- a/alert.go +++ b/alert.go @@ -3,10 +3,11 @@ package main import ( "bytes" "fmt" - "log" "os/exec" "text/template" "time" + + log "github.com/sirupsen/logrus" ) // Alert is a config driven mechanism for sending a notice @@ -38,9 +39,7 @@ func (alert Alert) IsValid() bool { // BuildTemplates compiles command templates for the Alert func (alert *Alert) BuildTemplates() error { - if LogDebug { - log.Printf("DEBUG: Building template for alert %s", alert.Name) - } + log.Debugf("Building template for alert %s", alert.Name) if alert.commandTemplate == nil && alert.Command != nil { alert.commandTemplate = []*template.Template{} for i, cmdPart := range alert.Command { @@ -60,8 +59,8 @@ 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) +func (alert Alert) Send(notice AlertNotice) (outputStr string, err error) { + log.Infof("Sending alert %s for %s", alert.Name, notice.MonitorName) var cmd *exec.Cmd if alert.commandTemplate != nil { command := []string{} @@ -95,10 +94,8 @@ func (alert Alert) Send(notice AlertNotice) (output_str string, err error) { var output []byte output, err = cmd.CombinedOutput() - output_str = string(output) - if LogDebug { - log.Printf("DEBUG: Alert output for: %s\n---\n%s\n---", alert.Name, output_str) - } + outputStr = string(output) + log.Debugf("Alert output for: %s\n---\n%s\n---", alert.Name, outputStr) - return output_str, err + return outputStr, err } diff --git a/alert_test.go b/alert_test.go index 8bf3a61..94f07c7 100644 --- a/alert_test.go +++ b/alert_test.go @@ -1,8 +1,9 @@ package main import ( - "log" "testing" + + log "github.com/sirupsen/logrus" ) func TestAlertIsValid(t *testing.T) { @@ -22,13 +23,13 @@ func TestAlertIsValid(t *testing.T) { } for _, c := range cases { - log.Printf("Testing case %s", c.name) + log.Debugf("Testing case %s", c.name) actual := c.alert.IsValid() if actual != c.expected { t.Errorf("IsValid(%v), expected=%t actual=%t", c.name, c.expected, actual) - log.Printf("Case failed: %s", c.name) + log.Debugf("Case failed: %s", c.name) } - log.Println("-----") + log.Debugf("-----") } } @@ -71,19 +72,19 @@ func TestAlertSend(t *testing.T) { } for _, c := range cases { - log.Printf("Testing case %s", c.name) + log.Debugf("Testing case %s", c.name) c.alert.BuildTemplates() output, err := c.alert.Send(c.notice) hasErr := (err != nil) if output != c.expectedOutput { t.Errorf("Send(%v output), expected=%v actual=%v", c.name, c.expectedOutput, output) - log.Printf("Case failed: %s", c.name) + log.Debugf("Case failed: %s", c.name) } if hasErr != c.expectErr { t.Errorf("Send(%v err), expected=%v actual=%v", c.name, "Err", err) - log.Printf("Case failed: %s", c.name) + log.Debugf("Case failed: %s", c.name) } - log.Println("-----") + log.Debugf("-----") } } @@ -94,7 +95,7 @@ func TestAlertSendNoTemplates(t *testing.T) { if err == nil { t.Errorf("Send(no template), expected=%v actual=%v", "Err", output) } - log.Println("-----") + log.Debugf("-----") } func TestAlertBuildTemplate(t *testing.T) { @@ -109,13 +110,13 @@ func TestAlertBuildTemplate(t *testing.T) { } for _, c := range cases { - log.Printf("Testing case %s", c.name) + log.Debugf("Testing case %s", c.name) err := c.alert.BuildTemplates() hasErr := (err != nil) if hasErr != c.expectErr { t.Errorf("IsValid(%v), expected=%t actual=%t", c.name, c.expectErr, err) - log.Printf("Case failed: %s", c.name) + log.Debugf("Case failed: %s", c.name) } - log.Println("-----") + log.Debugf("-----") } } diff --git a/config.go b/config.go index 0a044db..8e586e9 100644 --- a/config.go +++ b/config.go @@ -3,9 +3,9 @@ package main import ( "errors" "io/ioutil" - "log" "os" + log "github.com/sirupsen/logrus" "gopkg.in/yaml.v2" ) @@ -22,20 +22,20 @@ func (config Config) IsValid() (isValid bool) { // Validate monitors if config.Monitors == nil || len(config.Monitors) == 0 { - log.Printf("ERROR: Invalid monitor configuration: Must provide at least one monitor") + log.Errorf("Invalid monitor configuration: Must provide at least one monitor") isValid = false } for _, monitor := range config.Monitors { if !monitor.IsValid() { - log.Printf("ERROR: Invalid monitor configuration: %s", monitor.Name) + log.Errorf("Invalid monitor configuration: %s", monitor.Name) isValid = false } // Check that all Monitor alerts actually exist for _, isUp := range []bool{true, false} { for _, alertName := range monitor.GetAlertNames(isUp) { if _, ok := config.Alerts[alertName]; !ok { - log.Printf( - "ERROR: Invalid monitor configuration: %s. Unknown alert %s", + log.Errorf( + "Invalid monitor configuration: %s. Unknown alert %s", monitor.Name, alertName, ) isValid = false @@ -46,12 +46,12 @@ func (config Config) IsValid() (isValid bool) { // Validate alerts if config.Alerts == nil || len(config.Alerts) == 0 { - log.Printf("ERROR: Invalid alert configuration: Must provide at least one alert") + log.Errorf("Invalid alert configuration: Must provide at least one alert") isValid = false } for _, alert := range config.Alerts { if !alert.IsValid() { - log.Printf("ERROR: Invalid alert configuration: %s", alert.Name) + log.Errorf("Invalid alert configuration: %s", alert.Name) isValid = false } } @@ -85,9 +85,7 @@ func LoadConfig(filePath string) (config Config, err error) { return } - if LogDebug { - log.Printf("DEBUG: Config values:\n%v\n", config) - } + log.Debugf("Config values:\n%v\n", config) if !config.IsValid() { err = errors.New("Invalid configuration") diff --git a/go.mod b/go.mod index dac2c9d..b4bfe96 100644 --- a/go.mod +++ b/go.mod @@ -4,5 +4,6 @@ go 1.12 require ( github.com/prometheus/client_golang v1.2.1 + github.com/sirupsen/logrus v1.4.2 gopkg.in/yaml.v2 v2.2.4 ) diff --git a/go.sum b/go.sum index 8980eef..cbfa264 100644 --- a/go.sum +++ b/go.sum @@ -53,6 +53,7 @@ github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsT github.com/prometheus/procfs v0.0.5 h1:3+auTFlqw+ZaQYJARz6ArODtkaIwtvBTx3N2NehQlL8= github.com/prometheus/procfs v0.0.5/go.mod h1:4A/X28fw3Fc593LaREMrKMqOKvUAntwMDaekg4FpcdQ= github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= +github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4= github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -68,6 +69,7 @@ golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5h golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20191010194322-b09406accb47 h1:/XfQ9z7ib8eEJX2hdgFTZJ/ntt0swNk5oYBziWeTCvY= golang.org/x/sys v0.0.0-20191010194322-b09406accb47/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= diff --git a/main.go b/main.go index 27b894c..e1b318c 100644 --- a/main.go +++ b/main.go @@ -3,14 +3,12 @@ package main import ( "flag" "fmt" - "log" "time" + + log "github.com/sirupsen/logrus" ) var ( - // LogDebug will control whether debug messsages should be logged - LogDebug = false - // ExportMetrics will track whether or not we want to export metrics to prometheus ExportMetrics = false // MetricsPort is the port to expose metrics on @@ -35,9 +33,7 @@ func checkMonitors(config *Config) error { // Should probably consider refactoring everything below here if alertNotice != nil { - if LogDebug { - log.Printf("DEBUG: Recieved an alert notice from %s", alertNotice.MonitorName) - } + log.Debugf("Recieved an alert notice from %s", alertNotice.MonitorName) alertNames := monitor.GetAlertNames(alertNotice.IsUp) if alertNames == nil { // This should only happen for a recovery alert. AlertDown is validated not empty @@ -81,11 +77,16 @@ func checkMonitors(config *Config) error { func main() { // Get debug flag - flag.BoolVar(&LogDebug, "debug", false, "Enables debug logs (default: false)") + var debug = flag.Bool("debug", false, "Enables debug logs (default: false)") flag.BoolVar(&ExportMetrics, "metrics", false, "Enables prometheus metrics exporting (default: false)") var showVersion = flag.Bool("version", false, "Display the version of minitor and exit") flag.Parse() + // Set debug if flag is set + if *debug { + log.SetLevel(log.DebugLevel) + } + // Print version if flag is provided if *showVersion { log.Println("Minitor version:", version) diff --git a/monitor.go b/monitor.go index 2b7b4ef..9d38371 100644 --- a/monitor.go +++ b/monitor.go @@ -1,10 +1,11 @@ package main import ( - "log" "math" "os/exec" "time" + + log "github.com/sirupsen/logrus" ) // Monitor represents a particular periodic check of a command @@ -70,20 +71,18 @@ func (monitor *Monitor) Check() (bool, *AlertNotice) { alertNotice = monitor.failure() } - if LogDebug { - log.Printf("DEBUG: Command output: %s", monitor.lastOutput) - } + log.Debugf("Command output: %s", monitor.lastOutput) if err != nil { - if LogDebug { - log.Printf("DEBUG: Command result: %v", err) - } + log.Debugf("Command result: %v", err) } - log.Printf( - "INFO: %s success=%t, alert=%t", + log.WithFields(log.Fields{ + "monitor": monitor.Name, + "success": isSuccess, + "alert": alertNotice != nil, + }).Infof( + "%s checked", monitor.Name, - isSuccess, - alertNotice != nil, ) return isSuccess, alertNotice @@ -109,15 +108,13 @@ func (monitor *Monitor) failure() (notice *AlertNotice) { monitor.failureCount++ // If we haven't hit the minimum failures, we can exit if 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(), - ) - } + log.Debugf( + "%s failed but did not hit minimum failures. "+ + "Count: %v alert after: %v", + monitor.Name, + monitor.failureCount, + monitor.getAlertAfter(), + ) return } @@ -155,18 +152,16 @@ func (monitor Monitor) getAlertAfter() int16 { // Zero is one! if monitor.AlertAfter == 0 { return 1 - } else { - return monitor.AlertAfter } + return monitor.AlertAfter } // GetAlertNames gives a list of alert names for a given monitor status func (monitor Monitor) GetAlertNames(up bool) []string { if up { return monitor.AlertUp - } else { - return monitor.AlertDown } + return monitor.AlertDown } func (monitor Monitor) createAlertNotice(isUp bool) *AlertNotice {