Skip to content

Commit

Permalink
Support deadline in ping plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
everpcpc committed Feb 12, 2018
1 parent 42ccc9f commit 5c30579
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
2 changes: 2 additions & 0 deletions plugins/inputs/ping/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ urls = ["www.google.com"] # required
# ping_interval = 1.0
## per-ping timeout, in s. 0 == no timeout (ping -W <TIMEOUT>)
# timeout = 1.0
## total-ping deadline, in s. 0 == no deadline (ping -w <DEADLINE>)
# deadline = 10
## interface or source address to send ping from (ping -I <INTERFACE/SRC_ADDR>)
## on Darwin and Freebsd only source address possible: (ping -S <SRC_ADDR>)
# interface = ""
Expand Down
17 changes: 17 additions & 0 deletions plugins/inputs/ping/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ type Ping struct {
// Ping timeout, in seconds. 0 means no timeout (ping -W <TIMEOUT>)
Timeout float64

// Ping deadline, in seconds. 0 means no deadline. (ping -w <DEADLINE>)
Deadline int

// Interface or source address to send ping from (ping -I/-S <INTERFACE/SRC_ADDR>)
Interface string

Expand All @@ -60,6 +63,8 @@ const sampleConfig = `
# ping_interval = 1.0
## per-ping timeout, in s. 0 == no timeout (ping -W <TIMEOUT>)
# timeout = 1.0
## total-ping deadline, in s. 0 == no deadline (ping -w <DEADLINE>)
# deadline = 10
## interface or source address to send ping from (ping -I <INTERFACE/SRC_ADDR>)
## on Darwin and Freebsd only source address possible: (ping -S <SRC_ADDR>)
# interface = ""
Expand Down Expand Up @@ -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":
Expand Down Expand Up @@ -255,6 +271,7 @@ func init() {
PingInterval: 1.0,
Count: 1,
Timeout: 1.0,
Deadline: 10,
}
})
}
21 changes: 13 additions & 8 deletions plugins/inputs/ping/ping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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)
Expand Down

0 comments on commit 5c30579

Please sign in to comment.