fix: Update vpn_clients speed columns in ServerMonitoring for real-time display
This commit is contained in:
+56
-20
@@ -50,7 +50,8 @@ class ServerMonitoring
|
||||
$results = [];
|
||||
|
||||
foreach ($clients as $client) {
|
||||
if ($client['status'] !== 'active') continue;
|
||||
if ($client['status'] !== 'active')
|
||||
continue;
|
||||
|
||||
$stats = $this->getClientStats($client);
|
||||
if ($stats) {
|
||||
@@ -75,7 +76,7 @@ class ServerMonitoring
|
||||
$cmd = "top -bn1 | grep 'Cpu(s)' | sed 's/.*, *\\([0-9.]*\\)%* id.*/\\1/' | awk '{print 100 - \$1}'";
|
||||
$result = $this->execSSH($cmd);
|
||||
|
||||
return $result ? (float)trim($result) : null;
|
||||
return $result ? (float) trim($result) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -86,7 +87,7 @@ class ServerMonitoring
|
||||
$cmd = "free -m | grep Mem | awk '{print \$3}'";
|
||||
$result = $this->execSSH($cmd);
|
||||
|
||||
return $result ? (int)trim($result) : null;
|
||||
return $result ? (int) trim($result) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -97,7 +98,7 @@ class ServerMonitoring
|
||||
$cmd = "free -m | grep Mem | awk '{print \$2}'";
|
||||
$result = $this->execSSH($cmd);
|
||||
|
||||
return $result ? (int)trim($result) : null;
|
||||
return $result ? (int) trim($result) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -108,7 +109,7 @@ class ServerMonitoring
|
||||
$cmd = "df -BG / | tail -1 | awk '{print \$3}' | sed 's/G//'";
|
||||
$result = $this->execSSH($cmd);
|
||||
|
||||
return $result ? (float)trim($result) : null;
|
||||
return $result ? (float) trim($result) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -119,7 +120,7 @@ class ServerMonitoring
|
||||
$cmd = "df -BG / | tail -1 | awk '{print \$2}' | sed 's/G//'";
|
||||
$result = $this->execSSH($cmd);
|
||||
|
||||
return $result ? (float)trim($result) : null;
|
||||
return $result ? (float) trim($result) : null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -131,16 +132,18 @@ class ServerMonitoring
|
||||
$cmd = "cat /sys/class/net/\$(ip route | grep default | awk '{print \$5}' | head -1)/statistics/rx_bytes";
|
||||
$bytes1 = $this->execSSH($cmd);
|
||||
|
||||
if (!$bytes1) return null;
|
||||
if (!$bytes1)
|
||||
return null;
|
||||
|
||||
sleep(1); // Wait 1 second
|
||||
|
||||
$bytes2 = $this->execSSH($cmd);
|
||||
|
||||
if (!$bytes2) return null;
|
||||
if (!$bytes2)
|
||||
return null;
|
||||
|
||||
// Calculate speed in Mbps
|
||||
$bytesPerSec = (int)$bytes2 - (int)$bytes1;
|
||||
$bytesPerSec = (int) $bytes2 - (int) $bytes1;
|
||||
$mbps = ($bytesPerSec * 8) / 1000000;
|
||||
|
||||
return round($mbps, 2);
|
||||
@@ -155,16 +158,18 @@ class ServerMonitoring
|
||||
$cmd = "cat /sys/class/net/\$(ip route | grep default | awk '{print \$5}' | head -1)/statistics/tx_bytes";
|
||||
$bytes1 = $this->execSSH($cmd);
|
||||
|
||||
if (!$bytes1) return null;
|
||||
if (!$bytes1)
|
||||
return null;
|
||||
|
||||
sleep(1); // Wait 1 second
|
||||
|
||||
$bytes2 = $this->execSSH($cmd);
|
||||
|
||||
if (!$bytes2) return null;
|
||||
if (!$bytes2)
|
||||
return null;
|
||||
|
||||
// Calculate speed in Mbps
|
||||
$bytesPerSec = (int)$bytes2 - (int)$bytes1;
|
||||
$bytesPerSec = (int) $bytes2 - (int) $bytes1;
|
||||
$mbps = ($bytesPerSec * 8) / 1000000;
|
||||
|
||||
return round($mbps, 2);
|
||||
@@ -184,7 +189,8 @@ class ServerMonitoring
|
||||
$cmd = "docker exec {$containerName} wg show all dump | grep '{$publicKey}' | awk '{print \$6, \$7}'";
|
||||
$result = $this->execSSH($cmd);
|
||||
|
||||
if (!$result) return null;
|
||||
if (!$result)
|
||||
return null;
|
||||
|
||||
list($bytesReceived, $bytesSent) = explode(' ', trim($result));
|
||||
|
||||
@@ -206,8 +212,8 @@ class ServerMonitoring
|
||||
$timeDiff = time() - strtotime($previous['collected_at']);
|
||||
if ($timeDiff > 0) {
|
||||
// Calculate speed in Kbps
|
||||
$bytesDiffSent = (int)$bytesSent - (int)$previous['bytes_sent'];
|
||||
$bytesDiffReceived = (int)$bytesReceived - (int)$previous['bytes_received'];
|
||||
$bytesDiffSent = (int) $bytesSent - (int) $previous['bytes_sent'];
|
||||
$bytesDiffReceived = (int) $bytesReceived - (int) $previous['bytes_received'];
|
||||
|
||||
$speedUp = round(($bytesDiffSent * 8) / $timeDiff / 1000, 2);
|
||||
$speedDown = round(($bytesDiffReceived * 8) / $timeDiff / 1000, 2);
|
||||
@@ -215,8 +221,8 @@ class ServerMonitoring
|
||||
}
|
||||
|
||||
return [
|
||||
'bytes_sent' => (int)$bytesSent,
|
||||
'bytes_received' => (int)$bytesReceived,
|
||||
'bytes_sent' => (int) $bytesSent,
|
||||
'bytes_received' => (int) $bytesReceived,
|
||||
'speed_up_kbps' => $speedUp,
|
||||
'speed_down_kbps' => $speedDown,
|
||||
];
|
||||
@@ -268,14 +274,44 @@ class ServerMonitoring
|
||||
$stats['speed_down_kbps'],
|
||||
]);
|
||||
|
||||
// Update last_handshake in vpn_clients table
|
||||
// Update vpn_clients table with latest stats
|
||||
$stmt = $db->prepare("
|
||||
UPDATE vpn_clients
|
||||
SET last_handshake = NOW()
|
||||
SET bytes_sent = ?, bytes_received = ?, speed_up = ?, speed_down = ?, current_speed = ?, last_handshake = NOW(), last_sync_at = NOW()
|
||||
WHERE id = ?
|
||||
");
|
||||
|
||||
$stmt->execute([$clientId]);
|
||||
$currentSpeed = $stats['speed_up_kbps'] + $stats['speed_down_kbps']; // Total speed in Kbps? Or bytes/s?
|
||||
// Note: speed_up_kbps is in Kbps (kilobits?).
|
||||
// VpnClient stores speed in Bytes/s (based on my previous edit: bytesDiff/timeDiff).
|
||||
// ServerMonitoring calculates: round(($bytesDiffSent * 8) / $timeDiff / 1000, 2) -> Kbps
|
||||
|
||||
// Wait! VpnClient implementation I did:
|
||||
// $speedUp = (int) ($sentDiff / $timeDiff); // Bytes per second
|
||||
|
||||
// ServerMonitoring implementation:
|
||||
// $speedUp = round(($bytesDiffSent * 8) / $timeDiff / 1000, 2); // Kilobits per second
|
||||
|
||||
// I need to be consistent.
|
||||
// Frontend expects KB/s (KiloBYTES).
|
||||
// VpnClient stores BYTES per second. Twig does `speed / 1024` -> KB/s.
|
||||
|
||||
// So I should convert ServerMonitoring stats to Bytes/s before saving to vpn_clients.
|
||||
// ServerMonitoring $stats['speed_up_kbps'] is Kbps.
|
||||
// Bytes/s = Kbps * 1000 / 8.
|
||||
|
||||
$speedUpBytes = (int) ($stats['speed_up_kbps'] * 1000 / 8);
|
||||
$speedDownBytes = (int) ($stats['speed_down_kbps'] * 1000 / 8);
|
||||
$totalSpeedBytes = $speedUpBytes + $speedDownBytes;
|
||||
|
||||
$stmt->execute([
|
||||
$stats['bytes_sent'],
|
||||
$stats['bytes_received'],
|
||||
$speedUpBytes,
|
||||
$speedDownBytes,
|
||||
$totalSpeedBytes,
|
||||
$clientId
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user