dstat Command in Linux

Introduction

In this tutorial, we'll delve into the Linux `dstat` command, a dynamic system monitoring tool providing in-depth insights into various system resources like CPU, memory, network interfaces, and disk I/O. We'll begin with an introduction to the `dstat` command and demonstrate its use in monitoring CPU utilization and memory usage on an Ubuntu 22.04 Docker container. The `dstat` command is an invaluable asset for system administrators and developers seeking to optimize the performance and resource consumption of their Linux systems.

Introduction to dstat Command

This section introduces the `dstat` command, a flexible system monitoring utility for Linux. The `dstat` command presents detailed information about diverse system resources, encompassing CPU, memory, network, disk activity, and more.

First, let's install the `dstat` package on your Ubuntu 22.04 Docker container:

sudo apt-get update
sudo apt-get install -y dstat

Example output:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following additional packages will be installed:
  python3-dstat
The following NEW packages will be installed:
  dstat python3-dstat
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 72.0 kB of archives.
After this operation, 278 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
...

Now, execute the `dstat` command to observe the system's real-time statistics:

dstat

Example output:

----total-cpu-usage---- -dsk/total- -net/total- ---paging-- ---system--
usr sys idl wai hiq siq| read  writ| recv  send|  in   out | int   csw
  4   1  95   0   0   0|  45k   36k|   0     0 |   0     0 |  28   58
  3   1  96   0   0   0|  12k   16k|   0     0 |   0     0 |  27   57
  3   1  96   0   0   0|  12k   16k|   0     0 |   0     0 |  27   57
  3   1  96   0   0   0|  12k   16k|   0     0 |   0     0 |  27   57
  3   1  96   0   0   0|  12k   16k|   0     0 |   0     0 |  27   57

The `dstat` command offers a live view of various system metrics, including CPU utilization, disk I/O operations, network traffic volume, and more. The output can be tailored using various command-line options, for instance, dstat --cpu --mem --disk --net.

In the upcoming sections, we will examine how to utilize `dstat` for monitoring specific system parameters in greater detail.

Monitoring CPU Utilization with dstat

In this segment, we'll explore how to employ the `dstat` command to monitor CPU utilization on your Linux system.

Begin by executing the `dstat` command with the --cpu option to display detailed CPU statistics:

dstat --cpu

Example output:

----total-cpu-usage----
usr sys idl wai hiq siq
  3   1  96   0   0   0
  3   1  96   0   0   0
  3   1  96   0   0   0
  3   1  96   0   0   0
  3   1  96   0   0   0

The output presents the percentage of CPU time spent on user processes (usr), system processes (sys), idle time (idl), waiting for I/O (wai), hardware interrupts (hiq), and software interrupts (siq).

The --cpu-adv option offers a more granular view of CPU statistics:

dstat --cpu-adv

Example output:

--cpu-usr-- --cpu-sys-- --cpu-idl-- --cpu-wai-- --cpu-hiq-- --cpu-siq--
  3.00      1.00       96.00       0.00       0.00       0.00
  3.00      1.00       96.00       0.00       0.00       0.00
  3.00      1.00       96.00       0.00       0.00       0.00
  3.00      1.00       96.00       0.00       0.00       0.00
  3.00      1.00       96.00       0.00       0.00       0.00

This gives a detailed breakdown of CPU utilization by user, system, idle, wait, hardware interrupts, and software interrupts.

Combining --cpu and --cpu-adv provides both summary and detailed CPU statistics:

dstat --cpu --cpu-adv

Example output:

----total-cpu-usage---- --cpu-usr-- --cpu-sys-- --cpu-idl-- --cpu-wai-- --cpu-hiq-- --cpu-siq--
usr sys idl wai hiq siq
  3   1  96   0   0   0   3.00      1.00       96.00       0.00       0.00       0.00
  3   1  96   0   0   0   3.00      1.00       96.00       0.00       0.00       0.00
  3   1  96   0   0   0   3.00      1.00       96.00       0.00       0.00       0.00
  3   1  96   0   0   0   3.00      1.00       96.00       0.00       0.00       0.00
  3   1  96   0   0   0   3.00      1.00       96.00       0.00       0.00       0.00

This provides a complete view of the system's CPU usage, helping to rapidly pinpoint potential CPU bottlenecks or performance anomalies, especially important for systemadmin tasks.

Monitoring Memory Usage with dstat

In this section, we will explore using the `dstat` command to monitor memory usage on your Linux system.

Start by executing the `dstat` command with the --mem option to display detailed memory statistics:

dstat --mem

Example output:

-----memory-usage-----
used buff cache free
 1.2G  276M 1.1G  1.3G
 1.2G  276M 1.1G  1.3G
 1.2G  276M 1.1G  1.3G
 1.2G  276M 1.1G  1.3G
 1.2G  276M 1.1G  1.3G

The output shows the following memory usage metrics:

  • used: The amount of used memory
  • buff: The amount of memory used for buffers
  • cache: The amount of memory used for caching
  • free: The amount of free memory

You can also use the --swap option to monitor swap usage:

dstat --swap

Example output:

-----swap-----
used free
   0B  2.0G
   0B  2.0G
   0B  2.0G
   0B  2.0G
   0B  2.0G

This shows the amount of used and free swap space on the system. If the `used` column shows high usage, it might indicate memory pressure.

To get a comprehensive view of both memory and swap usage, you can combine the --mem and --swap options:

dstat --mem --swap

Example output:

-----memory-usage----- -----swap-----
used buff cache free used free
 1.2G  276M 1.1G  1.3G   0B  2.0G
 1.2G  276M 1.1G  1.3G   0B  2.0G
 1.2G  276M 1.1G  1.3G   0B  2.0G
 1.2G  276M 1.1G  1.3G   0B  2.0G
 1.2G  276M 1.1G  1.3G   0B  2.0G

This provides a detailed overview of the system's memory and swap usage, helping you to quickly identify potential memory-related issues or bottlenecks. As a root user or systemadmin, monitoring this data is critical.

Summary

In this lab, we explored the `dstat` command, a versatile system monitoring tool for Linux. We initiated the process by installing the `dstat` package and executing the basic `dstat` command to gain an overview of various system resources, encompassing CPU, memory, network, and disk I/O. Subsequently, we learned how to employ `dstat` for in-depth monitoring of CPU utilization and memory consumption. The `dstat` command delivers real-time, comprehensive information about the system, empowering users to swiftly diagnose and resolve performance problems, a core aspect of effective systemadmin practice on Linux systems.

400+ Linux Commands