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 {
if LogDebug {
log.Printf("DEBUG: Building template for alert %s", alert.Name) 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
} }

10
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 {
if LogDebug {
log.Printf("DEBUG: Recieved an alert notice from %s", alertNotice.MonitorName) 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,10 +67,14 @@ 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 {
if LogDebug {
log.Printf("DEBUG: Command result: %v", err) log.Printf("DEBUG: Command result: %v", err)
} }
}
log.Printf( log.Printf(
"INFO: %s success=%t, alert=%t", "INFO: %s success=%t, alert=%t",
@ -102,6 +106,7 @@ 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() {
if LogDebug {
log.Printf( log.Printf(
"DEBUG: %s failed but did not hit minimum failures. "+ "DEBUG: %s failed but did not hit minimum failures. "+
"Count: %v alert after: %v", "Count: %v alert after: %v",
@ -109,6 +114,7 @@ func (monitor *Monitor) failure() (notice *AlertNotice) {
monitor.failureCount, monitor.failureCount,
monitor.getAlertAfter(), monitor.getAlertAfter(),
) )
}
return return
} }