diff --git a/plugins/inputs/ping/README.md b/plugins/inputs/ping/README.md index 5e4c637f5..eadc60ab7 100644 --- a/plugins/inputs/ping/README.md +++ b/plugins/inputs/ping/README.md @@ -17,6 +17,8 @@ urls = ["www.google.com"] # required # ping_interval = 1.0 ## per-ping timeout, in s. 0 == no timeout (ping -W ) # timeout = 1.0 +## total-ping deadline, in s. 0 == no deadline (ping -w ) +# deadline = 10 ## interface or source address to send ping from (ping -I ) ## on Darwin and Freebsd only source address possible: (ping -S ) # interface = "" diff --git a/plugins/inputs/ping/ping.go b/plugins/inputs/ping/ping.go index 2fb48a20b..0609a367e 100644 --- a/plugins/inputs/ping/ping.go +++ b/plugins/inputs/ping/ping.go @@ -34,6 +34,9 @@ type Ping struct { // Ping timeout, in seconds. 0 means no timeout (ping -W ) Timeout float64 + // Ping deadline, in seconds. 0 means no deadline. (ping -w ) + Deadline int + // Interface or source address to send ping from (ping -I/-S ) Interface string @@ -60,6 +63,8 @@ const sampleConfig = ` # ping_interval = 1.0 ## per-ping timeout, in s. 0 == no timeout (ping -W ) # timeout = 1.0 + ## total-ping deadline, in s. 0 == no deadline (ping -w ) + # deadline = 10 ## interface or source address to send ping from (ping -I ) ## on Darwin and Freebsd only source address possible: (ping -S ) # interface = "" @@ -179,6 +184,17 @@ func (p *Ping) args(url string) []string { args = append(args, "-W", strconv.FormatFloat(p.Timeout, 'f', 1, 64)) } } + if p.Deadline > 0 { + switch runtime.GOOS { + case "darwin": + args = append(args, "-t", strconv.Itoa(p.Deadline)) + case "linux": + args = append(args, "-w", strconv.Itoa(p.Deadline)) + default: + // Not sure the best option here, just assume GNU ping? + args = append(args, "-w", strconv.Itoa(p.Deadline)) + } + } if p.Interface != "" { switch runtime.GOOS { case "linux": @@ -255,6 +271,7 @@ func init() { PingInterval: 1.0, Count: 1, Timeout: 1.0, + Deadline: 10, } }) } diff --git a/plugins/inputs/ping/ping_test.go b/plugins/inputs/ping/ping_test.go index eafe89428..ca50092a9 100644 --- a/plugins/inputs/ping/ping_test.go +++ b/plugins/inputs/ping/ping_test.go @@ -104,14 +104,22 @@ func TestArgs(t *testing.T) { case "darwin": expected = []string{"-c", "2", "-n", "-s", "16", "-I", "eth0", "-W", "12000.0", "www.google.com"} - case "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"} } + p.Deadline = 24 + actual = p.args("www.google.com") + switch runtime.GOOS { + case "darwin": + expected = []string{"-c", "2", "-n", "-s", "16", "-I", "eth0", "-W", + "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"} + } + sort.Strings(actual) sort.Strings(expected) assert.True(t, reflect.DeepEqual(expected, actual), @@ -122,13 +130,10 @@ func TestArgs(t *testing.T) { switch runtime.GOOS { case "darwin": expected = []string{"-c", "2", "-n", "-s", "16", "-I", "eth0", "-W", - "12000.0", "-i", "1.2", "www.google.com"} - case "freebsd": - expected = []string{"-c", "2", "-n", "-s", "16", "-I", "eth0", "-t", - "12.0", "-i", "1.2", "www.google.com"} + "12000.0", "-t", "24", "-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"} + "12.0", "-w", "24", "-i", "1.2", "www.google.com"} } sort.Strings(actual) sort.Strings(expected)