Forum Moderators: coopster
<?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
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.
<?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 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)