Solved

FSockOpen() Too Slow!

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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
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 count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

708 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

17 Experts available now in Live!

Get 1:1 Help Now