summaryrefslogtreecommitdiffstats
path: root/src/go/collectors/go.d.plugin/modules/dnsquery/init.go
blob: 05dad4ac1bd057dcf579b059269d4d910eab6d93 (plain)
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
// SPDX-License-Identifier: GPL-3.0-or-later

package dnsquery

import (
	"errors"
	"fmt"
	"github.com/netdata/netdata/go/go.d.plugin/agent/module"

	"github.com/miekg/dns"
)

func (d *DNSQuery) verifyConfig() error {
	if len(d.Domains) == 0 {
		return errors.New("no domains specified")
	}

	if len(d.Servers) == 0 {
		return errors.New("no servers specified")
	}

	if !(d.Network == "" || d.Network == "udp" || d.Network == "tcp" || d.Network == "tcp-tls") {
		return fmt.Errorf("wrong network transport : %s", d.Network)
	}

	if d.RecordType != "" {
		d.Warning("'record_type' config option is deprecated, use 'record_types' instead")
		d.RecordTypes = append(d.RecordTypes, d.RecordType)
	}

	if len(d.RecordTypes) == 0 {
		return errors.New("no record types specified")
	}

	return nil
}

func (d *DNSQuery) initRecordTypes() (map[string]uint16, error) {
	types := make(map[string]uint16)
	for _, v := range d.RecordTypes {
		rtype, err := parseRecordType(v)
		if err != nil {
			return nil, err
		}
		types[v] = rtype

	}

	return types, nil
}

func (d *DNSQuery) initCharts() (*module.Charts, error) {
	var charts module.Charts

	for _, srv := range d.Servers {
		for _, rtype := range d.RecordTypes {
			cs := newDNSServerCharts(srv, d.Network, rtype)
			if err := charts.Add(*cs...); err != nil {
				return nil, err
			}
		}
	}

	return &charts, nil
}

func parseRecordType(recordType string) (uint16, error) {
	var rtype uint16

	switch recordType {
	case "A":
		rtype = dns.TypeA
	case "AAAA":
		rtype = dns.TypeAAAA
	case "ANY":
		rtype = dns.TypeANY
	case "CNAME":
		rtype = dns.TypeCNAME
	case "MX":
		rtype = dns.TypeMX
	case "NS":
		rtype = dns.TypeNS
	case "PTR":
		rtype = dns.TypePTR
	case "SOA":
		rtype = dns.TypeSOA
	case "SPF":
		rtype = dns.TypeSPF
	case "SRV":
		rtype = dns.TypeSRV
	case "TXT":
		rtype = dns.TypeTXT
	default:
		return 0, fmt.Errorf("unknown record type : %s", recordType)
	}

	return rtype, nil
}