Linux dstat 命令实用示例

LinuxLinuxBeginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

介绍

在本实验中,我们将探索 Linux 的 dstat 命令,这是一个多功能的系统监控工具,能够提供关于各种系统资源的详细信息,包括 CPU、内存、网络和磁盘。我们将首先介绍 dstat 命令,并演示如何在 Ubuntu 22.04 Docker 容器中使用它来监控 CPU 利用率和内存使用情况。dstat 命令是系统管理员和开发人员的强大工具,他们需要了解 Linux 系统的性能和资源使用情况。

Linux 命令速查表


Skills Graph

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

dstat 命令介绍

在这一步中,我们将介绍 dstat 命令,这是一个多功能的 Linux 系统监控工具。dstat 命令提供了关于各种系统资源的详细信息,包括 CPU、内存、网络、磁盘等。

首先,让我们在 Ubuntu 22.04 Docker 容器中安装 dstat 包:

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

示例输出:

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
...

现在,让我们运行 dstat 命令来查看系统的实时统计信息:

dstat

示例输出:

----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

dstat 命令提供了各种系统资源的实时概览,包括 CPU 利用率、磁盘 I/O、网络流量等。你可以通过指定不同的选项来自定义输出,例如 dstat --cpu --mem --disk --net

在接下来的步骤中,我们将详细探讨如何使用 dstat 来监控特定的系统指标。

使用 dstat 监控 CPU 利用率

在这一步中,我们将学习如何使用 dstat 命令来监控 Linux 系统的 CPU 利用率。

首先,让我们运行带有 --cpu 选项的 dstat 命令,以显示详细的 CPU 统计信息:

dstat --cpu

示例输出:

----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

输出显示了用户进程 (usr)、系统进程 (sys)、空闲时间 (idl)、等待 I/O (wai)、硬件中断 (hiq) 和软件中断 (siq) 的 CPU 利用率百分比。

你还可以使用 --cpu-adv 选项来获取更详细的 CPU 统计信息:

dstat --cpu-adv

示例输出:

--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

这提供了更详细的 CPU 利用率细分,包括用户、系统、空闲、等待、硬件中断和软件中断。

你还可以结合 --cpu--cpu-adv 选项,以同时获取摘要和详细的 CPU 统计信息:

dstat --cpu --cpu-adv

示例输出:

----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

这提供了系统 CPU 利用率的全面视图,使你能够快速识别任何潜在的 CPU 瓶颈或性能问题。

使用 dstat 监控内存使用情况

在这一步中,我们将学习如何使用 dstat 命令来监控 Linux 系统的内存使用情况。

首先,让我们运行带有 --mem 选项的 dstat 命令,以显示详细的内存统计信息:

dstat --mem

示例输出:

-----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

输出显示了以下内存使用指标:

  • used:已使用的内存量
  • buff:用于缓冲区的内存量
  • cache:用于缓存的内存量
  • free:空闲的内存量

你还可以使用 --swap 选项来监控交换空间(swap)的使用情况:

dstat --swap

示例输出:

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

这显示了系统中已使用和空闲的交换空间量。

为了全面了解内存和交换空间的使用情况,你可以结合 --mem--swap 选项:

dstat --mem --swap

示例输出:

-----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

这提供了系统内存和交换空间使用情况的详细概览,使你能够快速识别任何潜在的内存相关问题或瓶颈。

总结

在本实验中,我们学习了如何使用 dstat 命令,这是一个多功能的 Linux 系统监控工具。我们首先安装了 dstat 包,并运行了基本的 dstat 命令来获取各种系统资源的概览,包括 CPU、内存、网络和磁盘 I/O。接着,我们深入探讨了如何使用 dstat 来详细监控 CPU 利用率和内存使用情况。dstat 命令提供了系统的实时详细信息,使用户能够快速识别和排查性能问题。

Linux 命令速查表