Files
jarvis/api/endpoints/do_server.php
T
myron dc55e6c45b Initial commit: JARVIS AI dashboard v2.3
- 4-tier chat: HA control → Ollama → Groq → Claude
- Push-based agent system with heartbeat/metrics
- Network monitoring, alerts, Proxmox, Home Assistant
- Windows + Linux agent installers
- Stats cache cron, facts collector, KB engine
2026-05-25 13:22:57 +00:00

78 lines
2.9 KiB
PHP

<?php
// Digital Ocean server monitoring via SSH
function sshCommand(string $cmd): string {
$sshCmd = sprintf(
'sshpass -p %s ssh -o StrictHostKeyChecking=no -o ConnectTimeout=8 %s@%s %s 2>/dev/null',
escapeshellarg(DO_SSH_PASS),
escapeshellarg(DO_SSH_USER),
escapeshellarg(DO_SERVER_IP),
escapeshellarg($cmd)
);
return shell_exec($sshCmd) ?? '';
}
// Check if sshpass is available
$hasSshpass = trim(shell_exec('which sshpass 2>/dev/null'));
if (!$hasSshpass) {
echo json_encode([
'error' => 'sshpass not installed on Jarvis server. Run: sudo apt-get install sshpass',
'ip' => DO_SERVER_IP,
]);
exit;
}
// Gather DO server stats in one SSH session
$statsRaw = sshCommand("echo CPU:$(grep 'cpu ' /proc/stat | awk '{u=\$2+\$4; t=\$2+\$3+\$4+\$5; print u/t*100}');echo MEM_TOTAL:\$(grep MemTotal /proc/meminfo | awk '{print \$2}');echo MEM_FREE:\$(grep MemAvailable /proc/meminfo | awk '{print \$2}');echo UPTIME:\$(cat /proc/uptime | awk '{print int(\$1)}');echo DISK_USED:\$(df / | tail -1 | awk '{print \$5}');echo LOAD:\$(cat /proc/loadavg | awk '{print \$1}')");
$stats = [];
foreach (explode("\n", trim($statsRaw)) as $line) {
[$key, $val] = explode(':', $line, 2) + [null, null];
if ($key) $stats[$key] = trim($val ?? '');
}
// Get running services on DO
$services = sshCommand("systemctl is-active lsphp85 lshttpd nginx apache2 mysql mariadb php8.1-fpm 2>/dev/null | paste <(echo -e 'lsphp85\nlshttpd\nnginx\napache2\nmysql\nmariadb\nphp8.1-fpm') - | awk '{print \$1\":\"\$2}'");
$svcMap = [];
foreach (explode("\n", trim($services)) as $line) {
if (!$line) continue;
[$name, $status] = explode(':', $line, 2) + [null, null];
if ($name && $status && trim($status) === 'active') {
$svcMap[trim($name)] = true;
}
}
// Get disk usage per site
$siteDisk = sshCommand("du -sh /home/*/public_html 2>/dev/null | sort -h");
$sites = [];
foreach (explode("\n", trim($siteDisk)) as $line) {
if (preg_match('/^([\d.]+\w)\s+\/home\/([^\/]+)/', $line, $m)) {
$sites[$m[2]] = $m[1];
}
}
$cpuPct = isset($stats['CPU']) ? round((float)$stats['CPU'], 1) : null;
$memTotal = (int)($stats['MEM_TOTAL'] ?? 0);
$memFree = (int)($stats['MEM_FREE'] ?? 0);
$memUsed = $memTotal - $memFree;
$uptimeSec = (int)($stats['UPTIME'] ?? 0);
$uptimeDays = intdiv($uptimeSec, 86400);
$uptimeHrs = intdiv($uptimeSec % 86400, 3600);
echo json_encode([
'ip' => DO_SERVER_IP,
'reachable' => !empty($statsRaw),
'cpu_pct' => $cpuPct,
'memory' => [
'total_mb' => round($memTotal / 1024),
'used_mb' => round($memUsed / 1024),
'percent' => $memTotal > 0 ? round(($memUsed/$memTotal)*100,1) : 0,
],
'disk_used_pct' => $stats['DISK_USED'] ?? null,
'load_1m' => (float)($stats['LOAD'] ?? 0),
'uptime' => "{$uptimeDays}d {$uptimeHrs}h",
'services' => $svcMap,
'sites' => $sites,
'timestamp' => date('c'),
]);