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

This commit is contained in:
Sven Rebhan 2022-08-02 19:26:54 +02:00 committed by GitHub
parent f9e06e9f99
commit ba2ebe896a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 23 deletions

View File

@ -30,9 +30,22 @@ server (RMS of difference of multiple time samples, milliseconds);
# Get standard NTP query metrics, requires ntpq executable. # Get standard NTP query metrics, requires ntpq executable.
[[inputs.ntpq]] [[inputs.ntpq]]
## If false, set the -n ntpq flag. Can reduce metric gather time. ## 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 ## Metrics
- ntpq - ntpq

View File

@ -11,7 +11,10 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/kballard/go-shellquote"
"github.com/influxdata/telegraf" "github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/internal/choice"
"github.com/influxdata/telegraf/plugins/inputs" "github.com/influxdata/telegraf/plugins/inputs"
) )
@ -58,7 +61,8 @@ var fieldElements = map[string]elementType{
} }
type NTPQ struct { 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) runQ func() ([]byte, error)
} }
@ -67,6 +71,31 @@ func (*NTPQ) SampleConfig() string {
return sampleConfig 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 { func (n *NTPQ) Gather(acc telegraf.Accumulator) error {
out, err := n.runQ() out, err := n.runQ()
if err != nil { if err != nil {
@ -174,25 +203,6 @@ func (n *NTPQ) Gather(acc telegraf.Accumulator) error {
return nil 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) { func processLine(line string) (string, []string) {
// if there is an ntpq state prefix, remove it and make it it's own tag // if there is an ntpq state prefix, remove it and make it it's own tag
// see https://github.com/influxdata/telegraf/issues/1161 // see https://github.com/influxdata/telegraf/issues/1161
@ -208,6 +218,9 @@ func processLine(line string) (string, []string) {
func init() { func init() {
inputs.Add("ntpq", func() telegraf.Input { inputs.Add("ntpq", func() telegraf.Input {
return &NTPQ{} return &NTPQ{
DNSLookup: true,
Options: "-p",
}
}) })
} }

View File

@ -1,4 +1,8 @@
# Get standard NTP query metrics, requires ntpq executable. # Get standard NTP query metrics, requires ntpq executable.
[[inputs.ntpq]] [[inputs.ntpq]]
## If false, set the -n ntpq flag. Can reduce metric gather time. ## 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"