github.com/bigcommerce/nomad@v0.9.3-bc/client/fingerprint/network_windows.go (about)

     1  package fingerprint
     2  
     3  import (
     4  	"fmt"
     5  	"os/exec"
     6  	"strconv"
     7  	"strings"
     8  )
     9  
    10  // linkSpeed returns link speed in Mb/s, or 0 when unable to determine it.
    11  func (f *NetworkFingerprint) linkSpeed(device string) int {
    12  	command := fmt.Sprintf("Get-NetAdapter -IncludeHidden | Where name -eq '%s' | Select -ExpandProperty LinkSpeed", device)
    13  	path := "powershell.exe"
    14  	outBytes, err := exec.Command(path, command).Output()
    15  
    16  	if err != nil {
    17  		f.logger.Warn("failed to detect link speed", "path", path, "command", command, "error", err)
    18  		return 0
    19  	}
    20  
    21  	output := strings.TrimSpace(string(outBytes))
    22  
    23  	return f.parseLinkSpeed(output)
    24  }
    25  
    26  func (f *NetworkFingerprint) parseLinkSpeed(commandOutput string) int {
    27  	args := strings.Split(commandOutput, " ")
    28  	if len(args) != 2 {
    29  		f.logger.Warn("couldn't split LinkSpeed output", "output", commandOutput)
    30  		return 0
    31  	}
    32  
    33  	unit := strings.Replace(args[1], "\r\n", "", -1)
    34  	value, err := strconv.Atoi(args[0])
    35  	if err != nil {
    36  		f.logger.Warn("unable to parse LinkSpeed value", "value", commandOutput)
    37  		return 0
    38  	}
    39  
    40  	switch unit {
    41  	case "Mbps":
    42  		return value
    43  	case "Kbps":
    44  		return value / 1000
    45  	case "Gbps":
    46  		return value * 1000
    47  	case "bps":
    48  		return value / 1000000
    49  	}
    50  
    51  	return 0
    52  }