MyBB Community Forums

Full Version: How mybb gets the server load
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Why does mybb use this to get the load:
/**
 * Returns the serverload of the system.
 *
 * @return int The serverload of the system.
 */
function get_server_load()
{
	global $lang;

	$serverload = array();

	// DIRECTORY_SEPARATOR checks if running windows
	if(DIRECTORY_SEPARATOR != '\\')
	{
		if(@file_exists("/proc/loadavg") && $load = @file_get_contents("/proc/loadavg"))
		{
			$serverload = explode(" ", $load);
			$serverload[0] = round($serverload[0], 4);
		}
		if(!is_numeric($serverload[0]))
		{
			if(@ini_get('safe_mode') == 'On')
			{
				return $lang->unknown;
			}
			
			// Suhosin likes to throw a warning if exec is disabled then die - weird
			if($func_blacklist = @ini_get('suhosin.executor.func.blacklist'))
			{
				if(strpos(",".$func_blacklist.",", 'exec') !== false)
				{
					return $lang->unknown;
				}
			}
			// PHP disabled functions?
			if($func_blacklist = @ini_get('disable_functions'))
			{
				if(strpos(",".$func_blacklist.",", 'exec') !== false)
				{
					return $lang->unknown;
				}
			}

			$load = @exec("uptime");
			$load = explode("load average: ", $load);
			$serverload = explode(",", $load[1]);
			if(!is_array($serverload))
			{
				return $lang->unknown;
			}
		}
	}
	else
	{
		return $lang->unknown;
	}

	$returnload = trim($serverload[0]);

	return $returnload;
}

Why not use sys_getloadavg? It works on lots more hosts than what this mess of code uses.
Probably because that's not php4 compatible. I'd assume we can get that changed for MyBB 1.6x which is only php5 compatible.
sys_getloadavg >= PHP 5.1.3
MyBB 1.6 >= PHP 5.1.0
Well why can't we use a more recent version for 1.6?
the function reference also says that the function is not implemented on windows platforms, so you'd pretty much have to work with function_exists() anyway... also all the function does is read /proc/loadavg which is pretty much the same as what the above PHP code is doing...

so yeah sure why not, anything beats exec uptime (which only reads /proc/loadavg too, at least on Linux, so they're all doing the same thing)
Thanks for the suggestion, I've stuck this in.

The reason this is a good suggestion is because many hosts disable exec() which we use to get the uptime so on many hosts it shows "Unknown" but with this function we won't have that issue.