github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/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 -Name '%s' -ErrorAction Ignore | Select-Object -ExpandProperty 'Speed'", device)
    13  	path := "powershell.exe"
    14  	powershellParams := "-NoProfile"
    15  
    16  	outBytes, err := exec.Command(path, powershellParams, command).Output()
    17  	if err != nil {
    18  		f.logger.Warn("failed to detect link speed", "device", device, "path", path, "command", command, "error", err)
    19  		return 0
    20  	}
    21  	output := strings.TrimSpace(string(outBytes))
    22  
    23  	value, err := strconv.Atoi(output)
    24  	if err != nil {
    25  		f.logger.Warn("unable to parse Speed value", "device", device, "value", output, "error", err)
    26  		return 0
    27  	}
    28  
    29  	return value / 1000000
    30  }