Posted by brant : 2009-09-22 at 1:31 pm

There are times when you may want to get the load averages for your server in your PHP script.  The reason of course is to prevent a script from running when the server is already taxed.  I saw this PHP function to grab load averages at stackoverflow.com in the comments and thought it deserved more attention.

Below you will find an easy to use function, which returns 3 variables I aptly named one two and three. I have only used this on a linux box, but it should work on windows as well.

list($one,$two,$three) = get_server_load();
// $one = Past minute , $two = 5 minutes, $three = 15 minutes

function get_server_load($windows = 0) {
        $os = strtolower(PHP_OS);
        if(strpos($os, "win") === false) {
                if(file_exists("/proc/loadavg")) {
                 $load = file_get_contents("/proc/loadavg");
                 $load = explode(' ', $load);
                 return $load;
                }
                elseif(function_exists("shell_exec")) {
                 $load = explode(' ', `uptime`);
                 return $load;
                }
                else {
                 return "";
                }
    }
}

 



Similar Blogs:
The Sad Truth About SEOBook
Cancel Your FreeCreditReport.com Account Without Calling
Automate Wordpress Posting
MySQL, How To SUM Time Correctly

blog comments powered by Disqus