Skip to content

Commit

Permalink
Fixing reporting of outages as a metric
Browse files Browse the repository at this point in the history
Improving logging
  • Loading branch information
edgan committed Nov 27, 2024
1 parent fbf08aa commit 5fe74c9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
6 changes: 6 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"os"
"time"
)

func logFatal(msg interface{}) {
Expand All @@ -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)
}
17 changes: 8 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ func main() {
// Parse flags
flag.Parse()

log := true

if *datadog {
*metric = true
log = false
}

for {
Expand All @@ -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
}
Expand All @@ -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{}
Expand All @@ -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))
}

Expand All @@ -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)
}
2 changes: 1 addition & 1 deletion statsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 5fe74c9

Please sign in to comment.