From cfba6f692e6cd1fcdb209e0bbc4a905cf3366542 Mon Sep 17 00:00:00 2001 From: lsy1990 Date: Tue, 26 Jul 2022 10:45:26 +0800 Subject: [PATCH 1/2] supply default dns server --- conf/input.dns_query/dns_query.toml | 3 +++ inputs/dns_query/dns_query.go | 19 ++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/conf/input.dns_query/dns_query.toml b/conf/input.dns_query/dns_query.toml index 825d8c1..d6fb555 100644 --- a/conf/input.dns_query/dns_query.toml +++ b/conf/input.dns_query/dns_query.toml @@ -8,6 +8,9 @@ # # interval = global.interval * interval_times # interval_times = 1 +# # +enable = false + ## servers to query # servers = ["8.8.8.8"] servers = [] diff --git a/inputs/dns_query/dns_query.go b/inputs/dns_query/dns_query.go index d02a8b1..714bed1 100644 --- a/inputs/dns_query/dns_query.go +++ b/inputs/dns_query/dns_query.go @@ -4,6 +4,7 @@ import ( "fmt" "log" "net" + "os" "strconv" "sync" "time" @@ -48,6 +49,7 @@ func (dq *DnsQuery) GetInstances() []inputs.Instance { type Instance struct { config.InstanceConfig + Enable bool `toml:"enable"` // Domains or subdomains to query Domains []string `toml:"domains"` @@ -68,8 +70,23 @@ type Instance struct { } func (ins *Instance) Init() error { + if ins.Enable == false { + return nil + } if len(ins.Servers) == 0 { - return types.ErrInstancesEmpty + resolvPath := "/etc/resolv.conf" + if _, err := os.Stat(resolvPath); os.IsNotExist(err) { + return nil + } + config, _ := dns.ClientConfigFromFile(resolvPath) + Servers := []string{} + for _, ipAddress := range config.Servers { + Servers = append(Servers, ipAddress) + } + ins.Servers = Servers + if len(ins.Servers) == 0 { + return nil + } } if ins.Network == "" { From 6c4ae75562fe9f7e3323e6c018f1157308faaae8 Mon Sep 17 00:00:00 2001 From: lsy1990 Date: Tue, 26 Jul 2022 16:05:55 +0800 Subject: [PATCH 2/2] add variable auto_detect_local_dns_server --- conf/input.dns_query/dns_query.toml | 2 +- inputs/dns_query/dns_query.go | 31 ++++++++++++++--------------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/conf/input.dns_query/dns_query.toml b/conf/input.dns_query/dns_query.toml index d6fb555..ee5f5bf 100644 --- a/conf/input.dns_query/dns_query.toml +++ b/conf/input.dns_query/dns_query.toml @@ -9,7 +9,7 @@ # interval_times = 1 # # -enable = false +auto_detect_local_dns_server = false ## servers to query # servers = ["8.8.8.8"] diff --git a/inputs/dns_query/dns_query.go b/inputs/dns_query/dns_query.go index 714bed1..4443b05 100644 --- a/inputs/dns_query/dns_query.go +++ b/inputs/dns_query/dns_query.go @@ -49,7 +49,7 @@ func (dq *DnsQuery) GetInstances() []inputs.Instance { type Instance struct { config.InstanceConfig - Enable bool `toml:"enable"` + EnableAutoDetectDnsServer bool `toml:"auto_detect_local_dns_server"` // Domains or subdomains to query Domains []string `toml:"domains"` @@ -70,23 +70,22 @@ type Instance struct { } func (ins *Instance) Init() error { - if ins.Enable == false { - return nil + if ins.EnableAutoDetectDnsServer == true { + if len(ins.Servers) == 0 { + resolvPath := "/etc/resolv.conf" + if _, err := os.Stat(resolvPath); os.IsNotExist(err) { + return nil + } + config, _ := dns.ClientConfigFromFile(resolvPath) + Servers := []string{} + for _, ipAddress := range config.Servers { + Servers = append(Servers, ipAddress) + } + ins.Servers = Servers + } } if len(ins.Servers) == 0 { - resolvPath := "/etc/resolv.conf" - if _, err := os.Stat(resolvPath); os.IsNotExist(err) { - return nil - } - config, _ := dns.ClientConfigFromFile(resolvPath) - Servers := []string{} - for _, ipAddress := range config.Servers { - Servers = append(Servers, ipAddress) - } - ins.Servers = Servers - if len(ins.Servers) == 0 { - return nil - } + return nil } if ins.Network == "" {