在我们使用Linux进行开发、运维时,难免会出现一些问题,而要解决这些问题我们就需要获取到系统当前的一些运行信息,来帮助我们快速锁定问题的原由,下面就介绍几个Linux常用的性能监控的命令。
全局监控
top
top 用于按一定的顺序显示所有正在运行而且处于活动状态的实时进程,而且会定期更新显示结果。这条命令显示了CPU的使用率、内存使用率、交换内存使用大小、高速缓存使用大小、缓冲区使用大小,进程PID、所使用命令以及其他。它还可以显示正在运行进程的内存和CPU占用多的情况。
[lee@VM-0-6-centos ~]$ top
top - 23:32:37 up 74 days, 13:49, 1 user, load average: 0.23, 0.44, 0.46
Tasks: 109 total, 1 running, 108 sleeping, 0 stopped, 0 zombie
%Cpu(s): 1.3 us, 0.3 sy, 0.0 ni, 98.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st
KiB Mem : 3880184 total, 151180 free, 1167056 used, 2561948 buff/cache
KiB Swap: 0 total, 0 free, 0 used. 2414408 avail Mem
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
15946 lee 20 0 2849664 484172 22256 S 2.0 12.5 3033:06 java
1087 root 20 0 50088 968 636 S 1.3 0.0 1438:53 rshim
3708 root 20 0 1026744 64760 11116 S 1.0 1.7 399:22.01 YDService
15922 root 20 0 205124 3144 1484 S 0.3 0.1 47:21.85 docker-proxy-cu
29931 root 20 0 742708 14424 2268 S 0.3 0.4 89:52.08 barad_agent
1 root 20 0 125600 4180 2648 S 0.0 0.1 7:15.25 systemd
2 root 20 0 0 0 0 S 0.0 0.0 0:05.53 kthreadd
4 root 0 -20 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0H
6 root 20 0 0 0 0 S 0.0 0.0 7:10.22 ksoftirqd/0
7 root rt 0 0 0 0 S 0.0 0.0 0:28.46 migration/0
8 root 20 0 0 0 0 S 0.0 0.0 0:00.00 rcu_bh
9 root 20 0 0 0 0 S 0.0 0.0 38:42.32 rcu_sched
CPU监控
uptime
uptime 主要用于获取当前时间、系统运行的时间、当前登录用户数,以及最近1、5、15分钟的CPU负载情况。
[root@VM-0-6-centos lee]
23:53:21 up 74 days, 14:10, 1 user, load average: 0.51, 0.27, 0.26
内存监控
free
free 主要用于监控内存与交换分区的使用情况。从左往右一次的参数为内存总量,已使用空间,未使用空间,共享内存,缓存,可用内存。
[lee@VM-0-6-centos ~]$ free
total used free shared buff/cache available
Mem: 3880184 1161644 149460 836 2569080 2419820
Swap: 0 0 0
vmstat
vmstat 命令是用于显示虚拟内存、内核线程、磁盘、系统进程、I/O 模块、中断、CPU 活跃状态等更多信息。
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
r b swpd free buff cache si so bi bo in cs us sy id wa st
1 1 0 124908 135168 2455520 0 0 0 15 2 2 2 1 97 0 0
磁盘监控
df
Linux中可以使用df 命令来监控当前硬盘的使用情况。从左到右的参数依次为磁盘名、磁盘大小、已使用空间、剩余可使用空间、使用率、挂载点。
[lee@VM-0-6-centos ~]$ df
Filesystem 1K-blocks Used Available Use% Mounted on
devtmpfs 1928476 0 1928476 0% /dev
tmpfs 1940092 24 1940068 1% /dev/shm
tmpfs 1940092 812 1939280 1% /run
tmpfs 1940092 0 1940092 0% /sys/fs/cgroup
/dev/vda1 51473868 10309776 38966848 21% /
tmpfs 388020 0 388020 0% /run/user/0
tmpfs 388020 0 388020 0% /run/user/1000
iostat
iostat 是一个用于收集显示系统存储设备输入和输出状态统计的简单工具。这个工具常常用来追踪存储设备的性能问题,其中存储设备包括设备、本地磁盘,以及诸如使用NFS等的远端磁盘。(第三方包sysstat 中)
[root@VM-0-6-centos lee]
Linux 3.10.0-1160.11.1.el7.x86_64 (VM-0-6-centos) 07/31/2021 _x86_64_ (2 CPU)
avg-cpu: %user %nice %system %iowait %steal %idle
2.19 0.00 1.19 0.06 0.00 96.56
Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn
vda 3.49 0.26 30.56 1660614 196986440
scd0 0.00 0.02 0.00 138978 0
网络监控
netstat
netstat 主要用于对网络信息进行监控,包含如网络连接、路由表、网络接口统计等信息,同时也可以查看当前正在运行的端口。
[lee@VM-0-6-centos ~]$ netstat
Active Internet connections (w/o servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 VM-0-6-centos:34626 172.17.0.2:wap-wsp ESTABLISHED
tcp 0 0 VM-0-6-centos:34644 172.17.0.2:wap-wsp ESTABLISHED
tcp 0 0 VM-0-6-centos:ssh mail.xegyptgroup.:51518 ESTABLISHED
tcp 0 0 VM-0-6-centos:58272 172.17.0.2:wap-wsp ESTABLISHED
tcp 0 0 VM-0-6-centos:ssh 121.4.180.253:47440 TIME_WAIT
tcp 0 52 VM-0-6-centos:ssh 61.49.248.105:58641 ESTABLISHED
tcp 0 0 VM-0-6-centos:34606 172.17.0.2:wap-wsp ESTABLISHED
tcp 0 1013 VM-0-6-centos:ssh 138.68.80.132:56406 ESTABLISHED
tcp 0 0 VM-0-6-centos:50170 169.254.0.4:http TIME_WAIT
tcp 0 0 VM-0-6-centos:ssh 61.49.248.105:58642 ESTABLISHED
tcp 0 0 VM-0-6-centos:42322 169.254.0.55:lsi-bobcat ESTABLISHED
tcp 0 0 VM-0-6-centos:ssh 220.85.233.146:48014 TIME_WAIT
tcp 0 0 VM-0-6-centos:34634 172.17.0.2:wap-wsp ESTABLISHED
tcp 0 0 VM-0-6-centos:34618 172.17.0.2:wap-wsp ESTABLISHED
tcp 0 0 VM-0-6-centos:54574 169.254.0.55:webcache TIME_WAIT
tcp 0 0 VM-0-6-centos:34632 172.17.0.2:wap-wsp ESTABLISHED
tcp 0 0 VM-0-6-centos:34600 172.17.0.2:wap-wsp ESTABLISHED
tcp6 0 0 172.21.0.6:wap-wsp 172.17.0.3:48028 ESTABLISHED
进程监控
ps
ps 主要用于查看进程的使用情况
[lee@VM-0-6-centos ~]$ ps -ef
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 May18 ? 00:07:15 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
root 2 0 0 May18 ? 00:00:05 [kthreadd]
root 4 2 0 May18 ? 00:00:00 [kworker/0:0H]
root 6 2 0 May18 ? 00:07:10 [ksoftirqd/0]
root 7 2 0 May18 ? 00:00:28 [migration/0]
root 8 2 0 May18 ? 00:00:00 [rcu_bh]
root 9 2 0 May18 ? 00:38:42 [rcu_sched]
root 10 2 0 May18 ? 00:00:00 [lru-add-drain]
lsof
lsof 主要以列表的形式显示打开的文件和进程,打开的文件主要包括磁盘文件、网络套接字、管道、设备和进程。
[lee@VM-0-6-centos ~]$ lsof
COMMAND PID TID USER FD TYPE DEVICE SIZE/OFF NODE NAME
systemd 1 root cwd unknown /proc/1/cwd (readlink: Permission denied)
systemd 1 root rtd unknown /proc/1/root (readlink: Permission denied)
systemd 1 root txt unknown /proc/1/exe (readlink: Permission denied)
systemd 1 root NOFD /proc/1/fd (opendir: Permission denied)
kthreadd 2 root cwd unknown /proc/2/cwd (readlink: Permission denied)
kthreadd 2 root rtd unknown /proc/2/root (readlink: Permission denied)
kthreadd 2 root txt unknown /proc/2/exe (readlink: Permission denied)
kthreadd 2 root NOFD /proc/2/fd (opendir: Permission denied)
kworker/0 4 root cwd unknown /proc/4/cwd (readlink: Permission denied)
|