- renamed plugin to dns_query

- domains are optional
- new record types
This commit is contained in:
Marcin Jasion 2016-02-21 18:43:24 +01:00
parent adde8e672e
commit 7fe185f1a5
5 changed files with 110 additions and 47 deletions

View File

@ -157,8 +157,7 @@ Currently implemented sources:
* bcache * bcache
* couchdb * couchdb
* disque * disque
* dns * dns query time
* query time
* docker * docker
* dovecot * dovecot
* elasticsearch * elasticsearch

View File

@ -6,7 +6,7 @@ import (
_ "github.com/influxdata/telegraf/plugins/inputs/bcache" _ "github.com/influxdata/telegraf/plugins/inputs/bcache"
_ "github.com/influxdata/telegraf/plugins/inputs/couchdb" _ "github.com/influxdata/telegraf/plugins/inputs/couchdb"
_ "github.com/influxdata/telegraf/plugins/inputs/disque" _ "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/docker"
_ "github.com/influxdata/telegraf/plugins/inputs/dovecot" _ "github.com/influxdata/telegraf/plugins/inputs/dovecot"
_ "github.com/influxdata/telegraf/plugins/inputs/elasticsearch" _ "github.com/influxdata/telegraf/plugins/inputs/elasticsearch"

View File

@ -7,13 +7,13 @@ The DNS plugin gathers dns query times in miliseconds - like [Dig](https://en.wi
``` ```
# Sample Config: # Sample Config:
[[inputs.dns_query]] [[inputs.dns_query]]
### Domains or subdomains to query
domains = ["mjasion.pl"] # required
### servers to query ### servers to query
servers = ["8.8.8.8"] # required servers = ["8.8.8.8"] # required
### Query record type. Posible values: A, CNAME, MX, TXT, NS. Default is "A" ### Domains or subdomains to query. "."(root) is default
domains = ["mjasion.pl"] #optional
### Query record type. Posible values: A, AAAA, ANY, CNAME, MX, NS, PTR, SOA, SPF, SRV, TXT. Default is "NS"
recordType = "A" # optional recordType = "A" # optional
### Dns server port. 53 is default ### Dns server port. 53 is default
@ -47,5 +47,5 @@ For querying more than one record type make:
``` ```
./telegraf -config telegraf.conf -test -input-filter dns_query -test ./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
``` ```

View File

@ -1,4 +1,4 @@
package dns package dns_query
import ( import (
"errors" "errors"
@ -11,7 +11,7 @@ import (
"time" "time"
) )
type Dns struct { type DnsQuery struct {
// Domains or subdomains to query // Domains or subdomains to query
Domains []string Domains []string
@ -29,14 +29,14 @@ type Dns struct {
} }
var sampleConfig = ` var sampleConfig = `
### Domains or subdomains to query
domains = ["mjasion.pl"] # required
### servers to query ### servers to query
servers = ["8.8.8.8"] # required 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
record_type = "A" # optional domains = ["mjasion.pl"] #optional
### Query record type. Posible values: A, AAAA, CNAME, MX, NS, PTR, TXT, SOA, SPF, SRV. Default is "NS"
recordType = "A" # optional
### Dns server port. 53 is default ### Dns server port. 53 is default
port = 53 # optional port = 53 # optional
@ -45,14 +45,14 @@ var sampleConfig = `
timeout = 2 # optional timeout = 2 # optional
` `
func (d *Dns) SampleConfig() string { func (d *DnsQuery) SampleConfig() string {
return sampleConfig return sampleConfig
} }
func (d *Dns) Description() string { func (d *DnsQuery) Description() string {
return "Query given DNS server and gives statistics" 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() d.setDefaultValues()
for _, domain := range d.Domains { for _, domain := range d.Domains {
for _, server := range d.Servers { for _, server := range d.Servers {
@ -67,26 +67,33 @@ func (d *Dns) Gather(acc telegraf.Accumulator) error {
} }
fields := map[string]interface{}{"query_time_ms": dnsQueryTime} fields := map[string]interface{}{"query_time_ms": dnsQueryTime}
acc.AddFields("dns", fields, tags) acc.AddFields("dns_query", fields, tags)
} }
} }
return nil return nil
} }
func (d *Dns) setDefaultValues() { func (d *DnsQuery) setDefaultValues() {
if len(d.RecordType) == 0 { 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 { if d.Port == 0 {
d.Port = 53 d.Port = 53
} }
if d.Timeout == 0 { if d.Timeout == 0 {
d.Timeout = 2 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) dnsQueryTime := float64(0)
c := new(dns.Client) c := new(dns.Client)
@ -111,7 +118,7 @@ func (d *Dns) getDnsQueryTime(domain string, server string) (float64, error) {
return dnsQueryTime, nil return dnsQueryTime, nil
} }
func (d *Dns) parseRecordType() (uint16, error) { func (d *DnsQuery) parseRecordType() (uint16, error) {
var recordType uint16 var recordType uint16
var error error var error error
@ -128,6 +135,14 @@ func (d *Dns) parseRecordType() (uint16, error) {
recordType = dns.TypeMX recordType = dns.TypeMX
case "NS": case "NS":
recordType = dns.TypeNS 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": case "TXT":
recordType = dns.TypeTXT recordType = dns.TypeTXT
default: default:
@ -139,6 +154,6 @@ func (d *Dns) parseRecordType() (uint16, error) {
func init() { func init() {
inputs.Add("dns_query", func() telegraf.Input { inputs.Add("dns_query", func() telegraf.Input {
return &Dns{} return &DnsQuery{}
}) })
} }

View File

@ -1,4 +1,4 @@
package dns package dns_query
import ( import (
"github.com/influxdata/telegraf/testutil" "github.com/influxdata/telegraf/testutil"
@ -12,21 +12,21 @@ var servers = []string{"8.8.8.8"}
var domains = []string{"mjasion.pl"} var domains = []string{"mjasion.pl"}
func TestGathering(t *testing.T) { func TestGathering(t *testing.T) {
var dnsConfig = Dns{ var dnsConfig = DnsQuery{
Servers: servers, Servers: servers,
Domains: domains, Domains: domains,
} }
var acc testutil.Accumulator var acc testutil.Accumulator
dnsConfig.Gather(&acc) dnsConfig.Gather(&acc)
metric, _ := acc.Get("dns") metric, _ := acc.Get("dns_query")
queryTime, _ := metric.Fields["query_time_ms"].(float64) queryTime, _ := metric.Fields["query_time_ms"].(float64)
assert.NotEqual(t, 0, queryTime) assert.NotEqual(t, 0, queryTime)
} }
func TestGatheringMxRecord(t *testing.T) { func TestGatheringMxRecord(t *testing.T) {
var dnsConfig = Dns{ var dnsConfig = DnsQuery{
Servers: servers, Servers: servers,
Domains: domains, Domains: domains,
} }
@ -34,14 +34,36 @@ func TestGatheringMxRecord(t *testing.T) {
dnsConfig.RecordType = "MX" dnsConfig.RecordType = "MX"
dnsConfig.Gather(&acc) dnsConfig.Gather(&acc)
metric, _ := acc.Get("dns") metric, _ := acc.Get("dns_query")
queryTime, _ := metric.Fields["query_time_ms"].(float64) queryTime, _ := metric.Fields["query_time_ms"].(float64)
assert.NotEqual(t, 0, queryTime) assert.NotEqual(t, 0, queryTime)
} }
func TestGatheringRootDomain(t *testing.T) {
var dnsConfig = DnsQuery{
Servers: servers,
Domains: []string{"."},
RecordType: "MX",
}
var acc testutil.Accumulator
tags := map[string]string{
"server": "8.8.8.8",
"domain": ".",
"record_type": "MX",
}
fields := map[string]interface{}{}
dnsConfig.Gather(&acc)
metric, _ := acc.Get("dns_query")
queryTime, _ := metric.Fields["query_time_ms"].(float64)
fields["query_time_ms"] = queryTime
acc.AssertContainsTaggedFields(t, "dns_query", fields, tags)
}
func TestMetricContainsServerAndDomainAndRecordTypeTags(t *testing.T) { func TestMetricContainsServerAndDomainAndRecordTypeTags(t *testing.T) {
var dnsConfig = Dns{ var dnsConfig = DnsQuery{
Servers: servers, Servers: servers,
Domains: domains, Domains: domains,
} }
@ -49,20 +71,20 @@ func TestMetricContainsServerAndDomainAndRecordTypeTags(t *testing.T) {
tags := map[string]string{ tags := map[string]string{
"server": "8.8.8.8", "server": "8.8.8.8",
"domain": "mjasion.pl", "domain": "mjasion.pl",
"record_type": "A", "record_type": "NS",
} }
fields := map[string]interface{}{} fields := map[string]interface{}{}
dnsConfig.Gather(&acc) dnsConfig.Gather(&acc)
metric, _ := acc.Get("dns") metric, _ := acc.Get("dns_query")
queryTime, _ := metric.Fields["query_time_ms"].(float64) queryTime, _ := metric.Fields["query_time_ms"].(float64)
fields["query_time_ms"] = queryTime fields["query_time_ms"] = queryTime
acc.AssertContainsTaggedFields(t, "dns", fields, tags) acc.AssertContainsTaggedFields(t, "dns_query", fields, tags)
} }
func TestGatheringTimeout(t *testing.T) { func TestGatheringTimeout(t *testing.T) {
var dnsConfig = Dns{ var dnsConfig = DnsQuery{
Servers: servers, Servers: servers,
Domains: domains, Domains: domains,
} }
@ -87,49 +109,76 @@ func TestGatheringTimeout(t *testing.T) {
} }
func TestSettingDefaultValues(t *testing.T) { func TestSettingDefaultValues(t *testing.T) {
dnsConfig := Dns{} dnsConfig := DnsQuery{}
dnsConfig.setDefaultValues() dnsConfig.setDefaultValues()
assert.Equal(t, "A", dnsConfig.RecordType, "Default record type not equal 'A'") assert.Equal(t, []string{"."}, dnsConfig.Domains, "Default domain not equal \".\"")
assert.Equal(t, "NS", dnsConfig.RecordType, "Default record type not equal 'NS'")
assert.Equal(t, 53, dnsConfig.Port, "Default port number not equal 53") assert.Equal(t, 53, dnsConfig.Port, "Default port number not equal 53")
assert.Equal(t, 2, dnsConfig.Timeout, "Default timeout not equal 2") assert.Equal(t, 2, dnsConfig.Timeout, "Default timeout not equal 2")
dnsConfig = DnsQuery{Domains: []string{"."}}
dnsConfig.setDefaultValues()
assert.Equal(t, "NS", dnsConfig.RecordType, "Default record type not equal 'NS'")
} }
func TestRecordTypeParser(t *testing.T) { func TestRecordTypeParser(t *testing.T) {
var dnsConfig = Dns{} var dnsConfig = DnsQuery{}
var recordType uint16 var recordType uint16
var err error
dnsConfig.RecordType = "A" dnsConfig.RecordType = "A"
recordType, err = dnsConfig.parseRecordType() recordType, _ = dnsConfig.parseRecordType()
assert.Equal(t, dns.TypeA, recordType) assert.Equal(t, dns.TypeA, recordType)
dnsConfig.RecordType = "AAAA" dnsConfig.RecordType = "AAAA"
recordType, err = dnsConfig.parseRecordType() recordType, _ = dnsConfig.parseRecordType()
assert.Equal(t, dns.TypeAAAA, recordType) assert.Equal(t, dns.TypeAAAA, recordType)
dnsConfig.RecordType = "ANY" dnsConfig.RecordType = "ANY"
recordType, err = dnsConfig.parseRecordType() recordType, _ = dnsConfig.parseRecordType()
assert.Equal(t, dns.TypeANY, recordType) assert.Equal(t, dns.TypeANY, recordType)
dnsConfig.RecordType = "CNAME" dnsConfig.RecordType = "CNAME"
recordType, err = dnsConfig.parseRecordType() recordType, _ = dnsConfig.parseRecordType()
assert.Equal(t, dns.TypeCNAME, recordType) assert.Equal(t, dns.TypeCNAME, recordType)
dnsConfig.RecordType = "MX" dnsConfig.RecordType = "MX"
recordType, err = dnsConfig.parseRecordType() recordType, _ = dnsConfig.parseRecordType()
assert.Equal(t, dns.TypeMX, recordType) assert.Equal(t, dns.TypeMX, recordType)
dnsConfig.RecordType = "NS" dnsConfig.RecordType = "NS"
recordType, err = dnsConfig.parseRecordType() recordType, _ = dnsConfig.parseRecordType()
assert.Equal(t, dns.TypeNS, recordType) assert.Equal(t, dns.TypeNS, recordType)
dnsConfig.RecordType = "PTR"
recordType, _ = dnsConfig.parseRecordType()
assert.Equal(t, dns.TypePTR, recordType)
dnsConfig.RecordType = "SOA"
recordType, _ = dnsConfig.parseRecordType()
assert.Equal(t, dns.TypeSOA, recordType)
dnsConfig.RecordType = "SPF"
recordType, _ = dnsConfig.parseRecordType()
assert.Equal(t, dns.TypeSPF, recordType)
dnsConfig.RecordType = "SRV"
recordType, _ = dnsConfig.parseRecordType()
assert.Equal(t, dns.TypeSRV, recordType)
dnsConfig.RecordType = "TXT" dnsConfig.RecordType = "TXT"
recordType, err = dnsConfig.parseRecordType() recordType, _ = dnsConfig.parseRecordType()
assert.Equal(t, dns.TypeTXT, recordType) assert.Equal(t, dns.TypeTXT, recordType)
}
func TestRecordTypeParserError(t *testing.T) {
var dnsConfig = DnsQuery{}
var err error
dnsConfig.RecordType = "nil" dnsConfig.RecordType = "nil"
recordType, err = dnsConfig.parseRecordType() _, err = dnsConfig.parseRecordType()
assert.Error(t, err) assert.Error(t, err)
} }