refactor(Admin.NET.Core): 适配 .NET 8.0 及以上版本的 CPU、运行时间和内存指标获取
- 更新了 ComputerUtil 和 MemoryMetricsClient 类中的方法,以支持 .NET 8.0 及以上版本 - 保留了原有 .NET 版本的兼容性代码 - 优化了代码结构,提高了可读性和可维护性
This commit is contained in:
parent
729c0c60f8
commit
22fd588830
@ -189,8 +189,11 @@ public static class ComputerUtil
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
#if NET8_0 || NET9_0
|
||||||
string output = ShellUtil.Cmd("wmic", "cpu get LoadPercentage");
|
string output = ShellUtil.Cmd("wmic", "cpu get LoadPercentage");
|
||||||
// string output = ShellUtil.PowerShell("Get-CimInstance -ClassName Win32_Processor | Measure-Command {Start-Sleep -Seconds 1}");
|
#else
|
||||||
|
string output = ShellUtil.PowerShell("Get-CimInstance -ClassName Win32_Processor | Measure-Command {Start-Sleep -Seconds 1}");
|
||||||
|
#endif
|
||||||
cpuRates.AddRange(output.Replace("LoadPercentage", string.Empty).Trim().Split("\r\r\n"));
|
cpuRates.AddRange(output.Replace("LoadPercentage", string.Empty).Trim().Split("\r\r\n"));
|
||||||
}
|
}
|
||||||
return cpuRates;
|
return cpuRates;
|
||||||
@ -205,10 +208,10 @@ public static class ComputerUtil
|
|||||||
string runTime = string.Empty;
|
string runTime = string.Empty;
|
||||||
if (IsMacOS())
|
if (IsMacOS())
|
||||||
{
|
{
|
||||||
//macOS 获取系统启动时间:
|
// macOS 获取系统启动时间:
|
||||||
//sysctl -n kern.boottime | awk '{print $4}' | tr -d ','
|
// sysctl -n kern.boottime | awk '{print $4}' | tr -d ','
|
||||||
//返回:1705379131
|
// 返回:1705379131
|
||||||
//使用date格式化即可
|
// 使用date格式化即可
|
||||||
string output = ShellUtil.Bash("date -r $(sysctl -n kern.boottime | awk '{print $4}' | tr -d ',') +\"%Y-%m-%d %H:%M:%S\"").Trim();
|
string output = ShellUtil.Bash("date -r $(sysctl -n kern.boottime | awk '{print $4}' | tr -d ',') +\"%Y-%m-%d %H:%M:%S\"").Trim();
|
||||||
runTime = DateTimeUtil.FormatTime((DateTime.Now - output.ParseToDateTime()).TotalMilliseconds.ToString().Split('.')[0].ParseToLong());
|
runTime = DateTimeUtil.FormatTime((DateTime.Now - output.ParseToDateTime()).TotalMilliseconds.ToString().Split('.')[0].ParseToLong());
|
||||||
}
|
}
|
||||||
@ -219,8 +222,11 @@ public static class ComputerUtil
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
#if NET8_0 || NET9_0
|
||||||
string output = ShellUtil.Cmd("wmic", "OS get LastBootUpTime/Value");
|
string output = ShellUtil.Cmd("wmic", "OS get LastBootUpTime/Value");
|
||||||
// string output = ShellUtil.PowerShell("(Get-Date) - (Get-CimInstance -ClassName Win32_OperatingSystem).LastBootUpTime");
|
#else
|
||||||
|
string output = ShellUtil.PowerShell("(Get-Date) - (Get-CimInstance -ClassName Win32_OperatingSystem).LastBootUpTime");
|
||||||
|
#endif
|
||||||
string[] outputArr = output.Split('=', (char)StringSplitOptions.RemoveEmptyEntries);
|
string[] outputArr = output.Split('=', (char)StringSplitOptions.RemoveEmptyEntries);
|
||||||
if (outputArr.Length == 2)
|
if (outputArr.Length == 2)
|
||||||
runTime = DateTimeUtil.FormatTime((DateTime.Now - outputArr[1].Split('.')[0].ParseToDateTime()).TotalMilliseconds.ToString().Split('.')[0].ParseToLong());
|
runTime = DateTimeUtil.FormatTime((DateTime.Now - outputArr[1].Split('.')[0].ParseToDateTime()).TotalMilliseconds.ToString().Split('.')[0].ParseToLong());
|
||||||
@ -331,8 +337,11 @@ public class MemoryMetricsClient
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static MemoryMetrics GetWindowsMetrics()
|
public static MemoryMetrics GetWindowsMetrics()
|
||||||
{
|
{
|
||||||
|
#if NET8_0 || NET9_0
|
||||||
string output = ShellUtil.Cmd("wmic", "OS get FreePhysicalMemory,TotalVisibleMemorySize /Value");
|
string output = ShellUtil.Cmd("wmic", "OS get FreePhysicalMemory,TotalVisibleMemorySize /Value");
|
||||||
// string output = ShellUtil.PowerShell("Get-CimInstance -ClassName Win32_OperatingSystem | Select FreePhysicalMemory, TotalVisibleMemorySize");
|
#else
|
||||||
|
string output = ShellUtil.PowerShell("Get-CimInstance -ClassName Win32_OperatingSystem | Select FreePhysicalMemory, TotalVisibleMemorySize");
|
||||||
|
#endif
|
||||||
var metrics = new MemoryMetrics();
|
var metrics = new MemoryMetrics();
|
||||||
var lines = output.Trim().Split('\n', (char)StringSplitOptions.RemoveEmptyEntries);
|
var lines = output.Trim().Split('\n', (char)StringSplitOptions.RemoveEmptyEntries);
|
||||||
if (lines.Length <= 0) return metrics;
|
if (lines.Length <= 0) return metrics;
|
||||||
@ -448,7 +457,6 @@ public class ShellUtil
|
|||||||
public static string PowerShell(string script)
|
public static string PowerShell(string script)
|
||||||
{
|
{
|
||||||
#if NET8_0 || NET9_0
|
#if NET8_0 || NET9_0
|
||||||
// net 8.0 可用
|
|
||||||
using var PowerShellInstance = System.Management.Automation.PowerShell.Create();
|
using var PowerShellInstance = System.Management.Automation.PowerShell.Create();
|
||||||
PowerShellInstance.AddScript(script);
|
PowerShellInstance.AddScript(script);
|
||||||
var PSOutput = PowerShellInstance.Invoke();
|
var PSOutput = PowerShellInstance.Invoke();
|
||||||
@ -459,6 +467,9 @@ public class ShellUtil
|
|||||||
output.AppendLine(outputItem.BaseObject.ToString());
|
output.AppendLine(outputItem.BaseObject.ToString());
|
||||||
}
|
}
|
||||||
return output.ToString();
|
return output.ToString();
|
||||||
|
#else
|
||||||
|
return "";
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -520,6 +531,7 @@ public class ShellHelper
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public static string PowerShell(string script)
|
public static string PowerShell(string script)
|
||||||
{
|
{
|
||||||
|
#if NET8_0 || NET9_0
|
||||||
using var PowerShellInstance = System.Management.Automation.PowerShell.Create();
|
using var PowerShellInstance = System.Management.Automation.PowerShell.Create();
|
||||||
PowerShellInstance.AddScript(script);
|
PowerShellInstance.AddScript(script);
|
||||||
var PSOutput = PowerShellInstance.Invoke();
|
var PSOutput = PowerShellInstance.Invoke();
|
||||||
@ -530,5 +542,8 @@ public class ShellHelper
|
|||||||
output.AppendLine(outputItem.BaseObject.ToString());
|
output.AppendLine(outputItem.BaseObject.ToString());
|
||||||
}
|
}
|
||||||
return output.ToString();
|
return output.ToString();
|
||||||
|
#else
|
||||||
|
return "";
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user