fix: Use PowerShell instead of deprecated wmic for Windows CPU detection
Some checks failed
CI / build-and-test (push) Failing after 37s
Release / build (amd64, linux) (push) Successful in 1m6s
Release / build (amd64, darwin) (push) Successful in 1m22s
Release / build (amd64, windows) (push) Successful in 49s
Release / build (arm64, darwin) (push) Successful in 1m1s
Release / build (arm64, linux) (push) Successful in 49s
Release / release (push) Successful in 18s
Some checks failed
CI / build-and-test (push) Failing after 37s
Release / build (amd64, linux) (push) Successful in 1m6s
Release / build (amd64, darwin) (push) Successful in 1m22s
Release / build (amd64, windows) (push) Successful in 49s
Release / build (arm64, darwin) (push) Successful in 1m1s
Release / build (arm64, linux) (push) Successful in 49s
Release / release (push) Successful in 18s
wmic is deprecated in newer Windows versions and returns empty results. Use Get-CimInstance Win32_Processor via PowerShell instead.
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
// Copyright 2026 MarketAlly. All rights reserved.
|
// Copyright 2026 MarketAlly. All rights reserved.
|
||||||
// SPDX-License-Identifier: MIT
|
// SPDX-License-Identifier: MIT
|
||||||
|
|
||||||
package envcheck
|
package envcheck
|
||||||
@@ -967,23 +967,19 @@ func detectCPULoad() *CPUInfo {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
case "windows":
|
case "windows":
|
||||||
// Windows doesn't have load average, use CPU usage via wmic
|
// Windows doesn't have load average, use PowerShell to get CPU usage
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
// wmic is deprecated, use Get-CimInstance instead
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
cmd := exec.CommandContext(ctx, "wmic", "cpu", "get", "loadpercentage")
|
cmd := exec.CommandContext(ctx, "powershell", "-NoProfile", "-Command",
|
||||||
|
"(Get-CimInstance Win32_Processor | Measure-Object -Property LoadPercentage -Average).Average")
|
||||||
output, err := cmd.Output()
|
output, err := cmd.Output()
|
||||||
if err == nil {
|
if err == nil {
|
||||||
lines := strings.Split(string(output), "\n")
|
line := strings.TrimSpace(string(output))
|
||||||
for _, line := range lines {
|
if load, err := parseFloat(line); err == nil {
|
||||||
line = strings.TrimSpace(line)
|
info.LoadPercent = load
|
||||||
if line != "" && line != "LoadPercentage" {
|
info.LoadAvg1m = load * float64(numCPU) / 100.0
|
||||||
if load, err := parseFloat(line); err == nil {
|
return info
|
||||||
// Convert percentage to "load" equivalent
|
|
||||||
info.LoadPercent = load
|
|
||||||
info.LoadAvg1m = load * float64(numCPU) / 100.0
|
|
||||||
return info
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user