|
| 1 | +/** |
| 2 | + * Monitoring — Raccolta metriche sistema e heartbeat agenti |
| 3 | + */ |
| 4 | +import os from 'node:os'; |
| 5 | +import type { SystemMetrics, AgentHeartbeat, MonitorConfig } from './types.js'; |
| 6 | +import { DEFAULT_MONITOR_CONFIG } from './types.js'; |
| 7 | + |
| 8 | +const heartbeats = new Map<string, { lastSeen: number; metadata?: Record<string, unknown> }>(); |
| 9 | +const metricsHistory: SystemMetrics[] = []; |
| 10 | +let config: Required<MonitorConfig> = { ...DEFAULT_MONITOR_CONFIG }; |
| 11 | + |
| 12 | +/** Configura il monitor */ |
| 13 | +export function configureMonitor(opts: MonitorConfig): void { |
| 14 | + config = { ...DEFAULT_MONITOR_CONFIG, ...opts }; |
| 15 | +} |
| 16 | + |
| 17 | +/** Raccoglie metriche di sistema correnti */ |
| 18 | +export function collectMetrics(): SystemMetrics { |
| 19 | + const totalMem = os.totalmem(); |
| 20 | + const freeMem = os.freemem(); |
| 21 | + const usedMem = totalMem - freeMem; |
| 22 | + const cpus = os.cpus(); |
| 23 | + let cpuUsage = 0; |
| 24 | + if (cpus.length > 0) { |
| 25 | + const totals = cpus.reduce((acc, c) => { |
| 26 | + const t = Object.values(c.times).reduce((s, v) => s + v, 0); |
| 27 | + return { idle: acc.idle + c.times.idle, total: acc.total + t }; |
| 28 | + }, { idle: 0, total: 0 }); |
| 29 | + cpuUsage = Math.round((1 - totals.idle / totals.total) * 10000) / 100; |
| 30 | + } |
| 31 | + |
| 32 | + const metrics: SystemMetrics = { |
| 33 | + cpuUsage, |
| 34 | + memoryUsedMB: Math.round(usedMem / 1048576), |
| 35 | + memoryTotalMB: Math.round(totalMem / 1048576), |
| 36 | + memoryPercent: Math.round((usedMem / totalMem) * 10000) / 100, |
| 37 | + uptimeSeconds: Math.floor(os.uptime()), |
| 38 | + loadAvg: os.loadavg() as [number, number, number], |
| 39 | + timestamp: Date.now(), |
| 40 | + }; |
| 41 | + |
| 42 | + metricsHistory.push(metrics); |
| 43 | + while (metricsHistory.length > config.metricsHistorySize) metricsHistory.shift(); |
| 44 | + |
| 45 | + return metrics; |
| 46 | +} |
| 47 | + |
| 48 | +/** Ritorna storico metriche */ |
| 49 | +export function getMetricsHistory(): SystemMetrics[] { |
| 50 | + return [...metricsHistory]; |
| 51 | +} |
| 52 | + |
| 53 | +/** Registra heartbeat di un agente */ |
| 54 | +export function registerHeartbeat(agentId: string, metadata?: Record<string, unknown>): void { |
| 55 | + heartbeats.set(agentId, { lastSeen: Date.now(), metadata }); |
| 56 | +} |
| 57 | + |
| 58 | +/** Stato di un singolo agente */ |
| 59 | +export function getAgentStatus(agentId: string): AgentHeartbeat | null { |
| 60 | + const hb = heartbeats.get(agentId); |
| 61 | + if (!hb) return null; |
| 62 | + return { agentId, lastSeen: hb.lastSeen, status: resolveStatus(hb.lastSeen), metadata: hb.metadata }; |
| 63 | +} |
| 64 | + |
| 65 | +/** Stato di tutti gli agenti registrati */ |
| 66 | +export function getAllAgentStatuses(): AgentHeartbeat[] { |
| 67 | + return [...heartbeats.entries()].map(([agentId, hb]) => ({ |
| 68 | + agentId, lastSeen: hb.lastSeen, status: resolveStatus(hb.lastSeen), metadata: hb.metadata, |
| 69 | + })); |
| 70 | +} |
| 71 | + |
| 72 | +/** Controlla heartbeat e ritorna agenti stale/dead */ |
| 73 | +export function checkHeartbeats(): AgentHeartbeat[] { |
| 74 | + return getAllAgentStatuses().filter(a => a.status !== 'alive'); |
| 75 | +} |
| 76 | + |
| 77 | +/** Rimuove un agente dal monitoraggio */ |
| 78 | +export function removeAgent(agentId: string): boolean { |
| 79 | + return heartbeats.delete(agentId); |
| 80 | +} |
| 81 | + |
| 82 | +/** Reset completo (per test) */ |
| 83 | +export function resetMonitor(): void { |
| 84 | + heartbeats.clear(); |
| 85 | + metricsHistory.length = 0; |
| 86 | + config = { ...DEFAULT_MONITOR_CONFIG }; |
| 87 | +} |
| 88 | + |
| 89 | +function resolveStatus(lastSeen: number): 'alive' | 'stale' | 'dead' { |
| 90 | + const elapsed = Date.now() - lastSeen; |
| 91 | + if (elapsed > config.heartbeatDeadMs) return 'dead'; |
| 92 | + if (elapsed > config.heartbeatStaleMs) return 'stale'; |
| 93 | + return 'alive'; |
| 94 | +} |
0 commit comments