15/09/05

IBBoard.co.uk server uptime

Permalink 03:28:18 pm, Categories: Programming, 222 words  

After trying to get some server load/uptime scripts running for my sites, I had to submit a support ticket to the techs to find a way of getting the average loads without fread()ing the /proc/uptime 'file'. Apparently we don't get read access to /proc/ because of security/hacking issues. The solution? use PHP's backticks (` - not to be confused with apostrophe's) and run the uptime command through PHP's shell execution!

Now it seems slightly counter-intuitive to me that /proc/ can't have read access, but we can execute shell commands, but such is life. Here is my simple server load and apache uptime script (note: you'll probably need to highlight the text and scroll it sideways):

<?php
$uptime = @exec('uptime');
//echo $uptime;
preg_match("/averages?: ([0-9\.]+),[\s]+([0-9\.]+),[\s]+([0-9\.]+)/",$uptime,$avgs);
$uptime = explode(' up ', $uptime);

$uptime = explode(',', $uptime[1]);

if (strpos($uptime[0],"day")===false){
if (strpos($uptime[0],"min")===false){
$days = 0;
list($hours, $minutes) = explode(':',trim($uptime[0]));
}
else{
$days = $hours = 0;
$minutes = (int)$uptime[0];
}
}
else{
$days = (int)$uptime[0];
list($hours, $minutes) = explode(':',trim($uptime[1]));
}

Header('Content-type: text/plain');
echo <<<EOF
Average Load (1 min): {$avgs[1]}
Average Load (5 min): {$avgs[2]}
Average Load (15min): {$avgs[3]}
Uptime: $days days $hours hours and $minutes minutes
EOF;
?>

And if you're interested in the current IBBoard server stats, it's running at http://www.ibboard.co.uk/uptime.php

Comments, Trackbacks:

No Comments/Trackbacks for this post.

Navigation