Support deadline in ping plugin (#3783)

This commit is contained in:
everpcpc 2018-02-16 12:11:07 +08:00 committed by Daniel Nelson
parent eceba38ec2
commit 37ae4b00bf
3 changed files with 32 additions and 8 deletions

View File

@ -17,6 +17,8 @@ urls = ["www.google.com"] # required
# ping_interval = 1.0 # ping_interval = 1.0
## per-ping timeout, in s. 0 == no timeout (ping -W <TIMEOUT>) ## per-ping timeout, in s. 0 == no timeout (ping -W <TIMEOUT>)
# timeout = 1.0 # 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>) ## 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>) ## on Darwin and Freebsd only source address possible: (ping -S <SRC_ADDR>)
# interface = "" # interface = ""

View File

@ -34,6 +34,9 @@ type Ping struct {
// Ping timeout, in seconds. 0 means no timeout (ping -W <TIMEOUT>) // Ping timeout, in seconds. 0 means no timeout (ping -W <TIMEOUT>)
Timeout float64 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 or source address to send ping from (ping -I/-S <INTERFACE/SRC_ADDR>)
Interface string Interface string
@ -60,6 +63,8 @@ const sampleConfig = `
# ping_interval = 1.0 # ping_interval = 1.0
## per-ping timeout, in s. 0 == no timeout (ping -W <TIMEOUT>) ## per-ping timeout, in s. 0 == no timeout (ping -W <TIMEOUT>)
# timeout = 1.0 # 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>) ## 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>) ## on Darwin and Freebsd only source address possible: (ping -S <SRC_ADDR>)
# interface = "" # interface = ""
@ -179,6 +184,17 @@ func (p *Ping) args(url string) []string {
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 {
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 != "" { if p.Interface != "" {
switch runtime.GOOS { switch runtime.GOOS {
case "linux": case "linux":
@ -255,6 +271,7 @@ func init() {
PingInterval: 1.0, PingInterval: 1.0,
Count: 1, Count: 1,
Timeout: 1.0, Timeout: 1.0,
Deadline: 10,
} }
}) })
} }

View File

@ -104,14 +104,22 @@ func TestArgs(t *testing.T) {
case "darwin": case "darwin":
expected = []string{"-c", "2", "-n", "-s", "16", "-I", "eth0", "-W", expected = []string{"-c", "2", "-n", "-s", "16", "-I", "eth0", "-W",
"12000.0", "www.google.com"} "12000.0", "www.google.com"}
case "freebsd":
expected = []string{"-c", "2", "-n", "-s", "16", "-I", "eth0", "-t",
"12.0", "www.google.com"}
default: default:
expected = []string{"-c", "2", "-n", "-s", "16", "-I", "eth0", "-W", expected = []string{"-c", "2", "-n", "-s", "16", "-I", "eth0", "-W",
"12.0", "www.google.com"} "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(actual)
sort.Strings(expected) sort.Strings(expected)
assert.True(t, reflect.DeepEqual(expected, actual), assert.True(t, reflect.DeepEqual(expected, actual),
@ -122,13 +130,10 @@ func TestArgs(t *testing.T) {
switch runtime.GOOS { switch runtime.GOOS {
case "darwin": case "darwin":
expected = []string{"-c", "2", "-n", "-s", "16", "-I", "eth0", "-W", expected = []string{"-c", "2", "-n", "-s", "16", "-I", "eth0", "-W",
"12000.0", "-i", "1.2", "www.google.com"} "12000.0", "-t", "24", "-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"}
default: default:
expected = []string{"-c", "2", "-n", "-s", "16", "-I", "eth0", "-W", 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(actual)
sort.Strings(expected) sort.Strings(expected)