## Contexte Implémentation des tickets #82, #83, #84, #85 — télémétrie inventaire matériel agent. L'objectif est de fournir au central un maximum d'informations sur les machines des agents (CPU, mémoire, disques, GPU, OS, réseau, conteneur) sans SSH. ## Changements ### Nouveaux collecteurs (src/Heimdall.Agent/Services/) - **CpuInfoProvider** : parse `/proc/cpuinfo` (vendor, modèle, cœurs physiques/logiques, fréquence, flags AVX/AVX2/FMA/AVX512, cache), topologie, détection cgroup CPU (`/sys/fs/cgroup/cpu.max` v2) - **DiskInventoryProvider** : énumère les montages (`/proc/mounts`), capacité/libre/type SSD/HDD via `/sys/block/*/queue/rotation` - **GpuInventoryProvider** : nvidia-smi, sysfs DRM, vainfo, rocm-smi avec fallback silencieux, multi-GPU - **HostInfoProvider** : os-release, uname, `/proc/uptime` (uptime machine ≠ process), températures sysfs - **ContainerInfoProvider** : détection Docker (/.dockerenv, cgroup), ID conteneur, limites CPU/mémoire ### Refonte SystemMetricsProvider - Séparation host vs process (RAM machine ≠ RAM conteneur ≠ WorkingSet64) - Uptime machine (`/proc/uptime`) distinct de l'uptime process - Mémoire swap ### Entité Agent + Migration - Champs structurés CPU/mémoire/disque/GPU/OS/réseau (tous nullable, rétrocompatible) - Migration EF `AddAgentTelemetryInventory` ### Transport heartbeat - Inventaire au register + refresh périodique (5 min, pas à chaque tick) - Champs JSON pour listes (disques, GPU, interfaces réseau, températures) ### UI Admin (AgentDetail.razor) - Sections CPU, Mémoire/Disques, GPU, Machine/Conteneur avec barres de progression et badges ### Tests - 40+ tests unitaires : providers, handlers, entité Agent - Total : 999 tests verts (Domain 149, Application 397, Agent 210, Web 243) ## Tickets liés - Fixes #82 — CPU inventory - Fixes #83 — Mémoire & disque - Fixes #84 — GPU inventory - Fixes #85 — OS, conteneur, réseau ## Résultats des tests - `dotnet build` : 0 erreur, 0 warning - Domain.Tests : 149 passed - Application.Tests : 397 passed - Agent.Tests : 210 passed - Web.Tests : 243 passed - Total : 999 tests ✅ ## Checklist - [x] `dotnet build` vert (TreatWarningsAsErrors) - [x] Tests unitaires verts - [x] Migration EF créée - [x] Champs nullable (rétrocompatible) - [x] Pas de référence Infrastructure dans les contrôleurs - [x] Collecteurs avec fallback silencieux (pas de crash sans GPU/Docker/sysfs) Reviewed-on: https://forgejo.tordais.cc/nonobis/Heimdall/pulls/137
214 lines
7.5 KiB
C#
214 lines
7.5 KiB
C#
using Heimdall.Application.Agents;
|
|
using Heimdall.Application.Agents.Commands.Heartbeat;
|
|
using Heimdall.Application.DTOs;
|
|
using Heimdall.Domain.Entities;
|
|
using Heimdall.Domain.Enums;
|
|
|
|
namespace Heimdall.Application.Tests.Agents;
|
|
|
|
public class AgentTelemetryMappingTests
|
|
{
|
|
[Fact]
|
|
public void ApplyTo_ShouldSetLiveMetrics()
|
|
{
|
|
var agent = CreateAgent();
|
|
var cmd = new HeartbeatCommand(agent.Id, null, null, null, null, null,
|
|
CpuUsagePercent: 42.5,
|
|
MemoryUsageBytes: 4_294_967_296,
|
|
DiskUsagePercent: 73.1,
|
|
UptimeSeconds: 86400);
|
|
|
|
cmd.ApplyTo(agent);
|
|
|
|
agent.CpuUsagePercent.ShouldBe(42.5);
|
|
agent.MemoryUsageBytes.ShouldBe(4_294_967_296);
|
|
agent.DiskUsagePercent.ShouldBe(73.1);
|
|
agent.UptimeSeconds.ShouldBe(86400);
|
|
}
|
|
|
|
[Fact]
|
|
public void ApplyTo_ShouldSetCpuInventory_WhenPresent()
|
|
{
|
|
var agent = CreateAgent();
|
|
var cmd = new HeartbeatCommand(agent.Id, null, null, null, null, null,
|
|
CpuVendor: "GenuineIntel",
|
|
CpuModel: "Intel Core i7-12700K",
|
|
CpuArchitecture: "x86_64",
|
|
PhysicalCores: 12,
|
|
LogicalCores: 20,
|
|
ThreadsPerCore: 2,
|
|
CpuFrequencyMhz: 3600.0,
|
|
CpuFlags: "avx,avx2,fma",
|
|
CpuCacheBytes: 25_165_824,
|
|
ContainerCpuLimit: 200);
|
|
|
|
cmd.ApplyTo(agent);
|
|
|
|
agent.CpuVendor.ShouldBe("GenuineIntel");
|
|
agent.CpuModel.ShouldBe("Intel Core i7-12700K");
|
|
agent.CpuArchitecture.ShouldBe("x86_64");
|
|
agent.PhysicalCores.ShouldBe(12);
|
|
agent.LogicalCores.ShouldBe(20);
|
|
agent.ThreadsPerCore.ShouldBe(2);
|
|
agent.CpuFrequencyMhz.ShouldBe(3600.0);
|
|
agent.CpuFlags.ShouldBe("avx,avx2,fma");
|
|
agent.CpuCacheBytes.ShouldBe(25_165_824);
|
|
agent.ContainerCpuLimit.ShouldBe(200);
|
|
}
|
|
|
|
[Fact]
|
|
public void ApplyTo_ShouldPreserveExistingCpuInventory_WhenNull()
|
|
{
|
|
var agent = CreateAgent();
|
|
agent.UpdateCpuInventory(new CpuInventoryData("AMD", "Ryzen", "x86_64", 8, 16, 2, 3400.0, "sse4", 16_777_216, null));
|
|
|
|
var cmd = new HeartbeatCommand(agent.Id, null, null, null, null, null,
|
|
CpuUsagePercent: 50.0);
|
|
|
|
cmd.ApplyTo(agent);
|
|
|
|
// CPU inventory should be preserved since no CPU inventory fields were sent
|
|
agent.CpuVendor.ShouldBe("AMD");
|
|
agent.CpuModel.ShouldBe("Ryzen");
|
|
agent.PhysicalCores.ShouldBe(8);
|
|
}
|
|
|
|
[Fact]
|
|
public void ApplyTo_ShouldSetMemoryDiskInventory_WhenPresent()
|
|
{
|
|
var agent = CreateAgent();
|
|
var cmd = new HeartbeatCommand(agent.Id, null, null, null, null, null,
|
|
MemTotalBytes: 17_179_869_184,
|
|
MemAvailableBytes: 8_589_934_592,
|
|
SwapTotalBytes: 2_147_483_648,
|
|
SwapFreeBytes: 1_073_741_824,
|
|
ContainerMemoryLimitBytes: 4_294_967_296,
|
|
DiskInventoryJson: """[{"Mount":"/"}]""");
|
|
|
|
cmd.ApplyTo(agent);
|
|
|
|
agent.MemTotalBytes.ShouldBe(17_179_869_184);
|
|
agent.MemAvailableBytes.ShouldBe(8_589_934_592);
|
|
agent.SwapTotalBytes.ShouldBe(2_147_483_648);
|
|
agent.SwapFreeBytes.ShouldBe(1_073_741_824);
|
|
agent.ContainerMemoryLimitBytes.ShouldBe(4_294_967_296);
|
|
agent.DiskInventoryJson.ShouldBe("""[{"Mount":"/"}]""");
|
|
}
|
|
|
|
[Fact]
|
|
public void ApplyTo_ShouldPreserveExistingMemoryDiskInventory_WhenNull()
|
|
{
|
|
var agent = CreateAgent();
|
|
agent.UpdateMemoryDiskInventory(new MemoryDiskInventoryData(8_000_000_000, 4_000_000_000, 2_000_000_000, 1_000_000_000, null, null));
|
|
|
|
var cmd = new HeartbeatCommand(agent.Id, null, null, null, null, null,
|
|
CpuUsagePercent: 50.0);
|
|
|
|
cmd.ApplyTo(agent);
|
|
|
|
agent.MemTotalBytes.ShouldBe(8_000_000_000);
|
|
}
|
|
|
|
[Fact]
|
|
public void ApplyTo_ShouldSetGpuInventory_WhenPresent()
|
|
{
|
|
var agent = CreateAgent();
|
|
var cmd = new HeartbeatCommand(agent.Id, null, null, null, null, null,
|
|
GpuInventoryJson: """[{"Name":"RTX 4090"}]""");
|
|
|
|
cmd.ApplyTo(agent);
|
|
|
|
agent.GpuInventoryJson.ShouldBe("""[{"Name":"RTX 4090"}]""");
|
|
}
|
|
|
|
[Fact]
|
|
public void ApplyTo_ShouldSetHostInventory_WhenPresent()
|
|
{
|
|
var agent = CreateAgent();
|
|
var cmd = new HeartbeatCommand(agent.Id, null, null, null, null, null,
|
|
OsName: "Ubuntu 22.04 LTS",
|
|
KernelVersion: "5.15.0-76-generic",
|
|
Arch: "x86_64",
|
|
UptimeMachineSeconds: 86400,
|
|
ContainerId: "abc123",
|
|
ContainerImage: "heimdall/agent:latest",
|
|
NetworkInterfacesJson: """[{"Name":"eth0"}]""",
|
|
TemperatureJson: """[{"Zone":"coretemp"}]""");
|
|
|
|
cmd.ApplyTo(agent);
|
|
|
|
agent.OsName.ShouldBe("Ubuntu 22.04 LTS");
|
|
agent.KernelVersion.ShouldBe("5.15.0-76-generic");
|
|
agent.Arch.ShouldBe("x86_64");
|
|
agent.UptimeMachineSeconds.ShouldBe(86400);
|
|
agent.ContainerId.ShouldBe("abc123");
|
|
agent.ContainerImage.ShouldBe("heimdall/agent:latest");
|
|
agent.NetworkInterfacesJson.ShouldBe("""[{"Name":"eth0"}]""");
|
|
agent.TemperatureJson.ShouldBe("""[{"Zone":"coretemp"}]""");
|
|
}
|
|
|
|
[Fact]
|
|
public void ApplyTo_ShouldPreserveExistingHostInventory_WhenNull()
|
|
{
|
|
var agent = CreateAgent();
|
|
agent.UpdateHostInventory(new HostInventoryData("Fedora", "6.5.0", "x86_64", 100000, "def456", "agent:v2", null, null));
|
|
|
|
var cmd = new HeartbeatCommand(agent.Id, null, null, null, null, null,
|
|
CpuUsagePercent: 50.0);
|
|
|
|
cmd.ApplyTo(agent);
|
|
|
|
agent.OsName.ShouldBe("Fedora");
|
|
agent.KernelVersion.ShouldBe("6.5.0");
|
|
}
|
|
|
|
[Fact]
|
|
public void ToTelemetryFields_ShouldReadAllFields()
|
|
{
|
|
var agent = CreateAgent();
|
|
agent.UpdateLiveMetrics(42.5, 4_294_967_296, 73.1, 86400);
|
|
agent.UpdateCpuInventory(new CpuInventoryData("Intel", "i7", "x86_64", 12, 20, 2, 3600.0, "avx", 25_165_824, 200));
|
|
agent.UpdateMemoryDiskInventory(new MemoryDiskInventoryData(17_179_869_184, 8_589_934_592, 2_147_483_648, 1_073_741_824, 4_294_967_296, """[{"Mount":"/"}]"""));
|
|
agent.UpdateGpuInventory("""[{"Name":"RTX 4090"}]""");
|
|
agent.UpdateHostInventory(new HostInventoryData("Ubuntu", "5.15.0", "x86_64", 86400, "abc", "img", "[]", "[]"));
|
|
|
|
var fields = agent.ToTelemetryFields();
|
|
|
|
fields.CpuUsagePercent.ShouldBe(42.5);
|
|
fields.MemoryUsageBytes.ShouldBe(4_294_967_296);
|
|
fields.DiskUsagePercent.ShouldBe(73.1);
|
|
fields.UptimeSeconds.ShouldBe(86400);
|
|
fields.CpuVendor.ShouldBe("Intel");
|
|
fields.MemTotalBytes.ShouldBe(17_179_869_184);
|
|
fields.GpuInventoryJson.ShouldBe("""[{"Name":"RTX 4090"}]""");
|
|
fields.OsName.ShouldBe("Ubuntu");
|
|
}
|
|
|
|
[Fact]
|
|
public void ApplyTo_RoundTrip_ShouldPreserveValues()
|
|
{
|
|
var agent = CreateAgent();
|
|
var cmd = new HeartbeatCommand(agent.Id, null, null, null, null, null,
|
|
CpuUsagePercent: 42.5,
|
|
MemoryUsageBytes: 4_294_967_296,
|
|
CpuVendor: "AMD",
|
|
MemTotalBytes: 16_000_000_000,
|
|
OsName: "Debian");
|
|
|
|
cmd.ApplyTo(agent);
|
|
|
|
var fields = agent.ToTelemetryFields();
|
|
|
|
fields.CpuUsagePercent.ShouldBe(42.5);
|
|
fields.MemoryUsageBytes.ShouldBe(4_294_967_296);
|
|
fields.CpuVendor.ShouldBe("AMD");
|
|
fields.MemTotalBytes.ShouldBe(16_000_000_000);
|
|
fields.OsName.ShouldBe("Debian");
|
|
}
|
|
|
|
private static Agent CreateAgent()
|
|
{
|
|
return new Agent(Guid.NewGuid(), "test-agent", "test-host", "1.0.0");
|
|
}
|
|
}
|