feat(inputs.ntpq): Add option to specify command flags (#11593)
This commit is contained in:
parent
f9e06e9f99
commit
ba2ebe896a
|
|
@ -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
|
||||||
|
|
|
||||||
|
|
@ -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",
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue