From 59f434ca4c9df685db095ab687340d6720546473 Mon Sep 17 00:00:00 2001 From: TimZ99 Date: Sun, 17 May 2020 22:55:38 +0200 Subject: [PATCH] Updated ping check Ping now checks for 0,0% packet loss and displays the avg time in ms. --- src/psm/Util/Server/Updater/StatusUpdater.php | 39 +++++++++---------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/src/psm/Util/Server/Updater/StatusUpdater.php b/src/psm/Util/Server/Updater/StatusUpdater.php index af3a8cdd..eeba1d17 100644 --- a/src/psm/Util/Server/Updater/StatusUpdater.php +++ b/src/psm/Util/Server/Updater/StatusUpdater.php @@ -179,36 +179,35 @@ class StatusUpdater $max_runs = 1; } $result = null; - // Execute ping + // Choose right ping version, ping6 for IPV6, ping for IPV4 $pingCommand = 'ping6'; $serverIp = $this->server['ip']; if (filter_var($serverIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) { $pingCommand = 'ping'; } - $txt = exec($pingCommand . " -c " . $max_runs . " " . $serverIp . " 2>&1", $output); - // Non-greedy match on filler - $re1 = '.*?'; - // Uninteresting: float - $re2 = '[+-]?\\d*\\.\\d+(?![-+0-9\\.])'; - // Non-greedy match on filler - $re3 = '.*?'; - // Float 1 - $re4 = '([+-]?\\d*\\.\\d+)(?![-+0-9\\.])'; - if (preg_match_all("/" . $re1 . $re2 . $re3 . $re4 . "/is", $txt, $matches)) { - $result = $matches[1][0]; - } - if (substr($output[0], 0, 4) == 'PING' && strpos($output[count($output) - 2], 'packets transmitted')) { - $result = 0; - } - if (!is_null($result)) { - $this->header = $output[0]; + + // execute PING + $txt = exec($pingCommand . " -c" . $max_runs . " " . $serverIp . " 2>&1", $output); + + // Check if output is PING and if 0,0% of packages are lost, otherwise fail. + if (substr($output[0], 0, 4) == 'PING' && strpos($output[count($output) - 2], '0.0% packet loss')) { + // Gets avg from 'round-trip min/avg/max/stddev = 7.109/7.109/7.109/0.000 ms' + preg_match_all("/(\d+\.\d+)/", $output[count($output) - 1], $result); + $result = floatval($result[0][1]); + + $this->header = ""; + foreach ($output as $key => $value) { + $this->header .= $value . "\n"; + } $status = true; } else { $this->header = "-"; - $this->error = $output[0]; + foreach ($output as $key => $value) { + $this->error .= $value . "\n"; + } $status = false; } - //Divide by a thousand to convert to milliseconds + // To miliseconds $this->rtime = $result / 1000; // check if server is available and rerun if asked.