forked from prometheus/blackbox_exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dns.go
174 lines (157 loc) · 4.92 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
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
// Copyright 2016 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"net"
"net/http"
"regexp"
"github.com/miekg/dns"
"github.com/prometheus/common/log"
)
// validRRs checks a slice of RRs received from the server against a DNSRRValidator.
func validRRs(rrs *[]dns.RR, v *DNSRRValidator) bool {
// Fail the probe if there are no RRs of a given type, but a regexp match is required
// (i.e. FailIfNotMatchesRegexp is set).
if len(*rrs) == 0 && len(v.FailIfNotMatchesRegexp) > 0 {
return false
}
for _, rr := range *rrs {
log.Debugf("Validating RR: %q", rr)
for _, re := range v.FailIfMatchesRegexp {
match, err := regexp.MatchString(re, rr.String())
if err != nil {
log.Errorf("Error matching regexp %q: %s", re, err)
return false
}
if match {
return false
}
}
for _, re := range v.FailIfNotMatchesRegexp {
match, err := regexp.MatchString(re, rr.String())
if err != nil {
log.Errorf("Error matching regexp %q: %s", re, err)
return false
}
if !match {
return false
}
}
}
return true
}
// validRcode checks rcode in the response against a list of valid rcodes.
func validRcode(rcode int, valid []string) bool {
var validRcodes []int
// If no list of valid rcodes is specified, only NOERROR is considered valid.
if valid == nil {
validRcodes = append(validRcodes, dns.StringToRcode["NOERROR"])
} else {
for _, rcode := range valid {
rc, ok := dns.StringToRcode[rcode]
if !ok {
log.Errorf("Invalid rcode %v. Existing rcodes: %v", rcode, dns.RcodeToString)
return false
}
validRcodes = append(validRcodes, rc)
}
}
for _, rc := range validRcodes {
if rcode == rc {
return true
}
}
log.Debugf("%s (%d) is not one of the valid rcodes (%v)", dns.RcodeToString[rcode], rcode, validRcodes)
return false
}
func probeDNS(target string, w http.ResponseWriter, module Module) bool {
var numAnswer, numAuthority, numAdditional int
var dialProtocol, fallbackProtocol string
defer func() {
// These metrics can be used to build additional alerting based on the number of replies.
// They should be returned even in case of errors.
fmt.Fprintf(w, "probe_dns_answer_rrs %d\n", numAnswer)
fmt.Fprintf(w, "probe_dns_authority_rrs %d\n", numAuthority)
fmt.Fprintf(w, "probe_dns_additional_rrs %d\n", numAdditional)
}()
if module.DNS.Protocol == "" {
module.DNS.Protocol = "udp"
}
if (module.DNS.Protocol == "tcp" || module.DNS.Protocol == "udp") && module.DNS.PreferredIpProtocol == "" {
module.DNS.PreferredIpProtocol = "ip6"
}
if module.DNS.PreferredIpProtocol == "ip6" {
fallbackProtocol = "ip4"
} else {
fallbackProtocol = "ip6"
}
dialProtocol = module.DNS.Protocol
if module.DNS.Protocol == "udp" || module.DNS.Protocol == "tcp" {
target_address, _, _ := net.SplitHostPort(target)
ip, err := net.ResolveIPAddr(module.DNS.PreferredIpProtocol, target_address)
if err != nil {
ip, err = net.ResolveIPAddr(fallbackProtocol, target_address)
if err != nil {
return false
}
}
if ip.IP.To4() == nil {
dialProtocol = module.DNS.Protocol + "6"
} else {
dialProtocol = module.DNS.Protocol + "4"
}
}
if dialProtocol[len(dialProtocol)-1] == '6' {
fmt.Fprintf(w, "probe_ip_protocol 6\n")
} else {
fmt.Fprintf(w, "probe_ip_protocol 4\n")
}
client := new(dns.Client)
client.Net = dialProtocol
client.Timeout = module.Timeout
qt := dns.TypeANY
if module.DNS.QueryType != "" {
var ok bool
qt, ok = dns.StringToType[module.DNS.QueryType]
if !ok {
log.Errorf("Invalid type %v. Existing types: %v", module.DNS.QueryType, dns.TypeToString)
return false
}
}
msg := new(dns.Msg)
msg.SetQuestion(dns.Fqdn(module.DNS.QueryName), qt)
response, _, err := client.Exchange(msg, target)
if err != nil {
log.Warnf("Error while sending a DNS query: %s", err)
return false
}
log.Debugf("Got response: %#v", response)
numAnswer, numAuthority, numAdditional = len(response.Answer), len(response.Ns), len(response.Extra)
if !validRcode(response.Rcode, module.DNS.ValidRcodes) {
return false
}
if !validRRs(&response.Answer, &module.DNS.ValidateAnswer) {
log.Debugf("Answer RRs validation failed")
return false
}
if !validRRs(&response.Ns, &module.DNS.ValidateAuthority) {
log.Debugf("Authority RRs validation failed")
return false
}
if !validRRs(&response.Extra, &module.DNS.ValidateAdditional) {
log.Debugf("Additional RRs validation failed")
return false
}
return true
}