MySQL_Narb Posted September 22, 2013 Share Posted September 22, 2013 I have a site where server owners can advertise their own servers, and I want to display these servers' online/offline statuses. When testing just 3 servers with the fsockopen, it takes a couple seconds. When my site grows, it's going to have 100s of servers. Is there a faster method? I plan on running a cron every 10 minutes to update server statuses. Quote Link to comment https://forums.phpfreaks.com/topic/282358-php-quickest-method-to-check-another-server-online-status/ Share on other sites More sharing options...
vinny42 Posted September 22, 2013 Share Posted September 22, 2013 I'm getting a deja-vu here, checking lots of servers every ten minutes... The only way to know if a service is running is to connect to it and if you read the manual for fsockopen you'll see that you can open blocking and non-blocking connections. Blockin connections (the default setting) will wait for the socket operation to complete before you can go on to the next, non-blocking operations return immediately and it's up to you to check the status regularly. Thus you can open connection 1, and immediately open connection 2 without having to waitfor connection 1 to respond. After you have opend all your connections you can start looking at their status. All connections can run at the same time. Quote Link to comment https://forums.phpfreaks.com/topic/282358-php-quickest-method-to-check-another-server-online-status/#findComment-1450630 Share on other sites More sharing options...
QuickOldCar Posted September 22, 2013 Share Posted September 22, 2013 Try using multi-curl or this rolling curl using it http://code.google.com/p/rolling-curl/ Would be best saving the status to a database, and updating it to alive or dead. Show from the database results and not a live script upon view. Quote Link to comment https://forums.phpfreaks.com/topic/282358-php-quickest-method-to-check-another-server-online-status/#findComment-1450634 Share on other sites More sharing options...
brentman Posted September 22, 2013 Share Posted September 22, 2013 You may also want to check like half the servers every 10 minutes, then the other half the next 10 minutes. That would cut your requests down 50% and 'maybe' won't be a big deal to what you are trying to accomplish? Quote Link to comment https://forums.phpfreaks.com/topic/282358-php-quickest-method-to-check-another-server-online-status/#findComment-1450720 Share on other sites More sharing options...
MySQL_Narb Posted September 22, 2013 Author Share Posted September 22, 2013 I'm getting a deja-vu here, checking lots of servers every ten minutes... The only way to know if a service is running is to connect to it and if you read the manual for fsockopen you'll see that you can open blocking and non-blocking connections. Blockin connections (the default setting) will wait for the socket operation to complete before you can go on to the next, non-blocking operations return immediately and it's up to you to check the status regularly. Thus you can open connection 1, and immediately open connection 2 without having to waitfor connection 1 to respond. After you have opend all your connections you can start looking at their status. All connections can run at the same time. I'll have to read back over the manual for fsock. Thanks! Try using multi-curl or this rolling curl using it http://code.google.com/p/rolling-curl/ Would be best saving the status to a database, and updating it to alive or dead. Show from the database results and not a live script upon view. I'm not sure cURL is what I need her. The servers I'm making requests to aren't HTTP servers. Quote Link to comment https://forums.phpfreaks.com/topic/282358-php-quickest-method-to-check-another-server-online-status/#findComment-1450721 Share on other sites More sharing options...
MySQL_Narb Posted September 23, 2013 Author Share Posted September 23, 2013 (edited) I still couldn't figure out how to make these parallel: foreach($servers as $server){ $ip = explode(':', $server['ip']); $port = (!isset($ip[1]) || empty($ip[1])) ? $port = '43594' : $ip[1]; $ip = $ip[0]; $socket = @fsockopen($ip, $port, $errNo, $errStr, 3); if(!$socket){ //offline }else{ //online } } Edited September 23, 2013 by MySQL_Narb Quote Link to comment https://forums.phpfreaks.com/topic/282358-php-quickest-method-to-check-another-server-online-status/#findComment-1450743 Share on other sites More sharing options...
vinny42 Posted September 23, 2013 Share Posted September 23, 2013 I've modified the user example from the manual a little. It calls a URL containing a script that just has "sleep(1)" in it. If you run the code it will return in just over one second, while it has called the URL three times, so in blokcing mod it would have taken over three seconds. function multiHTTP($urlArr){ $sockets = Array(); // socket array! $urlInfo = Array(); // info arr $retDone = Array(); $retData = Array(); $errno = Array(); $errstr = Array(); for ($x = 0; $x < count($urlArr); $x++) { $urlInfo[$x] = parse_url($urlArr[$x]); $urlInfo[$x]['port'] = (isset($urlInfo[$x]['port']) ? $urlInfo[$x]['port'] : 80); $urlInfo[$x]['path'] = ($urlInfo[$x]['path']) ? $urlInfo[$x]['path'] : "/"; $urlInfo[$x]['query'] = (isset($urlInfo[$x]['query']) ? $urlInfo[$x]['query'] : ''); var_dump($urlInfo[$x]); $s = fsockopen($urlInfo[$x]['host'], $urlInfo[$x]['port'], $errno[$x], $errstr[$x], 30); if (false !== ($s)) { $sockets[$x] = $s; socket_set_blocking($sockets[$x], false); $query = ($urlInfo[$x]['query']) ? "?" . $urlInfo[$x]['query'] : ""; $strHTTPRequest = "GET " . $urlInfo[$x]['path'] . "$query HTTP/1.0\r\nhost: " . $urlInfo[$x]['host'] . "\r\n\r\n"; // echo $strHTTPRequest; fputs($sockets[$x], $strHTTPRequest); $retData[$x] = ''; } } // ok read the data from each one $done = false; while (!$done) { for ($x = 0; $x < count($urlArr); $x++) { if (false === $sockets[$x]) { unset($sockets[$x]); continue; } if (!feof($sockets[$x])) { if ($retData[$x]) { $retData[$x] .= fgets($sockets[$x], 128); } else { $retData[$x] = fgets($sockets[$x], 128); } } else { $retDone[$x] = 1; } } $done = (array_sum($retDone) == count($urlArr)); } return $retData;} var_dump(multiHTTP(array("http://localhost/a.php?wait=1", "http://localhost/a.php?wait=2", "http://localhost/a.php?wait=3"))); exit; Quote Link to comment https://forums.phpfreaks.com/topic/282358-php-quickest-method-to-check-another-server-online-status/#findComment-1450767 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.