Skip to content

Commit 7daa9dc

Browse files
glintonJean-Louis Dupond
authored andcommitted
Add rcode tag and field to dns_query input (influxdata#5417)
1 parent ff096ee commit 7daa9dc

File tree

2 files changed

+39
-7
lines changed

2 files changed

+39
-7
lines changed

plugins/inputs/dns_query/README.md

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,40 @@ The DNS plugin gathers dns query times in miliseconds - like [Dig](https://en.wi
3434
- domain
3535
- record_type
3636
- result
37+
- rcode
3738
- fields:
3839
- query_time_ms (float)
3940
- result_code (int, success = 0, timeout = 1, error = 2)
41+
- rcode_value (int)
42+
43+
44+
### Rcode Descriptions
45+
|rcode_value|rcode|Description|
46+
|---|-----------|-----------------------------------|
47+
|0 | NoError | No Error |
48+
|1 | FormErr | Format Error |
49+
|2 | ServFail | Server Failure |
50+
|3 | NXDomain | Non-Existent Domain |
51+
|4 | NotImp | Not Implemented |
52+
|5 | Refused | Query Refused |
53+
|6 | YXDomain | Name Exists when it should not |
54+
|7 | YXRRSet | RR Set Exists when it should not |
55+
|8 | NXRRSet | RR Set that should exist does not |
56+
|9 | NotAuth | Server Not Authoritative for zone |
57+
|10 | NotZone | Name not contained in zone |
58+
|16 | BADSIG | TSIG Signature Failure |
59+
|16 | BADVERS | Bad OPT Version |
60+
|17 | BADKEY | Key not recognized |
61+
|18 | BADTIME | Signature out of time window |
62+
|19 | BADMODE | Bad TKEY Mode |
63+
|20 | BADNAME | Duplicate key name |
64+
|21 | BADALG | Algorithm not supported |
65+
|22 | BADTRUNC | Bad Truncation |
66+
|23 | BADCOOKIE | Bad/missing Server Cookie |
67+
4068

4169
### Example Output:
4270

4371
```
44-
dns_query,domain=mjasion.pl,record_type=A,server=8.8.8.8 query_time_ms=67.189842 1456082743585760680
72+
dns_query,domain=google.com,rcode=NOERROR,record_type=A,result=success,server=127.0.0.1 rcode_value=0i,result_code=0i,query_time_ms=0.13746 1550020750001000000
4573
```

plugins/inputs/dns_query/dns_query.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,11 @@ func (d *DnsQuery) Gather(acc telegraf.Accumulator) error {
8585
"record_type": d.RecordType,
8686
}
8787

88-
dnsQueryTime, err := d.getDnsQueryTime(domain, server)
88+
dnsQueryTime, rcode, err := d.getDnsQueryTime(domain, server)
89+
if rcode >= 0 {
90+
tags["rcode"] = dns.RcodeToString[rcode]
91+
fields["rcode_value"] = rcode
92+
}
8993
if err == nil {
9094
setResult(Success, fields, tags)
9195
fields["query_time_ms"] = dnsQueryTime
@@ -130,7 +134,7 @@ func (d *DnsQuery) setDefaultValues() {
130134
}
131135
}
132136

133-
func (d *DnsQuery) getDnsQueryTime(domain string, server string) (float64, error) {
137+
func (d *DnsQuery) getDnsQueryTime(domain string, server string) (float64, int, error) {
134138
dnsQueryTime := float64(0)
135139

136140
c := new(dns.Client)
@@ -140,20 +144,20 @@ func (d *DnsQuery) getDnsQueryTime(domain string, server string) (float64, error
140144
m := new(dns.Msg)
141145
recordType, err := d.parseRecordType()
142146
if err != nil {
143-
return dnsQueryTime, err
147+
return dnsQueryTime, -1, err
144148
}
145149
m.SetQuestion(dns.Fqdn(domain), recordType)
146150
m.RecursionDesired = true
147151

148152
r, rtt, err := c.Exchange(m, net.JoinHostPort(server, strconv.Itoa(d.Port)))
149153
if err != nil {
150-
return dnsQueryTime, err
154+
return dnsQueryTime, -1, err
151155
}
152156
if r.Rcode != dns.RcodeSuccess {
153-
return dnsQueryTime, errors.New(fmt.Sprintf("Invalid answer name %s after %s query for %s\n", domain, d.RecordType, domain))
157+
return dnsQueryTime, r.Rcode, fmt.Errorf("Invalid answer (%s) from %s after %s query for %s", dns.RcodeToString[r.Rcode], server, d.RecordType, domain)
154158
}
155159
dnsQueryTime = float64(rtt.Nanoseconds()) / 1e6
156-
return dnsQueryTime, nil
160+
return dnsQueryTime, r.Rcode, nil
157161
}
158162

159163
func (d *DnsQuery) parseRecordType() (uint16, error) {

0 commit comments

Comments
 (0)