|
|
@@ -18,8 +18,19 @@ public static class ComputerUtil
|
|
|
public static MemoryMetrics GetComputerInfo()
|
|
|
{
|
|
|
MemoryMetricsClient client = new();
|
|
|
- MemoryMetrics memoryMetrics = IsUnix() ? client.GetUnixMetrics() : client.GetWindowsMetrics();
|
|
|
-
|
|
|
+ MemoryMetrics memoryMetrics;
|
|
|
+ if (IsMacOS())
|
|
|
+ {
|
|
|
+ memoryMetrics = client.GetMacOSMetrics();
|
|
|
+ }
|
|
|
+ else if (IsUnix())
|
|
|
+ {
|
|
|
+ memoryMetrics = client.GetUnixMetrics();
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ memoryMetrics = client.GetWindowsMetrics();
|
|
|
+ }
|
|
|
memoryMetrics.FreeRam = Math.Round(memoryMetrics.Free / 1024, 2) + "GB";
|
|
|
memoryMetrics.UsedRam = Math.Round(memoryMetrics.Used / 1024, 2) + "GB";
|
|
|
memoryMetrics.TotalRam = Math.Round(memoryMetrics.Total / 1024, 2) + "GB";
|
|
|
@@ -28,6 +39,33 @@ public static class ComputerUtil
|
|
|
return memoryMetrics;
|
|
|
}
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// 获取正确的操作系统版本(Linux获取发行版本)
|
|
|
+ /// </summary>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static String GetOSInfo()
|
|
|
+ {
|
|
|
+ string opeartion = string.Empty;
|
|
|
+ if (IsMacOS())
|
|
|
+ {
|
|
|
+ var output = ShellHelper.Bash("sw_vers | awk 'NR<=2{printf \"%s \", $NF}'");
|
|
|
+ if (output != null)
|
|
|
+ {
|
|
|
+ opeartion = output.Replace("%", string.Empty);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if (IsUnix())
|
|
|
+ {
|
|
|
+ var output = ShellHelper.Bash("awk -F= '/^VERSION_ID/ {print $2}' /etc/os-release | tr -d '\"'");
|
|
|
+ opeartion = output ?? string.Empty;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ opeartion = RuntimeInformation.OSDescription;
|
|
|
+ }
|
|
|
+ return opeartion;
|
|
|
+ }
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 磁盘信息
|
|
|
/// </summary>
|
|
|
@@ -35,8 +73,30 @@ public static class ComputerUtil
|
|
|
public static List<DiskInfo> GetDiskInfos()
|
|
|
{
|
|
|
var diskInfos = new List<DiskInfo>();
|
|
|
+ if (IsMacOS())
|
|
|
+ {
|
|
|
+ var output = ShellHelper.Bash(@"df -m | awk '/^\/dev\/disk/ {print $1,$2,$3,$4,$5}'");
|
|
|
+ var disks = output.Split('\n', StringSplitOptions.RemoveEmptyEntries);
|
|
|
+ if (disks.Length < 1) return diskInfos;
|
|
|
+ foreach (var item in disks)
|
|
|
+ {
|
|
|
+ var disk = item.Split(' ', (char)StringSplitOptions.RemoveEmptyEntries);
|
|
|
+ if (disk == null || disk.Length < 5)
|
|
|
+ continue;
|
|
|
|
|
|
- if (IsUnix())
|
|
|
+ var diskInfo = new DiskInfo()
|
|
|
+ {
|
|
|
+ DiskName = disk[0],
|
|
|
+ TypeName = ShellHelper.Bash("diskutil info " + disk[0] + " | awk '/File System Personality/ {print $4}'").Replace("\n",string.Empty),
|
|
|
+ TotalSize = long.Parse(disk[1]) / 1024,
|
|
|
+ Used = long.Parse(disk[2]) / 1024,
|
|
|
+ AvailableFreeSpace = long.Parse(disk[3]) / 1024,
|
|
|
+ AvailablePercent = decimal.Parse(disk[4].Replace("%", ""))
|
|
|
+ };
|
|
|
+ diskInfos.Add(diskInfo);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if (IsUnix())
|
|
|
{
|
|
|
var output = ShellHelper.Bash(@"df -mT | awk '/^\/dev\/(sd|vd|xvd|nvme|sda|vda)/ {print $1,$2,$3,$4,$5,$6}'");
|
|
|
var disks = output.Split('\n', StringSplitOptions.RemoveEmptyEntries);
|
|
|
@@ -100,13 +160,23 @@ public static class ComputerUtil
|
|
|
|
|
|
public static bool IsUnix()
|
|
|
{
|
|
|
- return RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
|
|
|
+ return RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
|
|
|
+ }
|
|
|
+
|
|
|
+ public static bool IsMacOS()
|
|
|
+ {
|
|
|
+ return RuntimeInformation.IsOSPlatform(OSPlatform.OSX);
|
|
|
}
|
|
|
|
|
|
public static string GetCPURate()
|
|
|
{
|
|
|
string cpuRate;
|
|
|
- if (IsUnix())
|
|
|
+ if (IsMacOS())
|
|
|
+ {
|
|
|
+ string output = ShellUtil.Bash("top -l 1 | grep \"CPU usage\" | awk '{print $3 + $5}'");
|
|
|
+ cpuRate = output.Trim();
|
|
|
+ }
|
|
|
+ else if (IsUnix())
|
|
|
{
|
|
|
string output = ShellUtil.Bash("top -b -n1 | grep \"Cpu(s)\" | awk '{print $2 + $4}'");
|
|
|
cpuRate = output.Trim();
|
|
|
@@ -126,7 +196,16 @@ public static class ComputerUtil
|
|
|
public static string GetRunTime()
|
|
|
{
|
|
|
string runTime = string.Empty;
|
|
|
- if (IsUnix())
|
|
|
+ if (IsMacOS())
|
|
|
+ {
|
|
|
+ //macOS 获取系统启动时间:
|
|
|
+ //sysctl -n kern.boottime | awk '{print $4}' | tr -d ','
|
|
|
+ //返回:1705379131
|
|
|
+ //使用date格式化即可
|
|
|
+ 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());
|
|
|
+ }
|
|
|
+ else if (IsUnix())
|
|
|
{
|
|
|
string output = ShellUtil.Bash("uptime -s").Trim();
|
|
|
runTime = DateTimeUtil.FormatTime((DateTime.Now - output.ParseToDateTime()).TotalMilliseconds.ToString().Split('.')[0].ParseToLong());
|
|
|
@@ -272,6 +351,22 @@ public class MemoryMetricsClient
|
|
|
}
|
|
|
return metrics;
|
|
|
}
|
|
|
+ /// <summary>
|
|
|
+ /// macOS系统获取
|
|
|
+ /// </summary>
|
|
|
+ /// <returns></returns>
|
|
|
+ public MemoryMetrics GetMacOSMetrics()
|
|
|
+ {
|
|
|
+ var metrics = new MemoryMetrics();
|
|
|
+ //物理内存大小
|
|
|
+ var total = ShellUtil.Bash("sysctl -n hw.memsize | awk '{printf \"%.2f\", $1/1024/1024}'");
|
|
|
+ metrics.Total = float.Parse(total.Replace("%", string.Empty));
|
|
|
+ //TODO:占用内存,检查效率
|
|
|
+ var free = ShellUtil.Bash("top -l 1 -s 0 | awk '/PhysMem/ {print $6+$8}'");
|
|
|
+ metrics.Free = float.Parse(free);
|
|
|
+ metrics.Used = metrics.Total - metrics.Free;
|
|
|
+ return metrics;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public class ShellUtil
|