76 lines
2.9 KiB
C#
Raw Normal View History

using Infrastructure.Extensions;
2021-08-23 16:57:25 +08:00
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace ZR.Admin.WebApi.Controllers.monitor
{
2022-05-13 22:13:44 +08:00
/// <summary>
/// 系统监控
/// </summary>
2021-08-23 16:57:25 +08:00
public class MonitorController : BaseController
{
private OptionsSetting Options;
private IWebHostEnvironment HostEnvironment;
private NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
2021-08-23 16:57:25 +08:00
public MonitorController(IOptions<OptionsSetting> options, IWebHostEnvironment hostEnvironment)
{
this.HostEnvironment = hostEnvironment;
this.Options = options.Value;
}
/// <summary>
/// 获取缓存监控数据
/// </summary>
/// <returns></returns>
[HttpGet("monitor/cache")]
2023-06-02 18:33:07 +08:00
public IActionResult GetCache()
2021-08-23 16:57:25 +08:00
{
2023-06-02 18:33:07 +08:00
return SUCCESS(1);
2021-08-23 16:57:25 +08:00
}
/// <summary>
/// 获取服务器信息
/// </summary>
/// <returns></returns>
[HttpGet("monitor/server")]
//[AllowAnonymous]
public IActionResult Server()
{
//核心数
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 = Request.HttpContext.Connection.LocalIpAddress.MapToIPv4().ToString() + ":" + Request.HttpContext.Connection.LocalPort;//获取服务器IP
2021-08-23 16:57:25 +08:00
var programStartTime = Process.GetCurrentProcess().StartTime;
string programRunTime = DateTimeHelper.FormatTime((DateTime.Now - programStartTime).TotalMilliseconds.ToString().Split('.')[0].ParseToLong());
2021-08-23 16:57:25 +08:00
var data = new
{
cpu = ComputerHelper.GetComputerInfo(),
disk = ComputerHelper.GetDiskInfos(),
sys = new { cpuNum, computerName, osName, osArch, serverIP, runTime = sysRunTime },
2021-08-23 16:57:25 +08:00
app = new
{
name = HostEnvironment.EnvironmentName,
rootPath = HostEnvironment.ContentRootPath,
webRootPath = HostEnvironment.WebRootPath,
version,
appRAM,
startTime,
runTime = programRunTime,
2021-08-23 16:57:25 +08:00
host = serverIP
},
2021-08-23 16:57:25 +08:00
};
return SUCCESS(data);
}
}
}