add 服务监控

This commit is contained in:
2021-06-25 15:49:06 +08:00
parent a828a62d20
commit 87d303afb6
7 changed files with 569 additions and 14 deletions

View File

@@ -1,5 +1,6 @@
using Furion.RemoteRequest.Extensions;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
@@ -26,10 +27,8 @@ namespace Ewide.Core
var ramInfo = GetRamInfo();
return new
{
TotalRam = Math.Ceiling(ramInfo.Total / 1024).ToString() + " GB", // 总内存
RamRate = Math.Ceiling(100 * ramInfo.Used / ramInfo.Total), // 内存使用率
CpuRate = Math.Ceiling(double.Parse(GetCPURate())), // cpu使用率
RunTime = GetRunTime()
};
}
@@ -40,20 +39,28 @@ namespace Ewide.Core
public static async Task<dynamic> GetMachineBaseInfo()
{
var assemblyName = typeof(Furion.App).Assembly.GetName();
//var networkInfo = NetworkInfo.GetNetworkInfo();
//var (Received, Send) = networkInfo.GetInternetSpeed(1000);
return new
{
WanIp = await GetWanIpFromPCOnline(), // 外网IP
SendAndReceived = "",// "上行" + Math.Round(networkInfo.SendLength / 1024.0 / 1024 / 1024, 2) + "GB 下行" + Math.Round(networkInfo.ReceivedLength / 1024.0 / 1024 / 1024, 2) + "GB", // 上下行流量统计
LanIp = "",//networkInfo.AddressIpv4.ToString(), // 局域网IP
LanIp = Dns.GetHostAddresses(string.Empty).Last().ToString(), // 局域网IP
IpMac = "",//networkInfo.Mac, // Mac地址
HostName = Environment.MachineName, // HostName
CurrentDirectory = Environment.CurrentDirectory, // 系统路径
SystemOs = RuntimeInformation.OSDescription, // 系统名称
OsArchitecture = Environment.OSVersion.Platform.ToString() + " " + RuntimeInformation.OSArchitecture.ToString(), // 系统架构
ProcessorCount = Environment.ProcessorCount.ToString() + "核", // CPU核心数
ProcessorCount = Environment.ProcessorCount, // CPU核心数
FrameworkDescription = RuntimeInformation.FrameworkDescription + " + " + assemblyName.Name.ToString() + assemblyName.Version.ToString(), // .NET和Furion版本
NetworkSpeed = ""//"上行" + Send / 1024 + "kb/s 下行" + Received / 1024 + "kb/s" // 网络速度
NetworkSpeed = "",//"上行" + Send / 1024 + "kb/s 下行" + Received / 1024 + "kb/s" // 网络速度
// Cpu名称
CpuName = GetCPUName(),
// Cpu基准速度
CpuBaseSpeed = GetCPUSpeed(),
// 内存总量
TotalRam = GetRamInfo().Total,
// 运行时间
RunTime = (long)(DateTime.Now - Process.GetCurrentProcess().StartTime).TotalMilliseconds,
// 磁盘信息
DiskInfo = GetDiskInfo(),
};
}
@@ -94,24 +101,86 @@ namespace Ewide.Core
return RuntimeInformation.IsOSPlatform(OSPlatform.OSX) || RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
}
private static string GetCPUName()
{
string result;
if (IsUnix())
{
// ??????
var output = ShellUtil.Bash("");
result = output.Trim();
}
else
{
var output = ShellUtil.Cmd("wmic", "cpu get Name");
result = output.Replace("Name", string.Empty).Trim();
}
return result;
}
private static string GetCPUSpeed()
{
string result;
if (IsUnix())
{
// ??????
var output = ShellUtil.Bash("");
result = output.Trim();
}
else
{
var output = ShellUtil.Cmd("wmic", "cpu get CurrentClockSpeed");
result = output.Replace("CurrentClockSpeed", string.Empty).Trim();
}
return result;
}
private static List<Dictionary<string, string>> GetDiskInfo()
{
var result = new List<Dictionary<string, string>>();
if (IsUnix())
{
// ??????
var output = ShellUtil.Bash("");
}
else
{
var output = ShellUtil.Cmd("wmic", "LOGICALDISK get Name,Description,FileSystem,Size,FreeSpace");
var strArray = output.Replace("\r", "").Trim().Split('\n')
.Select(p => System.Text.RegularExpressions.Regex.Replace(p.Trim(), @"\s+", ",").Split(','));
var keyArray = strArray.First();
var valueArray = strArray.Where((p, i) => i > 0).ToArray();
foreach(var value in valueArray)
{
var dict = new Dictionary<string, string>();
for(var i = 0;i<keyArray.Length;i++)
{
dict.Add(keyArray[i], value[i]);
}
result.Add(dict);
}
}
return result;
}
/// <summary>
/// 获取CPU使用率
/// </summary>
/// <returns></returns>
private static string GetCPURate()
{
string cpuRate;
string result;
if (IsUnix())
{
var output = ShellUtil.Bash("top -b -n1 | grep \"Cpu(s)\" | awk '{print $2 + $4}'");
cpuRate = output.Trim();
result = output.Trim();
}
else
{
var output = ShellUtil.Cmd("wmic", "cpu get LoadPercentage");
cpuRate = output.Replace("LoadPercentage", string.Empty).Trim();
result = output.Replace("LoadPercentage", string.Empty).Trim();
}
return cpuRate;
return result;
}
/// <summary>