PDA

View Full Version : PHP check if server is up?


Fost
11-23-2005, 04:21 AM
I'm trying to add some extra bulletproofing to our download script. Currently, when a user inititiates a download, we hand them off to a php script, which checks a few things (mainly if it's someone we don't like ;) ) does some logging, then bumps them to a download server. This allows us to use multiple download servers, and scale up/down pretty quickly with demand.

I'm trying to add some code that will check if a server is up and running before initiating a download. Then if it isn't, I can try all other servers, and if there's still a problem, bump them to tucows download page.

My first thought was to get the contents of a small file from the download server in question, and I came up with this:

if ( file_exists('http://www.battlescapegame.com/live.test') )
{
echo "LIVE!!!";
}
else echo "DEAD!";


A 'true' test works ok, but if the file isn't there the script times out.

Am open to any random ideas at this point because I've been researching it on and off for a few days to no avail!

Sharkbait
11-23-2005, 04:56 AM
Quoted from here: http://php.mirrors.ilisys.com.au/manual/en/ref.url.php

"To check if a URL is valid, try to fopen() it. If fopen() results an error (returns false), then PHP cannot open the URL you asked. This is usually because it is not valid..."

P.S. Update your Dev Diary... I'm keen for new Mr Robot shots! :cool:

Gnatinator
11-23-2005, 05:25 AM
Aww, Sharkbait beat me to it.

Yes, keep the updates coming. ;) The Moonpod dev diary is one of the few logs I actually take the time to read. High quality stuff.

Fost
11-23-2005, 08:05 AM
"To check if a URL is valid, try to fopen() it. If fopen() results an error (returns false), then PHP cannot open the URL you asked. This is usually because it is not valid..."

Yeah, I tried that, but maybe not hard enough. Currently, if the page isn't there, it still returns true (presumably because the server is returning an error page whatever). That's ok, but if the server is not there, I get an error like:

'Warning: fopen(): php_network_getaddresses: getaddrinfo failed: Name or service not known in /var/www/html/livetest.php'

I'll have a look into that, it seems like all the php based ping scripts use it, so it must be what I need to do. Thanks...

P.S. Update your Dev Diary... I'm keen for new Mr Robot shots! :cool:

Thanks :) We have a backlog of stuff now - it's so time consuming writing it up, but it's very therapeutic doing it (usually because you end up wondering what the hell you have been doing with your time!!). I think we'll probably get one out at the beginning of December but put more into it. Hamish has already written an update for War Angels which outshines anything we are doing with Mr Robot anyway!

Sharkbait
11-23-2005, 08:25 AM
I had a similar issue with online hiscore tables in Atomixer, but it is obviously written in C++. I check if the server is available.. if it is, I further validate the response content, which is usually some simple text e.g. an "OK".

Fost
11-24-2005, 06:03 AM
I had a similar issue with online hiscore tables in Atomixer, but it is obviously written in C++. I check if the server is available.. if it is, I further validate the response content, which is usually some simple text e.g. an "OK".

We could probably write a small linux app to do it and call it from php (in fact, probably there's a ping command come to think of it.!). Anyway, this seems to work:


if ( $testSocket = @fsockopen("download2.moonpod.com", 80, $errno, $errstr, 2) )
{
echo 'LIVE!!!';
fclose($testSocket);
}
else echo 'DEAD!';



although I need to test it with a dead server that dns works for. I'm setting up a subdomain that points to garbage right now, so if there's a problem I'll append to this post.

MadSage
11-24-2005, 06:56 AM
We could probably write a small linux app to do it and call it from php (in fact, probably there's a ping command come to think of it.!).

A successful ping won't prove that the HTTP/FTP server is active, only that the machine is online ;)

Fost
11-24-2005, 07:46 AM
A successful ping won't prove that the HTTP/FTP server is active, only that the machine is online ;)

In our case, that would probably be enough. We are using multiple shared hosting accounts to serve files. Works out incredibly cheap that way, and if you get them from different ISPs and do the above check, it should always work out ok, even if some of them go down or get saturated.

Opening a socket on port 80 like I'm doing above should be a good enough test that the web server is running (not that I'm claiming to be an expert here!!), I just need to test the best timeout. Hmm, I think it might be best if I log every time the server test fails too :)

Kai Backman
11-24-2005, 08:11 AM
I think you need to use ICMP messages, but even that is a bad idea. I've been playing around with TCP for a while now (which HTTP builds on) and I suspect that in case the server is offline you might end up with a pretty long wait until the call returns. ICMP packets will give you more control over how timeouts work, but they still won't tell you that the HTTP server is online and sending out stuff.

May I suggest an alternative strategy. In the actual download script drive the user randomly to one of the available servers. Then run a background process that fetches a small file from each of the servers each 5 minutes or so. Based on those results the background process will then update the active server list. The background process could also be you with a slightly longer polling interval. This way the download script executes very quickly in 99.99% of cases. And when a server goes down there is a short interval where the user might get a dead server, but in that case they can just click the link again and have a high probability of getting a live one. Most users do this anyway if the download doesn't start immediately.

The network is slow enough, opening up another socket for each request will probably kill performance.

MadSage
11-24-2005, 08:26 AM
Although opening up a socket shouldn't be a big issue unless you have an incredibly popular download site, I agree with Kai's idea. It seems like the most resource friendly method, and it's easy to setup a cron job to do the check every 5 minutes. Also, you can display a link like the one you see on many download site eg. "Click here if your download does not start within 10 seconds".

Fost
11-24-2005, 08:50 AM
in case the server is offline you might end up with a pretty long wait until the call returns.
This should be ok - the last argument to fsockopen is a time in seconds to wait. I've had this at 1 second with no problems here, but I'd like to run some better tests and log it to see how it goes.

run a background process that fetches a small file from each of the servers each 5 minutes or so.
That sounds like the ideal way to do it, but I don't really want to bother Mark (the programmer here) to write a server app that does it - even if it would probably be quite easy to do something that updates a few database entries with the active servers. Something to go on the todo list maybe :)