The proc file system is found on many UNIX and Linux variants and gives a virtual view into the running system. Traditionally the proc file system is attached to the /proc
mount point, but proc is not really a file system and mostly contains "files" of zero size. Even though these objects look as if they are empty when they are listed, they actually contain quite a lot of information about the running system. When viewed, they show system information from the kernel's perspective: their contents are based on system resources and characteristics, such as memory, CPU, kernel, and network utilization.
There are several kinds of files in the /proc
directory tree on a Linux machine. The first type is a series of directories that have numbers as names, each containing a group of files that hold specific information about the running system. Each numbered directory corresponds to a running process ID. The files in these directories relate to the command that was invoked, the execution environment, parameters passed to the command, memory usage, and other valuable pieces of process information. The other file types and directories found in /proc
contain items such as current resource usage, system settings, hardware information, and network usage.
The man page for proc contains far more detail than this chapter, but please note that implementations of the proc file system on different operating systems are by no means identical. I have worked on older versions of Solaris whose /proc
contained only numbered directories for running processes, and those directories held files that were different from their Linux counterparts. With this state of affairs, you are certain to profit from exploring the documentation for your specific system.
The script we are about to explore has its origins in a utility that creates a brief system-status report. The script returned a single line of key performance indicators; the information was derived from files located in /proc
directories. It displayed all the process-performance values for each system being monitored on a few lines all contained in a single window, using colors to indicate load level, memory, and swap usage (green for normal usage and red for high). The main purpose of this tool was to permit a quick visual check of whether certain machines were overloaded.
Our code extends the functionality of the original script from providing just one line of output per system to displaying a more comprehensive report about the processes running on a single machine. The code itself is not very complex but it does demonstrate some of the /proc
system usage data. You will also learn how to add color to your output.
The first function sets up the colors that the script will use. These are standard ANSI color definitions, and each one begins with the string