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
run: minitor-go
./minitor-go
./minitor-go -debug
.PHONY: test
test:

View File

@ -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
}

12
main.go
View File

@ -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)

View File

@ -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
}