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
This commit is contained in:
IamTheFij 2019-10-04 16:05:25 -07:00
parent f99ee9891e
commit ac8ab9ef43
4 changed files with 34 additions and 13 deletions

View File

@ -9,7 +9,7 @@ minitor-go:
.PHONY: run .PHONY: run
run: minitor-go run: minitor-go
./minitor-go ./minitor-go -debug
.PHONY: test .PHONY: test
test: test:

View File

@ -38,7 +38,9 @@ func (alert Alert) IsValid() bool {
// BuildTemplates compiles command templates for the Alert // BuildTemplates compiles command templates for the Alert
func (alert *Alert) BuildTemplates() error { 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 { if alert.commandTemplate == nil && alert.Command != nil {
alert.commandTemplate = []*template.Template{} alert.commandTemplate = []*template.Template{}
for i, cmdPart := range alert.Command { 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 // Send will send an alert notice by executing the command template
func (alert Alert) Send(notice AlertNotice) (output_str string, err error) { 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 var cmd *exec.Cmd
if alert.commandTemplate != nil { if alert.commandTemplate != nil {
command := []string{} command := []string{}
@ -93,7 +96,9 @@ func (alert Alert) Send(notice AlertNotice) (output_str string, err error) {
var output []byte var output []byte
output, err = cmd.CombinedOutput() output, err = cmd.CombinedOutput()
output_str = string(output) 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 return output_str, err
} }

12
main.go
View File

@ -1,11 +1,15 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
"log" "log"
"time" "time"
) )
// LogDebug will control whether debug messsages should be logged
var LogDebug bool = false
func checkMonitors(config *Config) { func checkMonitors(config *Config) {
for _, monitor := range config.Monitors { for _, monitor := range config.Monitors {
if monitor.ShouldCheck() { if monitor.ShouldCheck() {
@ -13,7 +17,9 @@ func checkMonitors(config *Config) {
// Should probably consider refactoring everything below here // Should probably consider refactoring everything below here
if alertNotice != nil { 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) alertNames := monitor.GetAlertNames(alertNotice.IsUp)
if alertNames == nil { if alertNames == nil {
// TODO: Should this be a panic? Should this be validated against? Probably // TODO: Should this be a panic? Should this be validated against? Probably
@ -51,6 +57,10 @@ func checkMonitors(config *Config) {
} }
func main() { func main() {
// Get debug flag
flag.BoolVar(&LogDebug, "debug", false, "Enables debug logs (default: false)")
flag.Parse()
config, err := LoadConfig("config.yml") config, err := LoadConfig("config.yml")
if err != nil { if err != nil {
log.Fatalf("Error loading config: %v", err) log.Fatalf("Error loading config: %v", err)

View File

@ -67,9 +67,13 @@ func (monitor *Monitor) Check() (bool, *AlertNotice) {
alertNotice = monitor.failure() alertNotice = monitor.failure()
} }
// log.Printf("DEBUG: Command output: %s", monitor.lastOutput) if LogDebug {
log.Printf("DEBUG: Command output: %s", monitor.lastOutput)
}
if err != nil { if err != nil {
log.Printf("DEBUG: Command result: %v", err) if LogDebug {
log.Printf("DEBUG: Command result: %v", err)
}
} }
log.Printf( log.Printf(
@ -102,13 +106,15 @@ func (monitor *Monitor) failure() (notice *AlertNotice) {
monitor.failureCount++ monitor.failureCount++
// If we haven't hit the minimum failures, we can exit // If we haven't hit the minimum failures, we can exit
if monitor.failureCount < monitor.getAlertAfter() { if monitor.failureCount < monitor.getAlertAfter() {
log.Printf( if LogDebug {
"DEBUG: %s failed but did not hit minimum failures. "+ log.Printf(
"Count: %v alert after: %v", "DEBUG: %s failed but did not hit minimum failures. "+
monitor.Name, "Count: %v alert after: %v",
monitor.failureCount, monitor.Name,
monitor.getAlertAfter(), monitor.failureCount,
) monitor.getAlertAfter(),
)
}
return return
} }