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 neutral ).  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.

Re: Load Average reporting: Am I missing something?

What you can do is remove the @ signs in the scripts (for example, before file_exists()). The @ supresses any warnings and notices that PHP wants to spit out. That way, you may get a better idea of what is failing.

Or try the equivalent code bits from PunBB 1.3 (tries one more thing): http://dev.punbb.org/browser/branches/p … /index.php

"Programming is like sex: one mistake and you have to support it for the rest of your life."

Re: Load Average reporting: Am I missing something?

[Mon Jan 28 10:15:03 2008] [error] [client xxxxxxxxx] PHP Warning:  file_exists() [<a href='function.file-exists'>function.file-exists</a>]: open_basedir restriction in effect. File(/proc/loadavg) is not within the allowed path(s): (/home/vhosts/domain.com/httpdocs:/tmp) in /home/httpd/vhosts/domain.com/httpdocs/forum/admin_index.php on line 79, referer: /home/httpd/vhosts/domain.com/forum/admin_options.php

Plesk has default rules about open_basedir in each domain's httpd.include file and removing the @ revealed that it wasn't letting PHP access /proc/loadavg at all.  Your new solution from PunBB 1.3 Dev works just fine since my PHP is >= 5.1.3.  If one is using Plesk as I am, one can also edit the domain's vhost.conf file (in the conf directory at the domain's root) to override the setting, but that may not be feasible depending on ones level of access.

Here are the adjustments Rickard suggested you can make to admin_index.php if you have this issue.  Remember PHP Version >= 5.1.3 is required

Find:

if (@file_exists('/proc/loadavg') && is_readable('/proc/loadavg'))

Replace With:

else if (@file_exists('/proc/loadavg') && is_readable('/proc/loadavg'))

Before, Add:

if (function_exists('sys_getloadavg'))
{
    $load_averages = sys_getloadavg();
    array_walk($load_averages, create_function('&$v', '$v = round($v, 3);'));
    $server_load = $load_averages[0].' '.$load_averages[1].' '.$load_averages[2];
}

Thanks again for your help, Rickard.  It is greatly appreciated.