Add ResourceHub for real-time updates and implement web dashboard with REST API
- Created ResourceHub.cs for SignalR group management. - Developed a modern web dashboard using Tailwind CSS for responsive design. - Implemented real-time updates with SignalR for CPU, Memory, GPU, and Network usage. - Added REST API endpoints for resource information and process management. - Integrated process management features to view and terminate high-usage processes. - Enhanced UI with loading spinners, notifications, and responsive tables. - Included performance charts for historical CPU and Memory usage. - Configured Swagger UI for API documentation. - Established security features including process kill restrictions and API key authentication.
This commit is contained in:
@@ -0,0 +1,173 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using ResourceMonitorService.Models;
|
||||
using ResourceMonitorService.Services;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace ResourceMonitorService.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class ResourceController : ControllerBase
|
||||
{
|
||||
private readonly IResourceMonitorService _resourceMonitorService;
|
||||
private readonly ISystemInfoService _systemInfoService;
|
||||
private readonly ILogger<ResourceController> _logger;
|
||||
|
||||
public ResourceController(
|
||||
IResourceMonitorService resourceMonitorService,
|
||||
ISystemInfoService systemInfoService,
|
||||
ILogger<ResourceController> logger)
|
||||
{
|
||||
_resourceMonitorService = resourceMonitorService;
|
||||
_systemInfoService = systemInfoService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
[HttpGet("usage")]
|
||||
public async Task<ActionResult<ResourceUsage>> GetResourceUsage()
|
||||
{
|
||||
try
|
||||
{
|
||||
var usage = await _resourceMonitorService.GetResourceUsageAsync();
|
||||
return Ok(usage);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error getting resource usage");
|
||||
return StatusCode(500, "Internal server error");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("system-info")]
|
||||
public async Task<ActionResult<SystemInfo>> GetSystemInfo()
|
||||
{
|
||||
try
|
||||
{
|
||||
var systemInfo = await _systemInfoService.GetSystemInfoAsync();
|
||||
return Ok(systemInfo);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error getting system info");
|
||||
return StatusCode(500, "Internal server error");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("cpu")]
|
||||
public async Task<ActionResult<CpuUsage>> GetCpuUsage()
|
||||
{
|
||||
try
|
||||
{
|
||||
var cpuUsage = await _resourceMonitorService.GetCpuUsageAsync();
|
||||
return Ok(cpuUsage);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error getting CPU usage");
|
||||
return StatusCode(500, "Internal server error");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("memory")]
|
||||
public async Task<ActionResult<MemoryUsage>> GetMemoryUsage()
|
||||
{
|
||||
try
|
||||
{
|
||||
var memoryUsage = await _resourceMonitorService.GetMemoryUsageAsync();
|
||||
return Ok(memoryUsage);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error getting memory usage");
|
||||
return StatusCode(500, "Internal server error");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("gpu")]
|
||||
public async Task<ActionResult<GpuUsage>> GetGpuUsage()
|
||||
{
|
||||
try
|
||||
{
|
||||
var gpuUsage = await _resourceMonitorService.GetGpuUsageAsync();
|
||||
return Ok(gpuUsage);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error getting GPU usage");
|
||||
return StatusCode(500, "Internal server error");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("disks")]
|
||||
public async Task<ActionResult<List<DiskUsage>>> GetDiskUsage()
|
||||
{
|
||||
try
|
||||
{
|
||||
var diskUsage = await _resourceMonitorService.GetDiskUsageAsync();
|
||||
return Ok(diskUsage);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error getting disk usage");
|
||||
return StatusCode(500, "Internal server error");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("network")]
|
||||
public async Task<ActionResult<NetworkUsage>> GetNetworkUsage()
|
||||
{
|
||||
try
|
||||
{
|
||||
var networkUsage = await _resourceMonitorService.GetNetworkUsageAsync();
|
||||
return Ok(networkUsage);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error getting network usage");
|
||||
return StatusCode(500, "Internal server error");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpGet("processes")]
|
||||
public async Task<ActionResult<List<ProcessInfo>>> GetTopProcesses([FromQuery] int count = 10)
|
||||
{
|
||||
try
|
||||
{
|
||||
var processes = await _resourceMonitorService.GetTopProcessesAsync(count);
|
||||
return Ok(processes);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, "Error getting top processes");
|
||||
return StatusCode(500, "Internal server error");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost("kill-process/{processId}")]
|
||||
public ActionResult KillProcess(int processId)
|
||||
{
|
||||
try
|
||||
{
|
||||
var process = Process.GetProcessById(processId);
|
||||
if (process == null)
|
||||
{
|
||||
return NotFound($"Process with ID {processId} not found");
|
||||
}
|
||||
|
||||
process.Kill();
|
||||
_logger.LogInformation($"Process {process.ProcessName} (ID: {processId}) was terminated");
|
||||
|
||||
return Ok(new { message = $"Process {process.ProcessName} (ID: {processId}) was terminated successfully" });
|
||||
}
|
||||
catch (ArgumentException)
|
||||
{
|
||||
return NotFound($"Process with ID {processId} not found");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex, $"Error killing process {processId}");
|
||||
return StatusCode(500, "Internal server error");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user