Query servers in parallel in dns_query input (#4754)

This commit is contained in:
Lee Jaeyong 2018-09-28 09:26:36 +09:00 committed by Daniel Nelson
parent af0ef55c02
commit 7d97ae6421
1 changed files with 23 additions and 17 deletions

View File

@ -5,6 +5,7 @@ import (
"fmt" "fmt"
"net" "net"
"strconv" "strconv"
"sync"
"time" "time"
"github.com/miekg/dns" "github.com/miekg/dns"
@ -70,32 +71,37 @@ func (d *DnsQuery) Description() string {
return "Query given DNS server and gives statistics" return "Query given DNS server and gives statistics"
} }
func (d *DnsQuery) Gather(acc telegraf.Accumulator) error { func (d *DnsQuery) Gather(acc telegraf.Accumulator) error {
var wg sync.WaitGroup
d.setDefaultValues() d.setDefaultValues()
for _, domain := range d.Domains { for _, domain := range d.Domains {
for _, server := range d.Servers { for _, server := range d.Servers {
fields := make(map[string]interface{}, 2) wg.Add(1)
tags := map[string]string{ go func(domain, server string) {
"server": server, fields := make(map[string]interface{}, 2)
"domain": domain, tags := map[string]string{
"record_type": d.RecordType, "server": server,
} "domain": domain,
"record_type": d.RecordType,
}
dnsQueryTime, err := d.getDnsQueryTime(domain, server) dnsQueryTime, err := d.getDnsQueryTime(domain, server)
if err == nil { if err == nil {
setResult(Success, fields, tags) setResult(Success, fields, tags)
fields["query_time_ms"] = dnsQueryTime fields["query_time_ms"] = dnsQueryTime
} else if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() { } else if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() {
setResult(Timeout, fields, tags) setResult(Timeout, fields, tags)
} else if err != nil { } else if err != nil {
setResult(Error, fields, tags) setResult(Error, fields, tags)
acc.AddError(err) acc.AddError(err)
} }
acc.AddFields("dns_query", fields, tags) acc.AddFields("dns_query", fields, tags)
}(domain, server)
} }
} }
wg.Wait()
return nil return nil
} }