Solved

FSockOpen() Too Slow!

Posted on 2004-04-20
3
1,414 Views
Last Modified: 2010-05-19
Hi everyone, I will start with what I wanted to do, and how I am doing it- maybe someone can help me re-invent the wheel.

I had an idea < dangerous > and wanted to write a small network monitoring system.  I succeeded!  The software written looks at servers and specifically open ports running on servers, ie 1433 is MS Sql server, port 1677 is Groupwise POA, etc...

Using PHP and fsockopen() function, I am able to check to see if the host is alive, the port is open, etc...  It works great when everything is up.  When systems are down, the page refresh is god-awful slow.  I set the timeout on the FSockOpen to 1 second, and the load time for the page is sometimes 15 to 30 seconds.

Here is the snippet of code from the PHP application:

while ( $row = mysql_fetch_array($result) ) {
     $sysid = $row["sysid"];
     $hostname = $row["hostname"];
     $port = $row["port"];
     $hostdesc = $row["hostdesc"];

     $sockpointer = fsockopen($hostname,$port,$errno,$errstr,1);
     if($sockpointer) //check that it returns a value, if true, then host/port is alive, if false, host/port is dead.
    {
      //do some stuff- found an open port
      fclose($sockpointer); //close port
    } //end if
    else
    {
     //do some stuff- found dead port
    } //end else
    flush(); //flush the socket
} //end while

$hostname, $port are values from a MySQL database getting shoved into this function

As I have said, the program is lightning fast when all servers are up, just sloooow when they do not respond.  The 1 second timeout doesn't seem to change this delay either.  I had tried different values all together.

This all being said- is there a faster way to accomplish what I am looking to do?  Is my while loop structure horribly written- I am only a novice PHP scripter.  Any help is much appreciated.  

Along with the 250 points, I will gladly mention anyone who helps out in my PHP headers.  I plan to release this tiny code on the internet to sites as hotscripts.com etc....

thanks!
0
Comment
Question by:jhines2000
  • 2
3 Comments
 
LVL 6

Accepted Solution

by:
CosminB earned 250 total points
ID: 10875391
use this function to see which is the slowest part ot the code

function timer()
{
  list($usec, $sec) = explode(" ", microtime());
  return ((float)$usec + (float)$sec);
}

... smth like this:
while ( $row = mysql_fetch_array($result) ) {
     $sysid = $row["sysid"];
     $hostname = $row["hostname"];
     $port = $row["port"];
     $hostdesc = $row["hostdesc"];

     $start = timer()
     $sockpointer = fsockopen($hostname,$port,$errno,$errstr,1);
     $stop1 = timer();
     echo 'fsockopen: ', $stop1-$start;
     if($sockpointer) //check that it returns a value, if true, then host/port is alive, if false, host/port is dead.
    {
      //do some stuff- found an open port
      fclose($sockpointer); //close port
      $stop2 = timer();
      echo 'if: ', $stop2-$stop1;
    } //end if
    else
    {
     //do some stuff- found dead port
      $stop3 = timer();
      echo 'else: ', $stop3-$stop2;
    } //end else
    flush(); //flush the socket ?!? actually flush() flushes the output buffer not the socket
} //end while


also this is from the manual: " Depending on the environment, the Unix domain or the optional connect timeout may not be available."
you could also try to use the socket functions(http://www.php.net/sockets). This functions are not enabled by default. You'll have to recompile your php wth the --enable-sockets option
0
 
LVL 1

Author Comment

by:jhines2000
ID: 11259223
Long time working on this project!

Still not working as fast as it could be.  Are there any other ideas out there?
0
 
LVL 1

Author Comment

by:jhines2000
ID: 11561684
I havent been able to figure this out yet.  I am going to abandon this question.
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question