-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
75 lines (62 loc) · 1.71 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
package main
import (
"flag"
"log"
"net"
"net/http"
"strings"
"time"
"github.com/miekg/dns"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var dnsVar string
var interval time.Duration
func main() {
flag.StringVar(&dnsVar, "dnstargets", "", "comma separated list of dns servers which will be queried")
flag.DurationVar(&interval, "interval", 5*time.Second, "interval between measurements. default: 5s")
flag.Parse()
dnsTargets := strings.Split(dnsVar, ",")
if dnsVar == "" || len(dnsTargets) == 0 {
log.Fatal("need at least one target via -dnstargets")
}
log.Printf("querying %d targets: %s", len(dnsTargets), dnsTargets)
targetVec := prometheus.NewSummaryVec(
prometheus.SummaryOpts{
Name: "dnsquery",
Help: "dns query time in s",
Objectives: map[float64]float64{
0.5: 0.01,
0.9: 0.01,
0.99: 0.01,
},
},
[]string{"target", "success"},
)
for _, target := range dnsTargets {
go measure(targetVec, strings.TrimSpace(target))
}
reg := prometheus.NewRegistry()
reg.Register(targetVec)
http.Handle("/metrics", promhttp.HandlerFor(reg, promhttp.HandlerOpts{Registry: reg}))
log.Fatal(http.ListenAndServe(":8080", nil))
}
func measure(vec *prometheus.SummaryVec, target string) {
client := dns.Client{
Timeout: time.Duration(time.Minute),
}
for {
var msg dns.Msg
msg.SetQuestion(dns.Fqdn("."), dns.TypeNS)
msg.RecursionDesired = true
addr := net.JoinHostPort(target, "53")
_, rtt, err := client.Exchange(&msg, addr)
success := "true"
if err != nil {
log.Println(target, err.Error())
success = "false"
}
vec.WithLabelValues(target, success).Observe(rtt.Seconds())
time.Sleep(interval)
}
}