Web Hosting Talk







View Full Version : is this possible ???? ( php )


tuvok
05-27-2002, 11:48 AM
Hi guys, i am trying to add a functionality to my site that checks the status of a given server to see if its responding to http call ( port 80 )

Can this be done in php what i basically want to do is


1) check if given Ip is responding to http ( i.e if the server is up )

2) if yes then assign a variable say $image = gree_light.gif

if the server does not respond the $image - red_light.gif


I can then add this to my status page so that when it loads it checks the servers across my network and then the rest of the page will use the $image variable


Any ideas ? it would be great if i could do this in ASP but php is good too, its this part of the code that i am stuck with ( HOW to check or call an external servers httpd status )

Thanks guys/gals

mwatkins
05-27-2002, 12:16 PM
Just do a file open call... create a tiny text file on each server to reduce load.

$fp = fopen ("http://yoursite.com/test.txt", "r");
if $fp ... greenlight
else ... red light

tuvok
05-27-2002, 12:23 PM
Thanks

so is this the exact code

$fp = fopen ("http://yoursite.com/test.txt", "r");
if $fp ... greenlight
else ... red light

or would it be


$fp = fopen ("http://yoursite.com/test.txt", "r");
if $fp ... greenlight.gif
else ... redlight.gif

I am a complete newbie to php so so i would really be greatful if you showed me the complete routine, so i can then just use the variable image variabl ein the rest of the script which will esssentially be html ( lol )


thanks again

mwatkins
05-27-2002, 12:29 PM
No, the ... were for you to fill in the blanks. And those blanks would be:

$fp = fopen ("http://yoursite.com/test.txt", "r");
if ($fp)
$status_light = "/yourpath/to/an/image/greenlight.gif";
else
$status_light = "/yourpath/to/an/image/redlight.gif";

tuvok
05-27-2002, 12:34 PM
thanks a lot, i will use that code


Cheers (b)

DavidU
05-27-2002, 01:52 PM
The way mwatkins showed you works 100% and is good but if you want a slightly more flexible but a but more complex solution you could do something like this:

(I haven't tested this exact code because I'm just writing it here, but I do this all the time so it should do the job)


<?
//Setup all your servers and services you want to check for
//Could be made even more efficient if done an array but
//I've never thrown sockets into an array. (might work)

$www1 = fsockopen ("www1.dmz", 80, $errno, $errstr, 10);
$www2 = fsockopen ("www2.dmz", 80, $errno, $errstr, 10);
$ftp1 = fsockopen ("ftp1.dmz", 21, $errno, $errstr, 10);
$mail1 = fsockopen ("mail1.dmz", 25, $errno, $errstr, 10);

//Now to check their status
$www1_status = "www1 is ".($www1 ? 'up' : 'down');
$www2_status = "www2 is ".($www2 ? 'up' : 'down');
$ftp1_status = "www1 is ".($ftp1 ? 'up' : 'down');
$mail1_status = "www1 is ".($mail1 ? 'up' : 'down');

//since fsockopen isn't persistent you don't even have to close it
//but you should
fclose($www1);
fclose($www2);
fclose($ftp1);
fclose($mail1);
?>


I hope this helped -- I figure you're trying to make a monitoring script. Good luck and I hope you like PHP!

-davidu

tuvok
05-27-2002, 03:37 PM
thats some good coce there....( note to self ..."i wonder what it all means" ) LOL

But thanx, hey, same question i asked mwatkins how would i use this in real life i.e i am ok, with php ( a little ) but the syntax kill me

so if i wanted to say check server with IP 222.x.x.x.1

and if its up i want to assign a variable $image = up_pic.gif so that i can simply use this variable in my final display HTML, how would i apply your code, i will only be monitoring one server at a time and only port 80

thanks in advance for the help


( ...note to self ..."these guys are great" ) :)