Closed Thread
Results 1 to 10 of 10
Like Tree3Likes
  • 1 Post By TheJeffsta
  • 1 Post By Bryon
  • 1 Post By Torch

Thread: Server Load PHP Script

  1. #1
    TheJeffsta's Avatar
    TheJeffsta is offline x10 Lieutenant TheJeffsta is an unknown quantity at this point
    Join Date
    Sep 2005
    Location
    New Zealand
    Posts
    492

    Server Load PHP Script

    Does anyone know of any PHP code/script that displays the current (when the page is loaded) server load?

    I don't need uptime, I've already got uptime at http://www.thejeffsta.x10hosting.com/Status/Uptime.php (modified to make it reload every 5 seconds but dont really know why as it only adds 5 seconds to the uptime count).

    Also I have already got the server services status script, http://www.thejeffsta.x10hosting.com...x10Hosting.php

    Sort of like the server load in the following forum members signature:
    Last edited by TheJeffsta; 05-16-2006 at 03:55 AM.
    dinomirt96 likes this.
    TheJeffsta (Jeffrey N)

    "No matter how much you try, the end result will always end up the way you DO NOT want it!"

  2. #2
    theblazingangel is offline x10Hosting Member theblazingangel is an unknown quantity at this point
    Join Date
    Oct 2005
    Posts
    45

    Re: Server Load PHP Script

    try this php function

    the function returns the calculated value so use it like this:
    $load = getServerLoad();
    echo "Server load: " . $load;

    note, when the server is running windows, it can add a full second to your scripts execution time. by default it will not perform the calculation for windows, but you can force it to by changing the parameter '$windows = 0' to '$windows = 1'.

    Code:
    function getServerLoad($windows = 0) {
        $os = strtolower(PHP_OS);
        if (strpos($os, "win") === false) {
            if (file_exists("/proc/loadavg")) {
                $data = file_get_contents("/proc/loadavg");
                $load = explode(' ', $data);
                return $load[0];
    
            } elseif (function_exists("shell_exec")) {
    
                $load = explode(' ', `uptime`);
                return $load[count($load)-1];
    
            } else {
    
                return false;
            }
    
        } elseif($windows) {
    
            if(class_exists("COM")) {
                $wmi = new COM("WinMgmts:\\\\.");
                $cpus = $wmi->InstancesOf("Win32_Processor");
         
                 $cpuload = 0;
                 $i = 0;
    
                if(version_compare('4.50.0', PHP_VERSION) == 1) {
                    // PHP 4
                    while ($cpu = $cpus->Next()) {
                        $cpuload += $cpu->LoadPercentage;
                        $i++;
                    }
    
                } else {
    
                    // PHP 5
                    foreach($cpus as $cpu) {
                        $cpuload += $cpu->LoadPercentage;
                        $i++;
                    }
                }
    
                 $cpuload = round($cpuload / $i, 2);
                 return "$cpuload%";
    
            } else {
    
                return false;
            }
        }
    }

  3. #3
    Bryon is offline Administrator Bryon has disabled reputation
    Join Date
    Apr 2005
    Location
    Northfield, NH
    Posts
    7,608

    Re: Server Load PHP Script

    I use this:

    PHP Code:
    <?php
    $uptime 
    = @exec('uptime');
    preg_match("/averages?: ([0-9\.]+),[\s]+([0-9\.]+),[\s]+([0-9\.]+)/"$uptime$avgs);
    $uptime explode(' up '$uptime);
    $uptime explode(','$uptime[1]);
    $uptime $uptime[0] .', '$uptime[1];
    $start mktime(00011date('Y'), 0);
    $end mktime(000date('m'), date('j'), date('y'), 0);
    $diff $end $start;
    $days = @($diff 86400);
    $percentage = @($uptime $days) * 100;
    $load $avgs[1] + $avgs[2] + $avgs[3];
    $load $load 3;
    $load round($load3);

    // $load holds the load.

    ?>
    Either way works though.

    Edit: *nix only ;)
    Last edited by Bryon; 05-16-2006 at 02:17 PM.

  4. #4
    TheJeffsta's Avatar
    TheJeffsta is offline x10 Lieutenant TheJeffsta is an unknown quantity at this point
    Join Date
    Sep 2005
    Location
    New Zealand
    Posts
    492

    Re: Server Load PHP Script

    It doesnt work

    It only returns a blank page.
    TheJeffsta (Jeffrey N)

    "No matter how much you try, the end result will always end up the way you DO NOT want it!"

  5. #5
    Bryon is offline Administrator Bryon has disabled reputation
    Join Date
    Apr 2005
    Location
    Northfield, NH
    Posts
    7,608

    Re: Server Load PHP Script

    Which example posted? And how are you calling/using it?

  6. #6
    jensen's Avatar
    jensen is offline x10 Lieutenant jensen is an unknown quantity at this point
    Join Date
    Nov 2005
    Location
    At my desk
    Posts
    438

    Re: Server Load PHP Script

    tried both examples and they don't work.

    (Am running on another host because this host down for maintainance.)
    "For I am not ashamed of the gospel of Christ: for it is the power of God unto salvation to every one that believeth" Romans 1:16

  7. #7
    Bryon is offline Administrator Bryon has disabled reputation
    Join Date
    Apr 2005
    Location
    Northfield, NH
    Posts
    7,608

    Re: Server Load PHP Script

    How are you calling/using it?
    karimirt47 likes this.

  8. #8
    TheJeffsta's Avatar
    TheJeffsta is offline x10 Lieutenant TheJeffsta is an unknown quantity at this point
    Join Date
    Sep 2005
    Location
    New Zealand
    Posts
    492

    Re: Server Load PHP Script

    Doesnt that code you gave us work by itself?
    TheJeffsta (Jeffrey N)

    "No matter how much you try, the end result will always end up the way you DO NOT want it!"

  9. #9
    Torch's Avatar
    Torch is offline x10 Lieutenant Torch is an unknown quantity at this point
    Join Date
    Apr 2006
    Location
    Belgrade, Serbia
    Posts
    317

    Re: Server Load PHP Script

    You need to add "echo" to make them work for themselves. For Bryon's code you need to add
    Code:
    echo "Server uptime: $uptime";
    echo "<br>";
    echo "Server load: $load%";
    after "// $load holds the load."
    Last edited by Torch; 05-17-2006 at 08:46 AM.
    verlmirt17 likes this.

  10. #10
    Bryon is offline Administrator Bryon has disabled reputation
    Join Date
    Apr 2005
    Location
    Northfield, NH
    Posts
    7,608

    Re: Server Load PHP Script

    Quote Originally Posted by Torch
    You need to add "echo" to make them work for themselves. For Bryon's code you need to add
    Code:
    echo "Server uptime: $uptime";
    echo "<br>";
    echo "Server load: $load%";
    after "// $load holds the load."
    Yep, he's right with mine. You need to echo the variable holding the uptime to actually "see it."

    For the function that someone else posted, you need to call the function and echo the output.

    Example:
    PHP Code:
    ## 1 ##
    echo getServerLoad();

    ## 2 ##
    $output getServerLoad();

       
    // Other code here possibly..

    echo $output
    Last edited by Bryon; 05-17-2006 at 01:47 PM.

Closed Thread

Similar Threads

  1. [PHP] MySQL and PHP
    By Bryon in forum Tutorials
    Replies: 43
    Last Post: 03-24-2011, 07:27 AM
  2. Unstand PHP?
    By o0slowpaul0o in forum Tutorials
    Replies: 8
    Last Post: 01-07-2008, 09:16 PM
  3. Server UP time Script
    By dharmil in forum Scripts & 3rd Party Apps
    Replies: 2
    Last Post: 04-03-2006, 04:39 PM
  4. vBulletin - Optimization !!!!!
    By bin_asc in forum Scripts & 3rd Party Apps
    Replies: 7
    Last Post: 08-05-2005, 04:27 AM
  5. PHP Server Status.
    By Nathan in forum Scripts & 3rd Party Apps
    Replies: 2
    Last Post: 07-12-2005, 08:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers