## 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
137 lines
4.5 KiB
C#
137 lines
4.5 KiB
C#
namespace Heimdall.Agent.Services;
|
|
|
|
/// <summary>
|
|
/// Detects container environment (Docker, Podman, etc.) and reads cgroup limits.
|
|
/// </summary>
|
|
public sealed class ContainerInfoProvider
|
|
{
|
|
private const string ProcEnvironPath = "/proc/1/environ";
|
|
private const string Proc1CgroupPath = "/proc/1/cgroup";
|
|
private const string ProcSelfCgroupPath = "/proc/self/cgroup";
|
|
|
|
public ContainerInfo Collect()
|
|
{
|
|
var isContainer = DetectContainer();
|
|
return new ContainerInfo
|
|
{
|
|
IsContainer = isContainer,
|
|
ContainerId = isContainer ? GetContainerId() : null,
|
|
ContainerImage = isContainer ? GetContainerImage() : null,
|
|
MemoryLimitBytes = CgroupReader.GetMemoryLimit(),
|
|
CpuLimit = CgroupReader.GetCpuLimit()
|
|
};
|
|
}
|
|
|
|
private static bool DetectContainer()
|
|
{
|
|
try
|
|
{
|
|
if (File.Exists("/.dockerenv") || File.Exists("/run/.containerenv"))
|
|
return true;
|
|
|
|
if (DetectFromCgroup() || DetectFromEnviron())
|
|
return true;
|
|
}
|
|
catch { /* best effort */ }
|
|
return false;
|
|
}
|
|
|
|
private static bool DetectFromCgroup()
|
|
{
|
|
if (!File.Exists(Proc1CgroupPath)) return false;
|
|
|
|
var cgroupContent = File.ReadAllText(Proc1CgroupPath);
|
|
return cgroupContent.Contains("docker", StringComparison.OrdinalIgnoreCase) ||
|
|
cgroupContent.Contains("containerd", StringComparison.OrdinalIgnoreCase) ||
|
|
cgroupContent.Contains("kubepods", StringComparison.OrdinalIgnoreCase) ||
|
|
cgroupContent.Contains("lxc", StringComparison.OrdinalIgnoreCase) ||
|
|
cgroupContent.Contains("podman", StringComparison.OrdinalIgnoreCase);
|
|
}
|
|
|
|
private static bool DetectFromEnviron()
|
|
{
|
|
if (!File.Exists(ProcEnvironPath)) return false;
|
|
|
|
var environ = File.ReadAllText(ProcEnvironPath);
|
|
return environ.Contains("container=docker", StringComparison.OrdinalIgnoreCase) ||
|
|
environ.Contains("container=podman", StringComparison.OrdinalIgnoreCase);
|
|
}
|
|
|
|
private static string? GetContainerId()
|
|
{
|
|
try
|
|
{
|
|
var id = GetContainerIdFromCgroup();
|
|
if (id is not null) return id;
|
|
|
|
// Try hostname (Docker sets container ID as hostname)
|
|
var hostname = Environment.MachineName;
|
|
if (hostname.Length >= 12 && hostname.All(c => "0123456789abcdef".Contains(c)))
|
|
return hostname[..12];
|
|
}
|
|
catch { /* best effort */ }
|
|
return null;
|
|
}
|
|
|
|
private static string? GetContainerIdFromCgroup()
|
|
{
|
|
if (!File.Exists(ProcSelfCgroupPath)) return null;
|
|
|
|
foreach (var line in File.ReadLines(ProcSelfCgroupPath))
|
|
{
|
|
var parts = line.Split(':', StringSplitOptions.RemoveEmptyEntries);
|
|
if (parts.Length < 3) continue;
|
|
|
|
var segments = parts[2].Split('/', StringSplitOptions.RemoveEmptyEntries);
|
|
var lastSegment = segments.LastOrDefault();
|
|
if (lastSegment is { Length: >= 12 } && lastSegment.All(c => "0123456789abcdef".Contains(c)))
|
|
return lastSegment[..Math.Min(12, lastSegment.Length)];
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
private static string? GetContainerImage()
|
|
{
|
|
try
|
|
{
|
|
var envImage = Environment.GetEnvironmentVariable("DOCKER_IMAGE")
|
|
?? Environment.GetEnvironmentVariable("CONTAINER_IMAGE");
|
|
if (!string.IsNullOrWhiteSpace(envImage))
|
|
return envImage;
|
|
|
|
return ReadImageFromEnviron();
|
|
}
|
|
catch { /* best effort */ }
|
|
return null;
|
|
}
|
|
|
|
private static string? ReadImageFromEnviron()
|
|
{
|
|
if (!File.Exists(ProcEnvironPath)) return null;
|
|
|
|
var environ = File.ReadAllText(ProcEnvironPath);
|
|
foreach (var pair in environ.Split('\0'))
|
|
{
|
|
if (pair.StartsWith("DOCKER_IMAGE=", StringComparison.OrdinalIgnoreCase))
|
|
return pair["DOCKER_IMAGE=".Length..];
|
|
if (pair.StartsWith("IMAGE_NAME=", StringComparison.OrdinalIgnoreCase))
|
|
return pair["IMAGE_NAME=".Length..];
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Container environment information.
|
|
/// </summary>
|
|
public sealed record ContainerInfo
|
|
{
|
|
public bool IsContainer { get; init; }
|
|
public string? ContainerId { get; init; }
|
|
public string? ContainerImage { get; init; }
|
|
public long? MemoryLimitBytes { get; init; }
|
|
public long? CpuLimit { get; init; }
|
|
}
|