MonitorServerService.cs
2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System.Diagnostics;
using System.Runtime.InteropServices;
using Microsoft.AspNetCore.Hosting;
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;
namespace Yi.Framework.Rbac.Application.Services.Monitor
{
public class MonitorServerService : ApplicationService, IMonitorServerService
{
private IWebHostEnvironment _hostEnvironment;
private IHttpContextAccessor _httpContextAccessor;
public MonitorServerService(IWebHostEnvironment hostEnvironment, IHttpContextAccessor httpContextAccessor)
{
_hostEnvironment = hostEnvironment;
_httpContextAccessor = httpContextAccessor;
}
[HttpGet("monitor-server/info")]
public object GetInfo()
{
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
var programStartTime = Process.GetCurrentProcess().StartTime;
string programRunTime = DateTimeHelper.FormatTime(long.Parse((DateTime.Now - programStartTime).TotalMilliseconds.ToString().Split('.')[0]));
var data = new
{
memory = ComputerHelper.GetMemoryMetrics(),
cpu = ComputerHelper.GetCPUMetrics(),
disk = ComputerHelper.GetDiskInfos(),
sys = new {computerName, osName, osArch, serverIP, runTime = sysRunTime },
app = new
{
name = _hostEnvironment.EnvironmentName,
rootPath = _hostEnvironment.ContentRootPath,
webRootPath = _hostEnvironment.WebRootPath,
version,
appRAM,
startTime,
runTime = programRunTime,
host = serverIP
},
};
return data;
}
}
}