Retrieving System Information With patSysinfo - Carrying the Load (Page 5 of 9 )
If it's system performance you're interested in, patSysinfo can give you that as well. Consider the getLoadAvg() method, which returns the average CPU load for the last 1, 5 and 15 minutes, or the getNumberUsers() function, which returns the number of users currently logged in to the system.
<?php
// include class
include("patSysinfo.php");
// instantiate object
$sys = new patSysinfo();
// get load average
// returns array of 3 values
$loadArray = $sys->getLoadAvg();
// get number of users
$users = $sys->getNumberUser();
// output values
echo "Load averages " . $loadArray[0] . " (1 min) " . $loadArray[1] . " (5 min) " . $loadArray[2] . " (15 min), $users users";
? >
Here's the output:
Load averages 0.01 (1 min) 2.11 (5 min) 1.43 (15 min), 12 users
You can obtain a list of all running processes with the getProcesses() method, equivalent to the UNIX "ps" command. The list of processes is returned as an array, each element of which represents a running process and contains details on that process' owner, ID, current status and resource usage. Consider the following example, which illustrates:
<html>
<head>
</head>
<body>
<table border="1" cellspacing="0" cellpadding="5">
<tr>
<td><u>Process ID</u></td>
<td><u>Command</u></td>
<td><u>Owner</u></td>
<td><u>Terminal</u></td>
<td><u>Status</u></td>
<td><u>Start</u></td>
<td><u>Duration</u></td>
<td><u>CPU %</u></td>
<td><u>Memory %</u></td>
</tr>
<?php
// include class
include("patSysinfo.php");
// instantiate object
$sys = new patSysinfo();
// get process list
$processes = $sys->getProcesses();
foreach ($processes as $p)
{
echo "<tr>";
echo "<td>" . $p[pid] . "</td>";
echo "<td>" . $p[command] . "</td>";
echo "<td>" . $p[user] . "</td>";
echo "<td>" . $p[tty] . "</td>";
echo "<td>" . $p[stat] . "</td>";
echo "<td>" . $p[start] . "</td>";
echo "<td>" . $p[time] . "</td>";
echo "<td>" . $p[cpu] . "</td>";
echo "<td>" . $p[mem] . "</td>";
echo "</tr>";
}
? >
</table>
</body>
</html>
And here's what the output looks like:

A variant of this is the getTopProcesses() method, which returns the same output as getProcesses() but sorts the data by CPU usage. Here's the script,
<html>
<head>
</head>
<body>
<table border="1" cellspacing="0" cellpadding="5">
<tr>
<td><u>Process ID</u></td>
<td><u>Command</u></td>
<td><u>Owner</u></td>
<td><u>Terminal</u></td>
<td><u>Status</u></td>
<td><u>Start</u></td>
<td><u>Duration</u></td>
<td><u>CPU %</u></td>
<td><u>Memory %</u></td>
</tr>
<?php
// include class
include("patSysinfo.php");
// instantiate object
$sys = new patSysinfo();
// get process list
$processes = $sys->getTopProcesses();
foreach ($processes as $p)
{
echo "<tr>";
echo "<td>" . $p['pid'] . "</td>";
echo "<td>" . $p['command'] . "</td>";
echo "<td>" . $p['user'] . "</td>";
echo "<td>" . $p['tty'] . "</td>";
echo "<td>" . $p['stat'] . "</td>";
echo "<td>" . $p['start'] . "</td>";
echo "<td>" . $p['time'] . "</td>";
echo "<td>" . $p['cpu'] . "</td>";
echo "<td>" . $p['mem'] . "</td>";
echo "</tr>";
}
? >
</table>
</body>
</html>
and here's the output:

For script such as these, which retrieve real-time information from the system, it's a good idea to insert an auto-refresh header at the top of the page, so that the page automatically updates itself with the latest information at a user-specified interval.