summaryrefslogtreecommitdiffstats
path: root/src/go/plugin/go.d/modules/dnsmasq/collect.go
blob: 9f3f963f0209aaaf638f3ac209647a9ea6ffcf95 (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
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
// SPDX-License-Identifier: GPL-3.0-or-later

package dnsmasq

import (
	"fmt"
	"strconv"
	"strings"

	"github.com/miekg/dns"
)

func (d *Dnsmasq) collect() (map[string]int64, error) {
	mx := make(map[string]int64)

	if err := d.collectCacheStatistics(mx); err != nil {
		return nil, err
	}

	return mx, nil
}

func (d *Dnsmasq) collectCacheStatistics(mx map[string]int64) error {
	/*
		;; flags: qr aa rd ra; QUERY: 7, ANSWER: 7, AUTHORITY: 0, ADDITIONAL: 0

		;; QUESTION SECTION:
		;cachesize.bind.	CH	 TXT
		;insertions.bind.	CH	 TXT
		;evictions.bind.	CH	 TXT
		;hits.bind.			CH	 TXT
		;misses.bind.		CH	 TXT
		;auth.bind.			CH	 TXT
		;servers.bind.		CH	 TXT

		;; ANSWER SECTION:
		cachesize.bind.		0	CH	TXT	"150"
		insertions.bind.	0	CH	TXT	"1"
		evictions.bind.		0	CH	TXT	"0"
		hits.bind.			0	CH	TXT	"176"
		misses.bind.		0	CH	TXT	"4"
		auth.bind.			0	CH	TXT	"0"
		servers.bind.		0	CH	TXT	"10.0.0.1#53 0 0" "1.1.1.1#53 4 3" "1.0.0.1#53 3 0"
	*/

	questions := []string{
		"servers.bind.",
		"cachesize.bind.",
		"insertions.bind.",
		"evictions.bind.",
		"hits.bind.",
		"misses.bind.",
		// auth.bind query is only supported if dnsmasq has been built to support running as an authoritative name server
		// See https://github.com/netdata/netdata/issues/13766
		//"auth.bind.",
	}

	for _, q := range questions {
		resp, err := d.query(q)
		if err != nil {
			return err
		}

		for _, a := range resp.Answer {
			txt, ok := a.(*dns.TXT)
			if !ok {
				continue
			}

			idx := strings.IndexByte(txt.Hdr.Name, '.')
			if idx == -1 {
				continue
			}

			name := txt.Hdr.Name[:idx]

			switch name {
			case "servers":
				for _, entry := range txt.Txt {
					parts := strings.Fields(entry)
					if len(parts) != 3 {
						return fmt.Errorf("parse %s (%s): unexpected format", txt.Hdr.Name, entry)
					}
					queries, err := strconv.ParseFloat(parts[1], 64)
					if err != nil {
						return fmt.Errorf("parse '%s' (%s): %v", txt.Hdr.Name, entry, err)
					}
					failedQueries, err := strconv.ParseFloat(parts[2], 64)
					if err != nil {
						return fmt.Errorf("parse '%s' (%s): %v", txt.Hdr.Name, entry, err)
					}

					mx["queries"] += int64(queries)
					mx["failed_queries"] += int64(failedQueries)
				}
			case "cachesize", "insertions", "evictions", "hits", "misses", "auth":
				if len(txt.Txt) != 1 {
					return fmt.Errorf("parse '%s' (%v): unexpected format", txt.Hdr.Name, txt.Txt)
				}
				v, err := strconv.ParseFloat(txt.Txt[0], 64)
				if err != nil {
					return fmt.Errorf("parse '%s' (%s): %v", txt.Hdr.Name, txt.Txt[0], err)
				}

				mx[name] = int64(v)
			}
		}
	}

	return nil
}

func (d *Dnsmasq) query(question string) (*dns.Msg, error) {
	msg := &dns.Msg{
		MsgHdr: dns.MsgHdr{
			Id:               dns.Id(),
			RecursionDesired: true,
		},
		Question: []dns.Question{
			{Name: question, Qtype: dns.TypeTXT, Qclass: dns.ClassCHAOS},
		},
	}

	r, _, err := d.dnsClient.Exchange(msg, d.Address)
	if err != nil {
		return nil, err
	}

	if r == nil {
		return nil, fmt.Errorf("'%s' question '%s', returned an empty response", d.Address, question)
	}

	if r.Rcode != dns.RcodeSuccess {
		s := dns.RcodeToString[r.Rcode]
		return nil, fmt.Errorf("'%s' question '%s' returned '%s' (%d) response code", d.Address, question, s, r.Rcode)
	}

	return r, nil
}