-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
253 lines (202 loc) · 6.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// main hostlookuper
package main
import (
"context"
"flag"
"fmt"
"math/rand"
"net"
"net/http"
"os"
"strings"
"time"
"github.com/VictoriaMetrics/metrics"
"github.com/miekg/dns"
"github.com/peterbourgon/ff/v3"
"github.com/postfinance/flash"
"go.uber.org/zap"
)
const (
dnsDurationName = "hostlookuper_dns_lookup_duration_seconds"
dnsLookupTotalName = "hostlookuper_dns_lookup_total"
dnsErrorsTotalName = "hostlookuper_dns_errors_total"
)
//nolint:gochecknoglobals // There is no other way than doing so. Values will be set on build.
var (
version, date string
commit = "12345678"
)
// DNSServer describes a protocol (network) and an address to contact a dns server
type DNSServer struct {
network string
address string
name string
}
func (srv DNSServer) String() string {
return fmt.Sprintf("%s://%s", srv.network, srv.name)
}
func parseDNSServers(l *zap.SugaredLogger, dnsServersStr string) []DNSServer {
dnsServersList := strings.Split(dnsServersStr, ",")
dnsServers := make([]DNSServer, 0, len(dnsServersList))
for _, dnsServer := range dnsServersList {
var network, address, name string
spl := strings.Split(dnsServer, "://")
switch len(spl) {
case 1:
network = "udp"
address = spl[0]
case 2:
network = spl[0]
address = spl[1]
default:
l.Fatalw("parsing dns servers list failed, wrong format used",
"val", dnsServer)
}
if !strings.Contains(address, ":") { // port was not specified, implying port 53
address += ":53"
}
name = address
spl = strings.Split(address, ":")
host, port := spl[0], spl[1]
if ip := net.ParseIP(host); ip == nil { // dns server specified using DNS name
ips, err := net.DefaultResolver.LookupIP(context.Background(), "ip", host)
if err != nil {
l.Fatalw("could not resolve dns server ip address", "host", host, err)
}
if len(ips) > 1 {
l.Warnw("multiple DNS server IP resolved from host. arbitrarily picking the first resolved ip", "host", host, "resolved_ips", ips)
}
address = fmt.Sprintf("%v:%s", ips[0], port)
}
l.Infow("added a new DNS server", "name", name, "network", network, "address", address)
dnsServers = append(dnsServers, DNSServer{
network: network,
address: address,
name: name,
})
}
return dnsServers
}
func main() {
fs := flag.NewFlagSet("hostlookuper", flag.ExitOnError)
var (
debug = fs.Bool("debug", false, "enable verbose logging")
interval = fs.Duration("interval", 5*time.Second, "interval between DNS checks. must be in Go time.ParseDuration format, e.g. 5s or 5m or 1h, etc")
timeout = fs.Duration("timeout", 5*time.Second, "maximum timeout for a DNS query. must be in Go time.ParseDuration format, e.g. 5s or 5m or 1h, etc")
listen = fs.String("listen", ":9090", "address on which hostlookuper listens. e.g. 0.0.0.0:9090")
hostsVal = fs.String("hosts", "google.ch,ch.ch", "comma-separated list of hosts against which to perform DNS lookups")
dnsServersVal = fs.String("dns-servers", "udp://9.9.9.9:53,udp://8.8.8.8:53,udp://one.one.one.one:53", "comma-separated list of DNS servers. if the protocol is omitted, udp is implied, and if the port is omitted, 53 is implied")
)
err := ff.Parse(fs, os.Args[1:], ff.WithEnvVarPrefix("HOSTLOOKUPER"))
if err != nil {
fmt.Printf("unable to parse args/envs, exiting. error message: %v", err)
os.Exit(2)
}
logger := flash.New(flash.WithoutCaller())
logger.SetDebug(*debug)
l := logger.Get()
var hosts hosts = strings.Split(*hostsVal, ",")
if err := hosts.isValid(); err != nil {
l.Fatalw("parsing hosts failed",
"val", hostsVal,
"err", err,
)
}
dnsServers := parseDNSServers(l, *dnsServersVal)
for _, host := range hosts {
for _, dnsServer := range dnsServers {
look := newLookuper(host, dnsServer, timeout, l)
go func() {
look.start(*interval)
}()
}
}
http.HandleFunc("/metrics", func(w http.ResponseWriter, _ *http.Request) {
metrics.WritePrometheus(w, false)
})
l.Infow("starting server",
"listen", listen,
"interval", interval,
"hosts", hostsVal,
"version", version,
"commit", commit,
"date", date,
)
srv := &http.Server{
ReadTimeout: 1 * time.Second,
WriteTimeout: 1 * time.Second,
IdleTimeout: 30 * time.Second,
ReadHeaderTimeout: 2 * time.Second,
Handler: http.DefaultServeMux,
Addr: *listen,
}
l.Fatal(srv.ListenAndServe())
}
type lookuper struct {
host string
l *zap.SugaredLogger
c *dns.Client
dnsServer DNSServer
labels string
}
func newLookuper(host string, dnsServer DNSServer, timeout *time.Duration, log *zap.SugaredLogger) *lookuper {
c := dns.Client{
Net: dnsServer.network,
Timeout: *timeout,
}
return &lookuper{
host: host,
labels: fmt.Sprintf("host=%q,dns_server=%q", host, dnsServer),
l: log.With("host", host, "dnsServer", dnsServer),
c: &c,
dnsServer: dnsServer,
}
}
func (l *lookuper) start(interval time.Duration) {
//nolint:gosec // No need for a cryptographic secure random number since this is only used for a jitter.
jitter := time.Duration(rand.Float64() * float64(500*time.Millisecond))
l.l.Infow("start delayed",
"jitter", jitter,
)
metrics.GetOrCreateCounter(fmt.Sprintf("%s{%s}", dnsErrorsTotalName, l.labels)).Set(0)
time.Sleep(jitter)
ticker := time.NewTicker(interval)
defer ticker.Stop()
for range ticker.C {
l.l.Debug("lookup host")
m := new(dns.Msg)
m.SetQuestion(fmt.Sprintf("%s.", l.host), dns.TypeA)
msg, rtt, err := l.c.Exchange(m, l.dnsServer.address)
if err != nil {
metrics.GetOrCreateCounter(fmt.Sprintf("%s{%s}", dnsLookupTotalName, l.labels)).Inc()
metrics.GetOrCreateCounter(fmt.Sprintf("%s{%s}", dnsErrorsTotalName, l.labels)).Inc()
l.l.Errorw("dns lookup failed",
"host", l.host,
"time", rtt,
"err", err,
)
continue
}
rcodeStr, ok := dns.RcodeToString[msg.Rcode]
if !ok { // if rcode not known in table.
rcodeStr = fmt.Sprintf("%#x", msg.Rcode)
}
metrics.GetOrCreateCounter(fmt.Sprintf("%s{%s,rcode=%q}",
dnsLookupTotalName, l.labels, rcodeStr)).Inc()
metrics.GetOrCreateHistogram(fmt.Sprintf("%s{%s}",
dnsDurationName, l.labels)).Update(rtt.Seconds())
l.l.Debugw("lookup result",
"time", rtt,
"result length", len(msg.Answer),
)
}
}
type hosts []string
func (h hosts) isValid() error {
for _, host := range h {
if _, err := net.LookupHost(host); err != nil {
return fmt.Errorf("host %s is not valid: %s", host, err)
}
}
return nil
}