diff --git a/log.go b/log.go index f7e3300..f1cb54a 100644 --- a/log.go +++ b/log.go @@ -3,6 +3,7 @@ package main import ( "fmt" "os" + "time" ) func logFatal(msg interface{}) { @@ -24,3 +25,8 @@ func logFatalf(format string, args ...interface{}) { fmt.Fprintf(os.Stderr, format+"\n", args...) os.Exit(1) } + +func logWithTimestamp(message string) { + timestamp := time.Now().Format("2006-01-02 15:04:05") + fmt.Printf("[%s] %s\n", timestamp, message) +} diff --git a/main.go b/main.go index bdf6e5b..39aabcc 100644 --- a/main.go +++ b/main.go @@ -24,8 +24,11 @@ func main() { // Parse flags flag.Parse() + log := true + if *datadog { *metric = true + log = false } for { @@ -40,7 +43,7 @@ func main() { response, _, err := client.Exchange(message, *dnsserver+":53") if err == nil { // Process the results - outageDetected := processDNSResponse(response, *datadog, *debug, *gateway, *metric, *model, *statsdipport) + outageDetected := processDNSResponse(response, *datadog, *debug, *gateway, log, *metric, *model, *statsdipport) if outageDetected && *noloop { os.Exit(1) // Exit with return code 1 only if noloop is set } @@ -60,7 +63,7 @@ func main() { } } -func processDNSResponse(response *dns.Msg, datadog bool, debug bool, gateway string, metric bool, model string, statsdipport string) bool { +func processDNSResponse(response *dns.Msg, datadog bool, debug bool, gateway string, log bool, metric bool, model string, statsdipport string) bool { found := false metrics := []string{} @@ -75,14 +78,15 @@ func processDNSResponse(response *dns.Msg, datadog bool, debug bool, gateway str found = true ip := aRecord.A.String() if ip == gateway { - outageMetric = "1.0" + outageMetricValue = "1.0" outageMetric = outageMetricName + "=" + outageMetricValue metrics = append(metrics, outageMetric) if datadog { giveMetricsToDatadogStatsd(debug, metrics, model, statsdipport) - } else { + } + if debug || log { logWithTimestamp(fmt.Sprintf("Outage detected: %s", ip)) } @@ -105,8 +109,3 @@ func processDNSResponse(response *dns.Msg, datadog bool, debug bool, gateway str } return false } - -func logWithTimestamp(message string) { - timestamp := time.Now().Format("2006-01-02 15:04:05") - fmt.Printf("[%s] %s\n", timestamp, message) -} diff --git a/statsd.go b/statsd.go index 193e7de..76d4ee4 100644 --- a/statsd.go +++ b/statsd.go @@ -39,7 +39,7 @@ func giveMetricsToDatadogStatsd(debug bool, metrics []string, model string, stat for key, value := range retrievedFloatMetrics { if debug { - fmt.Println(key, value) + logWithTimestamp("Metric sent: " + key + "=" + strconv.FormatFloat(value, 'f', -1, 64)) } err = client.Gauge(key, value, []string{"gateway:" + model}, 1)