38 lines
No EOL
914 B
PHP
38 lines
No EOL
914 B
PHP
<?php
|
|
|
|
function testLatency($host) {
|
|
$start_time = microtime(true);
|
|
|
|
$output = shell_exec("ping -c 10 $host");
|
|
|
|
$end_time = microtime(true);
|
|
$latency = $end_time - $start_time;
|
|
|
|
if ($output !== null) {
|
|
if (preg_match('/avg\/min\/max = (\d+\.\d+)\/(\d+\.\d+)\/(\d+\.\d+)/', $output, $matches)) {
|
|
$avg_ping = $matches[1];
|
|
} else {
|
|
$avg_ping = 'Unknown';
|
|
}
|
|
|
|
$latency_rounded = round($latency, 2);
|
|
|
|
$latency_rounded = number_format($latency_rounded, 2, '.', '');
|
|
|
|
$response = [
|
|
'host' => $host,
|
|
'latency_ms' => $latency_rounded,
|
|
];
|
|
|
|
return json_encode($response, JSON_PRETTY_PRINT);
|
|
} else {
|
|
return json_encode([
|
|
'error' => 'Ping failed',
|
|
], JSON_PRETTY_PRINT);
|
|
}
|
|
}
|
|
|
|
$client_ip = $_SERVER['REMOTE_ADDR'];
|
|
|
|
echo testLatency($client_ip);
|
|
?>
|