PDA

View Full Version : Need serverload script



snaaps
08-15-2005, 04:11 PM
Hi, I need a script that I can use to show the serverload or cpu usage of a server on a webpage.

I have searched on google but I found nothing.

p.s for fedora core 3 systems
(optional that the scripts mail me when the serverload is higher than 2.0)

snaaps
08-15-2005, 04:36 PM
Ok I found this:


<?php
function getServerLoad()
{
if(file_exists('/proc/loadavg')){
$file = fopen('/proc/loadavg', 'r');

if(!$file){
return 0;
}

$load = fread($file, 6);
fclose($file);
$loadavg = explode(' ', $load);
}else{
$load = exec('uptime');
$load = split('load averages?: ', $load);
$loadavg = explode(',', $load[1]);
}
return trim($loadavg[0]);
}
echo getServerLoad();
?>

this works fine! But now I want a option that this script mail me when de load is higher than 2.0

resolveit
08-16-2005, 12:33 AM
<?php
function getServerLoad()
{
if(file_exists('/proc/loadavg')){
$file = fopen('/proc/loadavg', 'r');

if(!$file){
return 0;
}

$load = fread($file, 6);
fclose($file);
$loadavg = explode(' ', $load);
}else{
$load = exec('uptime');
$load = split('load averages?: ', $load);
$loadavg = explode(',', $load[1]);
}
return trim($loadavg[0]);
}
$serverload = getServerLoad();

echo $serverload.'<BR>';
if ($serverload >= 2) {
$header = "From: monitor@example.com \r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Mailer: Serverloadmonitor v1.0\r\n";
$header .= "Content-Type: text/plain; charset=\"iso-8859-1\"\r\n";
$header .= "Content-Transfer-Encoding: 8bit\r\n";

mail("youremail@example.com","Your serverload has reached 2 or more.","Your current serverload is $serverload<p>",$header);
}
?>



Replace the 2 entries where you see example.com and you should be set.

This script has not been tested and I will not be responsible if it blows up your server :)

Onno