use OS specific ping (CLI/socket)
This commit is contained in:
parent
97bbb4f8f7
commit
1a7e30bbf6
|
@ -180,49 +180,75 @@ class StatusUpdater
|
||||||
$max_runs = 1;
|
$max_runs = 1;
|
||||||
}
|
}
|
||||||
$serverIp = $this->server['ip'];
|
$serverIp = $this->server['ip'];
|
||||||
$pingCommand = 'ping6';
|
|
||||||
$ping_count = " -c ";
|
|
||||||
|
|
||||||
$result = null;
|
$result = null;
|
||||||
|
|
||||||
// Choose right ping version, ping6 for IPV6, ping for IPV4
|
// Windows / Linux variant: use socket on Windows, commandline on Linux
|
||||||
if (filter_var($serverIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) {
|
|
||||||
$pingCommand = 'ping';
|
|
||||||
}
|
|
||||||
|
|
||||||
// Use -n instead of -c for Windows machines
|
|
||||||
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
|
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
|
||||||
$ping_count = " -n ";
|
|
||||||
}
|
|
||||||
|
|
||||||
// execute PING
|
// socket ping - Code from http://stackoverflow.com/a/20467492
|
||||||
$txt = exec($pingCommand . $ping_count . $max_runs . " " . $serverIp . " 2>&1", $output);
|
|
||||||
|
|
||||||
// Check if output is PING and if transmitted packets is equal to received packets.
|
// save response time
|
||||||
preg_match('/^(\d{1,3}) packets transmitted, (\d{1,3}).*$/', $output[count($output) - 2], $output_package_loss);
|
$starttime = microtime(true);
|
||||||
|
|
||||||
if (
|
// set ping payload
|
||||||
substr($output[0], 0, 4) == 'PING' &&
|
$package = "\x08\x00\x7d\x4b\x00\x00\x00\x00PingHost";
|
||||||
!empty($output_package_loss) &&
|
|
||||||
$output_package_loss[1] === $output_package_loss[2]
|
|
||||||
) {
|
|
||||||
// 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 = "";
|
$socket = socket_create(AF_INET, SOCK_RAW, 1);
|
||||||
foreach ($output as $key => $value) {
|
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 10, 'usec' => 0));
|
||||||
$this->header .= $value . "\n";
|
socket_connect($socket, $serverIp, null);
|
||||||
|
|
||||||
|
socket_send($socket, $package, strLen($package), 0);
|
||||||
|
if (socket_read($socket, 255)) {
|
||||||
|
$status = true;
|
||||||
|
} else {
|
||||||
|
$status = false;
|
||||||
|
|
||||||
|
// set error message
|
||||||
|
$errorcode = socket_last_error();
|
||||||
|
$this->error = "Couldn't create socket [" . $errorcode . "]: " . socket_strerror($errorcode);
|
||||||
}
|
}
|
||||||
$status = true;
|
$this->rtime = microtime(true) - $starttime;
|
||||||
|
socket_close($socket);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
$this->header = "-";
|
|
||||||
foreach ($output as $key => $value) {
|
// Choose right ping version, ping6 for IPV6, ping for IPV4
|
||||||
$this->header .= $value . "\n";
|
$pingCommand = 'ping6';
|
||||||
|
if (filter_var($serverIp, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) === false) {
|
||||||
|
$pingCommand = 'ping';
|
||||||
}
|
}
|
||||||
$this->error = $output[count($output) - 2];
|
|
||||||
$status = false;
|
// execute PING
|
||||||
|
$txt = exec($pingCommand . " -c " . $max_runs . " " . $serverIp . " 2>&1", $output);
|
||||||
|
|
||||||
|
// Check if output is PING and if transmitted packets is equal to received packets.
|
||||||
|
preg_match('/^(\d{1,3}) packets transmitted, (\d{1,3}).*$/', $output[count($output) - 2], $output_package_loss);
|
||||||
|
|
||||||
|
if (
|
||||||
|
substr($output[0], 0, 4) == 'PING' &&
|
||||||
|
!empty($output_package_loss) &&
|
||||||
|
$output_package_loss[1] === $output_package_loss[2]
|
||||||
|
) {
|
||||||
|
// 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 = "-";
|
||||||
|
foreach ($output as $key => $value) {
|
||||||
|
$this->header .= $value . "\n";
|
||||||
|
}
|
||||||
|
$this->error = $output[count($output) - 2];
|
||||||
|
$status = false;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// To miliseconds
|
// To miliseconds
|
||||||
$this->rtime = $result / 1000;
|
$this->rtime = $result / 1000;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue