2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2016-07-20 20:13:50 +00:00
|
|
|
package fingerprint
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// linkSpeed returns link speed in Mb/s, or 0 when unable to determine it.
|
|
|
|
func (f *NetworkFingerprint) linkSpeed(device string) int {
|
2021-09-22 15:17:47 +00:00
|
|
|
command := fmt.Sprintf("Get-NetAdapter -Name '%s' -ErrorAction Ignore | Select-Object -ExpandProperty 'Speed'", device)
|
2016-07-20 20:13:50 +00:00
|
|
|
path := "powershell.exe"
|
2021-09-22 15:17:47 +00:00
|
|
|
powershellParams := "-NoProfile"
|
2016-07-20 20:13:50 +00:00
|
|
|
|
2021-09-22 15:17:47 +00:00
|
|
|
outBytes, err := exec.Command(path, powershellParams, command).Output()
|
2016-07-20 20:13:50 +00:00
|
|
|
if err != nil {
|
2021-09-16 14:48:31 +00:00
|
|
|
f.logger.Warn("failed to detect link speed", "device", device, "path", path, "command", command, "error", err)
|
2016-07-20 20:13:50 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
output := strings.TrimSpace(string(outBytes))
|
2016-07-22 20:41:36 +00:00
|
|
|
|
2021-09-22 15:17:47 +00:00
|
|
|
value, err := strconv.Atoi(output)
|
2016-07-20 20:13:50 +00:00
|
|
|
if err != nil {
|
2021-09-22 15:17:47 +00:00
|
|
|
f.logger.Warn("unable to parse Speed value", "device", device, "value", output, "error", err)
|
2016-07-20 20:13:50 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-09-22 15:17:47 +00:00
|
|
|
return value / 1000000
|
2016-07-20 20:13:50 +00:00
|
|
|
}
|