Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add explicit network control option to dns_query #3042

Merged
merged 1 commit into from
Jul 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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