diff --git a/plugins/inputs/ntpq/README.md b/plugins/inputs/ntpq/README.md index 1463ad21ac409..0409539c2b261 100644 --- a/plugins/inputs/ntpq/README.md +++ b/plugins/inputs/ntpq/README.md @@ -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 diff --git a/plugins/inputs/ntpq/ntpq.go b/plugins/inputs/ntpq/ntpq.go index 9b0c5e676c06d..411f80539a076 100644 --- a/plugins/inputs/ntpq/ntpq.go +++ b/plugins/inputs/ntpq/ntpq.go @@ -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" ) @@ -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) } @@ -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 { @@ -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 @@ -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", + } }) } diff --git a/plugins/inputs/ntpq/sample.conf b/plugins/inputs/ntpq/sample.conf index b94004c256073..8569b28ce6ce9 100644 --- a/plugins/inputs/ntpq/sample.conf +++ b/plugins/inputs/ntpq/sample.conf @@ -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"