Forum Moderators: coopster

Message Too Old, No Replies

Serving up a 503 page when server load exceeds x

         

Asia_Expat

7:43 pm on Dec 1, 2009 (gmt 0)

10+ Year Member



My server is suffering some capacity issues at the moment, long story, but will be resolved soon enough... but until such time, I'm trying to figure out how I can serve up my custom 503 page when the server load reaches, say, 5 or more. My page has this at the top...

<?php
header("HTTP/1.1 503 Service Unavailable");
header("Retry-After: 120");
?>

I've done some searching but only came up with whichcraft and sorcery. Can anyone point me in the right direction? Note that my PHP hardening has the following functions disabled...

show_source, system, shell_exec, passthru, exec, phpinfo, popen, proc_open, allow_url_fopen

vincevincevince

4:21 pm on Dec 2, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The only real way you can do that is by testing it the crude way, if all those functions are disabled.

Time how long it takes to process a little loop 1000 times (microtime() difference). Then, set up a flat file 'currentload' and store that time in there.

Every time someone hits the page, check if that file was last modified less than ten seconds ago. If it wasn't, then run the loop and update the value in the file.

If the value is over (e.g.) 0.03s, then you can assume a high server load and return your 503 response.

Asia_Expat

6:08 pm on Dec 2, 2009 (gmt 0)

10+ Year Member



Hmmm, sounds a bit like taking a walk from New York to London via Patagonia... If I was to forgo a little security, which function would I activate and how would I utilise it?

vincevincevince

1:45 am on Dec 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you enable exec() or shell_exec() something like the below should work:

<?php
$w=`w`; //maybe you need to use /usr/bin/w or shell_exec() instead of ``
preg_match("/load average\: ([0-9\.]+)\, ([0-9\.]+)\, ([0-9\.]+)/",$w,$m);
print "Load average is $m[1],$m[2],$m[3]";
?>

My 'w' command returns a first line as follows:

 09:39:49 up 1:38, 2 users, load average: 0.85, 0.65, 0.42

I have based the above code on that line. If yours is in a different format then you will need to adjust it to match.

Obviously you will want to put an if statement instead of a print statement to handle the load average data and return 503 if required.

If w is not installed, I have used top to get load data in the past. (top -bn1)

Asia_Expat

2:08 pm on Dec 3, 2009 (gmt 0)

10+ Year Member



Thanks vvv, I'll have a play around with that this evening.