Skip to content

Commit

Permalink
Fix ping exit code handling on non-Linux (influxdata#7658)
Browse files Browse the repository at this point in the history
  • Loading branch information
nferch authored and idohalevi committed Sep 23, 2020
1 parent a333a51 commit f445535
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions plugins/inputs/ping/ping_notwindows.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ func (p *Ping) pingToURL(u string, acc telegraf.Accumulator) {

out, err := p.pingHost(p.Binary, 60.0, p.args(u, runtime.GOOS)...)
if err != nil {
// Some implementations of ping return a 1 exit code on
// Some implementations of ping return a non-zero exit code on
// timeout, if this occurs we will not exit and try to parse
// the output.
// Linux iputils-ping returns 1, BSD-derived ping returns 2.
status := -1
if exitError, ok := err.(*exec.ExitError); ok {
if ws, ok := exitError.Sys().(syscall.WaitStatus); ok {
Expand All @@ -32,7 +33,17 @@ func (p *Ping) pingToURL(u string, acc telegraf.Accumulator) {
}
}

if status != 1 {
var timeoutExitCode int
switch runtime.GOOS {
case "freebsd", "netbsd", "openbsd", "darwin":
timeoutExitCode = 2
case "linux":
timeoutExitCode = 1
default:
timeoutExitCode = 1
}

if status != timeoutExitCode {
// Combine go err + stderr output
out = strings.TrimSpace(out)
if len(out) > 0 {
Expand Down

0 comments on commit f445535

Please sign in to comment.