-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
119 lines (104 loc) · 2.44 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
116
117
118
119
package main
import (
"fmt"
"log"
"net"
"os"
"strings"
"time"
"github.com/codegangsta/cli"
"github.com/miekg/dns"
)
func dnsCheck(name, ip, server string, timeout time.Duration) (error, time.Duration) {
m := new(dns.Msg)
m.SetQuestion(name+".", dns.TypeA)
c := new(dns.Client)
c.Dialer = &net.Dialer{
Timeout: timeout,
}
r, t, err := c.Exchange(m, server+":53")
if err != nil {
return err, t
}
if len(r.Answer) == 0 {
return err, t
}
for _, ans := range r.Answer {
aRecord := ans.(*dns.A)
addr := fmt.Sprintf("%s", aRecord.A)
if addr != ip {
err := fmt.Errorf("expected ip %s got %s", ip, addr)
return err, t
}
}
return nil, t
}
func monitor(name, ip string, servers []string, interval time.Duration, timeout time.Duration) {
log.Printf("Monitoring...")
ticker := time.NewTicker(interval)
for {
select {
case <-ticker.C:
for _, server := range servers {
err, t := dnsCheck(name, ip, server, timeout)
ms := int32(t/time.Millisecond)
if err != nil {
log.Printf("DNS Check Error (%dms): %v", ms, err.Error())
} else {
log.Printf("DNS Check Success (%dms)", ms)
}
}
}
}
}
func main() {
app := cli.NewApp()
app.Name = "dd-dns-monitor"
app.Usage = "log DNS server failures"
app.Author = "Chris Kite"
var name, ip, servers string
var interval, timeout time.Duration
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "name, n",
Usage: "dns name to lookup",
EnvVar: "DNS_NAME",
Destination: &name,
},
cli.StringFlag{
Name: "ip, i",
Usage: "ip address the dns name should resolve to",
EnvVar: "DNS_IP",
Destination: &ip,
},
cli.StringFlag{
Name: "servers, s",
Usage: "comma-separated list of servers to monitor",
EnvVar: "DNS_SERVERS",
Destination: &servers,
},
cli.DurationFlag{
Name: "timeout, t",
Usage: "dns query timeout",
Value: 5 * time.Second,
EnvVar: "DNS_INTERVAL",
Destination: &timeout,
},
cli.DurationFlag{
Name: "interval, l",
Usage: "interval in seconds to check at",
Value: 500 * time.Millisecond,
EnvVar: "DNS_INTERVAL",
Destination: &interval,
},
}
app.Action = func(c *cli.Context) {
if "" == name || "" == ip || "" == servers {
cli.ShowAppHelp(c)
return
}
servers := strings.Split(c.String("servers"), ",")
monitor(name, ip, servers, interval, timeout)
}
app.Run(os.Args)
}