135 lines
3.7 KiB
C#
135 lines
3.7 KiB
C#
using System;
|
||
using System.Collections.Generic;
|
||
using System.Globalization;
|
||
using System.IO;
|
||
using System.Runtime.InteropServices;
|
||
using Hua.DotNet.Code.Extension;
|
||
|
||
namespace Hua.DotNet.Code.Helper;
|
||
|
||
public class ComputerHelper
|
||
{
|
||
public static MemoryMetrics GetComputerInfo()
|
||
{
|
||
try
|
||
{
|
||
MemoryMetricsClient memoryMetricsClient = new MemoryMetricsClient();
|
||
MemoryMetrics memoryMetrics = (IsUnix() ? memoryMetricsClient.GetUnixMetrics() : memoryMetricsClient.GetWindowsMetrics());
|
||
memoryMetrics.FreeRam = Math.Round(memoryMetrics.Free / 1024.0, 2) + "GB";
|
||
memoryMetrics.UsedRam = Math.Round(memoryMetrics.Used / 1024.0, 2) + "GB";
|
||
memoryMetrics.TotalRAM = Math.Round(memoryMetrics.Total / 1024.0, 2) + "GB";
|
||
memoryMetrics.RAMRate = Math.Ceiling(100.0 * memoryMetrics.Used / memoryMetrics.Total).ToString(CultureInfo.InvariantCulture) + "%";
|
||
memoryMetrics.CPURate = Math.Ceiling(GetCPURate().ToDouble()) + "%";
|
||
return memoryMetrics;
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Log("获取内存使用出错,msg=" + ex.Message + "," + ex.StackTrace);
|
||
}
|
||
return new MemoryMetrics();
|
||
}
|
||
|
||
public static List<DiskInfo> GetDiskInfos()
|
||
{
|
||
List<DiskInfo> list = new List<DiskInfo>();
|
||
if (IsUnix())
|
||
{
|
||
try
|
||
{
|
||
string[] array = ShellHelper.Bash("df -m / | awk '{print $2,$3,$4,$5,$6}'").Split('\n');
|
||
if (array.Length == 0)
|
||
{
|
||
return list;
|
||
}
|
||
string[] array2 = array[1].Split(' ', '\u0001');
|
||
if (array2.Length == 0)
|
||
{
|
||
return list;
|
||
}
|
||
DiskInfo item = new DiskInfo
|
||
{
|
||
DiskName = "/",
|
||
TotalSize = long.Parse(array2[0]) / 1024,
|
||
Used = long.Parse(array2[1]) / 1024,
|
||
AvailableFreeSpace = long.Parse(array2[2]) / 1024,
|
||
AvailablePercent = decimal.Parse(array2[3].Replace("%", ""))
|
||
};
|
||
list.Add(item);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Log("获取磁盘信息出错了" + ex.Message);
|
||
}
|
||
}
|
||
else
|
||
{
|
||
DriveInfo[] drives = DriveInfo.GetDrives();
|
||
foreach (DriveInfo driveInfo in drives)
|
||
{
|
||
try
|
||
{
|
||
DiskInfo diskInfo = new DiskInfo
|
||
{
|
||
DiskName = driveInfo.Name,
|
||
TypeName = driveInfo.DriveType.ToString(),
|
||
TotalSize = driveInfo.TotalSize / 1024 / 1024 / 1024,
|
||
AvailableFreeSpace = driveInfo.AvailableFreeSpace / 1024 / 1024 / 1024
|
||
};
|
||
diskInfo.Used = diskInfo.TotalSize - diskInfo.AvailableFreeSpace;
|
||
diskInfo.AvailablePercent = decimal.Ceiling((decimal)diskInfo.Used / (decimal)diskInfo.TotalSize * 100m);
|
||
list.Add(diskInfo);
|
||
}
|
||
catch (Exception ex2)
|
||
{
|
||
LogHelper.Log("获取磁盘信息出错了" + ex2.Message);
|
||
}
|
||
}
|
||
}
|
||
return list;
|
||
}
|
||
|
||
public static bool IsUnix()
|
||
{
|
||
if (!RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
|
||
{
|
||
return RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public static string GetCPURate()
|
||
{
|
||
if (IsUnix())
|
||
{
|
||
return ShellHelper.Bash("top -b -n1 | grep \"Cpu(s)\" | awk '{print $2 + $4}'").Trim();
|
||
}
|
||
return ShellHelper.Cmd("wmic", "cpu get LoadPercentage").Replace("LoadPercentage", string.Empty).Trim();
|
||
}
|
||
|
||
public static string GetRunTime()
|
||
{
|
||
string result = string.Empty;
|
||
try
|
||
{
|
||
if (IsUnix())
|
||
{
|
||
string str = ShellHelper.Bash("uptime -s").Trim();
|
||
result = DateTimeHelper.FormatTime((DateTime.Now - str.ToDateTime()).TotalMilliseconds.ToString().Split('.')[0].ToLong());
|
||
}
|
||
else
|
||
{
|
||
string[] array = ShellHelper.Cmd("wmic", "OS get LastBootUpTime/Value").Split('=', '\u0001');
|
||
if (array.Length == 2)
|
||
{
|
||
result = DateTimeHelper.FormatTime((DateTime.Now - array[1].Split('.')[0].ToDateTime()).TotalMilliseconds.ToString(CultureInfo.InvariantCulture).Split('.')[0].ToLong());
|
||
}
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
LogHelper.Log("获取runTime出错" + ex.Message);
|
||
}
|
||
return result;
|
||
}
|
||
}
|