Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using fopen() for site monitoring

Status
Not open for further replies.

jmreinwald

Technical User
Jun 3, 2002
46
US
Hi all,

I am a rather green php programmer, and I've been given a project at work that I think I'm doing right. We have several portal servers that are on hot-swappable drives. At any time, there would only be 3 drives available out of the 9 or so portals we have. I have been tasked with creating a "monitoring" tool which will try and open each main page and return the link if it's available, or an "unavailable" message if it is not.

A couple of questions--it seems to 'think' a little longer than I think it should and eventually gives a blank page. Also, quite simply, is there an easier way of doing this?

Here's what I have so far (keep in mind I'm a newbie):

Code:
<?
$msg =""; //string starter

$url = array("[URL unfurl="true"]http://website1/index.html",[/URL]
		"[URL unfurl="true"]http://website2/index.html",[/URL]
		"[URL unfurl="true"]http://website3/index.html");[/URL]

$name = array("Site 1",
		"Site 2",
		"Site 3");

$i = 0;
while ($i<=2) {

$f = @fopen($url[$i], 'r');
if($f)
	{
 	$msg .= "<tr><td>$name[$i]</td><td><a href=\"$url[$i]\">Available</a></td></tr>\n";
	}    
else
	{
	$msg .= "<tr><td?$name[$i]</td><td>Unavailable</td></tr>\n";
	}

	$i++;
}


?>

I obviously have a table that this is getting echo'ed to. Also, there are only three examples above, but the arrays will eventually hold all 9 urls and names.

What am I doing wrong?

Thanks!
 
<aside>
You know, you could combine those two arrays into a single associative array:

$url = array("Site 1" => " "Site 2" => " "Site 3" => "
Also, there is a flaw in your program logic. If any site fails the test, all subsequent sites will not be tested. I recommend using the above array and a foreach() loop instead of the while() as your main loop.
</aside>

If a site is down, the connection has to time out before you get your negative response. There is no way to change the timeout using fopen().

Instead you could use cURL or sockets. Each of them will allow you to set a timeout.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top