feat(agent): télémétrie inventaire matériel — CPU/mémoire/disque/GPU/OS/réseau (Fixes #82 #83 #84 #85) (#137)

## 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
This commit is contained in:
Arnaud Dartois authored and Arnaud Dartois committed 2026-08-20 14:13:06 +02:00
1 parent f7e6b0cec3
commit 4792b69e46
63 files changed
+8711 -563

No files matched your search

@@ -3,9 +3,7 @@ using System.Threading.Channels;
using Microsoft.Extensions.Logging;
using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using ImageMagick;
namespace Heimdall.Agent.Services;
@@ -94,7 +92,7 @@ public sealed class FrameAnalyzerService : IDisposable
// Determine number of classes from output shape [1, 4+C, 8400]
var outputMeta = _session.OutputMetadata.Values.ToArray()[0];
_numClasses = outputMeta.Dimensions.Length >= 2 ? (int)outputMeta.Dimensions[1] - 4 : 80;
_numClasses = outputMeta.Dimensions.Length >= 2 ? outputMeta.Dimensions[1] - 4 : 80;
_logger.LogInformation(
"FrameAnalyzer initialized with model: {ModelPath} (input: {InputName}, classes: {Classes})",
@@ -204,10 +202,11 @@ public sealed class FrameAnalyzerService : IDisposable
if (_session is null)
return [];
using var image = Image.Load<Rgb24>(jpegData);
using var image = new MagickImage(jpegData);
image.Format = MagickFormat.Jpeg;
// Resize to model input size (640×640)
image.Mutate(x => x.Resize(InputSize, InputSize));
// Resize to model input size (640×640), ignoring aspect ratio (like ImageSharp Resize(640,640))
image.Resize(new MagickGeometry(InputSize, InputSize) { IgnoreAspectRatio = true });
var inputTensor = PreprocessImage(image);
@@ -219,32 +218,29 @@ public sealed class FrameAnalyzerService : IDisposable
using var results = _session.Run(inputs);
var output = results[0].AsTensor<float>().ToArray();
return ParseYoloOutput(output, image.Width, image.Height);
return ParseYoloOutput(output, (int)image.Width, (int)image.Height);
}
/// <summary>
/// Convert ImageSharp image to a normalized NCHW tensor [1, 3, 640, 640].
/// Convert Magick.NET image to a normalized NCHW tensor [1, 3, 640, 640].
/// YOLOv8 expects RGB channels, each normalized to [0, 1].
/// </summary>
private static DenseTensor<float> PreprocessImage(Image<Rgb24> image)
private static DenseTensor<float> PreprocessImage(MagickImage image)
{
var tensor = new DenseTensor<float>([1, 3, InputSize, InputSize]);
var width = image.Width;
// GetPixels().ToArray() returns contiguous interleaved RGB channel values
// (Q16 build => ushort ranging 0..65535), row-major.
var rgb = image.GetPixels()?.ToArray() ?? [];
image.ProcessPixelRows(accessor =>
var idx = 0;
for (var y = 0; y < InputSize; y++)
{
for (var y = 0; y < accessor.Height; y++)
for (var x = 0; x < InputSize; x++)
{
var pixelRow = accessor.GetRowSpan(y);
for (var x = 0; x < pixelRow.Length; x++)
{
var pixel = pixelRow[x];
tensor[0, 0, y, x] = pixel.R / 255f;
tensor[0, 1, y, x] = pixel.G / 255f;
tensor[0, 2, y, x] = pixel.B / 255f;
}
for (var c = 0; c < 3; c++)
tensor[0, c, y, x] = rgb[idx++] / 65535f;
}
});
}
return tensor;
}
@@ -329,21 +325,30 @@ public sealed class FrameAnalyzerService : IDisposable
Height: best.Y2 - best.Y1,
ClassIndex: best.ClassIdx));
// Suppress overlapping boxes of the same class
for (var j = i + 1; j < candidates.Count; j++)
{
if (suppressed[j]) continue;
if (candidates[j].ClassIdx != best.ClassIdx) continue;
var iou = ComputeIoU(best, candidates[j]);
if (iou >= NmsIouThreshold)
suppressed[j] = true;
}
SuppressOverlapping(candidates, suppressed, i, best);
}
return results.ToArray();
}
/// <summary>
/// Mark overlapping candidates of the same class as suppressed.
/// Extracted from ApplyNms to reduce cognitive complexity (S3776).
/// </summary>
private static void SuppressOverlapping(
List<RawDetection> candidates, bool[] suppressed, int index, RawDetection best)
{
for (var j = index + 1; j < candidates.Count; j++)
{
if (suppressed[j]) continue;
if (candidates[j].ClassIdx != best.ClassIdx) continue;
var iou = ComputeIoU(best, candidates[j]);
if (iou >= NmsIouThreshold)
suppressed[j] = true;
}
}
private static float ComputeIoU(RawDetection a, RawDetection b)
{
var interX1 = Math.Max(a.X1, b.X1);