Files
Yi.Admin/Yi.Abp.Net8/module/rbac/Yi.Framework.Rbac.Application/Services/MonitorServerService.cs

59 lines
2.6 KiB
C#
Raw Normal View History

2023-12-11 09:55:12 +08:00
using System.Diagnostics;
2023-04-19 22:38:46 +08:00
using System.Runtime.InteropServices;
using Microsoft.AspNetCore.Hosting;
2023-12-11 09:55:12 +08:00
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Volo.Abp.Application.Services;
using Yi.Framework.Core.Helper;
using Yi.Framework.Rbac.Application.Contracts.IServices;
2023-04-19 19:40:47 +08:00
2023-12-11 09:55:12 +08:00
namespace Yi.Framework.Rbac.Application.Services
2023-04-19 22:38:46 +08:00
{
2023-12-11 09:55:12 +08:00
public class MonitorServerService : ApplicationService, IMonitorServerService
2023-04-19 22:38:46 +08:00
{
private IWebHostEnvironment _hostEnvironment;
private IHttpContextAccessor _httpContextAccessor;
public MonitorServerService(IWebHostEnvironment hostEnvironment, IHttpContextAccessor httpContextAccessor)
{
2023-10-07 17:51:05 +08:00
_hostEnvironment = hostEnvironment;
2023-04-19 22:38:46 +08:00
_httpContextAccessor = httpContextAccessor;
}
2023-12-11 09:55:12 +08:00
[HttpGet("monitor-server/info")]
2023-04-19 22:38:46 +08:00
public object GetInfo()
{
int cpuNum = Environment.ProcessorCount;
string computerName = Environment.MachineName;
string osName = RuntimeInformation.OSDescription;
string osArch = RuntimeInformation.OSArchitecture.ToString();
string version = RuntimeInformation.FrameworkDescription;
string appRAM = ((double)Process.GetCurrentProcess().WorkingSet64 / 1048576).ToString("N2") + " MB";
string startTime = Process.GetCurrentProcess().StartTime.ToString("yyyy-MM-dd HH:mm:ss");
string sysRunTime = ComputerHelper.GetRunTime();
string serverIP = _httpContextAccessor.HttpContext.Connection.LocalIpAddress.MapToIPv4().ToString() + ":" + _httpContextAccessor.HttpContext.Connection.LocalPort;//获取服务器IP
2023-04-19 19:40:47 +08:00
2023-04-19 22:38:46 +08:00
var programStartTime = Process.GetCurrentProcess().StartTime;
2023-12-11 09:55:12 +08:00
string programRunTime = DateTimeHelper.FormatTime(long.Parse((DateTime.Now - programStartTime).TotalMilliseconds.ToString().Split('.')[0]));
2023-04-19 22:38:46 +08:00
var data = new
{
cpu = ComputerHelper.GetComputerInfo(),
disk = ComputerHelper.GetDiskInfos(),
sys = new { cpuNum, computerName, osName, osArch, serverIP, runTime = sysRunTime },
app = new
{
name = _hostEnvironment.EnvironmentName,
rootPath = _hostEnvironment.ContentRootPath,
webRootPath = _hostEnvironment.WebRootPath,
version,
appRAM,
startTime,
runTime = programRunTime,
host = serverIP
},
};
2023-04-19 19:40:47 +08:00
2023-04-19 22:38:46 +08:00
return data;
}
2023-04-19 19:40:47 +08:00
2023-04-19 22:38:46 +08:00
}
}