diff --git a/Yi.Abp.Net8/framework/Yi.Framework.Core/Helper/ComputerHelper.cs b/Yi.Abp.Net8/framework/Yi.Framework.Core/Helper/ComputerHelper.cs index c1f68058..d55c817c 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.Core/Helper/ComputerHelper.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.Core/Helper/ComputerHelper.cs @@ -78,12 +78,25 @@ namespace Yi.Framework.Core.Helper return 0; } } - + /// + /// CPU使用情况 + /// + /// + public static CPUMetrics GetCPUMetrics() + { + CPUMetrics cpuMetrics = new CPUMetrics(); + var cpudetail = GetCPUDetails(); + cpuMetrics.CoreTotal = cpudetail.Cores; + cpuMetrics.LogicalProcessors =cpudetail.LogicalProcessors; + cpuMetrics.CPURate = Math.Ceiling(ParseToDouble(GetCPURate())); + cpuMetrics.FreeRate = 1 - cpuMetrics.CPURate; + return cpuMetrics; + } /// /// 内存使用情况 /// /// - public static MemoryMetrics GetComputerInfo() + public static MemoryMetrics GetMemoryMetrics() { try { @@ -94,7 +107,7 @@ namespace Yi.Framework.Core.Helper memoryMetrics.UsedRam = Math.Round(memoryMetrics.Used / 1024, 2) + "GB"; memoryMetrics.TotalRAM = Math.Round(memoryMetrics.Total / 1024, 2) + "GB"; memoryMetrics.RAMRate = Math.Ceiling(100 * memoryMetrics.Used / memoryMetrics.Total).ToString() + "%"; - memoryMetrics.CPURate = Math.Ceiling(ParseToDouble(GetCPURate())); + return memoryMetrics; } catch (Exception ex) @@ -105,7 +118,7 @@ namespace Yi.Framework.Core.Helper } /// - /// 获取内存大小 + /// 获取磁盘信息 /// /// public static List GetDiskInfos() @@ -174,7 +187,7 @@ namespace Yi.Framework.Core.Helper var isUnix = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || RuntimeInformation.IsOSPlatform(OSPlatform.Linux); return isUnix; } - + public static string GetCPURate() { string cpuRate; @@ -221,8 +234,69 @@ namespace Yi.Framework.Core.Helper } return runTime; } - } + + + public static CPUInfo GetCPUDetails() + { + int logicalProcessors = 0; + int cores = 0; + + if (IsUnix()) + { + string logicalOutput = ShellHelper.Bash("lscpu | grep '^CPU(s):' | awk '{print $2}'"); + logicalProcessors = int.Parse(logicalOutput.Trim()); + + string coresOutput = ShellHelper.Bash("lscpu | grep 'Core(s) per socket:' | awk '{print $4}'"); + string socketsOutput = ShellHelper.Bash("lscpu | grep 'Socket(s):' | awk '{print $2}'"); + cores = int.Parse(coresOutput.Trim()) * int.Parse(socketsOutput.Trim()); + } + else + { + string output = ShellHelper.Cmd("wmic", "cpu get NumberOfCores,NumberOfLogicalProcessors /format:csv"); + var lines = output.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries); + + if (lines.Length > 1) + { + var values = lines[1].Split(','); + + cores = int.Parse(values[1].Trim()); + logicalProcessors =int.Parse(values[2].Trim()); + } + } + + return new CPUInfo + { + LogicalProcessors = logicalProcessors, + Cores = cores + }; + } + } + public class CPUInfo + { + public int LogicalProcessors { get; set; } + public int Cores { get; set; } + } + public class CPUMetrics + { + /// + /// 内核数 + /// + public int CoreTotal { get; set; } + /// + /// 逻辑处理器数 + /// + public int LogicalProcessors { get; set; } + /// + /// CPU使用率% + /// + public double CPURate { get; set; } + /// + /// CPU空闲率% + /// + public double FreeRate { get; set; } + } + /// /// 内存信息 /// @@ -236,10 +310,7 @@ namespace Yi.Framework.Core.Helper public double Free { get; set; } public string UsedRam { get; set; } - /// - /// CPU使用率% - /// - public double CPURate { get; set; } + /// /// 总内存 GB /// diff --git a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/Monitor/MonitorServerService.cs b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/Monitor/MonitorServerService.cs index 55af86d3..f8808fcd 100644 --- a/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/Monitor/MonitorServerService.cs +++ b/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/Monitor/MonitorServerService.cs @@ -21,7 +21,7 @@ namespace Yi.Framework.Rbac.Application.Services.Monitor [HttpGet("monitor-server/info")] public object GetInfo() { - int cpuNum = Environment.ProcessorCount; + string computerName = Environment.MachineName; string osName = RuntimeInformation.OSDescription; string osArch = RuntimeInformation.OSArchitecture.ToString(); @@ -35,9 +35,10 @@ namespace Yi.Framework.Rbac.Application.Services.Monitor string programRunTime = DateTimeHelper.FormatTime(long.Parse((DateTime.Now - programStartTime).TotalMilliseconds.ToString().Split('.')[0])); var data = new { - cpu = ComputerHelper.GetComputerInfo(), + memory = ComputerHelper.GetMemoryMetrics(), + cpu = ComputerHelper.GetCPUMetrics(), disk = ComputerHelper.GetDiskInfos(), - sys = new { cpuNum, computerName, osName, osArch, serverIP, runTime = sysRunTime }, + sys = new {computerName, osName, osArch, serverIP, runTime = sysRunTime }, app = new { name = _hostEnvironment.EnvironmentName, diff --git a/Yi.RuoYi.Vue3/src/views/monitor/server/index.vue b/Yi.RuoYi.Vue3/src/views/monitor/server/index.vue index cd046d78..ba1d897b 100644 --- a/Yi.RuoYi.Vue3/src/views/monitor/server/index.vue +++ b/Yi.RuoYi.Vue3/src/views/monitor/server/index.vue @@ -15,11 +15,11 @@
核心数
-
{{ server.sys.cpuNum }}
+
{{ server.cpu.coreTotal }}
-
总Cpu数
-
{{ server.cpu.total }}MB
+
逻辑处理器
+
{{ server.cpu.logicalProcessors }}
系统使用率
@@ -49,19 +49,19 @@
总内存
-
{{ server.cpu.totalRAM }}
+
{{ server.memory.totalRAM }}
已用内存
-
{{ server.cpu.usedRam}}
+
{{ server.memory.usedRam}}
剩余内存
-
{{ server.cpu.freeRam }}
+
{{ server.memory.freeRam }}
使用率
-
{{ server.cpu.ramRate }}
+
{{ server.memory.ramRate }}