Skip to content

Commit

Permalink
add option to disable dns lookup for chronyc
Browse files Browse the repository at this point in the history
closes #1265
  • Loading branch information
zbindenren authored and sparrc committed May 25, 2016
1 parent 9144f96 commit a8334c3
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 3 additions & 2 deletions plugins/inputs/chrony/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
17 changes: 14 additions & 3 deletions plugins/inputs/chrony/chrony.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,33 @@ var (
)

type Chrony struct {
path string
DNSLookup bool `toml:"dns_lookup"`
path string
}

func (*Chrony) Description() string {
return "Get standard chrony metrics, requires chronyc executable."
}

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))
Expand Down
22 changes: 18 additions & 4 deletions plugins/inputs/chrony/chrony_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)
Expand Down

0 comments on commit a8334c3

Please sign in to comment.