Updated ping check

Ping now checks for 0,0% packet loss and displays the avg time in ms.
This commit is contained in:
TimZ99 2020-05-17 22:55:38 +02:00
parent c0e0b260d5
commit 59f434ca4c
No known key found for this signature in database
GPG Key ID: 4D8268DC68E8339D
1 changed files with 19 additions and 20 deletions

View File

@ -179,36 +179,35 @@ class StatusUpdater
$max_runs = 1; $max_runs = 1;
} }
$result = null; $result = null;
// Execute ping // Choose right ping version, ping6 for IPV6, ping for IPV4
$pingCommand = 'ping6'; $pingCommand = 'ping6';
$serverIp = $this->server['ip']; $serverIp = $this->server['ip'];
if (filter_var($serverIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) { if (filter_var($serverIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) {
$pingCommand = 'ping'; $pingCommand = 'ping';
} }
$txt = exec($pingCommand . " -c " . $max_runs . " " . $serverIp . " 2>&1", $output);
// Non-greedy match on filler // execute PING
$re1 = '.*?'; $txt = exec($pingCommand . " -c" . $max_runs . " " . $serverIp . " 2>&1", $output);
// Uninteresting: float
$re2 = '[+-]?\\d*\\.\\d+(?![-+0-9\\.])'; // Check if output is PING and if 0,0% of packages are lost, otherwise fail.
// Non-greedy match on filler if (substr($output[0], 0, 4) == 'PING' && strpos($output[count($output) - 2], '0.0% packet loss')) {
$re3 = '.*?'; // Gets avg from 'round-trip min/avg/max/stddev = 7.109/7.109/7.109/0.000 ms'
// Float 1 preg_match_all("/(\d+\.\d+)/", $output[count($output) - 1], $result);
$re4 = '([+-]?\\d*\\.\\d+)(?![-+0-9\\.])'; $result = floatval($result[0][1]);
if (preg_match_all("/" . $re1 . $re2 . $re3 . $re4 . "/is", $txt, $matches)) {
$result = $matches[1][0]; $this->header = "";
} foreach ($output as $key => $value) {
if (substr($output[0], 0, 4) == 'PING' && strpos($output[count($output) - 2], 'packets transmitted')) { $this->header .= $value . "\n";
$result = 0; }
}
if (!is_null($result)) {
$this->header = $output[0];
$status = true; $status = true;
} else { } else {
$this->header = "-"; $this->header = "-";
$this->error = $output[0]; foreach ($output as $key => $value) {
$this->error .= $value . "\n";
}
$status = false; $status = false;
} }
//Divide by a thousand to convert to milliseconds // To miliseconds
$this->rtime = $result / 1000; $this->rtime = $result / 1000;
// check if server is available and rerun if asked. // check if server is available and rerun if asked.