Skip to content

Commit

Permalink
fix(inputs.ntpq): Avoid panic on empty lines and make sure -p is pres…
Browse files Browse the repository at this point in the history
  • Loading branch information
srebhan authored and s-r-engineer committed Nov 11, 2024
1 parent 6e4a769 commit 7386788
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions plugins/inputs/ntpq/ntpq.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ import (
"math/bits"
"os/exec"
"regexp"
"slices"
"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 @@ -87,10 +87,13 @@ func (n *NTPQ) Init() error {
return fmt.Errorf("splitting options failed: %w", err)
}
if !n.DNSLookup {
if !choice.Contains("-n", options) {
if !slices.Contains(options, "-n") {
options = append(options, "-n")
}
}
if !slices.Contains(options, "-p") {
options = append(options, "-p")
}

n.runQ = func(server string) ([]byte, error) {
bin, err := exec.LookPath("ntpq")
Expand All @@ -99,7 +102,7 @@ func (n *NTPQ) Init() error {
}

// Needs to be last argument
var args []string
args := make([]string, 0, len(options)+1)
args = append(args, options...)
if server != "" {
args = append(args, server)
Expand Down Expand Up @@ -159,6 +162,10 @@ func (n *NTPQ) gatherServer(acc telegraf.Accumulator, server string) {
for scanner.Scan() {
line := scanner.Text()

if line == "" {
continue
}

_, elements := processLine(line)
if len(elements) < 2 {
continue
Expand Down Expand Up @@ -191,6 +198,10 @@ func (n *NTPQ) gatherServer(acc telegraf.Accumulator, server string) {
for scanner.Scan() {
line := scanner.Text()

if line == "" {
continue
}

prefix, elements := processLine(line)
if len(elements) != len(columns) {
continue
Expand Down Expand Up @@ -298,7 +309,6 @@ func init() {
inputs.Add("ntpq", func() telegraf.Input {
return &NTPQ{
DNSLookup: true,
Options: "-p",
}
})
}

0 comments on commit 7386788

Please sign in to comment.