Retrieving System Information With patSysinfo

Retrieve real-time server information and display it to users in your Web application with patSysinfo.

Getting Real

One of my recent projects involved building the administration interface for a Web hosting company. This interface, primarily targeted at the company's clients, included the usual goodies - creating and deleting user and FTP accounts, configuring email forwards and spam filters, and viewing traffic statistics. One of the more novel aspects of this project, however, involved providing users with real-time server information - server uptime, memory usage, CPU load, number of concurrent users and so on.

Now, I'd never done something like this before, and I had no idea how to begin obtaining this information "live" from the system. A little research told me all about the /proc filesystem in Linux, and how it contains information on the system state, updated in real time. I played with this a little bit, confirmed that it contained most of the information I needed, and had started writing some code to interface with it when I decided - almost on an impulse - to see if someone had already invented this particular wheel.

Lo and behold, I found patSysinfo, the answer to all my problems (and then some). Flip the page, and let me tell you more...

Plug And Play

patSysinfo is a PHP-based tool designed, in the author's words, to " retrieve a lot of information about the system your HTTP server is running on". Developed by Gerd Schaufelberger, it is freely available for download and is packaged as a single PHP class which can be easily included in your application.

Very simply, patSysinfo provides application developers with a set of APIs that ease the task of reading system information. This information includes the system's name and IP address, kernel version and CPU specification; it also includes data on the numbers and types of devices connected to the system, and real-time statistics on memory usage. At any given instant, patSysinfo can tell you which processes are running, and how much memory each one is consuming, together with the system's load average and number of connected users. If the system has built-in hardware sensors, patSysinfo can go even deeper, interfacing with these sensors via the lm_sensors package at http://secure.netroedge.com/~lm78/ to help you monitor the status of your hardware.

Now, there's nothing very novel or unique about patSysinfo, nor does it do anything you wouldn't be able to do yourself, given sufficient time and motivation. Its true value lies in its robust implementation of common UNIX commands (like "top", "ps", "uptime" and "uname") in PHP, thereby making it possible to integrate the output of these commands into a Web application with minimal time and fuss. Written as a PHP class, patSysinfo can substantially reduce the amount of time you spend manipulating and reading system data - especially when you have a specialized application like the one I described earlier.

Once caveat, though: patSysinfo only works with Linux systems, as it uses the special Linux /proc filesystem to obtain runtime information on the system's state. If you were planning to use this class on Windows, you should probably stop reading right now.

Before proceeding further, you should visit the patSysinfo home page at http://www.php-tools.de/ and download a copy of the latest version. The package contains the main class file, documentation outlining the exposed methods and variables, and some example scripts.

What's In A Name?

Now that the hard sell is over and you're (hopefully) all set up with patSysinfo, let's take a simple example to see how it works.

Most of the patSysinfo API calls look like this - the word "get" followed by the information that is required. In the example above, the object first looks up the file "/proc/sys/kernel/hostname" for the current hostname, and then obtains the IP address using the host name and the gethostbyname() PHP function

Finally, all that's left is to actually use the data retrieved by the methods - in this case, print it all to the standard output device.

// output values
echo "This system is named $name and has IP address $ip";

Up And At 'Em

How about finding out how long it's been since the system's last reboot? Well, patSysinfo allows you to duplicate the "uptime" command with its getUptime() function. Take a look:

The getUptime() method returns the time since the system's last reboot as an associative array with keys "days", "mins" and "hours". This can then easily be formatted into a readable string, as above. Here's the output:

Last reboot was 0 days, 1 hours and 43 minutes ago

Want to know a little bit more about the hardware and software running on the system? Take a look at the getKernelVersion() and getCpu() functions, which return the current kernel version and processor details respectively.

The getCpu() function, in particular, deserves closer attention. This function returns an array, each element of which is itself an associative array representing one of the processors in the CPU (most often, you will have a single-processor CPU and therefore only one element in this array). The associative array for each processor itself contains information on the processor name, clock speed, cache size and other relevant values.

Carrying The Load

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.

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.

A Nifty Device

The running kernel stores detailed information on all devices connected to the system, and makes this information available via the /proc filesystem. This means that patSysinfo can read it. And read it it does - the class includes three methods designed specifically to provide information on the PCI, IDE and SCSI devices attached to the system. These functions are called getPCIDevs(), getIDEDevs() and getSCSIDevs()respectively, and they're demonstrated in the following script.

In a similar manner, patSysinfo also allows you to retrieve detailed data transfer statistics and information on the network interfaces that have been configured for the system. This is done via the getNetDevs() method, demonstrated below:

Mounting Up

Finally, you can obtain information on mounted file system with the getMounts() method, which returns an array containing detailed information from both the "df" command and "/proc/mounts". Take a look:

Link Zone

That's about it for the moment. In this article, I introduced you to the patSysinfo class, which is designed primarily to assist you in the reading of real-time system information on a Linux system. I showed you how to use built-in class methods to retrieve the system's uptime, load average, host name and IP address, and how to obtain the kernel version and CPU speed. I also showed you to how to retrieve real-time information on memory usage, running processes and the list of processes using the most CPU power. Finally, I wrapped things up with a look at the functions that retrieve the list of devices attached to the system, and the list of mounted file systems.

If you'd like to learn more about patSysinfo and its related tools, you should take a look at the following links:

Note: All examples in this article have been tested on Linux/i586 with PHP 5.0, Apache 1.3.23 and patSysinfo 1.7. Examples are illustrative only, and are not meant for a production environment. Melonfire provides no warranties or support for the source code described in this article. YMMV!

Note: Examples are illustrative only, and are not meant for a production environment. Melonfire provides no warranties or support for the source code described in this article. YMMV!