diff --git a/plugins/inputs/ping/ping.go b/plugins/inputs/ping/ping.go index 5de67322e..f347fa3d6 100644 --- a/plugins/inputs/ping/ping.go +++ b/plugins/inputs/ping/ping.go @@ -171,17 +171,17 @@ func (p *Ping) args(url string) []string { // Build the ping command args based on toml config args := []string{"-c", strconv.Itoa(p.Count), "-n", "-s", "16"} if p.PingInterval > 0 { - args = append(args, "-i", strconv.FormatFloat(p.PingInterval, 'f', 1, 64)) + args = append(args, "-i", strconv.FormatFloat(p.PingInterval, 'f', -1, 64)) } if p.Timeout > 0 { switch runtime.GOOS { case "darwin": - args = append(args, "-W", strconv.FormatFloat(p.Timeout*1000, 'f', 1, 64)) + args = append(args, "-W", strconv.FormatFloat(p.Timeout*1000, 'f', -1, 64)) case "linux": - args = append(args, "-W", strconv.FormatFloat(p.Timeout, 'f', 1, 64)) + args = append(args, "-W", strconv.FormatFloat(p.Timeout, 'f', -1, 64)) default: // Not sure the best option here, just assume GNU ping? - args = append(args, "-W", strconv.FormatFloat(p.Timeout, 'f', 1, 64)) + args = append(args, "-W", strconv.FormatFloat(p.Timeout, 'f', -1, 64)) } } if p.Deadline > 0 { @@ -243,21 +243,24 @@ func processPingOutput(out string) (int, int, float64, float64, float64, float64 } } else if strings.Contains(line, "min/avg/max") { stats := strings.Split(line, " ")[3] - min, err = strconv.ParseFloat(strings.Split(stats, "/")[0], 64) + data := strings.Split(stats, "/") + min, err = strconv.ParseFloat(data[0], 64) if err != nil { return trans, recv, min, avg, max, stddev, err } - avg, err = strconv.ParseFloat(strings.Split(stats, "/")[1], 64) + avg, err = strconv.ParseFloat(data[1], 64) if err != nil { return trans, recv, min, avg, max, stddev, err } - max, err = strconv.ParseFloat(strings.Split(stats, "/")[2], 64) + max, err = strconv.ParseFloat(data[2], 64) if err != nil { return trans, recv, min, avg, max, stddev, err } - stddev, err = strconv.ParseFloat(strings.Split(stats, "/")[3], 64) - if err != nil { - return trans, recv, min, avg, max, stddev, err + if len(data) == 4 { + stddev, err = strconv.ParseFloat(data[3], 64) + if err != nil { + return trans, recv, min, avg, max, stddev, err + } } } } diff --git a/plugins/inputs/ping/ping_test.go b/plugins/inputs/ping/ping_test.go index 85f5ca079..9817d07c6 100644 --- a/plugins/inputs/ping/ping_test.go +++ b/plugins/inputs/ping/ping_test.go @@ -41,6 +41,19 @@ PING www.google.com (216.58.218.164) 56(84) bytes of data. rtt min/avg/max/mdev = 35.225/43.628/51.806/5.325 ms ` +// BusyBox v1.24.1 (2017-02-28 03:28:13 CET) multi-call binary +var busyBoxPingOutput = ` +PING 8.8.8.8 (8.8.8.8): 56 data bytes +64 bytes from 8.8.8.8: seq=0 ttl=56 time=22.559 ms +64 bytes from 8.8.8.8: seq=1 ttl=56 time=15.810 ms +64 bytes from 8.8.8.8: seq=2 ttl=56 time=16.262 ms +64 bytes from 8.8.8.8: seq=3 ttl=56 time=15.815 ms + +--- 8.8.8.8 ping statistics --- +4 packets transmitted, 4 packets received, 0% packet loss +round-trip min/avg/max = 15.810/17.611/22.559 ms +` + // Fatal ping output (invalid argument) var fatalPingOutput = ` ping: -i interval too short: Operation not permitted @@ -65,6 +78,15 @@ func TestProcessPingOutput(t *testing.T) { assert.InDelta(t, 43.628, avg, 0.001) assert.InDelta(t, 51.806, max, 0.001) assert.InDelta(t, 5.325, stddev, 0.001) + + trans, rec, min, avg, max, stddev, err = processPingOutput(busyBoxPingOutput) + assert.NoError(t, err) + assert.Equal(t, 4, trans, "4 packets were transmitted") + assert.Equal(t, 4, rec, "4 packets were transmitted") + assert.InDelta(t, 15.810, min, 0.001) + assert.InDelta(t, 17.611, avg, 0.001) + assert.InDelta(t, 22.559, max, 0.001) + assert.InDelta(t, -1.0, stddev, 0.001) } // Test that processPingOutput returns an error when 'ping' fails to run, such @@ -106,7 +128,7 @@ func TestArgs(t *testing.T) { "12000.0", "www.google.com"} default: expected = []string{"-c", "2", "-n", "-s", "16", "-I", "eth0", "-W", - "12.0", "www.google.com"} + "12", "www.google.com"} } p.Deadline = 24 @@ -117,7 +139,7 @@ func TestArgs(t *testing.T) { "12000.0", "-t", "24", "www.google.com"} default: expected = []string{"-c", "2", "-n", "-s", "16", "-I", "eth0", "-W", - "12.0", "-w", "24", "www.google.com"} + "12", "-w", "24", "www.google.com"} } sort.Strings(actual) @@ -133,7 +155,7 @@ func TestArgs(t *testing.T) { "12000.0", "-t", "24", "-i", "1.2", "www.google.com"} default: expected = []string{"-c", "2", "-n", "-s", "16", "-I", "eth0", "-W", - "12.0", "-w", "24", "-i", "1.2", "www.google.com"} + "12", "-w", "24", "-i", "1.2", "www.google.com"} } sort.Strings(actual) sort.Strings(expected)