HomePHP 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.
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.
// 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();
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:
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.