PDA

View Full Version : Displaying Server Load Average


Jamie
November 3rd, 2005, 20:12
I am trying to display the server load average, but the place I want the average to output, is not on the server that I want to check.

I have tried Google, but can't come up with anything useful.

I wonder if somebody could point me to a script (PHP), or show me how to do this?

Tjobbe
November 3rd, 2005, 21:00
I personally do not know if it is possible mate.

The only thing I could think of is a site load status, similar to sxc.hu, it says "server load" on that site.

I think that it may be a PHP scipt written such as this:

IF users online equals more than 100 display 10% server load

else

IF users online equals more than 500 display 50% server load

or

IF users online equals more than 1000 display 100% server load

if you get me?

I could well be wrong, their may be a server load script, but I personally think there isn't..

inimino
November 3rd, 2005, 21:17
You have two servers, you want to get the load from one, and display it on the other, and you can run scripts on both, is that it?

Cameron
November 3rd, 2005, 21:49
To get the server's system load in linux, you need to read /proc/loadavg
0.20 0.12 0.11 1/188 21841 would be an example of the file.

Which you may want to read http://www.teamquest.com/resources/gunther/ldavg1.shtml for an idea on what the load average really represents.

Cameron
November 3rd, 2005, 21:52
Oh, and you might look into cacti to get a pretty (ok maybe not pretty... :P) graph representation of the load average :)

Jamie
November 4th, 2005, 10:59
You have two servers, you want to get the load from one, and display it on the other, and you can run scripts on both, is that it?

Basically, yes. Although I wouldn't need to run scripts on both. Let me try and explain it a bit better.

We have our customer server (Bumblebee (Great name, huh? ;))).

We then have our server with our support centre on. On this support centre, I am wanting to display the average server load of the Bumblebee server. (In a nice fashion, rather than say 5 sets of boring numbers that you get from /proc/loadavg)

If I had any idea of how to use Cacti, Cameron, I would, although like you say, it doesn't look too pretty.

I realise /proc/loadavg calls the average from the last 1min, 5min and 15min. Is it possible to simply call one of these, style it, and make it look half decent, and even possibly put it in a graph using Cacti? I can feel I'm going to be hunting for a quote in a second for all this. :lol:

Cameron
November 4th, 2005, 16:42
Cacti (linky (http://www.cacti.net/)) is an RRDTool based graphing script (or solution according to their website :P), basically it gathers data from a certain source, which would be a Round Robin Database. Which the data is transfered via SNMP (same concept as MRTG to an extent).

... This also occured to me, caching the output of `uptime` into a text file that can be easily read with PHP :)

Cameron
November 4th, 2005, 16:51
The "Server" Script - you'll need to edit the $cache_file variable to point to where uptime_cache.txt will go. Along with $uptime_bin if neccessary (do wheries uptime from the command line if its not in /usr/bin :))
<?php
// this script needs to be ran via cron
$cache_file = '/home/jamie/public_html/uptime_cache.txt';
$uptime_bin = '/usr/bin/uptime';
$output = '';

if(! $output = shell_exec( $uptime_bin ) )
{
$output = 'Access is denied to `uptime` be sure to check permissions';
}
else
{
if(! $fp = fopen( $cache_file, 'w') )
{
die( 'could not open ' . $cache_file .'!');
}
flock( $fp, LOCK_EX );
fwrite( $fp, $output );
flock( $fp, LOCK_UN );
fclose( $fp );
}
?>This will need to be run via cron e.g. every 5min or something like that.

The "Client" Script - $cache_file can either be a local file, or it can be on a different server (if fopen wrappers = On in php.ini). Change $servername as well to the servername you're getting the uptime of.
<?php
// this can be included via include() or require()
$cache_file = 'http://cameroncox.com/uptime_cache.txt';
$servername = 'bumblebee';
$output = '';
$matches = array();

if(! $output = file_get_contents( $cache_file ) )
{
$output = 'Could not read ' . $cache_file;
}
else
{
// laziness prevails huzzah.
// I should re-write this regex, its naaaasty.
preg_match( '/(............)([0-9]+)(......................................)(.*)/', $output, $matches );

echo $servername . ' has been up for ' . $matches[2] . ' days with a load average of ' . $matches[4];
}
?>

This may work for you :). Also I provide no warrenty so don't come knocking on my door, if either of these scripts a.) raise a sleeping god (cluthu) b.) cause the server to cache file c.) triggers WWIII.

crazyfish
November 4th, 2005, 17:25
What about something like this?
http://www.switch2hosting.com/Status/

Its being advertised at WHT http://webhostingtalk.com/showthread.php?t=447562

You can get it for win and nix. It looks pretty good too.

legend2
December 26th, 2005, 17:56
Here you go, this will parse the first 3 results that the load average has (you don't need the whole line) and output them into a txt file that you can include from another other site.


<?
$path_to_web = "/home/httpd/html/";
$output_file_name = "load.txt";


$result = shell_exec("cat /proc/loadavg");

$temp = strtok($result," ");

$i=0;

while($i<3) {

$finalresult .= $temp . " ";

$i++;

$temp = strtok(" ");

}

$finalresult = trim($finalresult);

$finalpath = $path_to_web . $output_file_name;

shell_exec("echo $finalresult > $finalpath");

shell_exec("chmod 644 $finalpath");

?>

Anoop
December 28th, 2005, 09:10
If you are using shell_exec, here is an easy way which gives the same result.

<?
$path_to_web = "/home/httpd/html/";
$output_file_name = "load.txt";

$finalpath = $path_to_web . $output_file_name;

shell_exec("cat /proc/loadavg | awk '{ print $1\" \"$2\" \"$3}' >".$finalpath);
shell_exec("chmod 644 $finalpath");

?>

:D

nishanth
January 11th, 2006, 06:21
Hello,

I think you can also transfer the content of the file /proc/loadavg to the admin server through socket on either of the servers.
Write a cron on the client server and the admin server so that the contents of the file /proc/loadavg are transfered to a /tmp/clientload file on the admin server and then its merely a file operation.
But be warned that there will be lots of security holes opened up when you open a socket and make it listen to a port permenantly.

ALTERNATE METHOD :
You can use ssh and use a key so that there is no need to login to the server everytime.You can execute commands on the server remotely.

Regards
Nishanth