Add updatePing method to check if ip is pingable

This commit is contained in:
Tom Hatzer 2016-12-23 11:29:28 +01:00 committed by GitHub
parent 2325a6c25f
commit 4c7d2bd4f3
1 changed files with 38 additions and 0 deletions

View File

@ -90,6 +90,9 @@ class StatusUpdater {
}
switch($this->server['type']) {
case 'ping':
$this->status_new = $this->updatePing($max_runs);
break;
case 'service':
$this->status_new = $this->updateService($max_runs);
break;
@ -134,6 +137,41 @@ class StatusUpdater {
}
/**
* Check the current servers ping status
* @param int $max_runs
* @param int $run
* @return boolean
*/
protected function updatePing($max_runs, $run = 1) {
$errno = 0;
// save response time
$starttime = microtime(true);
// set ping payload
$package = "\x08\x00\x7d\x4b\x00\x00\x00\x00PingHost";
$fp = @fsockopen ($this->server['ip'], $this->server['port'], $errno, $this->error, 10);
$socket = socket_create(AF_INET, SOCK_RAW, 1);
socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => 10, 'usec' => 0));
socket_connect($socket, $this->server['ip'], null);
socket_send($socket, $package, strLen($package), 0);
if (socket_read($socket, 255)) {
$this->rtime = microtime(true) - $starttime;
$status = true;
} else {
$status = false;
}
socket_close($socket);
// check if server is available and rerun if asked.
if(!$status && $run < $max_runs) {
return $this->updatePing($max_runs, $run + 1);
}
return $status;
}
/**
* Check the current server as a service
* @param int $max_runs