fix(inputs.ping): Avoid -x/-X on FreeBSD 13 and newer with ping6 (#12171)

This commit is contained in:
Joshua Powers 2022-11-08 07:24:50 -07:00 committed by GitHub
parent cd7a4f2756
commit 969188e9db
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 2 deletions

View File

@ -105,7 +105,7 @@ func (p *Ping) args(url string, system string) []string {
case "darwin":
args = append(args, "-W", strconv.FormatFloat(p.Timeout*1000, 'f', -1, 64))
case "freebsd":
if strings.Contains(p.Binary, "ping6") {
if strings.Contains(p.Binary, "ping6") && freeBSDMajorVersion() <= 12 {
args = append(args, "-x", strconv.FormatFloat(p.Timeout*1000, 'f', -1, 64))
} else {
args = append(args, "-W", strconv.FormatFloat(p.Timeout*1000, 'f', -1, 64))
@ -122,7 +122,7 @@ func (p *Ping) args(url string, system string) []string {
if p.Deadline > 0 {
switch system {
case "freebsd":
if strings.Contains(p.Binary, "ping6") {
if strings.Contains(p.Binary, "ping6") && freeBSDMajorVersion() <= 12 {
args = append(args, "-X", strconv.Itoa(p.Deadline))
} else {
args = append(args, "-t", strconv.Itoa(p.Deadline))
@ -251,3 +251,21 @@ func checkRoundTripTimeStats(line string) (roundTripTimeStats, error) {
}
return roundTripTimeStats, err
}
// Due to different behavior in version of freebsd, get the major
// version number. In the event of an error we assume we return a low number
// to avoid changing behavior.
func freeBSDMajorVersion() int {
out, err := exec.Command("freebsd-version", "-u").Output()
if err != nil {
return -1
}
majorVersionStr := strings.Split(string(out), ".")[0]
majorVersion, err := strconv.Atoi(majorVersionStr)
if err != nil {
return -1
}
return majorVersion
}