fix: Update vpn_clients speed columns in ServerMonitoring for real-time display
This commit is contained in:
@@ -50,7 +50,8 @@ class ServerMonitoring
|
|||||||
$results = [];
|
$results = [];
|
||||||
|
|
||||||
foreach ($clients as $client) {
|
foreach ($clients as $client) {
|
||||||
if ($client['status'] !== 'active') continue;
|
if ($client['status'] !== 'active')
|
||||||
|
continue;
|
||||||
|
|
||||||
$stats = $this->getClientStats($client);
|
$stats = $this->getClientStats($client);
|
||||||
if ($stats) {
|
if ($stats) {
|
||||||
@@ -131,13 +132,15 @@ class ServerMonitoring
|
|||||||
$cmd = "cat /sys/class/net/\$(ip route | grep default | awk '{print \$5}' | head -1)/statistics/rx_bytes";
|
$cmd = "cat /sys/class/net/\$(ip route | grep default | awk '{print \$5}' | head -1)/statistics/rx_bytes";
|
||||||
$bytes1 = $this->execSSH($cmd);
|
$bytes1 = $this->execSSH($cmd);
|
||||||
|
|
||||||
if (!$bytes1) return null;
|
if (!$bytes1)
|
||||||
|
return null;
|
||||||
|
|
||||||
sleep(1); // Wait 1 second
|
sleep(1); // Wait 1 second
|
||||||
|
|
||||||
$bytes2 = $this->execSSH($cmd);
|
$bytes2 = $this->execSSH($cmd);
|
||||||
|
|
||||||
if (!$bytes2) return null;
|
if (!$bytes2)
|
||||||
|
return null;
|
||||||
|
|
||||||
// Calculate speed in Mbps
|
// Calculate speed in Mbps
|
||||||
$bytesPerSec = (int) $bytes2 - (int) $bytes1;
|
$bytesPerSec = (int) $bytes2 - (int) $bytes1;
|
||||||
@@ -155,13 +158,15 @@ class ServerMonitoring
|
|||||||
$cmd = "cat /sys/class/net/\$(ip route | grep default | awk '{print \$5}' | head -1)/statistics/tx_bytes";
|
$cmd = "cat /sys/class/net/\$(ip route | grep default | awk '{print \$5}' | head -1)/statistics/tx_bytes";
|
||||||
$bytes1 = $this->execSSH($cmd);
|
$bytes1 = $this->execSSH($cmd);
|
||||||
|
|
||||||
if (!$bytes1) return null;
|
if (!$bytes1)
|
||||||
|
return null;
|
||||||
|
|
||||||
sleep(1); // Wait 1 second
|
sleep(1); // Wait 1 second
|
||||||
|
|
||||||
$bytes2 = $this->execSSH($cmd);
|
$bytes2 = $this->execSSH($cmd);
|
||||||
|
|
||||||
if (!$bytes2) return null;
|
if (!$bytes2)
|
||||||
|
return null;
|
||||||
|
|
||||||
// Calculate speed in Mbps
|
// Calculate speed in Mbps
|
||||||
$bytesPerSec = (int) $bytes2 - (int) $bytes1;
|
$bytesPerSec = (int) $bytes2 - (int) $bytes1;
|
||||||
@@ -184,7 +189,8 @@ class ServerMonitoring
|
|||||||
$cmd = "docker exec {$containerName} wg show all dump | grep '{$publicKey}' | awk '{print \$6, \$7}'";
|
$cmd = "docker exec {$containerName} wg show all dump | grep '{$publicKey}' | awk '{print \$6, \$7}'";
|
||||||
$result = $this->execSSH($cmd);
|
$result = $this->execSSH($cmd);
|
||||||
|
|
||||||
if (!$result) return null;
|
if (!$result)
|
||||||
|
return null;
|
||||||
|
|
||||||
list($bytesReceived, $bytesSent) = explode(' ', trim($result));
|
list($bytesReceived, $bytesSent) = explode(' ', trim($result));
|
||||||
|
|
||||||
@@ -268,14 +274,44 @@ class ServerMonitoring
|
|||||||
$stats['speed_down_kbps'],
|
$stats['speed_down_kbps'],
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Update last_handshake in vpn_clients table
|
// Update vpn_clients table with latest stats
|
||||||
$stmt = $db->prepare("
|
$stmt = $db->prepare("
|
||||||
UPDATE vpn_clients
|
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 = ?
|
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