Server Load PHP Script

Status
Not open for further replies.

TheJeffsta

New Member
Messages
984
Reaction score
0
Points
0
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/Status/x10Hosting.php

Sort of like the server load in the following forum members signature:
index.php
 
Last edited:

theblazingangel

New Member
Prime Account
Messages
90
Reaction score
0
Points
0
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;
        }
    }
}
 

Bryon

I Fix Things
Messages
8,149
Reaction score
101
Points
48
I use this:​
PHP:
<?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(0, 0, 0, 1, 1, date('Y'), 0);
$end = mktime(0, 0, 0, date('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($load, 3);

// $load holds the load.

?>

Either way works though.

Edit: *nix only ;)
 
Last edited:

Bryon

I Fix Things
Messages
8,149
Reaction score
101
Points
48
Which example posted? And how are you calling/using it?
 

jensen

Active Member
Messages
1,168
Reaction score
22
Points
38
tried both examples and they don't work.

(Am running on another host because this host down for maintainance.)
 

Torch

New Member
Messages
634
Reaction score
0
Points
0
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:

Bryon

I Fix Things
Messages
8,149
Reaction score
101
Points
48
Torch said:
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:
## 1 ##
echo getServerLoad();

## 2 ##
$output = getServerLoad();

   // Other code here possibly..

echo $output;
 
Last edited:
Status
Not open for further replies.
Top