0 ? round((($dTotal - $dIdle) / $dTotal) * 100, 1) : 0.0; } function getMemory(): array { $lines = file('/proc/meminfo'); $mem = []; foreach ($lines as $l) { if (preg_match('/^(\w+):\s+(\d+)/', $l, $m)) $mem[$m[1]] = (int)$m[2]; } $total = $mem['MemTotal'] ?? 0; $available = $mem['MemAvailable'] ?? 0; $used = $total - $available; return [ 'total_mb' => round($total / 1024), 'used_mb' => round($used / 1024), 'free_mb' => round($available / 1024), 'percent' => $total > 0 ? round(($used / $total) * 100, 1) : 0, ]; } function getDisk(): array { $disks = []; foreach (disk_total_space('/') as $dummy) break; // warm up $total = disk_total_space('/'); $free = disk_free_space('/'); $used = $total - $free; return [ 'total_gb' => round($total / 1073741824, 1), 'used_gb' => round($used / 1073741824, 1), 'free_gb' => round($free / 1073741824, 1), 'percent' => $total > 0 ? round(($used / $total) * 100, 1) : 0, ]; } function getDisk2(): array { $total = disk_total_space('/'); $free = disk_free_space('/'); $used = $total - $free; return [ 'total_gb' => round($total / 1073741824, 1), 'used_gb' => round($used / 1073741824, 1), 'free_gb' => round($free / 1073741824, 1), 'percent' => $total > 0 ? round(($used / $total) * 100, 1) : 0, ]; } function getUptime(): string { $secs = (int)file_get_contents('/proc/uptime'); $d = intdiv($secs, 86400); $h = intdiv($secs % 86400, 3600); $m = intdiv($secs % 3600, 60); return "{$d}d {$h}h {$m}m"; } function getLoadAvg(): array { $l = explode(' ', file_get_contents('/proc/loadavg')); return ['1m' => (float)$l[0], '5m' => (float)$l[1], '15m' => (float)$l[2]]; } function getNetworkIO(): array { $lines = file('/proc/net/dev'); $ifaces = []; foreach ($lines as $line) { if (strpos($line, ':') === false) continue; [$name, $stats] = explode(':', $line, 2); $name = trim($name); if ($name === 'lo') continue; $vals = preg_split('/\s+/', trim($stats)); $ifaces[$name] = [ 'rx_mb' => round($vals[0] / 1048576, 2), 'tx_mb' => round($vals[8] / 1048576, 2), ]; } return $ifaces; } function getServices(): array { $services = ['apache2', 'mysql']; $result = []; foreach ($services as $svc) { $out = shell_exec('systemctl is-active ' . escapeshellarg($svc) . ' 2>/dev/null'); $result[$svc] = trim($out ?? '') === 'active'; } return $result; } function getTopProcesses(): array { $out = shell_exec("ps aux --sort=-%cpu | awk 'NR>1 && NR<=6 {print $11\",\"$3\",\"$4}' 2>/dev/null"); $procs = []; foreach (explode("\n", trim($out ?? '')) as $line) { if (!$line) continue; [$cmd, $cpu, $mem] = explode(',', $line, 3); $procs[] = ['cmd' => basename($cmd), 'cpu' => (float)$cpu, 'mem' => (float)$mem]; } return $procs; } $cpu = getCpuUsage(); $memory = getMemory(); $disk = getDisk2(); $stats = [ 'cpu' => $cpu, 'memory' => $memory, 'disk' => $disk, 'uptime' => getUptime(), 'load' => getLoadAvg(), 'network_io' => getNetworkIO(), 'services' => getServices(), 'processes' => getTopProcesses(), 'hostname' => gethostname(), 'ip' => JARVIS_IP, 'timestamp' => date('c'), ]; // Log to history JarvisDB::execute( 'INSERT INTO metrics_history (metric_name, metric_value, host) VALUES (?,?,?),(?,?,?),(?,?,?)', ['cpu', $cpu, 'jarvis', 'memory', $memory['percent'], 'jarvis', 'disk', $disk['percent'], 'jarvis'] ); echo json_encode($stats);