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:
1 parent
f7e6b0cec3
commit
4792b69e46
63 files changed
+8711
-563
No files matched your search
+3
-3
@@ -13,10 +13,10 @@ public class SubmitAgentLogsHandler : IRequestHandler<SubmitAgentLogsCommand, Se
|
||||
|
||||
public SubmitAgentLogsHandler(IApplicationDbContext db) => _db = db;
|
||||
|
||||
public async Task<ServiceResult> Handle(SubmitAgentLogsCommand request, CancellationToken ct)
|
||||
public async Task<ServiceResult> Handle(SubmitAgentLogsCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var agentExists = await _db.Agents
|
||||
.AnyAsync(a => a.Id == request.AgentId, ct)
|
||||
.AnyAsync(a => a.Id == request.AgentId, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
if (!agentExists)
|
||||
return ServiceResult.NotFound("Agent not found");
|
||||
@@ -56,7 +56,7 @@ public class SubmitAgentLogsHandler : IRequestHandler<SubmitAgentLogsCommand, Se
|
||||
|
||||
foreach (var entry in entries)
|
||||
_db.AgentLogEntries.Add(entry);
|
||||
await _db.SaveChangesWithRetryAsync(ct: ct).ConfigureAwait(false);
|
||||
await _db.SaveChangesWithRetryAsync(ct: cancellationToken).ConfigureAwait(false);
|
||||
return ServiceResult.Success();
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ public class GetAgentLogsHandler : IRequestHandler<GetAgentLogsQuery, ServiceRes
|
||||
|
||||
public GetAgentLogsHandler(IApplicationDbContext db) => _db = db;
|
||||
|
||||
public async Task<ServiceResult<PagedResult<AgentLogEntryDto>>> Handle(GetAgentLogsQuery request, CancellationToken ct)
|
||||
public async Task<ServiceResult<PagedResult<AgentLogEntryDto>>> Handle(GetAgentLogsQuery request, CancellationToken cancellationToken)
|
||||
{
|
||||
var query = _db.AgentLogEntries.Where(e => e.AgentId == request.AgentId);
|
||||
|
||||
@@ -37,7 +37,7 @@ public class GetAgentLogsHandler : IRequestHandler<GetAgentLogsQuery, ServiceRes
|
||||
|| (e.Source != null && e.Source.Contains(search, StringComparison.OrdinalIgnoreCase)));
|
||||
}
|
||||
|
||||
var total = await query.CountAsync(ct).ConfigureAwait(false);
|
||||
var total = await query.CountAsync(cancellationToken).ConfigureAwait(false);
|
||||
|
||||
var items = await query
|
||||
.OrderByDescending(e => e.TimestampUtc)
|
||||
@@ -51,7 +51,7 @@ public class GetAgentLogsHandler : IRequestHandler<GetAgentLogsQuery, ServiceRes
|
||||
e.Message,
|
||||
e.Exception,
|
||||
e.Source))
|
||||
.ToListAsync(ct)
|
||||
.ToListAsync(cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
|
||||
return ServiceResult<PagedResult<AgentLogEntryDto>>.Success(
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
using Heimdall.Application.DTOs;
|
||||
using Heimdall.Domain.Entities;
|
||||
|
||||
namespace Heimdall.Application.Agents;
|
||||
/// <summary>
|
||||
/// Extension methods to map <see cref="Agent"/> entity to <see cref="AgentResponse"/> DTO.
|
||||
/// Centralizes the mapping used across GetAllAgents, GetAgentsBySite, and GetAgent queries.
|
||||
/// </summary>
|
||||
public static class AgentExtensions
|
||||
{
|
||||
public static AgentResponse ToResponse(this Agent a) => new(
|
||||
a.Id, a.SiteId, a.Site?.Name ?? string.Empty, a.Name, a.Hostname, a.Version, a.Status,
|
||||
a.LastHeartbeat, a.PublicIp, a.HasGpu, a.CpuCores, a.MemoryMb,
|
||||
a.GpuType.ToString(), a.CreatedAt,
|
||||
a.CpuUsagePercent, a.MemoryUsageBytes, a.DiskUsagePercent, a.UptimeSeconds,
|
||||
// CPU inventory
|
||||
a.CpuVendor, a.CpuModel, a.CpuArchitecture,
|
||||
a.PhysicalCores, a.LogicalCores, a.ThreadsPerCore,
|
||||
a.CpuFrequencyMhz, a.CpuFlags, a.CpuCacheBytes, a.ContainerCpuLimit,
|
||||
// Memory inventory
|
||||
a.MemTotalBytes, a.MemAvailableBytes, a.SwapTotalBytes, a.SwapFreeBytes,
|
||||
a.ContainerMemoryLimitBytes,
|
||||
// Disk inventory
|
||||
a.DiskInventoryJson,
|
||||
// GPU inventory
|
||||
a.GpuInventoryJson,
|
||||
// Host info
|
||||
a.OsName, a.KernelVersion, a.Arch, a.UptimeMachineSeconds,
|
||||
// Container info
|
||||
a.ContainerId, a.ContainerImage,
|
||||
// Network & Temperature
|
||||
a.NetworkInterfacesJson, a.TemperatureJson);
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
using Heimdall.Application.DTOs;
|
||||
using Heimdall.Domain.Entities;
|
||||
|
||||
namespace Heimdall.Application.Agents;
|
||||
|
||||
/// <summary>
|
||||
/// Shared mapping between <see cref="Agent"/> entity and <see cref="AgentTelemetryFields"/>.
|
||||
/// Eliminates field-by-field duplication across HeartbeatHandler and AgentExtensions.
|
||||
/// </summary>
|
||||
public static class AgentTelemetryMapping
|
||||
{
|
||||
/// <summary>
|
||||
/// Reads all telemetry properties from an <see cref="Agent"/> entity
|
||||
/// and returns them as an <see cref="AgentTelemetryFields"/> record.
|
||||
/// </summary>
|
||||
public static AgentTelemetryFields ToTelemetryFields(this Agent a) => new(
|
||||
a.CpuUsagePercent, a.MemoryUsageBytes, a.DiskUsagePercent, a.UptimeSeconds,
|
||||
a.CpuVendor, a.CpuModel, a.CpuArchitecture,
|
||||
a.PhysicalCores, a.LogicalCores, a.ThreadsPerCore,
|
||||
a.CpuFrequencyMhz, a.CpuFlags, a.CpuCacheBytes, a.ContainerCpuLimit,
|
||||
a.MemTotalBytes, a.MemAvailableBytes, a.SwapTotalBytes, a.SwapFreeBytes,
|
||||
a.ContainerMemoryLimitBytes,
|
||||
a.DiskInventoryJson, a.GpuInventoryJson,
|
||||
a.OsName, a.KernelVersion, a.Arch, a.UptimeMachineSeconds,
|
||||
a.ContainerId, a.ContainerImage,
|
||||
a.NetworkInterfacesJson, a.TemperatureJson);
|
||||
|
||||
/// <summary>
|
||||
/// Applies all telemetry fields from a <see cref="AgentTelemetryFields"/> source
|
||||
/// (HeartbeatCommand, AgentHeartbeatRequest, etc.) to an <see cref="Agent"/> entity.
|
||||
/// Preserves existing inventory when no new data is present.
|
||||
/// </summary>
|
||||
public static void ApplyTo(this AgentTelemetryFields fields, Agent agent)
|
||||
{
|
||||
// Live metrics — always update
|
||||
agent.UpdateLiveMetrics(
|
||||
fields.CpuUsagePercent,
|
||||
fields.MemoryUsageBytes,
|
||||
fields.DiskUsagePercent,
|
||||
fields.UptimeSeconds);
|
||||
|
||||
// CPU inventory — update only when new data is present
|
||||
if (fields.CpuVendor is not null || fields.CpuModel is not null ||
|
||||
fields.PhysicalCores.HasValue || fields.LogicalCores.HasValue)
|
||||
{
|
||||
agent.UpdateCpuInventory(new CpuInventoryData(
|
||||
fields.CpuVendor, fields.CpuModel, fields.CpuArchitecture,
|
||||
fields.PhysicalCores, fields.LogicalCores, fields.ThreadsPerCore,
|
||||
fields.CpuFrequencyMhz, fields.CpuFlags, fields.CpuCacheBytes,
|
||||
fields.ContainerCpuLimit));
|
||||
}
|
||||
|
||||
// Memory/disk inventory — update only when new data is present
|
||||
if (fields.MemTotalBytes.HasValue || fields.DiskInventoryJson is not null)
|
||||
{
|
||||
agent.UpdateMemoryDiskInventory(new MemoryDiskInventoryData(
|
||||
fields.MemTotalBytes, fields.MemAvailableBytes,
|
||||
fields.SwapTotalBytes, fields.SwapFreeBytes,
|
||||
fields.ContainerMemoryLimitBytes, fields.DiskInventoryJson));
|
||||
}
|
||||
|
||||
// GPU inventory — update only when present
|
||||
if (fields.GpuInventoryJson is not null)
|
||||
agent.UpdateGpuInventory(fields.GpuInventoryJson);
|
||||
|
||||
// Host inventory — update only when new data is present
|
||||
if (fields.OsName is not null || fields.KernelVersion is not null ||
|
||||
fields.ContainerId is not null || fields.NetworkInterfacesJson is not null)
|
||||
{
|
||||
agent.UpdateHostInventory(new HostInventoryData(
|
||||
fields.OsName, fields.KernelVersion, fields.Arch,
|
||||
fields.UptimeMachineSeconds, fields.ContainerId,
|
||||
fields.ContainerImage, fields.NetworkInterfacesJson,
|
||||
fields.TemperatureJson));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
using Heimdall.Application.Common;
|
||||
using Heimdall.Application.DTOs;
|
||||
using Heimdall.Domain.Entities;
|
||||
using Heimdall.Domain.Enums;
|
||||
using MediatR;
|
||||
|
||||
@@ -15,4 +17,80 @@ public record HeartbeatCommand(
|
||||
double? CpuUsagePercent = null,
|
||||
long? MemoryUsageBytes = null,
|
||||
double? DiskUsagePercent = null,
|
||||
long? UptimeSeconds = null) : IRequest<ServiceResult>;
|
||||
long? UptimeSeconds = null,
|
||||
// CPU inventory
|
||||
string? CpuVendor = null,
|
||||
string? CpuModel = null,
|
||||
string? CpuArchitecture = null,
|
||||
int? PhysicalCores = null,
|
||||
int? LogicalCores = null,
|
||||
int? ThreadsPerCore = null,
|
||||
double? CpuFrequencyMhz = null,
|
||||
string? CpuFlags = null,
|
||||
long? CpuCacheBytes = null,
|
||||
long? ContainerCpuLimit = null,
|
||||
// Memory inventory
|
||||
long? MemTotalBytes = null,
|
||||
long? MemAvailableBytes = null,
|
||||
long? SwapTotalBytes = null,
|
||||
long? SwapFreeBytes = null,
|
||||
long? ContainerMemoryLimitBytes = null,
|
||||
// Disk inventory (JSON)
|
||||
string? DiskInventoryJson = null,
|
||||
// GPU inventory (JSON)
|
||||
string? GpuInventoryJson = null,
|
||||
// Host info
|
||||
string? OsName = null,
|
||||
string? KernelVersion = null,
|
||||
string? Arch = null,
|
||||
long? UptimeMachineSeconds = null,
|
||||
// Container info
|
||||
string? ContainerId = null,
|
||||
string? ContainerImage = null,
|
||||
// Network & Temperature (JSON)
|
||||
string? NetworkInterfacesJson = null,
|
||||
string? TemperatureJson = null) : AgentTelemetryFields(
|
||||
CpuUsagePercent, MemoryUsageBytes, DiskUsagePercent, UptimeSeconds,
|
||||
CpuVendor, CpuModel, CpuArchitecture, PhysicalCores, LogicalCores,
|
||||
ThreadsPerCore, CpuFrequencyMhz, CpuFlags, CpuCacheBytes, ContainerCpuLimit,
|
||||
MemTotalBytes, MemAvailableBytes, SwapTotalBytes, SwapFreeBytes, ContainerMemoryLimitBytes,
|
||||
DiskInventoryJson, GpuInventoryJson, OsName, KernelVersion, Arch, UptimeMachineSeconds,
|
||||
ContainerId, ContainerImage, NetworkInterfacesJson, TemperatureJson),
|
||||
IRequest<ServiceResult>
|
||||
{
|
||||
/// <summary>Returns true if any CPU inventory field is non-null.</summary>
|
||||
public bool HasCpuInventory() =>
|
||||
CpuVendor is not null || CpuModel is not null || CpuArchitecture is not null ||
|
||||
PhysicalCores is not null || LogicalCores is not null || ThreadsPerCore is not null ||
|
||||
CpuFrequencyMhz is not null || CpuFlags is not null || CpuCacheBytes is not null ||
|
||||
ContainerCpuLimit is not null;
|
||||
|
||||
/// <summary>Returns true if any memory/disk inventory field is non-null.</summary>
|
||||
public bool HasMemoryDiskInventory() =>
|
||||
MemTotalBytes is not null || MemAvailableBytes is not null ||
|
||||
SwapTotalBytes is not null || SwapFreeBytes is not null ||
|
||||
ContainerMemoryLimitBytes is not null || DiskInventoryJson is not null;
|
||||
|
||||
/// <summary>Returns true if any host inventory field is non-null.</summary>
|
||||
public bool HasHostInventory() =>
|
||||
OsName is not null || KernelVersion is not null || Arch is not null ||
|
||||
UptimeMachineSeconds is not null || ContainerId is not null ||
|
||||
ContainerImage is not null || NetworkInterfacesJson is not null ||
|
||||
TemperatureJson is not null;
|
||||
|
||||
/// <summary>Converts CPU inventory fields to <see cref="CpuInventoryData"/>.</summary>
|
||||
public CpuInventoryData ToCpuInventoryData() => new(
|
||||
CpuVendor, CpuModel, CpuArchitecture,
|
||||
PhysicalCores, LogicalCores, ThreadsPerCore,
|
||||
CpuFrequencyMhz, CpuFlags, CpuCacheBytes, ContainerCpuLimit);
|
||||
|
||||
/// <summary>Converts memory/disk inventory fields to <see cref="MemoryDiskInventoryData"/>.</summary>
|
||||
public MemoryDiskInventoryData ToMemoryDiskInventoryData() => new(
|
||||
MemTotalBytes, MemAvailableBytes, SwapTotalBytes, SwapFreeBytes,
|
||||
ContainerMemoryLimitBytes, DiskInventoryJson);
|
||||
|
||||
/// <summary>Converts host inventory fields to <see cref="HostInventoryData"/>.</summary>
|
||||
public HostInventoryData ToHostInventoryData() => new(
|
||||
OsName, KernelVersion, Arch, UptimeMachineSeconds,
|
||||
ContainerId, ContainerImage, NetworkInterfacesJson, TemperatureJson);
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
using Heimdall.Application.Agents;
|
||||
using Heimdall.Application.Common;
|
||||
using Heimdall.Domain.Enums;
|
||||
using MediatR;
|
||||
@@ -11,39 +12,39 @@ public class HeartbeatHandler : IRequestHandler<HeartbeatCommand, ServiceResult>
|
||||
|
||||
public HeartbeatHandler(IApplicationDbContext db) => _db = db;
|
||||
|
||||
public async Task<ServiceResult> Handle(HeartbeatCommand request, CancellationToken ct)
|
||||
public async Task<ServiceResult> Handle(HeartbeatCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
var agent = await _db.Agents.FirstOrDefaultAsync(a => a.Id == request.AgentId, ct).ConfigureAwait(false);
|
||||
var agent = await _db.Agents.FirstOrDefaultAsync(a => a.Id == request.AgentId, cancellationToken).ConfigureAwait(false);
|
||||
if (agent is null)
|
||||
return ServiceResult.NotFound("Agent not found");
|
||||
|
||||
if (request.IsOffline)
|
||||
{
|
||||
// Graceful shutdown: reflect it immediately instead of waiting for
|
||||
// the 90s HeartbeatMonitorJob timeout.
|
||||
agent.MarkOffline();
|
||||
}
|
||||
else
|
||||
{
|
||||
agent.UpdateHeartbeat(request.PublicIp);
|
||||
|
||||
if (request.HasGpu.HasValue || request.CpuCores.HasValue || request.MemoryMb.HasValue || request.GpuType.HasValue)
|
||||
{
|
||||
agent.UpdateCapabilities(
|
||||
request.HasGpu ?? false,
|
||||
request.CpuCores ?? 0,
|
||||
request.MemoryMb ?? 0,
|
||||
request.GpuType ?? GpuAcceleration.None);
|
||||
}
|
||||
|
||||
agent.UpdateLiveMetrics(
|
||||
request.CpuUsagePercent,
|
||||
request.MemoryUsageBytes,
|
||||
request.DiskUsagePercent,
|
||||
request.UptimeSeconds);
|
||||
UpdateAgentState(agent, request);
|
||||
}
|
||||
|
||||
await _db.SaveChangesWithRetryAsync(ct: ct).ConfigureAwait(false);
|
||||
await _db.SaveChangesWithRetryAsync(ct: cancellationToken).ConfigureAwait(false);
|
||||
return ServiceResult.Success();
|
||||
}
|
||||
|
||||
private static void UpdateAgentState(Domain.Entities.Agent agent, HeartbeatCommand request)
|
||||
{
|
||||
agent.UpdateHeartbeat(request.PublicIp);
|
||||
|
||||
if (request.HasGpu.HasValue || request.CpuCores.HasValue || request.MemoryMb.HasValue || request.GpuType.HasValue)
|
||||
{
|
||||
agent.UpdateCapabilities(
|
||||
request.HasGpu ?? false,
|
||||
request.CpuCores ?? 0,
|
||||
request.MemoryMb ?? 0,
|
||||
request.GpuType ?? GpuAcceleration.None);
|
||||
}
|
||||
|
||||
// Apply all telemetry fields via shared helper
|
||||
request.ApplyTo(agent);
|
||||
}
|
||||
}
|
||||
@@ -21,12 +21,6 @@ public class GetAgentHandler : IRequestHandler<GetAgentQuery, ServiceResult<Agen
|
||||
if (agent is null)
|
||||
return ServiceResult<AgentResponse>.NotFound("Agent not found");
|
||||
|
||||
return ServiceResult<AgentResponse>.Success(MapResponse(agent));
|
||||
return ServiceResult<AgentResponse>.Success(agent.ToResponse());
|
||||
}
|
||||
|
||||
private static AgentResponse MapResponse(Domain.Entities.Agent a) => new(
|
||||
a.Id, a.SiteId, a.Site?.Name ?? string.Empty, a.Name, a.Hostname, a.Version, a.Status,
|
||||
a.LastHeartbeat, a.PublicIp, a.HasGpu, a.CpuCores, a.MemoryMb,
|
||||
a.GpuType.ToString(), a.CreatedAt,
|
||||
a.CpuUsagePercent, a.MemoryUsageBytes, a.DiskUsagePercent, a.UptimeSeconds);
|
||||
}
|
||||
@@ -27,16 +27,10 @@ public class GetAgentsBySiteHandler : IRequestHandler<GetAgentsBySiteQuery, Serv
|
||||
var items = await query
|
||||
.Skip((request.Page - 1) * request.PageSize)
|
||||
.Take(request.PageSize)
|
||||
.Select(a => MapResponse(a))
|
||||
.Select(a => a.ToResponse())
|
||||
.ToListAsync(ct);
|
||||
|
||||
return ServiceResult<PagedResult<AgentResponse>>.Success(
|
||||
new PagedResult<AgentResponse>(items, total, request.Page, request.PageSize));
|
||||
}
|
||||
|
||||
private static AgentResponse MapResponse(Domain.Entities.Agent a) => new(
|
||||
a.Id, a.SiteId, a.Site?.Name ?? string.Empty, a.Name, a.Hostname, a.Version, a.Status,
|
||||
a.LastHeartbeat, a.PublicIp, a.HasGpu, a.CpuCores, a.MemoryMb,
|
||||
a.GpuType.ToString(), a.CreatedAt,
|
||||
a.CpuUsagePercent, a.MemoryUsageBytes, a.DiskUsagePercent, a.UptimeSeconds);
|
||||
}
|
||||
@@ -23,16 +23,10 @@ public class GetAllAgentsHandler : IRequestHandler<GetAllAgentsQuery, ServiceRes
|
||||
var items = await query
|
||||
.Skip((request.Page - 1) * request.PageSize)
|
||||
.Take(request.PageSize)
|
||||
.Select(a => MapResponse(a))
|
||||
.Select(a => a.ToResponse())
|
||||
.ToListAsync(ct);
|
||||
|
||||
return ServiceResult<PagedResult<AgentResponse>>.Success(
|
||||
new PagedResult<AgentResponse>(items, total, request.Page, request.PageSize));
|
||||
}
|
||||
|
||||
private static AgentResponse MapResponse(Domain.Entities.Agent a) => new(
|
||||
a.Id, a.SiteId, a.Site?.Name ?? string.Empty, a.Name, a.Hostname, a.Version, a.Status,
|
||||
a.LastHeartbeat, a.PublicIp, a.HasGpu, a.CpuCores, a.MemoryMb,
|
||||
a.GpuType.ToString(), a.CreatedAt,
|
||||
a.CpuUsagePercent, a.MemoryUsageBytes, a.DiskUsagePercent, a.UptimeSeconds);
|
||||
}
|
||||
+5
-5
@@ -33,17 +33,17 @@ public class ReportCameraStatusCommandHandler : IRequestHandler<ReportCameraStat
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task<ServiceResult> Handle(ReportCameraStatusCommand request, CancellationToken ct)
|
||||
public async Task<ServiceResult> Handle(ReportCameraStatusCommand request, CancellationToken cancellationToken)
|
||||
{
|
||||
// Resolve the agent → site, then verify the camera belongs to that site.
|
||||
var agent = await _db.Agents.AsNoTracking()
|
||||
.FirstOrDefaultAsync(a => a.Id == request.AgentId, ct)
|
||||
.FirstOrDefaultAsync(a => a.Id == request.AgentId, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
if (agent is null)
|
||||
return ServiceResult.NotFound("Agent not found");
|
||||
|
||||
var camera = await _db.Cameras
|
||||
.FirstOrDefaultAsync(c => c.Id == request.CameraId, ct)
|
||||
.FirstOrDefaultAsync(c => c.Id == request.CameraId, cancellationToken)
|
||||
.ConfigureAwait(false);
|
||||
if (camera is null)
|
||||
return ServiceResult.NotFound("Camera not found");
|
||||
@@ -54,14 +54,14 @@ public class ReportCameraStatusCommandHandler : IRequestHandler<ReportCameraStat
|
||||
if (camera.Status != request.Status)
|
||||
{
|
||||
camera.SetStatus(request.Status);
|
||||
await _db.SaveChangesWithRetryAsync(ct: ct).ConfigureAwait(false);
|
||||
await _db.SaveChangesWithRetryAsync(ct: cancellationToken).ConfigureAwait(false);
|
||||
|
||||
_logger.LogInformation(
|
||||
"Agent {AgentId} reported camera {CameraId} status -> {Status}",
|
||||
request.AgentId, request.CameraId, request.Status);
|
||||
|
||||
await _monitoring.NotifyCameraStatusChanged(
|
||||
agent.SiteId, request.CameraId, request.Status.ToString(), ct).ConfigureAwait(false);
|
||||
agent.SiteId, request.CameraId, request.Status.ToString(), cancellationToken).ConfigureAwait(false);
|
||||
}
|
||||
|
||||
return ServiceResult.Success();
|
||||
|
||||
@@ -18,7 +18,45 @@ public record AgentHeartbeatRequest(
|
||||
double? CpuUsagePercent = null,
|
||||
long? MemoryUsageBytes = null,
|
||||
double? DiskUsagePercent = null,
|
||||
long? UptimeSeconds = null);
|
||||
long? UptimeSeconds = null,
|
||||
// CPU inventory
|
||||
string? CpuVendor = null,
|
||||
string? CpuModel = null,
|
||||
string? CpuArchitecture = null,
|
||||
int? PhysicalCores = null,
|
||||
int? LogicalCores = null,
|
||||
int? ThreadsPerCore = null,
|
||||
double? CpuFrequencyMhz = null,
|
||||
string? CpuFlags = null,
|
||||
long? CpuCacheBytes = null,
|
||||
long? ContainerCpuLimit = null,
|
||||
// Memory inventory
|
||||
long? MemTotalBytes = null,
|
||||
long? MemAvailableBytes = null,
|
||||
long? SwapTotalBytes = null,
|
||||
long? SwapFreeBytes = null,
|
||||
long? ContainerMemoryLimitBytes = null,
|
||||
// Disk inventory (JSON)
|
||||
string? DiskInventoryJson = null,
|
||||
// GPU inventory (JSON)
|
||||
string? GpuInventoryJson = null,
|
||||
// Host info
|
||||
string? OsName = null,
|
||||
string? KernelVersion = null,
|
||||
string? Arch = null,
|
||||
long? UptimeMachineSeconds = null,
|
||||
// Container info
|
||||
string? ContainerId = null,
|
||||
string? ContainerImage = null,
|
||||
// Network & Temperature (JSON)
|
||||
string? NetworkInterfacesJson = null,
|
||||
string? TemperatureJson = null) : AgentTelemetryFields(
|
||||
CpuUsagePercent, MemoryUsageBytes, DiskUsagePercent, UptimeSeconds,
|
||||
CpuVendor, CpuModel, CpuArchitecture, PhysicalCores, LogicalCores,
|
||||
ThreadsPerCore, CpuFrequencyMhz, CpuFlags, CpuCacheBytes, ContainerCpuLimit,
|
||||
MemTotalBytes, MemAvailableBytes, SwapTotalBytes, SwapFreeBytes, ContainerMemoryLimitBytes,
|
||||
DiskInventoryJson, GpuInventoryJson, OsName, KernelVersion, Arch, UptimeMachineSeconds,
|
||||
ContainerId, ContainerImage, NetworkInterfacesJson, TemperatureJson);
|
||||
|
||||
public record AgentResponse(
|
||||
Guid Id,
|
||||
@@ -38,7 +76,45 @@ public record AgentResponse(
|
||||
double? CpuUsagePercent = null,
|
||||
long? MemoryUsageBytes = null,
|
||||
double? DiskUsagePercent = null,
|
||||
long? UptimeSeconds = null);
|
||||
long? UptimeSeconds = null,
|
||||
// CPU inventory
|
||||
string? CpuVendor = null,
|
||||
string? CpuModel = null,
|
||||
string? CpuArchitecture = null,
|
||||
int? PhysicalCores = null,
|
||||
int? LogicalCores = null,
|
||||
int? ThreadsPerCore = null,
|
||||
double? CpuFrequencyMhz = null,
|
||||
string? CpuFlags = null,
|
||||
long? CpuCacheBytes = null,
|
||||
long? ContainerCpuLimit = null,
|
||||
// Memory inventory
|
||||
long? MemTotalBytes = null,
|
||||
long? MemAvailableBytes = null,
|
||||
long? SwapTotalBytes = null,
|
||||
long? SwapFreeBytes = null,
|
||||
long? ContainerMemoryLimitBytes = null,
|
||||
// Disk inventory (JSON)
|
||||
string? DiskInventoryJson = null,
|
||||
// GPU inventory (JSON)
|
||||
string? GpuInventoryJson = null,
|
||||
// Host info
|
||||
string? OsName = null,
|
||||
string? KernelVersion = null,
|
||||
string? Arch = null,
|
||||
long? UptimeMachineSeconds = null,
|
||||
// Container info
|
||||
string? ContainerId = null,
|
||||
string? ContainerImage = null,
|
||||
// Network & Temperature (JSON)
|
||||
string? NetworkInterfacesJson = null,
|
||||
string? TemperatureJson = null) : AgentTelemetryFields(
|
||||
CpuUsagePercent, MemoryUsageBytes, DiskUsagePercent, UptimeSeconds,
|
||||
CpuVendor, CpuModel, CpuArchitecture, PhysicalCores, LogicalCores,
|
||||
ThreadsPerCore, CpuFrequencyMhz, CpuFlags, CpuCacheBytes, ContainerCpuLimit,
|
||||
MemTotalBytes, MemAvailableBytes, SwapTotalBytes, SwapFreeBytes, ContainerMemoryLimitBytes,
|
||||
DiskInventoryJson, GpuInventoryJson, OsName, KernelVersion, Arch, UptimeMachineSeconds,
|
||||
ContainerId, ContainerImage, NetworkInterfacesJson, TemperatureJson);
|
||||
|
||||
public record RegisterAgentResponse(
|
||||
Guid AgentId,
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
namespace Heimdall.Application.DTOs;
|
||||
|
||||
/// <summary>
|
||||
/// Common telemetry fields shared by AgentHeartbeatRequest, HeartbeatCommand, and AgentResponse.
|
||||
/// Extracted to eliminate code duplication across these DTOs.
|
||||
/// </summary>
|
||||
public record AgentTelemetryFields(
|
||||
// System metrics
|
||||
double? CpuUsagePercent = null,
|
||||
long? MemoryUsageBytes = null,
|
||||
double? DiskUsagePercent = null,
|
||||
long? UptimeSeconds = null,
|
||||
// CPU inventory
|
||||
string? CpuVendor = null,
|
||||
string? CpuModel = null,
|
||||
string? CpuArchitecture = null,
|
||||
int? PhysicalCores = null,
|
||||
int? LogicalCores = null,
|
||||
int? ThreadsPerCore = null,
|
||||
double? CpuFrequencyMhz = null,
|
||||
string? CpuFlags = null,
|
||||
long? CpuCacheBytes = null,
|
||||
long? ContainerCpuLimit = null,
|
||||
// Memory inventory
|
||||
long? MemTotalBytes = null,
|
||||
long? MemAvailableBytes = null,
|
||||
long? SwapTotalBytes = null,
|
||||
long? SwapFreeBytes = null,
|
||||
long? ContainerMemoryLimitBytes = null,
|
||||
// Disk inventory (JSON)
|
||||
string? DiskInventoryJson = null,
|
||||
// GPU inventory (JSON)
|
||||
string? GpuInventoryJson = null,
|
||||
// Host info
|
||||
string? OsName = null,
|
||||
string? KernelVersion = null,
|
||||
string? Arch = null,
|
||||
long? UptimeMachineSeconds = null,
|
||||
// Container info
|
||||
string? ContainerId = null,
|
||||
string? ContainerImage = null,
|
||||
// Network & Temperature (JSON)
|
||||
string? NetworkInterfacesJson = null,
|
||||
string? TemperatureJson = null);
|
||||
@@ -142,7 +142,7 @@ public static class CameraStreamTemplates
|
||||
|
||||
if (urls.Count == 0)
|
||||
{
|
||||
var fallbackPort = filter?.Contains(554) == true ? 554 : 554;
|
||||
const int fallbackPort = 554;
|
||||
urls.Add($"rtsp://{ip}:{fallbackPort}/stream1");
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user