From 8f3843c28ebf85bf23d879f0aa5092907bc44368 Mon Sep 17 00:00:00 2001 From: Luiz Aoqui Date: Thu, 16 Sep 2021 15:27:05 -0400 Subject: [PATCH] fingerprint: simplify windows network speed fingerprinting --- client/fingerprint/network_windows.go | 34 ++++----------------- client/fingerprint/network_windows_test.go | 35 ---------------------- 2 files changed, 5 insertions(+), 64 deletions(-) delete mode 100644 client/fingerprint/network_windows_test.go diff --git a/client/fingerprint/network_windows.go b/client/fingerprint/network_windows.go index 5ca1dff56f10..c843da32ef75 100644 --- a/client/fingerprint/network_windows.go +++ b/client/fingerprint/network_windows.go @@ -9,46 +9,22 @@ import ( // linkSpeed returns link speed in Mb/s, or 0 when unable to determine it. func (f *NetworkFingerprint) linkSpeed(device string) int { - command := fmt.Sprintf("Get-NetAdapter -IncludeHidden | Where name -eq '%s' | Select -ExpandProperty LinkSpeed", device) + command := fmt.Sprintf("Get-NetAdapter -Name '%s' -ErrorAction Ignore | Select-Object -ExpandProperty 'Speed'", device) path := "powershell.exe" powershellParams := "-NoProfile" - outBytes, err := exec.Command(path, powershellParams, command).Output() + outBytes, err := exec.Command(path, powershellParams, command).Output() if err != nil { f.logger.Warn("failed to detect link speed", "device", device, "path", path, "command", command, "error", err) return 0 } - output := strings.TrimSpace(string(outBytes)) - return f.parseLinkSpeed(device, output) -} - -func (f *NetworkFingerprint) parseLinkSpeed(device, commandOutput string) int { - args := strings.Split(commandOutput, " ") - if len(args) != 2 { - f.logger.Warn("couldn't split LinkSpeed output", "device", device, "output", commandOutput) - return 0 - } - - unit := strings.Replace(args[1], "\r\n", "", -1) - valueFloat, err := strconv.ParseFloat(args[0], 64) + value, err := strconv.Atoi(output) if err != nil { - f.logger.Warn("unable to parse LinkSpeed value", "device", device, "value", commandOutput, "error", err) + f.logger.Warn("unable to parse Speed value", "device", device, "value", output, "error", err) return 0 } - value := int(valueFloat) - - switch unit { - case "Mbps": - return value - case "Kbps": - return value / 1000 - case "Gbps": - return value * 1000 - case "bps": - return value / 1000000 - } - return 0 + return value / 1000000 } diff --git a/client/fingerprint/network_windows_test.go b/client/fingerprint/network_windows_test.go deleted file mode 100644 index 004749cd1e62..000000000000 --- a/client/fingerprint/network_windows_test.go +++ /dev/null @@ -1,35 +0,0 @@ -package fingerprint - -import ( - "testing" - - "github.com/hashicorp/nomad/helper/testlog" -) - -func TestNetworkFingerPrint_linkspeed_parse(t *testing.T) { - f := &NetworkFingerprint{logger: testlog.HCLogger(t), interfaceDetector: &DefaultNetworkInterfaceDetector{}} - - var outputTests = []struct { - in string - out int - }{ - {"10 Mbps", 10}, - {"2 bps", 0}, - {"1 Gbps", 1000}, - {"2Mbps", 0}, - {"1000 Kbps", 1}, - {"1 Kbps", 0}, - {"0 Mbps", 0}, - {"2 2 Mbps", 0}, - {"a Mbps", 0}, - {"1 Tbps", 0}, - {"866.7 Mbps", 866}, - } - - for _, ot := range outputTests { - out := f.parseLinkSpeed(ot.in) - if out != ot.out { - t.Errorf("parseLinkSpeed(%s) => %d, should be %d", ot.in, out, ot.out) - } - } -}