renamed plugin to dns_query and value to query_time_ms

small polishings

added more record types - AAAA and ANY
This commit is contained in:
Marcin Jasion 2016-02-15 16:10:04 +01:00 committed by Michele Fadda
parent 2ad3242559
commit d1785a7f15
4 changed files with 38 additions and 27 deletions

View File

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

View File

@ -1,4 +1,4 @@
# DNS Input Plugin # DNS Query Input Plugin
The DNS plugin gathers dns query times in miliseconds - like [Dig](https://en.wikipedia.org/wiki/Dig_\(command\)) The DNS plugin gathers dns query times in miliseconds - like [Dig](https://en.wikipedia.org/wiki/Dig_\(command\))
@ -6,7 +6,7 @@ The DNS plugin gathers dns query times in miliseconds - like [Dig](https://en.wi
``` ```
# Sample Config: # Sample Config:
[[inputs.dns]] [[inputs.dns_query]]
### Domains or subdomains to query ### Domains or subdomains to query
domains = ["mjasion.pl"] # required domains = ["mjasion.pl"] # required
@ -26,12 +26,12 @@ The DNS plugin gathers dns query times in miliseconds - like [Dig](https://en.wi
For querying more than one record type make: For querying more than one record type make:
``` ```
[[inputs.dns]] [[inputs.dns_query]]
domains = ["mjasion.pl"] domains = ["mjasion.pl"]
servers = ["8.8.8.8", "8.8.4.4"] servers = ["8.8.8.8", "8.8.4.4"]
recordType = "A" recordType = "A"
[[inputs.dns]] [[inputs.dns_query]]
domains = ["mjasion.pl"] domains = ["mjasion.pl"]
servers = ["8.8.8.8", "8.8.4.4"] servers = ["8.8.8.8", "8.8.4.4"]
recordType = "MX" recordType = "MX"
@ -46,6 +46,6 @@ For querying more than one record type make:
### Example output: ### Example output:
``` ```
./telegraf -config telegraf.conf -test -input-filter dns -test ./telegraf -config telegraf.conf -test -input-filter dns_query -test
> dns,domain=mjasion.pl,recordType=A,server=8.8.8.8 value=25.236181 1455452083165126877 > dns,domain=mjasion.pl,record_type=A,server=8.8.8.8 query_time_ms=36.327025 1455548824989943491
``` ```

View File

@ -19,7 +19,7 @@ type Dns struct {
Servers []string Servers []string
// Record type // Record type
RecordType string RecordType string `toml:"record_type"`
// DNS server port number // DNS server port number
Port int Port int
@ -35,8 +35,8 @@ var sampleConfig = `
### 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" ### Query record type. Posible values: A, AAAA, CNAME, MX, TXT, NS, ANY. Default is "A"
recordType = "A" # optional record_type = "A" # optional
### Dns server port. 53 is default ### Dns server port. 53 is default
port = 53 # optional port = 53 # optional
@ -63,10 +63,11 @@ func (d *Dns) Gather(acc telegraf.Accumulator) error {
tags := map[string]string{ tags := map[string]string{
"server": server, "server": server,
"domain": domain, "domain": domain,
"recordType": d.RecordType, "record_type": d.RecordType,
} }
acc.Add("dns", dnsQueryTime, tags) fields := map[string]interface{}{"query_time_ms": dnsQueryTime}
acc.AddFields("dns", fields, tags)
} }
} }
@ -99,18 +100,14 @@ func (d *Dns) getDnsQueryTime(domain string, server string) (float64, error) {
m.SetQuestion(dns.Fqdn(domain), recordType) m.SetQuestion(dns.Fqdn(domain), recordType)
m.RecursionDesired = true m.RecursionDesired = true
start_time := time.Now() r, rtt, err := c.Exchange(m, net.JoinHostPort(server, strconv.Itoa(d.Port)))
r, _, err := c.Exchange(m, net.JoinHostPort(server, strconv.Itoa(d.Port)))
queryDuration := time.Since(start_time)
if err != nil { if err != nil {
return dnsQueryTime, err return dnsQueryTime, err
} }
if r.Rcode != dns.RcodeSuccess { if r.Rcode != dns.RcodeSuccess {
return dnsQueryTime, errors.New(fmt.Sprintf("Invalid answer name %s after %s query for %s\n", domain, d.RecordType, domain)) return dnsQueryTime, errors.New(fmt.Sprintf("Invalid answer name %s after %s query for %s\n", domain, d.RecordType, domain))
} }
dnsQueryTime = float64(rtt.Nanoseconds()) / 1e6
dnsQueryTime = float64(queryDuration.Nanoseconds()) / 1e6
return dnsQueryTime, nil return dnsQueryTime, nil
} }
@ -121,6 +118,10 @@ func (d *Dns) parseRecordType() (uint16, error) {
switch d.RecordType { switch d.RecordType {
case "A": case "A":
recordType = dns.TypeA recordType = dns.TypeA
case "AAAA":
recordType = dns.TypeAAAA
case "ANY":
recordType = dns.TypeANY
case "CNAME": case "CNAME":
recordType = dns.TypeCNAME recordType = dns.TypeCNAME
case "MX": case "MX":
@ -137,7 +138,7 @@ func (d *Dns) parseRecordType() (uint16, error) {
} }
func init() { func init() {
inputs.Add("dns", func() telegraf.Input { inputs.Add("dns_query", func() telegraf.Input {
return &Dns{} return &Dns{}
}) })
} }

View File

@ -20,7 +20,7 @@ func TestGathering(t *testing.T) {
dnsConfig.Gather(&acc) dnsConfig.Gather(&acc)
metric, _ := acc.Get("dns") metric, _ := acc.Get("dns")
queryTime, _ := metric.Fields["value"].(float64) queryTime, _ := metric.Fields["query_time_ms"].(float64)
assert.NotEqual(t, 0, queryTime) assert.NotEqual(t, 0, queryTime)
} }
@ -35,7 +35,7 @@ func TestGatheringMxRecord(t *testing.T) {
dnsConfig.Gather(&acc) dnsConfig.Gather(&acc)
metric, _ := acc.Get("dns") metric, _ := acc.Get("dns")
queryTime, _ := metric.Fields["value"].(float64) queryTime, _ := metric.Fields["query_time_ms"].(float64)
assert.NotEqual(t, 0, queryTime) assert.NotEqual(t, 0, queryTime)
} }
@ -49,15 +49,15 @@ 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",
"recordType": "A", "record_type": "A",
} }
fields := map[string]interface{}{} fields := map[string]interface{}{}
dnsConfig.Gather(&acc) dnsConfig.Gather(&acc)
metric, _ := acc.Get("dns") metric, _ := acc.Get("dns")
queryTime, _ := metric.Fields["value"].(float64) queryTime, _ := metric.Fields["query_time_ms"].(float64)
fields["value"] = queryTime fields["query_time_ms"] = queryTime
acc.AssertContainsTaggedFields(t, "dns", fields, tags) acc.AssertContainsTaggedFields(t, "dns", fields, tags)
} }
@ -105,6 +105,14 @@ func TestRecordTypeParser(t *testing.T) {
recordType, err = dnsConfig.parseRecordType() recordType, err = dnsConfig.parseRecordType()
assert.Equal(t, dns.TypeA, recordType) assert.Equal(t, dns.TypeA, recordType)
dnsConfig.RecordType = "AAAA"
recordType, err = dnsConfig.parseRecordType()
assert.Equal(t, dns.TypeAAAA, recordType)
dnsConfig.RecordType = "ANY"
recordType, err = dnsConfig.parseRecordType()
assert.Equal(t, dns.TypeANY, recordType)
dnsConfig.RecordType = "CNAME" dnsConfig.RecordType = "CNAME"
recordType, err = dnsConfig.parseRecordType() recordType, err = dnsConfig.parseRecordType()
assert.Equal(t, dns.TypeCNAME, recordType) assert.Equal(t, dns.TypeCNAME, recordType)