Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option to disable dns lookup for chronyc #1265

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Features

- [#1247](https://github.com/influxdata/telegraf/pull/1247): rollbar input plugin. Thanks @francois2metz and @cduez!
- [#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