Linux dstat Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, we will explore the Linux dstat command, a versatile system monitoring tool that provides detailed information about various system resources, including CPU, memory, network, and disk. We will start by introducing the dstat command and demonstrating how to use it to monitor CPU utilization and memory usage on our Ubuntu 22.04 Docker container. The dstat command is a powerful tool for system administrators and developers who need to understand the performance and resource usage of their Linux systems.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/PackagesandSoftwaresGroup(["`Packages and Softwares`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/PackagesandSoftwaresGroup -.-> linux/apt("`Package Handling`") linux/SystemInformationandMonitoringGroup -.-> linux/ps("`Process Displaying`") linux/SystemInformationandMonitoringGroup -.-> linux/top("`Task Displaying`") linux/SystemInformationandMonitoringGroup -.-> linux/free("`Memory Reporting`") subgraph Lab Skills linux/apt -.-> lab-422651{{"`Linux dstat Command with Practical Examples`"}} linux/ps -.-> lab-422651{{"`Linux dstat Command with Practical Examples`"}} linux/top -.-> lab-422651{{"`Linux dstat Command with Practical Examples`"}} linux/free -.-> lab-422651{{"`Linux dstat Command with Practical Examples`"}} end

Introduction to dstat Command

In this step, we will introduce the dstat command, a versatile system monitoring tool for Linux. The dstat command provides detailed information about various system resources, including CPU, memory, network, disk, and more.

First, let's install the dstat package on our 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, let's run the dstat command to see 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 provides a real-time overview of various system resources, including CPU utilization, disk I/O, network traffic, and more. You can customize the output by specifying different options, such as dstat --cpu --mem --disk --net.

In the next steps, we will explore how to use dstat to monitor specific system metrics in more detail.

Monitoring CPU Utilization with dstat

In this step, we will learn how to use the dstat command to monitor the CPU utilization on our Linux system.

First, let's run 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 shows the percentage of CPU utilization for user processes (usr), system processes (sys), idle time (idl), waiting for I/O (wai), hardware interrupts (hiq), and software interrupts (siq).

You can also use the --cpu-adv option to get more detailed 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 provides a more detailed breakdown of CPU utilization, including user, system, idle, wait, hardware interrupts, and software interrupts.

You can also combine the --cpu and --cpu-adv options to get both the 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 comprehensive view of the system's CPU utilization, allowing you to quickly identify any potential CPU bottlenecks or performance issues.

Monitoring Memory Usage with dstat

In this step, we will learn how to use the dstat command to monitor the memory usage on our Linux system.

First, let's run 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.

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, allowing you to quickly identify any potential memory-related issues or bottlenecks.

Summary

In this lab, we learned how to use the dstat command, a versatile system monitoring tool for Linux. We started by installing the dstat package and running the basic dstat command to get an overview of various system resources, including CPU, memory, network, and disk I/O. We then explored how to use dstat to monitor CPU utilization and memory usage in more detail. The dstat command provides real-time, detailed information about the system, allowing users to quickly identify and troubleshoot performance issues.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like