using Microsoft.AspNetCore.Mvc; using watcher_monitoring.Payloads; using watcher_monitoring.Data; using watcher_monitoring.Models; using Microsoft.AspNetCore.Authorization.Infrastructure; namespace watcher_monitoring.Controllers; [Route("[controller]")] public class MonitoringController : Controller { private readonly WatcherDbContext _context; private readonly ILogger _logger; public MonitoringController(WatcherDbContext context, ILogger logger) { _context = context; _logger = logger; } // Registration Endpoint for watcher-agent [HttpPost("register")] public async Task Register([FromBody] RegistrationDto dto) { // payload check if (!ModelState.IsValid) { var errors = ModelState.Values .SelectMany(v => v.Errors) .Select(e => e.ErrorMessage) .ToList(); _logger.LogError("Invalid registration payload"); return BadRequest(new { error = "Invalid registration payload", details = errors }); } try { Server server = new Server { Name = "test", IPAddress = dto.IpAddress }; _context.Servers.Add(server); await _context.SaveChangesAsync(); return Ok(); } catch (Exception ex) { Console.WriteLine(ex.Message); _logger.LogError(ex.Message); return BadRequest(); } } // Hardware Configuration Endpoint for watcher-agent [HttpPost("hardware-configuration")] public async Task HardwareConfiguration ([FromBody] HardwareDto dto) { // payload check if (!ModelState.IsValid) { var errors = ModelState.Values .SelectMany(v => v.Errors) .Select(e => e.ErrorMessage) .ToList(); _logger.LogError("Invalid hardware configuration"); return BadRequest(new { error = "Invalid Hardware Configuration Payload", details = errors }); } try { // Find Server in Database Server server = await _context.Servers.FindAsync(dto.Id); // Add Hardware Configuration try { server.CpuType = dto.CpuType; server.CpuCores = dto.CpuCores; server.GpuType = dto.GpuType; server.RamSize = dto.RamSize; // Diskspace fehlt } catch (Exception ex) { _logger.LogError(ex.Message); } } catch (Exception ex) { _logger.LogError(ex.Message); } return Ok(); } // Server-Metrics endpoint for watcher-agent // Service-Detection endpoint for watcher-agent // Service-Metrics endpoint for watcher-agent }