## 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
89 lines
3.2 KiB
C#
89 lines
3.2 KiB
C#
namespace Heimdall.Agent.Services;
|
|
|
|
/// <summary>
|
|
/// Shared cgroup limit readers. Eliminates duplication between
|
|
/// <see cref="CpuInfoProvider"/> and <see cref="ContainerInfoProvider"/>.
|
|
/// </summary>
|
|
internal static class CgroupReader
|
|
{
|
|
/// <summary>
|
|
/// Reads the CPU limit from cgroup v1/v2. Returns the limit as a
|
|
/// percentage-based value (e.g. 200 = 2 cores), or null if unlimited.
|
|
/// </summary>
|
|
public static long? GetCpuLimit()
|
|
{
|
|
try
|
|
{
|
|
if (!OperatingSystem.IsLinux()) return null;
|
|
|
|
// cgroup v2: /sys/fs/cgroup/cpu.max → "max 100000" or "200000 100000"
|
|
var cpuMaxPath = "/sys/fs/cgroup/cpu.max";
|
|
if (File.Exists(cpuMaxPath))
|
|
{
|
|
var content = File.ReadAllText(cpuMaxPath).Trim();
|
|
var parts = content.Split(' ', StringSplitOptions.RemoveEmptyEntries);
|
|
if (parts.Length == 2 && parts[0] != "max" &&
|
|
long.TryParse(parts[0], out var quota) &&
|
|
long.TryParse(parts[1], out var period) && period > 0)
|
|
{
|
|
return (long)Math.Ceiling((double)quota / period * 100);
|
|
}
|
|
}
|
|
|
|
// cgroup v1: /sys/fs/cgroup/cpu/cpu.cfs_quota_us and cpu.cfs_period_us
|
|
var quotaPath = "/sys/fs/cgroup/cpu/cpu.cfs_quota_us";
|
|
var periodPath = "/sys/fs/cgroup/cpu/cpu.cfs_period_us";
|
|
if (File.Exists(quotaPath) && File.Exists(periodPath))
|
|
{
|
|
var quotaStr = File.ReadAllText(quotaPath).Trim();
|
|
var periodStr = File.ReadAllText(periodPath).Trim();
|
|
if (long.TryParse(quotaStr, out var quota) && quota > 0 &&
|
|
long.TryParse(periodStr, out var period) && period > 0)
|
|
{
|
|
return (long)Math.Ceiling((double)quota / period * 100);
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
catch
|
|
{
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Reads the memory limit from cgroup v1/v2. Returns bytes, or null if unlimited.
|
|
/// </summary>
|
|
public static long? GetMemoryLimit()
|
|
{
|
|
try
|
|
{
|
|
if (!OperatingSystem.IsLinux()) return null;
|
|
|
|
// cgroup v2: /sys/fs/cgroup/memory.max
|
|
var memMaxPath = "/sys/fs/cgroup/memory.max";
|
|
if (File.Exists(memMaxPath))
|
|
{
|
|
var content = File.ReadAllText(memMaxPath).Trim();
|
|
if (content != "max" && long.TryParse(content, out var limit) && limit > 0)
|
|
return limit;
|
|
}
|
|
|
|
// cgroup v1: /sys/fs/cgroup/memory/memory.limit_in_bytes
|
|
var v1Path = "/sys/fs/cgroup/memory/memory.limit_in_bytes";
|
|
if (File.Exists(v1Path))
|
|
{
|
|
var content = File.ReadAllText(v1Path).Trim();
|
|
if (long.TryParse(content, out var limit) && limit > 0 &&
|
|
limit < long.MaxValue / 2) // sanity: ignore unlimited
|
|
{
|
|
return limit;
|
|
}
|
|
}
|
|
}
|
|
catch { /* best effort */ }
|
|
return null;
|
|
}
|
|
}
|