Home arrow PHP arrow Page 5 - Retrieving System Information With patSysinfo

Carrying the Load - PHP

Linux file structure contains within it a special area called /proc. Now, some believe that there's black magic in that directory. For those who know better than to fear the /proc, there awaits much good magic, in the form of server info. Looking for a way to retrieve real-time server information and display it to users in your Web application? Today's your lucky day! Take a look at the patSysinfo PHP class, which lets you do that and a whole lot more.

TABLE OF CONTENTS:
  1. Retrieving System Information With patSysinfo
  2. Plug and Play
  3. What's in a Name?
  4. Up and at 'Em
  5. Carrying the Load
  6. A Nifty Device
  7. Running Out of RAM
  8. Mounting Up
  9. Link Zone
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 19
February 16, 2004

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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:

patSysinfo

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:

patSysinfo

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.



 
 
>>> More PHP Articles          >>> More By icarus, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: