Merge remote-tracking branch 'origin/abp' into abp

This commit is contained in:
橙子
2024-09-18 22:17:55 +08:00
3 changed files with 108 additions and 31 deletions

View File

@@ -78,12 +78,25 @@ namespace Yi.Framework.Core.Helper
return 0; return 0;
} }
} }
/// <summary>
/// CPU使用情况
/// </summary>
/// <returns></returns>
public static CPUMetrics GetCPUMetrics()
{
CPUMetrics cpuMetrics = new CPUMetrics();
var cpudetail = GetCPUDetails();
cpuMetrics.CoreTotal = cpudetail.Cores;
cpuMetrics.LogicalProcessors =cpudetail.LogicalProcessors;
cpuMetrics.CPURate = Math.Ceiling(ParseToDouble(GetCPURate()));
cpuMetrics.FreeRate = 1 - cpuMetrics.CPURate;
return cpuMetrics;
}
/// <summary> /// <summary>
/// 内存使用情况 /// 内存使用情况
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public static MemoryMetrics GetComputerInfo() public static MemoryMetrics GetMemoryMetrics()
{ {
try try
{ {
@@ -94,7 +107,7 @@ namespace Yi.Framework.Core.Helper
memoryMetrics.UsedRam = Math.Round(memoryMetrics.Used / 1024, 2) + "GB"; memoryMetrics.UsedRam = Math.Round(memoryMetrics.Used / 1024, 2) + "GB";
memoryMetrics.TotalRAM = Math.Round(memoryMetrics.Total / 1024, 2) + "GB"; memoryMetrics.TotalRAM = Math.Round(memoryMetrics.Total / 1024, 2) + "GB";
memoryMetrics.RAMRate = Math.Ceiling(100 * memoryMetrics.Used / memoryMetrics.Total).ToString() + "%"; memoryMetrics.RAMRate = Math.Ceiling(100 * memoryMetrics.Used / memoryMetrics.Total).ToString() + "%";
memoryMetrics.CPURate = Math.Ceiling(ParseToDouble(GetCPURate()));
return memoryMetrics; return memoryMetrics;
} }
catch (Exception ex) catch (Exception ex)
@@ -105,7 +118,7 @@ namespace Yi.Framework.Core.Helper
} }
/// <summary> /// <summary>
/// 获取内存大小 /// 获取磁盘信息
/// </summary> /// </summary>
/// <returns></returns> /// <returns></returns>
public static List<DiskInfo> GetDiskInfos() public static List<DiskInfo> GetDiskInfos()
@@ -174,7 +187,7 @@ namespace Yi.Framework.Core.Helper
var isUnix = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || RuntimeInformation.IsOSPlatform(OSPlatform.Linux); var isUnix = RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
return isUnix; return isUnix;
} }
public static string GetCPURate() public static string GetCPURate()
{ {
string cpuRate; string cpuRate;
@@ -221,8 +234,69 @@ namespace Yi.Framework.Core.Helper
} }
return runTime; return runTime;
} }
}
public static CPUInfo GetCPUDetails()
{
int logicalProcessors = 0;
int cores = 0;
if (IsUnix())
{
string logicalOutput = ShellHelper.Bash("lscpu | grep '^CPU(s):' | awk '{print $2}'");
logicalProcessors = int.Parse(logicalOutput.Trim());
string coresOutput = ShellHelper.Bash("lscpu | grep 'Core(s) per socket:' | awk '{print $4}'");
string socketsOutput = ShellHelper.Bash("lscpu | grep 'Socket(s):' | awk '{print $2}'");
cores = int.Parse(coresOutput.Trim()) * int.Parse(socketsOutput.Trim());
}
else
{
string output = ShellHelper.Cmd("wmic", "cpu get NumberOfCores,NumberOfLogicalProcessors /format:csv");
var lines = output.Split(new[] { '\n', '\r' }, StringSplitOptions.RemoveEmptyEntries);
if (lines.Length > 1)
{
var values = lines[1].Split(',');
cores = int.Parse(values[1].Trim());
logicalProcessors =int.Parse(values[2].Trim());
}
}
return new CPUInfo
{
LogicalProcessors = logicalProcessors,
Cores = cores
};
}
}
public class CPUInfo
{
public int LogicalProcessors { get; set; }
public int Cores { get; set; }
}
public class CPUMetrics
{
/// <summary>
/// 内核数
/// </summary>
public int CoreTotal { get; set; }
/// <summary>
/// 逻辑处理器数
/// </summary>
public int LogicalProcessors { get; set; }
/// <summary>
/// CPU使用率%
/// </summary>
public double CPURate { get; set; }
/// <summary>
/// CPU空闲率%
/// </summary>
public double FreeRate { get; set; }
}
/// <summary> /// <summary>
/// 内存信息 /// 内存信息
/// </summary> /// </summary>
@@ -236,10 +310,7 @@ namespace Yi.Framework.Core.Helper
public double Free { get; set; } public double Free { get; set; }
public string UsedRam { get; set; } public string UsedRam { get; set; }
/// <summary>
/// CPU使用率%
/// </summary>
public double CPURate { get; set; }
/// <summary> /// <summary>
/// 总内存 GB /// 总内存 GB
/// </summary> /// </summary>
@@ -306,20 +377,25 @@ namespace Yi.Framework.Core.Helper
/// <returns></returns> /// <returns></returns>
public MemoryMetrics GetUnixMetrics() public MemoryMetrics GetUnixMetrics()
{ {
string output = ShellHelper.Bash("free -m | awk '{print $2,$3,$4,$5,$6}'"); string output = ShellHelper.Bash(@"
# 从 /proc/meminfo 文件中提取总内存
total_mem=$(cat /proc/meminfo | grep -i ""MemTotal"" | awk '{print $2}')
# 从 /proc/meminfo 文件中提取剩余内存
free_mem=$(cat /proc/meminfo | grep -i ""MemFree"" | awk '{print $2}')
# 显示提取的信息
echo $total_mem $used_mem $free_mem
");
var metrics = new MemoryMetrics(); var metrics = new MemoryMetrics();
var lines = output.Split('\n', (char)StringSplitOptions.RemoveEmptyEntries);
if (!string.IsNullOrWhiteSpace(output))
if (lines.Length <= 0) return metrics;
if (lines != null && lines.Length > 0)
{ {
var memory = lines[1].Split(' ', (char)StringSplitOptions.RemoveEmptyEntries); var memory = output.Split(' ', (char)StringSplitOptions.RemoveEmptyEntries);
if (memory.Length >= 3) if (memory.Length >= 2)
{ {
metrics.Total = double.Parse(memory[0]); metrics.Total = Math.Round(double.Parse(memory[0]) / 1024, 0);
metrics.Used = double.Parse(memory[1]);
metrics.Free = double.Parse(memory[2]);//m metrics.Free = Math.Round(double.Parse(memory[1])/ 1024, 0);//m
metrics.Used = metrics.Total - metrics.Free;
} }
} }
return metrics; return metrics;

View File

@@ -21,7 +21,7 @@ namespace Yi.Framework.Rbac.Application.Services.Monitor
[HttpGet("monitor-server/info")] [HttpGet("monitor-server/info")]
public object GetInfo() public object GetInfo()
{ {
int cpuNum = Environment.ProcessorCount;
string computerName = Environment.MachineName; string computerName = Environment.MachineName;
string osName = RuntimeInformation.OSDescription; string osName = RuntimeInformation.OSDescription;
string osArch = RuntimeInformation.OSArchitecture.ToString(); string osArch = RuntimeInformation.OSArchitecture.ToString();
@@ -35,9 +35,10 @@ namespace Yi.Framework.Rbac.Application.Services.Monitor
string programRunTime = DateTimeHelper.FormatTime(long.Parse((DateTime.Now - programStartTime).TotalMilliseconds.ToString().Split('.')[0])); string programRunTime = DateTimeHelper.FormatTime(long.Parse((DateTime.Now - programStartTime).TotalMilliseconds.ToString().Split('.')[0]));
var data = new var data = new
{ {
cpu = ComputerHelper.GetComputerInfo(), memory = ComputerHelper.GetMemoryMetrics(),
cpu = ComputerHelper.GetCPUMetrics(),
disk = ComputerHelper.GetDiskInfos(), disk = ComputerHelper.GetDiskInfos(),
sys = new { cpuNum, computerName, osName, osArch, serverIP, runTime = sysRunTime }, sys = new {computerName, osName, osArch, serverIP, runTime = sysRunTime },
app = new app = new
{ {
name = _hostEnvironment.EnvironmentName, name = _hostEnvironment.EnvironmentName,

View File

@@ -15,11 +15,11 @@
<tbody> <tbody>
<tr> <tr>
<td class="el-table__cell is-leaf"><div class="cell">核心数</div></td> <td class="el-table__cell is-leaf"><div class="cell">核心数</div></td>
<td class="el-table__cell is-leaf"><div class="cell" v-if="server.sys">{{ server.sys.cpuNum }}</div></td> <td class="el-table__cell is-leaf"><div class="cell" v-if="server.sys">{{ server.cpu.coreTotal }}</div></td>
</tr> </tr>
<tr> <tr>
<td class="el-table__cell is-leaf"><div class="cell">总Cpu数</div></td> <td class="el-table__cell is-leaf"><div class="cell">逻辑处理器</div></td>
<td class="el-table__cell is-leaf"><div class="cell" v-if="server.cpu">{{ server.cpu.total }}MB</div></td> <td class="el-table__cell is-leaf"><div class="cell" v-if="server.cpu">{{ server.cpu.logicalProcessors }}</div></td>
</tr> </tr>
<tr> <tr>
<td class="el-table__cell is-leaf"><div class="cell">系统使用率</div></td> <td class="el-table__cell is-leaf"><div class="cell">系统使用率</div></td>
@@ -49,19 +49,19 @@
<tbody> <tbody>
<tr> <tr>
<td class="el-table__cell is-leaf"><div class="cell">总内存</div></td> <td class="el-table__cell is-leaf"><div class="cell">总内存</div></td>
<td class="el-table__cell is-leaf"><div class="cell" v-if="server.cpu">{{ server.cpu.totalRAM }}</div></td> <td class="el-table__cell is-leaf"><div class="cell" v-if="server.cpu">{{ server.memory.totalRAM }}</div></td>
</tr> </tr>
<tr> <tr>
<td class="el-table__cell is-leaf"><div class="cell">已用内存</div></td> <td class="el-table__cell is-leaf"><div class="cell">已用内存</div></td>
<td class="el-table__cell is-leaf"><div class="cell" v-if="server.cpu">{{ server.cpu.usedRam}}</div></td> <td class="el-table__cell is-leaf"><div class="cell" v-if="server.cpu">{{ server.memory.usedRam}}</div></td>
</tr> </tr>
<tr> <tr>
<td class="el-table__cell is-leaf"><div class="cell">剩余内存</div></td> <td class="el-table__cell is-leaf"><div class="cell">剩余内存</div></td>
<td class="el-table__cell is-leaf"><div class="cell" v-if="server.cpu">{{ server.cpu.freeRam }}</div></td> <td class="el-table__cell is-leaf"><div class="cell" v-if="server.cpu">{{ server.memory.freeRam }}</div></td>
</tr> </tr>
<tr> <tr>
<td class="el-table__cell is-leaf"><div class="cell">使用率</div></td> <td class="el-table__cell is-leaf"><div class="cell">使用率</div></td>
<td class="el-table__cell is-leaf"><div class="cell" v-if="server.cpu" :class="{'text-danger': server.cpu.ramRate > 80}">{{ server.cpu.ramRate }}</div></td> <td class="el-table__cell is-leaf"><div class="cell" v-if="server.cpu" :class="{'text-danger': server.memory.ramRate > 80}">{{ server.memory.ramRate }}</div></td>
</tr> </tr>
</tbody> </tbody>