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:
parent
f99ee9891e
commit
ac8ab9ef43
2
Makefile
2
Makefile
@ -9,7 +9,7 @@ minitor-go:
|
||||
|
||||
.PHONY: run
|
||||
run: minitor-go
|
||||
./minitor-go
|
||||
./minitor-go -debug
|
||||
|
||||
.PHONY: test
|
||||
test:
|
||||
|
7
alert.go
7
alert.go
@ -38,7 +38,9 @@ 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)
|
||||
}
|
||||
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
|
||||
}
|
||||
|
10
main.go
10
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 {
|
||||
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)
|
||||
|
@ -67,10 +67,14 @@ 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 {
|
||||
if LogDebug {
|
||||
log.Printf("DEBUG: Command result: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf(
|
||||
"INFO: %s success=%t, alert=%t",
|
||||
@ -102,6 +106,7 @@ 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",
|
||||
@ -109,6 +114,7 @@ func (monitor *Monitor) failure() (notice *AlertNotice) {
|
||||
monitor.failureCount,
|
||||
monitor.getAlertAfter(),
|
||||
)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user