Solved

FSockOpen() Too Slow!

Posted on 2004-04-20
3
1,409 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

930 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now