48 lines
1.3 KiB
C#
48 lines
1.3 KiB
C#
using System;
|
|
|
|
namespace Hua.DotNet.Code.Helper;
|
|
|
|
public class MemoryMetricsClient
|
|
{
|
|
public MemoryMetrics GetWindowsMetrics()
|
|
{
|
|
string text = ShellHelper.Cmd("wmic", "OS get FreePhysicalMemory,TotalVisibleMemorySize /Value");
|
|
MemoryMetrics memoryMetrics = new MemoryMetrics();
|
|
string[] array = text.Trim().Split('\n', '\u0001');
|
|
if (array.Length == 0)
|
|
{
|
|
return memoryMetrics;
|
|
}
|
|
string[] array2 = array[0].Split('=', '\u0001');
|
|
string[] array3 = array[1].Split('=', '\u0001');
|
|
memoryMetrics.Total = Math.Round(double.Parse(array3[1]) / 1024.0, 0);
|
|
memoryMetrics.Free = Math.Round(double.Parse(array2[1]) / 1024.0, 0);
|
|
memoryMetrics.Used = memoryMetrics.Total - memoryMetrics.Free;
|
|
return memoryMetrics;
|
|
}
|
|
|
|
public MemoryMetrics GetUnixMetrics()
|
|
{
|
|
string text = ShellHelper.Bash("free -m | awk '{print $2,$3,$4,$5,$6}'");
|
|
MemoryMetrics memoryMetrics = new MemoryMetrics();
|
|
string[] array = text.Split('\n', '\u0001');
|
|
if (array.Length == 0)
|
|
{
|
|
return memoryMetrics;
|
|
}
|
|
if (array.Length == 0)
|
|
{
|
|
return memoryMetrics;
|
|
}
|
|
string[] array2 = array[1].Split(' ', '\u0001');
|
|
if (array2.Length < 3)
|
|
{
|
|
return memoryMetrics;
|
|
}
|
|
memoryMetrics.Total = double.Parse(array2[0]);
|
|
memoryMetrics.Used = double.Parse(array2[1]);
|
|
memoryMetrics.Free = double.Parse(array2[2]);
|
|
return memoryMetrics;
|
|
}
|
|
}
|