Skip to content

Commit

Permalink
dns_query plugin fixups:
Browse files Browse the repository at this point in the history
- renamed plugin to dns_query
- domains are optional
- new record types

closes influxdata#694
  • Loading branch information
Marcin Jasion authored and geodimm committed Mar 10, 2016
1 parent 9ff5ae1 commit e56ab2b
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 58 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Features
- [#727](https://github.com/influxdata/telegraf/pull/727): riak input, thanks @jcoene!
- [#694](https://github.com/influxdata/telegraf/pull/694): DNS Query input, thanks @mjasion!

### Bugfixes

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,4 @@ test-short: vet
vet:
go vet ./...

.PHONY: test
.PHONY: test test-short vet build default
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,7 @@ Currently implemented sources:
* bcache
* couchdb
* disque
* dns
* query time
* dns query time
* docker
* dovecot
* elasticsearch
Expand Down
2 changes: 1 addition & 1 deletion plugins/inputs/all/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
_ "github.com/influxdata/telegraf/plugins/inputs/bcache"
_ "github.com/influxdata/telegraf/plugins/inputs/couchdb"
_ "github.com/influxdata/telegraf/plugins/inputs/disque"
_ "github.com/influxdata/telegraf/plugins/inputs/dns"
_ "github.com/influxdata/telegraf/plugins/inputs/dns_query"
_ "github.com/influxdata/telegraf/plugins/inputs/docker"
_ "github.com/influxdata/telegraf/plugins/inputs/dovecot"
_ "github.com/influxdata/telegraf/plugins/inputs/elasticsearch"
Expand Down
28 changes: 14 additions & 14 deletions plugins/inputs/dns/README.md → plugins/inputs/dns_query/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,45 @@ The DNS plugin gathers dns query times in miliseconds - like [Dig](https://en.wi
```
# Sample Config:
[[inputs.dns_query]]
### Domains or subdomains to query
domains = ["mjasion.pl"] # required
### servers to query
## servers to query
servers = ["8.8.8.8"] # required
### Query record type. Posible values: A, CNAME, MX, TXT, NS. Default is "A"
recordType = "A" # optional
## Domains or subdomains to query. "." (root) is default
domains = ["."] # optional
## Query record type. Posible values: A, AAAA, ANY, CNAME, MX, NS, PTR, SOA, SPF, SRV, TXT. Default is "NS"
record_type = "A" # optional
### Dns server port. 53 is default
## Dns server port. 53 is default
port = 53 # optional
### Query timeout in seconds. Default is 2 seconds
## Query timeout in seconds. Default is 2 seconds
timeout = 2 # optional
```

For querying more than one record type make:

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

### Tags:

- server
- server
- domain
- recordType
- record_type

### Example output:

```
./telegraf -config telegraf.conf -test -input-filter dns_query -test
> dns,domain=mjasion.pl,record_type=A,server=8.8.8.8 query_time_ms=36.327025 1455548824989943491
> dns_query,domain=mjasion.pl,record_type=A,server=8.8.8.8 query_time_ms=67.189842 1456082743585760680
```
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dns
package dns_query

import (
"errors"
Expand All @@ -11,7 +11,7 @@ import (
"time"
)

type Dns struct {
type DnsQuery struct {
// Domains or subdomains to query
Domains []string

Expand All @@ -29,30 +29,30 @@ type Dns struct {
}

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

func (d *Dns) SampleConfig() string {
func (d *DnsQuery) SampleConfig() string {
return sampleConfig
}

func (d *Dns) Description() string {
func (d *DnsQuery) Description() string {
return "Query given DNS server and gives statistics"
}
func (d *Dns) Gather(acc telegraf.Accumulator) error {
func (d *DnsQuery) Gather(acc telegraf.Accumulator) error {
d.setDefaultValues()
for _, domain := range d.Domains {
for _, server := range d.Servers {
Expand All @@ -67,26 +67,33 @@ func (d *Dns) Gather(acc telegraf.Accumulator) error {
}

fields := map[string]interface{}{"query_time_ms": dnsQueryTime}
acc.AddFields("dns", fields, tags)
acc.AddFields("dns_query", fields, tags)
}
}

return nil
}

func (d *Dns) setDefaultValues() {
func (d *DnsQuery) setDefaultValues() {
if len(d.RecordType) == 0 {
d.RecordType = "A"
d.RecordType = "NS"
}

if len(d.Domains) == 0 {
d.Domains = []string{"."}
d.RecordType = "NS"
}

if d.Port == 0 {
d.Port = 53
}

if d.Timeout == 0 {
d.Timeout = 2
}
}

func (d *Dns) getDnsQueryTime(domain string, server string) (float64, error) {
func (d *DnsQuery) getDnsQueryTime(domain string, server string) (float64, error) {
dnsQueryTime := float64(0)

c := new(dns.Client)
Expand All @@ -111,7 +118,7 @@ func (d *Dns) getDnsQueryTime(domain string, server string) (float64, error) {
return dnsQueryTime, nil
}

func (d *Dns) parseRecordType() (uint16, error) {
func (d *DnsQuery) parseRecordType() (uint16, error) {
var recordType uint16
var error error

Expand All @@ -128,6 +135,14 @@ func (d *Dns) parseRecordType() (uint16, error) {
recordType = dns.TypeMX
case "NS":
recordType = dns.TypeNS
case "PTR":
recordType = dns.TypePTR
case "SOA":
recordType = dns.TypeSOA
case "SPF":
recordType = dns.TypeSPF
case "SRV":
recordType = dns.TypeSRV
case "TXT":
recordType = dns.TypeTXT
default:
Expand All @@ -139,6 +154,6 @@ func (d *Dns) parseRecordType() (uint16, error) {

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

0 comments on commit e56ab2b

Please sign in to comment.