Skip to content

Commit

Permalink
Add network option to dns_query (influxdata#3042)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielnelson authored and jeichorn committed Jul 24, 2017
1 parent 8f69476 commit 418780c
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 20 deletions.
24 changes: 14 additions & 10 deletions plugins/inputs/dns_query/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,23 @@ The DNS plugin gathers dns query times in miliseconds - like [Dig](https://en.wi
# Sample Config:
[[inputs.dns_query]]
## servers to query
servers = ["8.8.8.8"] # required
servers = ["8.8.8.8"]
## Domains or subdomains to query. "." (root) is default
domains = ["."] # optional
## Network is the network protocol name.
# network = "udp"
## Query record type. Posible values: A, AAAA, ANY, CNAME, MX, NS, PTR, SOA, SPF, SRV, TXT. Default is "NS"
record_type = "A" # optional
## Domains or subdomains to query.
# domains = ["."]
## Dns server port. 53 is default
port = 53 # optional
## Query record type.
## Posible values: A, AAAA, CNAME, MX, NS, PTR, TXT, SOA, SPF, SRV.
# record_type = "A"
## Query timeout in seconds. Default is 2 seconds
timeout = 2 # optional
## Dns server port.
# port = 53
## Query timeout in seconds.
# timeout = 2
```

For querying more than one record type make:
Expand All @@ -46,6 +50,6 @@ For querying more than one record type make:
### Example output:

```
./telegraf --config telegraf.conf --input-filter dns_query --test
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
```
32 changes: 22 additions & 10 deletions plugins/inputs/dns_query/dns_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package dns_query
import (
"errors"
"fmt"
"github.com/miekg/dns"
"net"
"strconv"
"time"

"github.com/miekg/dns"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/plugins/inputs"
)
Expand All @@ -16,6 +17,9 @@ type DnsQuery struct {
// Domains or subdomains to query
Domains []string

// Network protocl name
Network string

// Server to query
Servers []string

Expand All @@ -31,20 +35,23 @@ type DnsQuery struct {

var sampleConfig = `
## servers to query
servers = ["8.8.8.8"] # required
servers = ["8.8.8.8"]
## Domains or subdomains to query. "."(root) is default
domains = ["."] # optional
## Network is the network protocol name.
# network = "udp"
## Query record type. Default is "A"
## Domains or subdomains to query.
# domains = ["."]
## Query record type.
## Posible values: A, AAAA, CNAME, MX, NS, PTR, TXT, SOA, SPF, SRV.
record_type = "A" # optional
# record_type = "A"
## Dns server port. 53 is default
port = 53 # optional
## Dns server port.
# port = 53
## Query timeout in seconds. Default is 2 seconds
timeout = 2 # optional
## Query timeout in seconds.
# timeout = 2
`

func (d *DnsQuery) SampleConfig() string {
Expand Down Expand Up @@ -76,6 +83,10 @@ func (d *DnsQuery) Gather(acc telegraf.Accumulator) error {
}

func (d *DnsQuery) setDefaultValues() {
if d.Network == "" {
d.Network = "udp"
}

if len(d.RecordType) == 0 {
d.RecordType = "NS"
}
Expand All @@ -99,6 +110,7 @@ func (d *DnsQuery) getDnsQueryTime(domain string, server string) (float64, error

c := new(dns.Client)
c.ReadTimeout = time.Duration(d.Timeout) * time.Second
c.Net = d.Network

m := new(dns.Msg)
recordType, err := d.parseRecordType()
Expand Down

0 comments on commit 418780c

Please sign in to comment.