diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-05 11:19:16 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-07-24 09:53:24 +0000 |
commit | b5f8ee61a7f7e9bd291dd26b0585d03eb686c941 (patch) | |
tree | d4d31289c39fc00da064a825df13a0b98ce95b10 /src/go/collectors/go.d.plugin/modules/dnsquery/collect.go | |
parent | Adding upstream version 1.44.3. (diff) | |
download | netdata-b5f8ee61a7f7e9bd291dd26b0585d03eb686c941.tar.xz netdata-b5f8ee61a7f7e9bd291dd26b0585d03eb686c941.zip |
Adding upstream version 1.46.3.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | src/go/collectors/go.d.plugin/modules/dnsquery/collect.go | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/src/go/collectors/go.d.plugin/modules/dnsquery/collect.go b/src/go/collectors/go.d.plugin/modules/dnsquery/collect.go new file mode 100644 index 000000000..a98e37cad --- /dev/null +++ b/src/go/collectors/go.d.plugin/modules/dnsquery/collect.go @@ -0,0 +1,73 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + +package dnsquery + +import ( + "math/rand" + "net" + "strconv" + "sync" + "time" + + "github.com/miekg/dns" +) + +func (d *DNSQuery) collect() (map[string]int64, error) { + if d.dnsClient == nil { + d.dnsClient = d.newDNSClient(d.Network, d.Timeout.Duration()) + } + + mx := make(map[string]int64) + domain := randomDomain(d.Domains) + d.Debugf("current domain : %s", domain) + + var wg sync.WaitGroup + var mux sync.RWMutex + for _, srv := range d.Servers { + for rtypeName, rtype := range d.recordTypes { + wg.Add(1) + go func(srv, rtypeName string, rtype uint16, wg *sync.WaitGroup) { + defer wg.Done() + + msg := new(dns.Msg) + msg.SetQuestion(dns.Fqdn(domain), rtype) + address := net.JoinHostPort(srv, strconv.Itoa(d.Port)) + + resp, rtt, err := d.dnsClient.Exchange(msg, address) + + mux.Lock() + defer mux.Unlock() + + px := "server_" + srv + "_record_" + rtypeName + "_" + + mx[px+"query_status_success"] = 0 + mx[px+"query_status_network_error"] = 0 + mx[px+"query_status_dns_error"] = 0 + + if err != nil { + d.Debugf("error on querying %s after %s query for %s : %s", srv, rtypeName, domain, err) + mx[px+"query_status_network_error"] = 1 + return + } + + if resp != nil && resp.Rcode != dns.RcodeSuccess { + d.Debugf("invalid answer from %s after %s query for %s (rcode %d)", srv, rtypeName, domain, resp.Rcode) + mx[px+"query_status_dns_error"] = 1 + } else { + mx[px+"query_status_success"] = 1 + } + mx["server_"+srv+"_record_"+rtypeName+"_query_time"] = rtt.Nanoseconds() + + }(srv, rtypeName, rtype, &wg) + } + } + wg.Wait() + + return mx, nil +} + +func randomDomain(domains []string) string { + src := rand.NewSource(time.Now().UnixNano()) + r := rand.New(src) + return domains[r.Intn(len(domains))] +} |