diff --git a/Infrastructure/Helper/ComputerHelper.cs b/Infrastructure/Helper/ComputerHelper.cs index b26e37b..20ae87e 100644 --- a/Infrastructure/Helper/ComputerHelper.cs +++ b/Infrastructure/Helper/ComputerHelper.cs @@ -1,6 +1,8 @@ using Infrastructure.Extensions; using System; using System.Collections.Generic; +using System.IO; +using System.Linq; using System.Runtime.InteropServices; using System.Text; @@ -8,23 +10,53 @@ namespace Infrastructure { public class ComputerHelper { - public static ComputerInfo GetComputerInfo() + /// + /// 内存使用情况 + /// + /// + public static MemoryMetrics GetComputerInfo() { - ComputerInfo computerInfo = new ComputerInfo(); try { - MemoryMetricsClient client = new MemoryMetricsClient(); + MemoryMetricsClient client = new(); MemoryMetrics memoryMetrics = client.GetMetrics(); - computerInfo.TotalRAM = Math.Ceiling(memoryMetrics.Total / 1024).ToString() + " GB"; - computerInfo.RAMRate = Math.Ceiling(100 * memoryMetrics.Used / memoryMetrics.Total).ToString() + " %"; - computerInfo.CPURate = Math.Ceiling(GetCPURate().ParseToDouble()) + " %"; - computerInfo.RunTime = GetRunTime(); + memoryMetrics.FreeRam = Math.Ceiling(memoryMetrics.Free / 1024) + "GB"; + memoryMetrics.UsedRam = Math.Ceiling(memoryMetrics.Used / 1024) + "GB"; + memoryMetrics.TotalRAM = Math.Ceiling(memoryMetrics.Total / 1024).ToString() + " GB"; + memoryMetrics.RAMRate = Math.Ceiling(100 * memoryMetrics.Used / memoryMetrics.Total).ToString() + "%"; + memoryMetrics.CPURate = Math.Ceiling(GetCPURate().ParseToDouble()) + "%"; + return memoryMetrics; } catch (Exception ex) { - //LogHelper.Error(ex); + Console.WriteLine(ex.Message); } - return computerInfo; + return null; + } + + /// + /// 获取内存大小 + /// + /// + public static List GetDiskInfos() + { + List diskInfos = new(); + + var driv = DriveInfo.GetDrives(); + foreach (var item in driv) + { + var obj = new DiskInfo() + { + DickName = item.Name, + TypeName = item.DriveType.ToString(), + TotalFree = item.TotalFreeSpace / 1024 / 1024 / 1024, + TotalSize = item.TotalSize / 1024 / 1024 / 1024, + AvailableFreeSpace = item.AvailableFreeSpace / 1024 / 1024 / 1024, + }; + obj.AvailablePercent = decimal.Ceiling(((decimal)(obj.TotalSize - obj.AvailableFreeSpace) /(decimal)obj.TotalSize) * 100); + diskInfos.Add(obj); + } + return diskInfos; } public static bool IsUnix() @@ -35,7 +67,7 @@ namespace Infrastructure public static string GetCPURate() { - string cpuRate = string.Empty; + string cpuRate; if (IsUnix()) { string output = ShellHelper.Bash("top -b -n1 | grep \"Cpu(s)\" | awk '{print $2 + $4}'"); @@ -72,17 +104,54 @@ namespace Infrastructure } catch (Exception ex) { - //LogHelper.Error(ex); + Console.WriteLine(ex.Message); } return runTime; } } + /// + /// 内存信息 + /// public class MemoryMetrics { public double Total { get; set; } public double Used { get; set; } public double Free { get; set; } + + public string UsedRam { get; set; } + /// + /// CPU使用率% + /// + public string CPURate { get; set; } + /// + /// 总内存 GB + /// + public string TotalRAM { get; set; } + /// + /// 内存使用率 % + /// + public string RAMRate { get; set; } + /// + /// 空闲内存 + /// + public string FreeRam { get; set; } + } + + public class DiskInfo + { + /// + /// 磁盘名 + /// + public string DickName { get; set; } + public string TypeName { get; set; } + public long TotalFree { get; set; } + public long TotalSize { get; set; } + /// + /// 可使用 + /// + public long AvailableFreeSpace { get; set; } + public decimal AvailablePercent { get; set; } } public class MemoryMetricsClient @@ -96,6 +165,10 @@ namespace Infrastructure return GetWindowsMetrics(); } + /// + /// windows系统获取内存信息 + /// + /// private MemoryMetrics GetWindowsMetrics() { string output = ShellHelper.Cmd("wmic", "OS get FreePhysicalMemory,TotalVisibleMemorySize /Value"); @@ -112,6 +185,20 @@ namespace Infrastructure return metrics; } + /// + /// windows 获取磁盘信息 + /// + private void GetWindowsDiskInfo() + { + string output = ShellHelper.Cmd("wmic", "diskdrive get Size /Value"); + + Console.WriteLine(output); + } + + /// + /// Unix系统获取 + /// + /// private MemoryMetrics GetUnixMetrics() { string output = ShellHelper.Bash("free -m"); @@ -128,23 +215,4 @@ namespace Infrastructure } } - public class ComputerInfo - { - /// - /// CPU使用率 - /// - public string CPURate { get; set; } - /// - /// 总内存 - /// - public string TotalRAM { get; set; } - /// - /// 内存使用率 - /// - public string RAMRate { get; set; } - /// - /// 系统运行时间 - /// - public string RunTime { get; set; } - } } \ No newline at end of file diff --git a/Infrastructure/Helper/ShellHelper.cs b/Infrastructure/Helper/ShellHelper.cs index 4ca9e78..6c56ad1 100644 --- a/Infrastructure/Helper/ShellHelper.cs +++ b/Infrastructure/Helper/ShellHelper.cs @@ -7,6 +7,11 @@ namespace Infrastructure { public class ShellHelper { + /// + /// linux 系统命令 + /// + /// + /// public static string Bash(string command) { var escapedArgs = command.Replace("\"", "\\\""); @@ -28,6 +33,12 @@ namespace Infrastructure return result; } + /// + /// windows系统命令 + /// + /// + /// + /// public static string Cmd(string fileName, string args) { string output = string.Empty; diff --git a/ZR.Vue/src/views/monitor/server/index.vue b/ZR.Vue/src/views/monitor/server/index.vue index e99bf99..1688005 100644 --- a/ZR.Vue/src/views/monitor/server/index.vue +++ b/ZR.Vue/src/views/monitor/server/index.vue @@ -1,87 +1,62 @@ @@ -343,7 +284,27 @@ table tr { height: 30px; } .is-leaf { - text-align: left; + text-align: center; padding: 0 10px; } +.title { + text-align: center; + font-size: 15px; + font-weight: 500; + color: #999; + margin-bottom: 16px; +} +.footer { + text-align: center; + font-size: 15px; + font-weight: 500; + color: #999; + margin-top: -5px; + margin-bottom: 10px; +} +.content { + text-align: center; + margin-top: 5px; + margin-bottom: 5px; +} \ No newline at end of file