-
-
Notifications
You must be signed in to change notification settings - Fork 588
/
dns.go
48 lines (42 loc) · 1.06 KB
/
dns.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
package observer
import (
"fmt"
"time"
"github.com/miekg/dns"
)
// DNSProbe is the exported handler object for monitors configured to
// perform DNS queries
type DNSProbe struct {
Proto string
Server string
Recurse bool
QName string
QType uint16
}
// Name returns a name that uniquely identifies the monitor
func (p DNSProbe) Name() string {
return fmt.Sprintf("%s-%s-%s-%s", p.Proto, p.Server, p.QName, dns.TypeToString[p.QType])
}
// Kind returns a name that uniquely identifies the prober
func (p DNSProbe) Kind() string {
return "DNS"
}
// Do is the query handler for HTTP probes
func (p DNSProbe) Do(timeout time.Duration) (bool, time.Duration) {
m := new(dns.Msg)
m.SetQuestion(dns.Fqdn(p.QName), p.QType)
m.RecursionDesired = p.Recurse
start := time.Now()
c := dns.Client{Timeout: timeout, Net: p.Proto}
r, _, err := c.Exchange(m, p.Server)
if err != nil {
return false, time.Since(start)
}
if r == nil {
return false, time.Since(start)
}
if r.Rcode != dns.RcodeSuccess {
return false, time.Since(start)
}
return true, time.Since(start)
}