Jump to content

PHP, quickest method to check another server online status?


Recommended Posts

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.

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.

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 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.

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 by MySQL_Narb

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;

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.