use OS specific ping (CLI/socket)
parent
97bbb4f8f7
commit
1a7e30bbf6
|
@ -180,23 +180,46 @@ 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;
|
||||||
|
|
||||||
|
// Windows / Linux variant: use socket on Windows, commandline on Linux
|
||||||
|
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
|
||||||
|
|
||||||
|
// socket ping - Code from http://stackoverflow.com/a/20467492
|
||||||
|
|
||||||
|
// save response time
|
||||||
|
$starttime = microtime(true);
|
||||||
|
|
||||||
|
// set ping payload
|
||||||
|
$package = "\x08\x00\x7d\x4b\x00\x00\x00\x00PingHost";
|
||||||
|
|
||||||
|
$socket = socket_create(AF_INET, SOCK_RAW, 1);
|
||||||
|
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 10, 'usec' => 0));
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
$this->rtime = microtime(true) - $starttime;
|
||||||
|
socket_close($socket);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
|
||||||
// Choose right ping version, ping6 for IPV6, ping for IPV4
|
// Choose right ping version, ping6 for IPV6, ping for IPV4
|
||||||
|
$pingCommand = 'ping6';
|
||||||
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';
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use -n instead of -c for Windows machines
|
|
||||||
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
|
|
||||||
$ping_count = " -n ";
|
|
||||||
}
|
|
||||||
|
|
||||||
// execute PING
|
// execute PING
|
||||||
$txt = exec($pingCommand . $ping_count . $max_runs . " " . $serverIp . " 2>&1", $output);
|
$txt = exec($pingCommand . " -c " . $max_runs . " " . $serverIp . " 2>&1", $output);
|
||||||
|
|
||||||
// Check if output is PING and if transmitted packets is equal to received packets.
|
// 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);
|
preg_match('/^(\d{1,3}) packets transmitted, (\d{1,3}).*$/', $output[count($output) - 2], $output_package_loss);
|
||||||
|
@ -223,6 +246,9 @@ class StatusUpdater
|
||||||
$this->error = $output[count($output) - 2];
|
$this->error = $output[count($output) - 2];
|
||||||
$status = false;
|
$status = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
// To miliseconds
|
// To miliseconds
|
||||||
$this->rtime = $result / 1000;
|
$this->rtime = $result / 1000;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue