Using shell-based resource monitors

When you install any Linux distribution with a desktop environment, chances are there will be a graphical system monitor bundled along with it. Popular among these are KSysGuard and the GNOME System Monitor, but there are many others. For the most part, these are fine and do the job well. The GNOME System Monitor is capable of showing you your load average, currently running processes (as well as their PID, CPU percent, memory, and more), and how much of your disks are being used. Many graphical system monitors also show this information and more. While these tools are great, nodes within a typical Linux-based network don't always have a graphical user interface available. Thankfully, there are many different resource monitoring tools available via the shell and they don't require that you're running a desktop environment at all. Some of these are so great that you'll, at some point, forego the graphical tools for the shell tools. Popular tools in this category include top, htop, iotop, and ncdu.

First, we would need to make sure the aforementioned tools are installed on our system. In most cases, top is already installed for us but the others will need to be installed manually. You can verify that top is installed by running:

which top

You should see the following output:

/usr/bin/top

You can use your distribution's package manager to install the others. For Debian, you can install them all in one shot:

# apt-get install htop iotop ncdu

Unfortunately, on CentOS, not all of these packages are available in the default repositories. To install these tools on CentOS, you'll first need to add the epel repository, and then you can install all of the packages. The following outlines the commands to use:

# yum install epel-release
# yum install htop iotop ncdu

Feel free to give these tools a try. The top and htop commands will both run without root access. However, you'll need to run iotop with at least sudo for it to function. The ncdu command will function as a normal user, but would then be limited to viewing only the resources that user has access to. Let's take a closer look at these tools.

What do these tools do for us, anyway? First, top is tried and true; something that you've probably used before if you're not new to Linux. When it comes to seeing what's running on your system, top is quite common. With top, you'll see all kinds of information, such as uptime, load average, used memory, used swap, cache, and more. In the bottom section of the screen, you'll see a list of processes. When you're finished, simply press Q to exit.

Using shell-based resource monitors

The top command running on a CentOS system

There are several ways in which you can run top. By running top with no parameters, you'll see a screen similar to what was shown earlier in this section. You will see a summary of system performance in the upper section and various processes in the bottom. However, if you already know which process you want to monitor, you can use the -p flag coupled with a PID to watch only that process. For example, we could use the following to monitor a process with a PID of 12844:

top -p 12844

By default, the output within the top command updates every three seconds. To change this, you can use the -d flag to choose a different frequency (in seconds):

top -d 2

If you prefer, frequency can be less than a second:

top -d 0.5

If top is already running and you would like to change how frequently it updates, you don't have to close it and start it up again. You can type s while it is running and you'll be prompted to designate a new frequency.

Within top, you can change how the process list is sorted by pressing a key on your keyboard. If you type P, you'll sort by CPU usage; using M, you can sort by memory usage (capitalization matters here). You can even kill a process from here if you wish, by pressing k, which will then prompt you for a PID to kill. Be careful though; this defaults to whatever happens to be at the top of your process list at the time you press it, so make sure that you don't press Enter until you've actually typed the PID or you may kill a process you didn't mean to.

So, why use top anyway? The main purpose that administrators use top for is to help determine what is causing a system to become CPU or memory bound. Most often, top is never the solution, but rather the beginning of a root cause analysis. You can immediately see which process is consuming your CPU or RAM, but depending on the context you may not have an idea yet on how to correct the problem. With top, you're only able to discover the culprit. Unfortunately, top may not always show you the root cause process, but it's definitely a very easy first place to look when you have a system that's running sluggish.

To begin your troubleshooting, the information at the top would give you a starting point to see which resource is being used up. On the %Cpu(s) line, we can tell immediately if the system is suffering from excessive I/O wait (the %wa field), which would basically mean there is more being thrown at the CPU than it's capable of handling. In this situation, tasks would back up and the load average would increase. Idle time (or %id) is a number that's better the higher it gets, which means your system would have CPU time to spare.

In some cases, you may find excessive CPU usage but not a lot in the process list to show for it. In such a case, you may bring up iotop in order to determine if your system is I/O bound. Using iotop (requires root) you can see just how much data is being written to or read from your disks. Using the left and right arrows, you can change focus from one column to another, which sorts the process list by that column.

Using shell-based resource monitors

Running iotop on a Debian system

By default, the list of processes within iotop is quite crowded. You can slim it down by executing:

# iotop --only

By appending -only, you'll only see processes that have actual read and write operations occurring. In the iotop screenshot in this section, you can see that there are quite a few processes with no activity happening at all. But with -only, it may be easier to read since it cleans up the output. You can actually activate -only while iotop is running, by simply pressing O on your keyboard. In addition, another useful keyboard shortcut is the ability to change the sort order of any column with r.

Next in this section, we have htop. While top is the the tried and true standard for viewing system resources on a Linux system, htop is increasing in popularity very quickly.

Using shell-based resource monitors

The htop command in action

The basic idea of htop is the same as top—the top area shows current CPU and memory usage and the bottom section provides a list of processes. But where htop differs is how it presents this information, which is easier to read and offers an area for graphs of your CPU's usage. In addition to that, it allows you to easily send a specific signal to a process. Earlier, we covered various signals you can use to end a process. Here, we can see that same concept illustrated graphically. To send a signal to a process, use the up and down arrows on your keyboard to highlight a process, and then press F9 to choose a specific signal. SIGTERM is selected by default, but you can send any of the other signals to a process as well.

Using shell-based resource monitors

Preparing to send a signal to a process in htop

The process list in htop can be sorted similar to iotop. One thing that may not be apparent at first is that htop supports mouse input. While you can select columns with arrow keys, you can also click on them.

Another benefit to htop is how customizable it is. Although the default layout is decent for most use cases, you can add additional meters. To do so, press F2 or click on Setup and you'll be brought to a menu where you can add or remove meters from the current view. Under Available Meters, highlight one that you want to add and press F5 to add it to the left column or F6 to add it to the right column. One meter you may find useful is the CPU average. Once you've added a new meter, you can reposition it by highlighting it and pressing F7 to move it up or F8 to move it down. When finished, press Esc to return to the main screen. These changes are saved automatically, so the next time you open htop, your custom layout will be intact.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset