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

feat(inputs.ntpq): Add option to specify command flags #11593

Merged
merged 2 commits into from
Aug 2, 2022
Merged
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
15 changes: 14 additions & 1 deletion plugins/inputs/ntpq/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,22 @@ server (RMS of difference of multiple time samples, milliseconds);
# Get standard NTP query metrics, requires ntpq executable.
[[inputs.ntpq]]
## If false, set the -n ntpq flag. Can reduce metric gather time.
dns_lookup = true
## DEPRECATED since 1.24.0: add '-n' to 'options' instead to skip DNS lookup
# dns_lookup = true

## Options to pass to the ntpq command.
# options = "-p"
```

You can pass arbitrary options accepted by the `ntpq` command using the
`options` setting. In case you want to skip DNS lookups use

```toml
options = "-p -n"
```

for example.

## Metrics

- ntpq
Expand Down
55 changes: 34 additions & 21 deletions plugins/inputs/ntpq/ntpq.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import (
"strconv"
"strings"

"github.com/kballard/go-shellquote"

"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal/choice"
"github.com/influxdata/telegraf/plugins/inputs"
)

Expand Down Expand Up @@ -58,7 +61,8 @@ var fieldElements = map[string]elementType{
}

type NTPQ struct {
DNSLookup bool `toml:"dns_lookup"`
DNSLookup bool `toml:"dns_lookup" deprecated:"1.24.0;add '-n' to 'options' instead to skip DNS lookup"`
Options string `toml:"options"`

runQ func() ([]byte, error)
}
Expand All @@ -67,6 +71,31 @@ func (*NTPQ) SampleConfig() string {
return sampleConfig
}

func (n *NTPQ) Init() error {
if n.runQ == nil {
args, err := shellquote.Split(n.Options)
if err != nil {
return fmt.Errorf("splitting options failed: %w", err)
}
n.runQ = func() ([]byte, error) {
bin, err := exec.LookPath("ntpq")
if err != nil {
return nil, err
}

if !n.DNSLookup {
if !choice.Contains("-n", args) {
args = append(args, "-n")
}
}
fmt.Println(args)
cmd := exec.Command(bin, args...)
return cmd.Output()
}
}
return nil
}

func (n *NTPQ) Gather(acc telegraf.Accumulator) error {
out, err := n.runQ()
if err != nil {
Expand Down Expand Up @@ -174,25 +203,6 @@ func (n *NTPQ) Gather(acc telegraf.Accumulator) error {
return nil
}

func (n *NTPQ) Init() error {
if n.runQ == nil {
n.runQ = func() ([]byte, error) {
bin, err := exec.LookPath("ntpq")
if err != nil {
return nil, err
}

args := []string{"-p"}
if !n.DNSLookup {
args = append(args, "-n")
}
cmd := exec.Command(bin, args...)
return cmd.Output()
}
}
return nil
}

func processLine(line string) (string, []string) {
// if there is an ntpq state prefix, remove it and make it it's own tag
// see https://github.com/influxdata/telegraf/issues/1161
Expand All @@ -208,6 +218,9 @@ func processLine(line string) (string, []string) {

func init() {
inputs.Add("ntpq", func() telegraf.Input {
return &NTPQ{}
return &NTPQ{
DNSLookup: true,
Options: "-p",
}
})
}
6 changes: 5 additions & 1 deletion plugins/inputs/ntpq/sample.conf
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Get standard NTP query metrics, requires ntpq executable.
[[inputs.ntpq]]
## If false, set the -n ntpq flag. Can reduce metric gather time.
dns_lookup = true
## DEPRECATED since 1.24.0: add '-n' to 'options' instead to skip DNS lookup
# dns_lookup = true

## Options to pass to the ntpq command.
# options = "-p"