diff --git a/README.md b/README.md index 9b1e7b171..34a3df7ed 100644 --- a/README.md +++ b/README.md @@ -157,6 +157,8 @@ Currently implemented sources: * bcache * couchdb * disque +* dns + * query time * docker * dovecot * elasticsearch diff --git a/plugins/inputs/dns/README.md b/plugins/inputs/dns/README.md index eb2f99d6c..564743d9c 100644 --- a/plugins/inputs/dns/README.md +++ b/plugins/inputs/dns/README.md @@ -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\)) @@ -6,7 +6,7 @@ The DNS plugin gathers dns query times in miliseconds - like [Dig](https://en.wi ``` # Sample Config: -[[inputs.dns]] +[[inputs.dns_query]] ### Domains or subdomains to query 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: ``` -[[inputs.dns]] +[[inputs.dns_query]] domains = ["mjasion.pl"] servers = ["8.8.8.8", "8.8.4.4"] recordType = "A" -[[inputs.dns]] +[[inputs.dns_query]] domains = ["mjasion.pl"] servers = ["8.8.8.8", "8.8.4.4"] recordType = "MX" @@ -46,6 +46,6 @@ For querying more than one record type make: ### Example output: ``` -./telegraf -config telegraf.conf -test -input-filter dns -test -> dns,domain=mjasion.pl,recordType=A,server=8.8.8.8 value=25.236181 1455452083165126877 +./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 ``` diff --git a/plugins/inputs/dns/dns.go b/plugins/inputs/dns/dns_query.go similarity index 79% rename from plugins/inputs/dns/dns.go rename to plugins/inputs/dns/dns_query.go index d8440f104..5ca58d880 100644 --- a/plugins/inputs/dns/dns.go +++ b/plugins/inputs/dns/dns_query.go @@ -19,7 +19,7 @@ type Dns struct { Servers []string // Record type - RecordType string + RecordType string `toml:"record_type"` // DNS server port number Port int @@ -35,8 +35,8 @@ var sampleConfig = ` ### 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 + ### Query record type. Posible values: A, AAAA, CNAME, MX, TXT, NS, ANY. Default is "A" + record_type = "A" # optional ### Dns server port. 53 is default port = 53 # optional @@ -61,12 +61,13 @@ func (d *Dns) Gather(acc telegraf.Accumulator) error { return err } tags := map[string]string{ - "server": server, - "domain": domain, - "recordType": d.RecordType, + "server": server, + "domain": domain, + "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.RecursionDesired = true - start_time := time.Now() - r, _, err := c.Exchange(m, net.JoinHostPort(server, strconv.Itoa(d.Port))) - queryDuration := time.Since(start_time) - + r, rtt, err := c.Exchange(m, net.JoinHostPort(server, strconv.Itoa(d.Port))) if err != nil { return dnsQueryTime, err } 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)) } - - dnsQueryTime = float64(queryDuration.Nanoseconds()) / 1e6 + dnsQueryTime = float64(rtt.Nanoseconds()) / 1e6 return dnsQueryTime, nil } @@ -121,6 +118,10 @@ func (d *Dns) parseRecordType() (uint16, error) { switch d.RecordType { case "A": recordType = dns.TypeA + case "AAAA": + recordType = dns.TypeAAAA + case "ANY": + recordType = dns.TypeANY case "CNAME": recordType = dns.TypeCNAME case "MX": @@ -137,7 +138,7 @@ func (d *Dns) parseRecordType() (uint16, error) { } func init() { - inputs.Add("dns", func() telegraf.Input { + inputs.Add("dns_query", func() telegraf.Input { return &Dns{} }) } diff --git a/plugins/inputs/dns/dns_test.go b/plugins/inputs/dns/dns_query_test.go similarity index 83% rename from plugins/inputs/dns/dns_test.go rename to plugins/inputs/dns/dns_query_test.go index 038ff8e40..44a62d708 100644 --- a/plugins/inputs/dns/dns_test.go +++ b/plugins/inputs/dns/dns_query_test.go @@ -20,7 +20,7 @@ func TestGathering(t *testing.T) { dnsConfig.Gather(&acc) metric, _ := acc.Get("dns") - queryTime, _ := metric.Fields["value"].(float64) + queryTime, _ := metric.Fields["query_time_ms"].(float64) assert.NotEqual(t, 0, queryTime) } @@ -35,7 +35,7 @@ func TestGatheringMxRecord(t *testing.T) { dnsConfig.Gather(&acc) metric, _ := acc.Get("dns") - queryTime, _ := metric.Fields["value"].(float64) + queryTime, _ := metric.Fields["query_time_ms"].(float64) assert.NotEqual(t, 0, queryTime) } @@ -47,17 +47,17 @@ func TestMetricContainsServerAndDomainAndRecordTypeTags(t *testing.T) { } var acc testutil.Accumulator tags := map[string]string{ - "server": "8.8.8.8", - "domain": "mjasion.pl", - "recordType": "A", + "server": "8.8.8.8", + "domain": "mjasion.pl", + "record_type": "A", } fields := map[string]interface{}{} dnsConfig.Gather(&acc) 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) } @@ -105,6 +105,14 @@ func TestRecordTypeParser(t *testing.T) { recordType, err = dnsConfig.parseRecordType() 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" recordType, err = dnsConfig.parseRecordType() assert.Equal(t, dns.TypeCNAME, recordType)