-
Notifications
You must be signed in to change notification settings - Fork 24
/
dns.go
87 lines (69 loc) · 2.75 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
package dns
import (
"context"
"errors"
"strings"
"time"
"github.com/grafana/synthetic-monitoring-agent/internal/prober/logger"
sm "github.com/grafana/synthetic-monitoring-agent/pkg/pb/synthetic_monitoring"
"github.com/prometheus/blackbox_exporter/config"
bbeprober "github.com/prometheus/blackbox_exporter/prober"
"github.com/prometheus/client_golang/prometheus"
)
var errUnsupportedCheck = errors.New("unsupported check")
type Prober struct {
target string
config config.Module
}
func NewProber(check sm.Check) (Prober, error) {
if check.Settings.Dns == nil {
return Prober{}, errUnsupportedCheck
}
cfg := settingsToModule(check.Settings.Dns, check.Target)
cfg.Timeout = time.Duration(check.Timeout) * time.Millisecond
return Prober{
target: check.Settings.Dns.Server,
config: cfg,
}, nil
}
func (p Prober) Name() string {
return "dns"
}
func (p Prober) Probe(ctx context.Context, target string, registry *prometheus.Registry, logger logger.Logger) bool {
// The target of the BBE DNS check is the _DNS server_, while
// the target of the SM DNS check is the _query_, so we need
// pass the server as the target parameter, and ignore the
// _target_ paramater that is passed to this function.
return bbeprober.ProbeDNS(ctx, p.target, p.config, registry, logger)
}
func settingsToModule(settings *sm.DnsSettings, target string) config.Module {
var m config.Module
m.Prober = sm.CheckTypeDns.String()
m.DNS.IPProtocol, m.DNS.IPProtocolFallback = settings.IpVersion.ToIpProtocol()
// BBE dns_probe actually tests the DNS server, so we
// need to pass the query (e.g. www.grafana.com) as part
// of the configuration and the server as the target
// parameter.
m.DNS.QueryName = target
m.DNS.QueryType = settings.RecordType.String()
m.DNS.SourceIPAddress = settings.SourceIpAddress
// In the protobuffer definition the protocol is either
// "TCP" or "UDP", but blackbox-exporter wants "tcp" or
// "udp".
m.DNS.TransportProtocol = strings.ToLower(settings.Protocol.String())
m.DNS.Recursion = true
m.DNS.ValidRcodes = settings.ValidRCodes
if settings.ValidateAnswer != nil {
m.DNS.ValidateAnswer.FailIfMatchesRegexp = settings.ValidateAnswer.FailIfMatchesRegexp
m.DNS.ValidateAnswer.FailIfNotMatchesRegexp = settings.ValidateAnswer.FailIfNotMatchesRegexp
}
if settings.ValidateAuthority != nil {
m.DNS.ValidateAuthority.FailIfMatchesRegexp = settings.ValidateAuthority.FailIfMatchesRegexp
m.DNS.ValidateAuthority.FailIfNotMatchesRegexp = settings.ValidateAuthority.FailIfNotMatchesRegexp
}
if settings.ValidateAdditional != nil {
m.DNS.ValidateAdditional.FailIfMatchesRegexp = settings.ValidateAdditional.FailIfMatchesRegexp
m.DNS.ValidateAdditional.FailIfNotMatchesRegexp = settings.ValidateAdditional.FailIfNotMatchesRegexp
}
return m
}