Add latency.php
This commit is contained in:
parent
5baee345de
commit
9827fb10de
1 changed files with 38 additions and 0 deletions
38
latency.php
Normal file
38
latency.php
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
<?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);
|
||||
?>
|
||||
Loading…
Add table
Add a link
Reference in a new issue