From a8334c32613cc4f0af99ad4e89ec33476c299187 Mon Sep 17 00:00:00 2001 From: Rene Zbinden Date: Wed, 25 May 2016 12:46:14 +0200 Subject: [PATCH] add option to disable dns lookup for chronyc closes #1265 --- CHANGELOG.md | 1 + plugins/inputs/chrony/README.md | 5 +++-- plugins/inputs/chrony/chrony.go | 17 ++++++++++++++--- plugins/inputs/chrony/chrony_test.go | 22 ++++++++++++++++++---- 4 files changed, 36 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c02fbd89..9d4d74ff2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ time before a new metric is included by the plugin. - [#1208](https://github.com/influxdata/telegraf/pull/1208): Standardized AWS credentials evaluation & wildcard CloudWatch dimensions. Thanks @johnrengelman! - [#1264](https://github.com/influxdata/telegraf/pull/1264): Add SSL config options to http_response plugin. - [#1272](https://github.com/influxdata/telegraf/pull/1272): graphite parser: add ability to specify multiple tag keys, for consistency with influxdb parser. +- [#1265](https://github.com/influxdata/telegraf/pull/1265): Make dns lookups for chrony configurable. Thanks @zbindenren! ### Bugfixes diff --git a/plugins/inputs/chrony/README.md b/plugins/inputs/chrony/README.md index e12506ecb..43eb77757 100644 --- a/plugins/inputs/chrony/README.md +++ b/plugins/inputs/chrony/README.md @@ -40,7 +40,7 @@ is computed for the new frequency, with weights depending on these accuracies. I measurements from the reference source follow a consistent trend, the residual will be driven to zero over time. - Skew - This is the estimated error bound on the frequency. -- Root delay -This is the total of the network path delays to the stratum-1 computer +- Root delay - This is the total of the network path delays to the stratum-1 computer from which the computer is ultimately synchronised. In certain extreme situations, this value can be negative. (This can arise in a symmetric peer arrangement where the computers’ frequencies are not tracking each other and the network delay is very short relative to the @@ -56,7 +56,8 @@ Delete second or Not synchronised. ```toml # Get standard chrony metrics, requires chronyc executable. [[inputs.chrony]] - # no configuration + ## If true, chronyc tries to perform a DNS lookup for the time server. + # dns_lookup = false ``` ### Measurements & Fields: diff --git a/plugins/inputs/chrony/chrony.go b/plugins/inputs/chrony/chrony.go index b4d874e60..51081b87b 100644 --- a/plugins/inputs/chrony/chrony.go +++ b/plugins/inputs/chrony/chrony.go @@ -20,7 +20,8 @@ var ( ) type Chrony struct { - path string + DNSLookup bool `toml:"dns_lookup"` + path string } func (*Chrony) Description() string { @@ -28,14 +29,24 @@ func (*Chrony) Description() string { } func (*Chrony) SampleConfig() string { - return "" + return ` + ## If true, chronyc tries to perform a DNS lookup for the time server. + # dns_lookup = false + ` } func (c *Chrony) Gather(acc telegraf.Accumulator) error { if len(c.path) == 0 { return errors.New("chronyc not found: verify that chrony is installed and that chronyc is in your PATH") } - cmd := execCommand(c.path, "tracking") + + flags := []string{} + if !c.DNSLookup { + flags = append(flags, "-n") + } + flags = append(flags, "tracking") + + cmd := execCommand(c.path, flags...) out, err := internal.CombinedOutputTimeout(cmd, time.Second*5) if err != nil { return fmt.Errorf("failed to run command %s: %s - %s", strings.Join(cmd.Args, " "), err, string(out)) diff --git a/plugins/inputs/chrony/chrony_test.go b/plugins/inputs/chrony/chrony_test.go index 0e7d8a1a8..143d7e9d9 100644 --- a/plugins/inputs/chrony/chrony_test.go +++ b/plugins/inputs/chrony/chrony_test.go @@ -42,6 +42,15 @@ func TestGather(t *testing.T) { } acc.AssertContainsTaggedFields(t, "chrony", fields, tags) + + // test with dns lookup + c.DNSLookup = true + err = c.Gather(&acc) + if err != nil { + t.Fatal(err) + } + acc.AssertContainsTaggedFields(t, "chrony", fields, tags) + } // fackeExecCommand is a helper function that mock @@ -63,8 +72,9 @@ func TestHelperProcess(t *testing.T) { return } - mockData := `Reference ID : 192.168.1.22 (ntp.example.com) -Stratum : 3 + lookup := "Reference ID : 192.168.1.22 (ntp.example.com)\n" + noLookup := "Reference ID : 192.168.1.22 (192.168.1.22)\n" + mockData := `Stratum : 3 Ref time (UTC) : Thu May 12 14:27:07 2016 System time : 0.000020390 seconds fast of NTP time Last offset : +0.000012651 seconds @@ -84,8 +94,12 @@ Leap status : Normal // /tmp/go-build970079519/…/_test/integration.test -test.run=TestHelperProcess -- cmd, args := args[3], args[4:] - if cmd == "chronyc" && args[0] == "tracking" { - fmt.Fprint(os.Stdout, mockData) + if cmd == "chronyc" { + if args[0] == "tracking" { + fmt.Fprint(os.Stdout, lookup+mockData) + } else { + fmt.Fprint(os.Stdout, noLookup+mockData) + } } else { fmt.Fprint(os.Stdout, "command not found") os.Exit(1)