Topic: Load Average reporting: Am I missing something?
Version: 1.2.16
PHP: 5.2.5 /w eAccelerator 0.9.5.2
OS: Linux
Recently, I have had more time to administrate my forum (and my server in general), and I have been going back to make enhancements and fixes to my forum. Thanks to another thread on a forum, I was able to tweak it so that PunBB checked the php.ini to see if eAccelerator is installed (the current version only checked for MMCache and ionCube Accelerator, which haven't been updated in ages ). The Load Average reporter on the same admin_index.php page, however always says "Not Available". I looked into the code and it makes sense:
// Get the server load averages (if possible)
if (@file_exists('/proc/loadavg') && is_readable('/proc/loadavg'))
{
// We use @ just in case
$fh = @fopen('/proc/loadavg', 'r');
$load_averages = @fread($fh, 64);
@fclose($fh);
$load_averages = @explode(' ', $load_averages);
$server_load = isset($load_averages[2]) ? $load_averages[0].' '.$load_averages[1].' '.$load_averages[2] : 'Not available';
}
else if (!in_array(PHP_OS, array('WINNT', 'WIN32')) && preg_match('/averages?: ([0-9\.]+),[\s]+([0-9\.]+),[\s]+([0-9\.]+)/i', @exec('uptime'), $load_average$
$server_load = $load_averages[1].' '.$load_averages[2].' '.$load_averages[3];
else
$server_load = 'Not available';
.
.
.
<?php echo $server_load ?>
Im concerned with the if since it's Linux. Checks if /proc/loadavg exists and is readable, then opens it for reading, sticks it in $load_averages, and closes that file.
Ok yeah, I follow:
<?php
if (@file_exists('/proc/loadavg') && is_readable('/proc/loadavg'))
{
// We use @ just in case
$fh = @fopen('/proc/loadavg', 'r');
$load_averages = @fread($fh, 64);
@fclose($fh);
}
echo $load_averages
?>
su webusername -
$ php blah
0.00 0.00 0.00 1/153 816
Then, it uses explode() to delimit load_averages into its own array using space as a delimiter, followed by asking if the 3rd element of the array (the past 15 min load average) is actually set to something. Obviously since it was, the $server_load variable is set to the first 3 elements in load_averages separated by spaces. So, everything's cool right? Should be reporting the 1, 5, and 15 minute load averages to the admin_index page right?
<?php
if (@file_exists('/proc/loadavg') && is_readable('/proc/loadavg'))
{
// We use @ just in case
$fh = @fopen('/proc/loadavg', 'r');
$load_averages = @fread($fh, 64);
@fclose($fh);
$load_averages = @explode(' ', $load_averages);
echo $load_averages[2]."\n";
$server_load = isset($load_averages[2]) ? $load_averages[0].' '.$load_averages[1].' '.$load_averages[2] : 'Not available';
}
echo $server_load;
?>
su webusername -
$ php blah
0.00
0.00 0.01 0.00
The sucky part is that my admin_index.php page is still reporting "Not Available", which means according to the code, it can't detect the OS or it detects linux and couldn't read /proc/loadavg correctly. I ran my test code as the same user and permissions that the admin_index.php page has, in the same directory, so I'm not really too sure why this is happening. The only thing I can think of is that Apache is screwing it up somehow, but I wouldn't know how to troubleshoot that. I am fairly inexperienced, so I might be missing something major here. Please let me know if you have any questions about my setup. Thank you.