From f5f43e6d1bf50ddb3f105f615ef224a85a5d8f50 Mon Sep 17 00:00:00 2001 From: Cameron Sparr Date: Tue, 2 Feb 2016 18:43:03 -0700 Subject: [PATCH] ping plugin: use -W for linux, -t for bsd/darwin closes #443 --- plugins/inputs/ping/ping.go | 11 ++++++++++- plugins/inputs/ping/ping_test.go | 24 ++++++++++++++++++++---- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/plugins/inputs/ping/ping.go b/plugins/inputs/ping/ping.go index 3368a25e0..9ca6b3498 100644 --- a/plugins/inputs/ping/ping.go +++ b/plugins/inputs/ping/ping.go @@ -5,6 +5,7 @@ package ping import ( "errors" "os/exec" + "runtime" "strconv" "strings" "sync" @@ -133,7 +134,15 @@ func (p *Ping) args(url string) []string { args = append(args, "-i", strconv.FormatFloat(p.PingInterval, 'f', 1, 64)) } if p.Timeout > 0 { - args = append(args, "-t", strconv.FormatFloat(p.Timeout, 'f', 1, 64)) + switch runtime.GOOS { + case "darwin", "freebsd": + args = append(args, "-t", strconv.FormatFloat(p.Timeout, 'f', 1, 64)) + case "linux": + 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)) + } } if p.Interface != "" { args = append(args, "-I", p.Interface) diff --git a/plugins/inputs/ping/ping_test.go b/plugins/inputs/ping/ping_test.go index 6663207a0..cd61a4fb2 100644 --- a/plugins/inputs/ping/ping_test.go +++ b/plugins/inputs/ping/ping_test.go @@ -5,6 +5,7 @@ package ping import ( "errors" "reflect" + "runtime" "sort" "testing" @@ -84,7 +85,8 @@ func TestArgs(t *testing.T) { p.Interface = "eth0" actual = p.args("www.google.com") - expected = []string{"-c", "2", "-n", "-s", "16", "-I", "eth0", "www.google.com"} + expected = []string{"-c", "2", "-n", "-s", "16", "-I", "eth0", + "www.google.com"} sort.Strings(actual) sort.Strings(expected) assert.True(t, reflect.DeepEqual(expected, actual), @@ -92,7 +94,15 @@ func TestArgs(t *testing.T) { p.Timeout = 12.0 actual = p.args("www.google.com") - expected = []string{"-c", "2", "-n", "-s", "16", "-I", "eth0", "-t", "12.0", "www.google.com"} + switch runtime.GOOS { + case "darwin", "freebsd": + expected = []string{"-c", "2", "-n", "-s", "16", "-I", "eth0", "-t", + "12.0", "www.google.com"} + default: + expected = []string{"-c", "2", "-n", "-s", "16", "-I", "eth0", "-W", + "12.0", "www.google.com"} + } + sort.Strings(actual) sort.Strings(expected) assert.True(t, reflect.DeepEqual(expected, actual), @@ -100,8 +110,14 @@ func TestArgs(t *testing.T) { p.PingInterval = 1.2 actual = p.args("www.google.com") - expected = []string{"-c", "2", "-n", "-s", "16", "-I", "eth0", "-t", "12.0", "-i", "1.2", - "www.google.com"} + switch runtime.GOOS { + case "darwin", "freebsd": + expected = []string{"-c", "2", "-n", "-s", "16", "-I", "eth0", "-t", + "12.0", "-i", "1.2", "www.google.com"} + default: + expected = []string{"-c", "2", "-n", "-s", "16", "-I", "eth0", "-W", + "12.0", "-i", "1.2", "www.google.com"} + } sort.Strings(actual) sort.Strings(expected) assert.True(t, reflect.DeepEqual(expected, actual),