Server load check -

Discussion in 'PHP' started by Seaji, May 31, 2008.

  1. #1
    Hi,

    I have a question about a possible script I would like...

    I would like to include it in my index.php and what it should do is check the server load, and if the server load is above 3 then it either disables the script or redirects the person to another page.

    Any suggestions? couldn't find anything on google similar to it
     
    Seaji, May 31, 2008 IP
  2. commandos

    commandos Notable Member

    Messages:
    3,648
    Likes Received:
    329
    Best Answers:
    0
    Trophy Points:
    280
    #2
    Found that on invisionpower.com

    
    
    // Original url : http://forums.invisionpower.com/lofiversion/index.php/t156890.html
    
    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[0];
     }
     elseif(function_exists("shell_exec")) {
         $load = explode(' ', `uptime`);
         return $load[count($load)-1];
     }
     else {
         return "";
     }
        }
        elseif($windows) {
     if(class_exists("COM")) {
         $wmi = new COM("WinMgmts:\\\\.");
         $cpus = $wmi->InstancesOf("Win32_Processor");
         
         $cpuload = 0;
         $i = 0;
         while ($cpu = $cpus->Next()) {
       $cpuload += $cpu->LoadPercentage;
       $i++;
         }
         
         $cpuload = round($cpuload / $i, 2);
         return "$cpuload%";
     }
     else {
         return "";
     }
        }
    }
    PHP:
    this will return server load after that you can do what u want ...


    call a redirect if > 3 or whatever
     
    commandos, May 31, 2008 IP
  3. CodyRo

    CodyRo Peon

    Messages:
    365
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for that function, found the Windows portion kind of cool (calling COM).

    Cheers :)!
     
    CodyRo, May 31, 2008 IP