Uptime and load on Unix systems

por | 5 julio, 2006

This block makes it possible to show the current load and uptime in a block. The code will not work as is on all Unixes. One might consider it a security risc to show what your uptime is (kernel patching requires reboot), so .. batteries not included, always look both sides when crossing a road etc.

You can learn from this code how to execute shell commands form php.

<?php
$uptime
= shell_exec("cut -d. -f1 /proc/uptime");
$loadavg_array = explode(" ", exec("cat /proc/loadavg"));
$loadavg = $loadavg_array[2];
$days = floor($uptime/60/60/24);
$hours = $uptime/60/60%24;
$mins = $uptime/60%60;
$secs = $uptime%60;
echo
"This server has been up $days day(s) $hours hour(s) $mins minute(s) and $secs second(s)";
echo
"<p><br>";
print(
"[ Current server load: " . $loadavg . " ]");
?>

Contributed addition by mikegull
To add to this block the current memory stats, in kb, for the server, use the following. This should work on most Linux servers, but not all.

<?php
$memstats
= shell_exec("grep Mem /proc/meminfo");
print
'Server Memory:<BR />' . str_replace("\n", '<BR />', $memstats);
?>