Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ping plugin: use -W for linux, -t for bsd/darwin #658

Merged
merged 1 commit into from
Feb 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion plugins/inputs/ping/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package ping
import (
"errors"
"os/exec"
"runtime"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 20 additions & 4 deletions plugins/inputs/ping/ping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package ping
import (
"errors"
"reflect"
"runtime"
"sort"
"testing"

Expand Down Expand Up @@ -84,24 +85,39 @@ 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),
"Expected: %s Actual: %s", expected, actual)

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),
"Expected: %s Actual: %s", expected, actual)

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),
Expand Down