Home > System Tutorial > LINUX > body text

Use nmon to troubleshoot and monitor Linux system performance

WBOY
Release: 2024-02-09 14:20:13
forward
978 people have browsed it

Nigel’s Monitor (nmon) is a system performance monitoring tool originally developed by IBM for the AIX operating system and later ported to Linux systems (supporting multiple CPU architectures).

nmon Provides a simple interface that can monitor various aspects of the system, such as CPU utilization, memory, disk busyness, and network utilization. This is one of its main advantages. If nmon is not available, you need to use specialized monitoring tools, such as top (for monitoring processes), iostat (for monitoring disks) and ifstat (used to monitor the network) to monitor various resources. These tools all present data differently.

I first used nmon as an AIX system administrator about 10 years ago. By using nmon, I was able to troubleshoot some hard-to-find problems, including what initially looked like a network bottleneck. I used nmon to check multiple resources at the same time and eventually found out it was an application issue caused by having too many files open.

In addition to interactive monitoring systems, you can also use nmon in batch mode to collect and store performance data for analysis. For more information about the Linux version of nmon, see the project's official website.

This article will introduce this powerful monitoring tool and start with installing it.

Install nmon

nmon is available in the default repositories of many Linux distributions. For example, we can install it using DNF on Fedora35:

$ sudo dnf -y install nmon
Copy after login
Copy after login

For Red Hat Enterprise Linux or CentOS, nmon is available in the EPEL (Extra Packages for Enterprise Linux) repository.

To install nmon, you first need to enable EPEL:

$ sudo dnf -y install \
https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm

$ sudo dnf repolist
Copy after login

Next use dnf to install nmon:

$ sudo dnf -y install nmon
Copy after login
Copy after login

Now that you have installed nmon, use it to monitor your system!

以交互的方式使用 nmon

安装完应用程序后,您可以运行 nmon。之后您会看到一个包含系统信息和一个小帮助菜单的初始屏幕,它显示了您可以按哪些键来切换不同的指标:

$ nmon

