Merge pull request 'feature/Monitoring-Logic' (#24) from feature/Monitoring-Logic into development
Reviewed-on: daniel-hbn/Watcher#24
This commit is contained in:
131
Watcher/Controllers/MonitoringController.cs
Normal file
131
Watcher/Controllers/MonitoringController.cs
Normal file
@@ -0,0 +1,131 @@
|
|||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using Microsoft.AspNetCore.Authorization;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.CodeAnalysis;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.IdentityModel.Tokens;
|
||||||
|
using Watcher.Data;
|
||||||
|
using Watcher.Models;
|
||||||
|
using Watcher.ViewModels;
|
||||||
|
|
||||||
|
namespace Watcher.Controllers;
|
||||||
|
|
||||||
|
public class RegistrationDto
|
||||||
|
{
|
||||||
|
// Server Identity
|
||||||
|
[Required]
|
||||||
|
public int Id { get; set; }
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
public string? IpAddress { get; set; }
|
||||||
|
|
||||||
|
// Hardware Info
|
||||||
|
[Required]
|
||||||
|
public string? CpuType { get; set; }
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
public int CpuCores { get; set; }
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
public string? GpuType { get; set; }
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
public double RamSize { get; set; }
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public class MetricDto
|
||||||
|
{
|
||||||
|
// Server Identity
|
||||||
|
[Required]
|
||||||
|
public int Id { get; set; }
|
||||||
|
|
||||||
|
[Required]
|
||||||
|
public string? IpAddress { get; set; }
|
||||||
|
|
||||||
|
// Metrics
|
||||||
|
// CPU Auslastung, CPU Temperatur
|
||||||
|
// GPU Auslastung, GPU Temperatur
|
||||||
|
// RAM Auslastung
|
||||||
|
// Disks?
|
||||||
|
}
|
||||||
|
|
||||||
|
[ApiController]
|
||||||
|
[Route("[monitoring]")]
|
||||||
|
public class MonitoringController : Controller
|
||||||
|
{
|
||||||
|
private readonly AppDbContext _context;
|
||||||
|
|
||||||
|
public MonitoringController(AppDbContext context)
|
||||||
|
{
|
||||||
|
_context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("register")]
|
||||||
|
public async Task<IActionResult> Register([FromBody] RegistrationDto dto)
|
||||||
|
{
|
||||||
|
// Gültigkeit des Payloads prüfen
|
||||||
|
if (!ModelState.IsValid)
|
||||||
|
{
|
||||||
|
var errors = ModelState.Values
|
||||||
|
.SelectMany(v => v.Errors)
|
||||||
|
.Select(e => e.ErrorMessage)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
return BadRequest(new { error = "Ungültiger Payload", details = errors });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Server in Datenbank finden
|
||||||
|
var server = await _context.Servers
|
||||||
|
.FirstOrDefaultAsync(s => s.IPAddress == dto.IpAddress);
|
||||||
|
|
||||||
|
if (server != null)
|
||||||
|
{
|
||||||
|
// Serverdaten in Datenbank eintragen
|
||||||
|
server.CpuType = dto.CpuType;
|
||||||
|
server.CpuCores = dto.CpuCores;
|
||||||
|
server.GpuType = dto.GpuType;
|
||||||
|
server.RamSize = dto.RamSize;
|
||||||
|
|
||||||
|
// Änderungen in Datenbank speichern
|
||||||
|
await _context.SaveChangesAsync();
|
||||||
|
|
||||||
|
// Success
|
||||||
|
return Ok();
|
||||||
|
}
|
||||||
|
|
||||||
|
return NotFound("No Matching Server found.");
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
[HttpPost("metric")]
|
||||||
|
public async Task<IActionResult> Receive([FromBody] MetricDto dto)
|
||||||
|
{
|
||||||
|
// Gültigkeit des Payloads prüfen
|
||||||
|
if (!ModelState.IsValid)
|
||||||
|
{
|
||||||
|
var errors = ModelState.Values
|
||||||
|
.SelectMany(v => v.Errors)
|
||||||
|
.Select(e => e.ErrorMessage)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
return BadRequest(new { error = "Ungültiger Payload", details = errors });
|
||||||
|
}
|
||||||
|
|
||||||
|
// Server in Datenbank finden
|
||||||
|
var server = await _context.Servers
|
||||||
|
.FirstOrDefaultAsync(s => s.IPAddress == dto.IpAddress);
|
||||||
|
|
||||||
|
if (server != null)
|
||||||
|
{
|
||||||
|
// Serverdaten in Datenbank eintragen
|
||||||
|
|
||||||
|
// Success
|
||||||
|
return Ok();
|
||||||
|
}
|
||||||
|
|
||||||
|
return NotFound("No Matching Server found.");
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
300
Watcher/Migrations/20250616181808_Server-Hardware-Infos.Designer.cs
generated
Normal file
300
Watcher/Migrations/20250616181808_Server-Hardware-Infos.Designer.cs
generated
Normal file
@@ -0,0 +1,300 @@
|
|||||||
|
// <auto-generated />
|
||||||
|
using System;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||||
|
using Watcher.Data;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace Watcher.Migrations
|
||||||
|
{
|
||||||
|
[DbContext(typeof(AppDbContext))]
|
||||||
|
[Migration("20250616181808_Server-Hardware-Infos")]
|
||||||
|
partial class ServerHardwareInfos
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void BuildTargetModel(ModelBuilder modelBuilder)
|
||||||
|
{
|
||||||
|
#pragma warning disable 612, 618
|
||||||
|
modelBuilder
|
||||||
|
.HasAnnotation("ProductVersion", "8.0.6")
|
||||||
|
.HasAnnotation("Relational:MaxIdentifierLength", 64);
|
||||||
|
|
||||||
|
modelBuilder.Entity("Watcher.Models.Container", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime>("CreatedAt")
|
||||||
|
.HasColumnType("datetime(6)");
|
||||||
|
|
||||||
|
b.Property<string>("Hostname")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("longtext");
|
||||||
|
|
||||||
|
b.Property<int?>("ImageId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<bool>("IsRunning")
|
||||||
|
.HasColumnType("tinyint(1)");
|
||||||
|
|
||||||
|
b.Property<string>("Name")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("longtext");
|
||||||
|
|
||||||
|
b.Property<string>("Status")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("longtext");
|
||||||
|
|
||||||
|
b.Property<int?>("TagId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<string>("Type")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("longtext");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("ImageId");
|
||||||
|
|
||||||
|
b.HasIndex("TagId");
|
||||||
|
|
||||||
|
b.ToTable("Containers");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("Watcher.Models.Image", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<string>("Name")
|
||||||
|
.HasColumnType("longtext");
|
||||||
|
|
||||||
|
b.Property<string>("Tag")
|
||||||
|
.HasColumnType("longtext");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.ToTable("Images");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("Watcher.Models.LogEvent", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<int?>("ContainerId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<string>("Level")
|
||||||
|
.HasColumnType("longtext");
|
||||||
|
|
||||||
|
b.Property<string>("Message")
|
||||||
|
.HasColumnType("longtext");
|
||||||
|
|
||||||
|
b.Property<int?>("ServerId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime>("Timestamp")
|
||||||
|
.HasColumnType("datetime(6)");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("ContainerId");
|
||||||
|
|
||||||
|
b.HasIndex("ServerId");
|
||||||
|
|
||||||
|
b.ToTable("LogEvents");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("Watcher.Models.Metric", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<int?>("ContainerId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<int?>("ServerId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<DateTime>("Timestamp")
|
||||||
|
.HasColumnType("datetime(6)");
|
||||||
|
|
||||||
|
b.Property<string>("Type")
|
||||||
|
.HasColumnType("longtext");
|
||||||
|
|
||||||
|
b.Property<double>("Value")
|
||||||
|
.HasColumnType("double");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("ContainerId");
|
||||||
|
|
||||||
|
b.HasIndex("ServerId");
|
||||||
|
|
||||||
|
b.ToTable("Metrics");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("Watcher.Models.Server", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<int>("CpuCores")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<string>("CpuType")
|
||||||
|
.HasColumnType("longtext");
|
||||||
|
|
||||||
|
b.Property<DateTime>("CreatedAt")
|
||||||
|
.HasColumnType("datetime(6)");
|
||||||
|
|
||||||
|
b.Property<string>("Description")
|
||||||
|
.HasColumnType("longtext");
|
||||||
|
|
||||||
|
b.Property<string>("GpuType")
|
||||||
|
.HasColumnType("longtext");
|
||||||
|
|
||||||
|
b.Property<string>("IPAddress")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("longtext");
|
||||||
|
|
||||||
|
b.Property<bool>("IsOnline")
|
||||||
|
.HasColumnType("tinyint(1)");
|
||||||
|
|
||||||
|
b.Property<DateTime>("LastSeen")
|
||||||
|
.HasColumnType("datetime(6)");
|
||||||
|
|
||||||
|
b.Property<string>("Name")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("longtext");
|
||||||
|
|
||||||
|
b.Property<double>("RamSize")
|
||||||
|
.HasColumnType("double");
|
||||||
|
|
||||||
|
b.Property<int?>("TagId")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<string>("Type")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("longtext");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.HasIndex("TagId");
|
||||||
|
|
||||||
|
b.ToTable("Servers");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("Watcher.Models.Tag", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<string>("Name")
|
||||||
|
.HasColumnType("longtext");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.ToTable("Tags");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("Watcher.Models.User", b =>
|
||||||
|
{
|
||||||
|
b.Property<int>("Id")
|
||||||
|
.ValueGeneratedOnAdd()
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<string>("Email")
|
||||||
|
.HasColumnType("longtext");
|
||||||
|
|
||||||
|
b.Property<DateTime>("LastLogin")
|
||||||
|
.HasColumnType("datetime(6)");
|
||||||
|
|
||||||
|
b.Property<string>("PocketId")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("longtext");
|
||||||
|
|
||||||
|
b.Property<string>("PreferredUsername")
|
||||||
|
.IsRequired()
|
||||||
|
.HasColumnType("longtext");
|
||||||
|
|
||||||
|
b.HasKey("Id");
|
||||||
|
|
||||||
|
b.ToTable("Users");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("Watcher.Models.Container", b =>
|
||||||
|
{
|
||||||
|
b.HasOne("Watcher.Models.Image", "Image")
|
||||||
|
.WithMany("Containers")
|
||||||
|
.HasForeignKey("ImageId");
|
||||||
|
|
||||||
|
b.HasOne("Watcher.Models.Tag", null)
|
||||||
|
.WithMany("Containers")
|
||||||
|
.HasForeignKey("TagId");
|
||||||
|
|
||||||
|
b.Navigation("Image");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("Watcher.Models.LogEvent", b =>
|
||||||
|
{
|
||||||
|
b.HasOne("Watcher.Models.Container", "Container")
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey("ContainerId");
|
||||||
|
|
||||||
|
b.HasOne("Watcher.Models.Server", "Server")
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey("ServerId");
|
||||||
|
|
||||||
|
b.Navigation("Container");
|
||||||
|
|
||||||
|
b.Navigation("Server");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("Watcher.Models.Metric", b =>
|
||||||
|
{
|
||||||
|
b.HasOne("Watcher.Models.Container", "Container")
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey("ContainerId");
|
||||||
|
|
||||||
|
b.HasOne("Watcher.Models.Server", "Server")
|
||||||
|
.WithMany()
|
||||||
|
.HasForeignKey("ServerId");
|
||||||
|
|
||||||
|
b.Navigation("Container");
|
||||||
|
|
||||||
|
b.Navigation("Server");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("Watcher.Models.Server", b =>
|
||||||
|
{
|
||||||
|
b.HasOne("Watcher.Models.Tag", null)
|
||||||
|
.WithMany("Servers")
|
||||||
|
.HasForeignKey("TagId");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("Watcher.Models.Image", b =>
|
||||||
|
{
|
||||||
|
b.Navigation("Containers");
|
||||||
|
});
|
||||||
|
|
||||||
|
modelBuilder.Entity("Watcher.Models.Tag", b =>
|
||||||
|
{
|
||||||
|
b.Navigation("Containers");
|
||||||
|
|
||||||
|
b.Navigation("Servers");
|
||||||
|
});
|
||||||
|
#pragma warning restore 612, 618
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
62
Watcher/Migrations/20250616181808_Server-Hardware-Infos.cs
Normal file
62
Watcher/Migrations/20250616181808_Server-Hardware-Infos.cs
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
using Microsoft.EntityFrameworkCore.Migrations;
|
||||||
|
|
||||||
|
#nullable disable
|
||||||
|
|
||||||
|
namespace Watcher.Migrations
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
public partial class ServerHardwareInfos : Migration
|
||||||
|
{
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Up(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.AddColumn<int>(
|
||||||
|
name: "CpuCores",
|
||||||
|
table: "Servers",
|
||||||
|
type: "int",
|
||||||
|
nullable: false,
|
||||||
|
defaultValue: 0);
|
||||||
|
|
||||||
|
migrationBuilder.AddColumn<string>(
|
||||||
|
name: "CpuType",
|
||||||
|
table: "Servers",
|
||||||
|
type: "longtext",
|
||||||
|
nullable: true)
|
||||||
|
.Annotation("MySql:CharSet", "utf8mb4");
|
||||||
|
|
||||||
|
migrationBuilder.AddColumn<string>(
|
||||||
|
name: "GpuType",
|
||||||
|
table: "Servers",
|
||||||
|
type: "longtext",
|
||||||
|
nullable: true)
|
||||||
|
.Annotation("MySql:CharSet", "utf8mb4");
|
||||||
|
|
||||||
|
migrationBuilder.AddColumn<double>(
|
||||||
|
name: "RamSize",
|
||||||
|
table: "Servers",
|
||||||
|
type: "double",
|
||||||
|
nullable: false,
|
||||||
|
defaultValue: 0.0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void Down(MigrationBuilder migrationBuilder)
|
||||||
|
{
|
||||||
|
migrationBuilder.DropColumn(
|
||||||
|
name: "CpuCores",
|
||||||
|
table: "Servers");
|
||||||
|
|
||||||
|
migrationBuilder.DropColumn(
|
||||||
|
name: "CpuType",
|
||||||
|
table: "Servers");
|
||||||
|
|
||||||
|
migrationBuilder.DropColumn(
|
||||||
|
name: "GpuType",
|
||||||
|
table: "Servers");
|
||||||
|
|
||||||
|
migrationBuilder.DropColumn(
|
||||||
|
name: "RamSize",
|
||||||
|
table: "Servers");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -145,12 +145,21 @@ namespace Watcher.Migrations
|
|||||||
.ValueGeneratedOnAdd()
|
.ValueGeneratedOnAdd()
|
||||||
.HasColumnType("int");
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<int>("CpuCores")
|
||||||
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
b.Property<string>("CpuType")
|
||||||
|
.HasColumnType("longtext");
|
||||||
|
|
||||||
b.Property<DateTime>("CreatedAt")
|
b.Property<DateTime>("CreatedAt")
|
||||||
.HasColumnType("datetime(6)");
|
.HasColumnType("datetime(6)");
|
||||||
|
|
||||||
b.Property<string>("Description")
|
b.Property<string>("Description")
|
||||||
.HasColumnType("longtext");
|
.HasColumnType("longtext");
|
||||||
|
|
||||||
|
b.Property<string>("GpuType")
|
||||||
|
.HasColumnType("longtext");
|
||||||
|
|
||||||
b.Property<string>("IPAddress")
|
b.Property<string>("IPAddress")
|
||||||
.IsRequired()
|
.IsRequired()
|
||||||
.HasColumnType("longtext");
|
.HasColumnType("longtext");
|
||||||
@@ -165,6 +174,9 @@ namespace Watcher.Migrations
|
|||||||
.IsRequired()
|
.IsRequired()
|
||||||
.HasColumnType("longtext");
|
.HasColumnType("longtext");
|
||||||
|
|
||||||
|
b.Property<double>("RamSize")
|
||||||
|
.HasColumnType("double");
|
||||||
|
|
||||||
b.Property<int?>("TagId")
|
b.Property<int?>("TagId")
|
||||||
.HasColumnType("int");
|
.HasColumnType("int");
|
||||||
|
|
||||||
|
@@ -19,5 +19,13 @@ public class Server
|
|||||||
|
|
||||||
public string? Description { get; set; }
|
public string? Description { get; set; }
|
||||||
|
|
||||||
|
// Hardware Infos
|
||||||
|
public string? CpuType { get; set; } = string.Empty;
|
||||||
|
public int CpuCores { get; set; }
|
||||||
|
public string? GpuType { get; set; } = string.Empty;
|
||||||
|
public double RamSize { get; set; }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user