Skip to content

Commit

Permalink
Don't report 0ms on timeout in dns_query (#4118)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnelson committed May 8, 2018
1 parent ce58926 commit c16ecaa
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 27 deletions.
38 changes: 14 additions & 24 deletions plugins/inputs/dns_query/README.md
Expand Up @@ -3,9 +3,8 @@
The DNS plugin gathers dns query times in miliseconds - like [Dig](https://en.wikipedia.org/wiki/Dig_\(command\))

### Configuration:

```
# Sample Config:
```toml
# Query given DNS server and gives statistics
[[inputs.dns_query]]
## servers to query
servers = ["8.8.8.8"]
Expand All @@ -27,29 +26,20 @@ The DNS plugin gathers dns query times in miliseconds - like [Dig](https://en.wi
# timeout = 2
```

For querying more than one record type make:

```
[[inputs.dns_query]]
domains = ["mjasion.pl"]
servers = ["8.8.8.8", "8.8.4.4"]
record_type = "A"
[[inputs.dns_query]]
domains = ["mjasion.pl"]
servers = ["8.8.8.8", "8.8.4.4"]
record_type = "MX"
```

### Tags:
### Metrics:

- server
- domain
- record_type
- dns_query
- tags:
- server
- domain
- record_type
- result
- fields:
- query_time_ms (float)
- result_code (int, success = 0, timeout = 1, error = 2)

### Example output:
### Example Output:

```
telegraf --input-filter dns_query --test
> dns_query,domain=mjasion.pl,record_type=A,server=8.8.8.8 query_time_ms=67.189842 1456082743585760680
dns_query,domain=mjasion.pl,record_type=A,server=8.8.8.8 query_time_ms=67.189842 1456082743585760680
```
38 changes: 35 additions & 3 deletions plugins/inputs/dns_query/dns_query.go
Expand Up @@ -13,6 +13,14 @@ import (
"github.com/influxdata/telegraf/plugins/inputs"
)

type ResultType uint64

const (
Success ResultType = 0
Timeout = 1
Error = 2
)

type DnsQuery struct {
// Domains or subdomains to query
Domains []string
Expand Down Expand Up @@ -66,15 +74,24 @@ func (d *DnsQuery) Gather(acc telegraf.Accumulator) error {

for _, domain := range d.Domains {
for _, server := range d.Servers {
dnsQueryTime, err := d.getDnsQueryTime(domain, server)
acc.AddError(err)
fields := make(map[string]interface{}, 2)
tags := map[string]string{
"server": server,
"domain": domain,
"record_type": d.RecordType,
}

fields := map[string]interface{}{"query_time_ms": dnsQueryTime}
dnsQueryTime, err := d.getDnsQueryTime(domain, server)
if err == nil {
setResult(Success, fields, tags)
fields["query_time_ms"] = dnsQueryTime
} else if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() {
setResult(Timeout, fields, tags)
} else if err != nil {
setResult(Error, fields, tags)
acc.AddError(err)
}

acc.AddFields("dns_query", fields, tags)
}
}
Expand Down Expand Up @@ -165,6 +182,21 @@ func (d *DnsQuery) parseRecordType() (uint16, error) {
return recordType, error
}

func setResult(result ResultType, fields map[string]interface{}, tags map[string]string) {
var tag string
switch result {
case Success:
tag = "success"
case Timeout:
tag = "timeout"
case Error:
tag = "error"
}

tags["result"] = tag
fields["result_code"] = uint64(result)
}

func init() {
inputs.Add("dns_query", func() telegraf.Input {
return &DnsQuery{}
Expand Down

0 comments on commit c16ecaa

Please sign in to comment.