┌─16k──[H for help]──Hostname=f35ks01─Refresh= 2secs ─18:04:27────┐
│                                                                 │
│------------------------------                                   │
│ _ __  _ __ ___   ___  _ __    For help type H or ...            │
│| '_ \| '_ ` _ \ / _ \| '_ \    nmon -?  - hint                  │
│| | | | | | | | | (_) | | | |   nmon -h  - full details          │
│|_| |_|_| |_| |_|\___/|_| |_|                                    │
│                               To stop nmon type q to Quit       │
│------------------------------                                   │
│                                                                 │
│Fedora release 35 (Thirty Five) VERSION="35 (Thirty Five)"       │
│Vendor=AuthenticAMD Model=AMD EPYC-Milan Processor               │
│MHz=1911.004 bogomips=3822.00     lscpu:CPU=4 Little Endian      │
│ProcessorChips=4 PhysicalCores=1        Sockets=4 Cores=1 Thrds=1│
│                 VirtualCPUs =4         MHz=0 max=0 min=0        │
│                                                                 │
│Use these keys to toggle statistics on/off:                      │
│  c = CPU         l = CPU Long-term     - = Faster screen updates│
│  C = " WideView  U = Utilisation       + = Slower screen updates│
│  m = Memory      V = Virtual memory    j = File Systems         │
│  d = Disks       n = Network           . = only busy disks/procs│
│  r = Resource    N = NFS               h = more options         │
│  k = Kernel      t = Top-processes     q = Quit                 │
│─────────────────────────────────────────────────────────────────│
Copy after login

例如,按 C 键会切换为 CPU 利用率显式:

┌─16k──[H for help]──Hostname=f35ks01─Refresh= 2secs ─18:05:58─┐
│ CPU Utilisation ─────────────────────────────────────────────│
│---------------------------+---------------------------------+│
│CPU User%  Sys% Wait%  Idle|0          |25   |50 |75   100|   │
│  1   3.6  17.2   0.0  79.2|Ussssssss>                    |   │
│  2   2.1   6.2   0.0  91.7|Usss    >                     |   │
│  3   0.0   1.1   0.0  98.9| >                            |   │
│  4   6.1  17.2   0.0   0.5|UUUssssssss>                  |   │
│---------------------------+---------------------------------+│
│Avg   3.0  10.3   0.0  86.7|Usssss>                       |   │
│---------------------------+---------------------------------+│
│──────────────────────────────────────────────────────────────│
Copy after login

N 键会切换为网络显示,并自动将其堆叠在 CPU 显示的下方:

┌─16k──[H for help]──Hostname=f35ks01─Refresh= 2secs ─18:17:01─┐
│ CPU Utilisation ─────────────────────────────────────────────│
│---------------------------+---------------------------------+│
│CPU User%  Sys% Wait%  Idle|0         |25    |50  |75  100|   │
│  1   2.6  10.3   0.0  87.1|Usssss     >                  |   │
│  2   0.0   2.2   0.0  97.8|s           >                 |   │
│  3   6.5  11.0   0.0  82.5|UUUsssss>                     |   │
│  4   0.0   0.5   0.0  99.5|               >              |   │
│---------------------------+---------------------------------+│
│Avg   2.4   5.9   0.0  91.6|Uss     >                     |   │
│---------------------------+---------------------------------+│
│ Network I/O ─────────────────────────────────────────────────│
│I/F Name Recv=KB/s Trans=KB/s p_in p_out in_sz out_sz Peak->..│
│      lo      0.0       0.0       0.0    0.0     0.0    0.0...│
│  enp1s0      0.0       0.0       0.5    0.0    52.0    0.0...│
│  enp7s0    218.6    5923.4    3335.5 9989.68   67.1  607.2...│
│ Network Error Counters ──────────────────────────────────────│
│I/F Name iErrors iDrop iOverrun iFrame oErrors   oDrop oOver..│
│      lo       0       0       0       0       0       0   ...│
│  enp1s0       0   34419       0       0       0       0   ...│
│  enp7s0       0   34419       0       0       0       0   ...│
│──────────────────────────────────────────────────────────────│
Copy after login

继续按下其他按键可在同一个视图中堆叠其他指标。例如,可使用 D 键来显式磁盘 I/O,可使用 M 键来显式内存信息,可使用 T 键来显式Top 进程

使用 nmon 来排查与监控 Linux 系统的性能

除了堆叠新显示外,您还可以按点(.)键仅显示繁忙的 CPU 和磁盘,也可以 + 键来增加或减少屏幕刷新时间。使用 H 键显示帮助。

由于您可以随时启用禁用不同的监视器和指标,所以 nmon 是一个可帮助您了解系统行为的高度灵活的工具。它还可以通过同时显示不同的指标来帮助解决问题,通过提供系统的整体视图以提高您理解问题的能力。

完成后,按 Q 键退出并结束监控会话。

收集性能数据

除了提供系统的整体交互式视图外,您还可以在批处理模式下运行 nmon,这样就可以从系统中收集长时间的性能数据以进行后验分析。

除了交互模式提供的实时指标外,此功能还允许您了解系统在较长时间内的行为。这是一项强大的功能,可以通过它进行容量规划,并有机会解决难以实时发现的零星问题。

要在批处理模式下运行 nmon,请在启动应用程序时使用标志 -f 参数(它必须是提供的第一个参数)。默认情况下,在一天内,nmon 每五分钟收集一次数据。您可以用参数 -s 来指定收集间隔和参数 -c 来指定快照数量。

例如,开启 nmon 批处理模式,我们想在一小时内,每分钟收集数据一次,同时包括捕获顶级进程 -t,执行以下命令:

$ nmon -f -s60 -c60 -t
Copy after login

您的终端提示符将会立即返回,因为当您执行此命令时应用程序正在后台运行。您可以接着使用 ps 命令,然后就可以看到它正在运行了:

$ ps -ef | grep nmon
ricardo 1849 1 0 12:27 pts/0 00:00:00 nmon -f -s60 -c60 -t
Copy after login

在批处理模式下,nmon 使用逗号分隔值 (Comma-Separated Value, CSV) 格式将收集的数据保存在当前目录中,其扩展名为 .nmon

$ ls -l
total 36
-rw-r--r--. 1 ricardo ricardo 36395 Feb 13 12:28 f35ks01_220213_1227.nmon
Copy after login

由于这是一个 CSV 文件,因此您可以使用其他 Linux 文本操作(命令行)工具对其进行处理,或者使用电子表格软件将其打开,以进行进一步分析。

$ head f35ks01_220213_1227.nmon
AAA,progname,nmon
AAA,command,nmon -f -s60 -c60 -t
AAA,version,16k
AAA,disks_per_line,150
AAA,max_disks,256,set by -d option
AAA,disks,10,
AAA,host,f35ks01
AAA,user,ricardo
AAA,OS,Linux,5.16.8-200.fc35.x86_64,#1 SMP PREEMPT Tue Feb 8 20:58:59 UTC 2022,x86_64
AAA,runname,f35ks01
Copy after login

除了这些方法之外,nmon 开发团队还提供了一个脚本,该脚本可将这些数据转换为 Web 浏览器能可视化查看的图表。接下来我们看看如何做到这一点。

生成图表

CSV 是一种灵活的格式,它允许您使用电子表格处理软件等不同的应用程序导出、分析和查看这些数据。为了使这个过程更加容易,nmon 的开发人员创造了 nmonchart。此 shell 脚本使用 Google Charts(JavaScript)将这些数据转换为 HTML,以使其在图形环境中可视化数据并与之交互。

若要使用 nmoncart,请从 nmon 主页下载:

$ curl -LO http://sourceforge.net/projects/nmon/files/nmonchart40.tar
Copy after login

然后使用 tar 来提取包的内容:

$ mkdir nmonchart

$ tar xvf nmonchart40.tar -C nmonchart
Copy after login

nmonchart 需要使用 ksh(Korn SHell),因此要创建图表,首先应使用 dnf 在您的系统中安装 ksh

$ sudo dnf install -y ksh
Copy after login

接下来,使用以下命令转换 .nmon 文件(该文件是在批处理模式下执行 nmon 时创建的):

$ ./nmonchart/nmonchart f35ks01_220213_1227.nmon 
Copy after login

该脚本创建一个与源 .nmon 文件同名的 HTML 文件。或者,您可以将目标文件名作为第二个参数传递给 nmonchart。这是一个例子:

$ ls -lg
total 4164
-rw-r--r--. 1 ricardo 113822 Feb 14 18:52 f35ks01_220213_1227.html
-rw-r--r--. 1 ricardo 93436 Feb 13 13:26 f35ks01_220213_1227.nmon
drwx------. 2 ricardo 4096 Aug 28 2020 nmonchart
Copy after login

您的系统图表现已准备就绪。您的下一步是查看它们。

推荐一个关于 bash shell 脚本的书籍————《Bash shell scripting cheat sheet》

可视化性能图表

nmonchart 为您提供的每个 .nmon 源文件创建一个 HTML 文件,其中嵌入了所需的图表 JavaScript 库。如果本地计算机上有 HTML 文件,请直接在 Web 浏览器中查看它。

您还可以使用 Web 服务器提供这些文件。我最喜欢的方法之一是在 Podman 容器中使用 Nginx,正如我在将 20 个单行 Linux 命令添加到您的工具箱中所建议的那样。

首先,创建一个目录并将 HTML 文件移到那里:

$ mkdir html

$ mv f35ks01_220213_1227.html html/
Copy after login

然后,在 8080 端口上使用 Podman 去服务来自于 Ngnix 的 html 目录中的内容:

$ podman run --rm -v ./html:/usr/share/nginx/html:ro,Z -p 8080:80 -d nginx
Copy after login

如果需要的话,请在服务器防火墙上打开 8080 端口以允许流量通过:

$ sudo firewall-cmd --add-port 8080/tcp 
Copy after login

然后,为了查看图标,请将您的浏览器指向您的 URL http://<your_server_ip_addr>:8080/f35ks01_220213_1227.html</your_server_ip_addr>

使用 nmon 来排查与监控 Linux 系统的性能

单击屏幕顶部的任何按钮以查看相应的图表(图表是交互式的)。您可以放大和缩小以查看更精细的数据,或将鼠标悬停在图表上以查看给定时间的详细数据。

总结

nmon 是一款灵活且功能强大的系统监控应用程序,它可帮助您更好地了解系统,并在出现问题时及时地对其进行故障排除。它结合了监控能力、整合来自不同系统资源的实时数据的能力,以及长期性能数据收集和分析的能力。这使其成为您的系统管理员的绝佳工具。


The above is the detailed content of Use nmon to troubleshoot and monitor Linux system performance. For more information, please follow other related articles on the PHP Chinese website!

source:lxlinux.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!