PHP system(uptime)

I am running the following code:

system(‘uptime’)

It gives me the following output:

21:34:15 up 5 days, 7:41, 2 users, load average: 0.12, 0.20, 0.08

I’m wanting to take the load averages from that output:

0.12
0.20
0.08

Add them together and divide by 3 to get a final result.

Any ideas?

You could do it like this:

<?php
$uptime = @system('uptime');
if ($uptime && preg_match("#average: ([0-9,\\.\\s]+)$#", $uptime, $loads)) {
	echo "The average load is: " . number_format((array_sum(explode(", ", $loads[1])) / 3), 2);
} else {
	echo "The uptime could not be determined";
}
?>

It’s just a very basic way to do it. Normally you should properly identify the values with REGEX, then make sure each load value is stripped or any spaces that might get from your output. It takes more resources to allocate variables and perform a foreach() or a for() but that’s what you have to do to make it flawless.

May I ask, why? Those values already are averages, is picking the appropriate one not good enough?

:stuck_out_tongue: Overkill comes to mind but on the other hand he might be interested in special cases such as load spikes.