增加磁盘使用率
This commit is contained in:
@@ -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()
|
||||
/// <summary>
|
||||
/// 内存使用情况
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取内存大小
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static List<DiskInfo> GetDiskInfos()
|
||||
{
|
||||
List<DiskInfo> 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;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 内存信息
|
||||
/// </summary>
|
||||
public class MemoryMetrics
|
||||
{
|
||||
public double Total { get; set; }
|
||||
public double Used { get; set; }
|
||||
public double Free { get; set; }
|
||||
|
||||
public string UsedRam { get; set; }
|
||||
/// <summary>
|
||||
/// CPU使用率%
|
||||
/// </summary>
|
||||
public string CPURate { get; set; }
|
||||
/// <summary>
|
||||
/// 总内存 GB
|
||||
/// </summary>
|
||||
public string TotalRAM { get; set; }
|
||||
/// <summary>
|
||||
/// 内存使用率 %
|
||||
/// </summary>
|
||||
public string RAMRate { get; set; }
|
||||
/// <summary>
|
||||
/// 空闲内存
|
||||
/// </summary>
|
||||
public string FreeRam { get; set; }
|
||||
}
|
||||
|
||||
public class DiskInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// 磁盘名
|
||||
/// </summary>
|
||||
public string DickName { get; set; }
|
||||
public string TypeName { get; set; }
|
||||
public long TotalFree { get; set; }
|
||||
public long TotalSize { get; set; }
|
||||
/// <summary>
|
||||
/// 可使用
|
||||
/// </summary>
|
||||
public long AvailableFreeSpace { get; set; }
|
||||
public decimal AvailablePercent { get; set; }
|
||||
}
|
||||
|
||||
public class MemoryMetricsClient
|
||||
@@ -96,6 +165,10 @@ namespace Infrastructure
|
||||
return GetWindowsMetrics();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// windows系统获取内存信息
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private MemoryMetrics GetWindowsMetrics()
|
||||
{
|
||||
string output = ShellHelper.Cmd("wmic", "OS get FreePhysicalMemory,TotalVisibleMemorySize /Value");
|
||||
@@ -112,6 +185,20 @@ namespace Infrastructure
|
||||
return metrics;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// windows 获取磁盘信息
|
||||
/// </summary>
|
||||
private void GetWindowsDiskInfo()
|
||||
{
|
||||
string output = ShellHelper.Cmd("wmic", "diskdrive get Size /Value");
|
||||
|
||||
Console.WriteLine(output);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Unix系统获取
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private MemoryMetrics GetUnixMetrics()
|
||||
{
|
||||
string output = ShellHelper.Bash("free -m");
|
||||
@@ -128,23 +215,4 @@ namespace Infrastructure
|
||||
}
|
||||
}
|
||||
|
||||
public class ComputerInfo
|
||||
{
|
||||
/// <summary>
|
||||
/// CPU使用率
|
||||
/// </summary>
|
||||
public string CPURate { get; set; }
|
||||
/// <summary>
|
||||
/// 总内存
|
||||
/// </summary>
|
||||
public string TotalRAM { get; set; }
|
||||
/// <summary>
|
||||
/// 内存使用率
|
||||
/// </summary>
|
||||
public string RAMRate { get; set; }
|
||||
/// <summary>
|
||||
/// 系统运行时间
|
||||
/// </summary>
|
||||
public string RunTime { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,11 @@ namespace Infrastructure
|
||||
{
|
||||
public class ShellHelper
|
||||
{
|
||||
/// <summary>
|
||||
/// linux 系统命令
|
||||
/// </summary>
|
||||
/// <param name="command"></param>
|
||||
/// <returns></returns>
|
||||
public static string Bash(string command)
|
||||
{
|
||||
var escapedArgs = command.Replace("\"", "\\\"");
|
||||
@@ -28,6 +33,12 @@ namespace Infrastructure
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// windows系统命令
|
||||
/// </summary>
|
||||
/// <param name="fileName"></param>
|
||||
/// <param name="args"></param>
|
||||
/// <returns></returns>
|
||||
public static string Cmd(string fileName, string args)
|
||||
{
|
||||
string output = string.Empty;
|
||||
|
||||
Reference in New Issue
Block a user