-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
115 lines (90 loc) · 3.02 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"flag"
"os/exec"
"strings"
"time"
"github.com/mattmattox/k8s-monitor-dns/pkg/config"
"github.com/mattmattox/k8s-monitor-dns/pkg/logging"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var log = logging.SetupLogging()
var (
dnsStatus = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "dns_status",
Help: "DNS up/down status. 1 for up, 0 for down.",
},
[]string{"type"}, // type can be "internal" or "external"
)
dnsResponseTime = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "dns_response_time_seconds",
Help: "Histogram of response times for DNS checks in seconds.",
Buckets: prometheus.DefBuckets, // Default buckets, adjust as needed
},
[]string{"type"}, // type can be "internal" or "external"
)
)
func init() {
// Register metrics with Prometheus
prometheus.MustRegister(dnsStatus)
prometheus.MustRegister(dnsResponseTime)
}
func main() {
flag.Parse() // Parse command-line flags
config := config.LoadConfigFromEnv()
logging.SetupLogging()
if config.Debug {
log.Debug("Debug mode enabled")
} else {
log.Info("Debug mode disabled")
}
log.Info("Starting k8s-monitor-dns")
// Set up HTTP server for Prometheus metrics
http.Handle("/metrics", promhttp.Handler())
go func() {
MetircsPort := ":" + config.MetircsPort
if err := http.ListenAndServe(MetircsPort, nil); err != nil {
log.Errorf("Error starting Prometheus HTTP server: %v", err)
}
}()
// Parse timeout and delay
timeout := time.Duration(config.Timeout) * time.Second
delay := time.Duration(config.Delay) * time.Second
for {
log.Infof("Checking Internal DNS: %s", config.InternalHost)
checkDNS(config.InternalIP, config.InternalHost, config.InternalIP, timeout, "internal")
log.Infof("Checking External DNS: %s", config.ExternalHost)
checkDNS(config.ExternalIP, config.ExternalHost, config.ExternalIP, timeout, "external")
time.Sleep(delay)
}
}
func checkDNS(ip, host, expectedIP string, timeout time.Duration, dnsType string) {
start := time.Now() // Start time for measuring response time
// Execute the DNS check
cmd := exec.Command("timeout", timeout.String(), "dig", "+short", "@"+ip, host)
output, err := cmd.Output()
elapsed := time.Since(start) // Calculate the elapsed time
// Update Prometheus metrics for response time
dnsResponseTime.WithLabelValues(dnsType).Observe(elapsed.Seconds())
if err != nil {
// DNS check failed (down)
dnsStatus.WithLabelValues(dnsType).Set(0) // DNS is down
log.Errorf("DNS check for %s failed: %v", host, err)
return
}
// Trim the output and compare with expected IP
actualIP := strings.TrimSpace(string(output))
if actualIP == expectedIP {
// DNS check succeeded (up)
dnsStatus.WithLabelValues(dnsType).Set(1) // DNS is up
log.Infof("%s DNS is OK", host)
} else {
// DNS check returned an unexpected result (down)
dnsStatus.WithLabelValues(dnsType).Set(0) // DNS is down
log.Errorf("%s DNS returned a bad IP: %s", host, actualIP)
}
}