每个系统管理员应该知道的10个监控工具

10 Linux System Monitoring Tools Every SysAdmin Should Know


这是一篇关于检测工具该如何使用的文章

welcome to my blog. This is an article about how monitoring tools should be used.



可莉会监督你好好学习

适用于但不仅限于这些系统 [没写那么多其实是因为懒 _]

ID sysctem
1 Centos
2 Debian
3 Ubuntu

1 如何查看活动进程呢?

top - 进程活动监视命令

Process activity monitoring command

top命令显示Linux进程信息。它提供了运行系统的动态实时视图,即实际的流程活动。默认情况下,它显示服务器上运行的cpu密集型任务最多的任务,并每5秒更新一次列表,还可以键入enter进行查看,键入Ctrl + C 进行关闭。

如下图



常用的top命令及其需要添加的参数

参考下表

Key Usage
t 关闭和打开显示摘要信息
m 关闭和打开显示内存信息
A 根据各种系统资源的顶级使用者对显示进行排序。用于快速识别系统上需要性能的任务
f 进入top的交互式配置屏幕。有助于为特定的任务设置顶部
o 使您能够以交互方式选择top中的顺序
r 发出renice命令[renice:重新指定一个或多个进程的优先级]
k 发出kill命令
z 打开或关闭颜色/单色

2 Linux如何查找CPU利用率?

How do I Find Out Linux CPU Utilization?

vmstat 命令报告有关进程、内存、分页、块IO、trap和cpu活动的信息

The vmstat command reports information about processes, memory, paging, block IO, traps, and cpu activity.
vmstat - 虚拟内存统计信息

1
# vmstat 3

参考下图

1
2
3
4
5
6
7
8
9
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
r b swpd free buff cache si so bi bo in cs us sy id wa st
0 0 0 2540988 522188 5130400 0 0 2 32 4 2 4 1 96 0 0
1 0 0 2540988 522188 5130400 0 0 0 720 1199 665 1 0 99 0 0
0 0 0 2540956 522188 5130400 0 0 0 0 1151 1569 4 1 95 0 0
0 0 0 2540956 522188 5130500 0 0 0 6 1117 439 1 0 99 0 0
0 0 0 2540940 522188 5130512 0 0 0 536 1189 932 1 0 98 0 0
0 0 0 2538444 522188 5130588 0 0 0 0 1187 1417 4 1 96 0 0
0 0 0 2490060 522188 5130640 0 0 0 18 1253 1123 5 1 94 0 0

显示内存利用率Slabinfo

Display Memory Utilization Slabinfo

1
vmstat -m

获取有关活动/非活动内存页的信息

Get Information About Active / Inactive Memory Pages

1
vmstat -a

如何查找Linux资源利用率以检测系统瓶颈?

Linux查找哪些进程正在使用交换空间

可以使用 smem 命令

1
smem

另一种选择是将pgrep command与grep命令结合使用,以查找SWAP mem的使用情况:

1
2
pgrep memcached
grep --color VmSwap /proc/48440/status



Linux查找哪些进程正在使用交换空间


3 w - 查找谁登录了系统以及他们在做什么

We use the w command displays information about the users currently on the machine, and their processes.

我们可以使用 w 命令显示当前机器上的用户,及其进程信息

1
2
# w username
# w vivek

4 uptime - 告诉我们Linux系统已经运行了多长时间。我们使用uptime命令来查看服务器已经运行了多长时间。当前时间、系统运行了多长时间、当前有多少用户登录,以及过去1分钟、5分钟和15分钟的系统负载平均值。

1
# uptime

5 ps -显示Linux进程使用ps命令报告Linux下当前进程的快照。要选择所有的进程,通过下面的-A或-e选项

1
# ps -A

命令操作图如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 PID TTY          TIME CMD
1 ? 00:00:02 init
2 ? 00:00:02 migration/0
3 ? 00:00:01 ksoftirqd/0
4 ? 00:00:00 watchdog/0
5 ? 00:00:00 migration/1
6 ? 00:00:15 ksoftirqd/1
....
.....
4881 ? 00:53:28 java
4885 tty1 00:00:00 mingetty
4886 tty2 00:00:00 mingetty
4887 tty3 00:00:00 mingetty
4888 tty4 00:00:00 mingetty
4891 tty5 00:00:00 mingetty
4892 tty6 00:00:00 mingetty
4893 ttyS1 00:00:00 agetty
12853 ? 00:00:00 cifsoplockd
12854 ? 00:00:00 cifsdnotifyd
14231 ? 00:10:34 lighttpd
14232 ? 00:00:00 php-cgi
54981 pts/0 00:00:00 vim
55465 ? 00:00:00 php-cgi
55546 ? 00:00:00 bind9-snmp-stat
55704 pts/1 00:00:00 ps
注意,ps命令就像top命令,可以提供多种信息,但是需要添加参数。

命令示范:

显示长格式输出

Show Long Format Output

1
# ps -Al

要打开完整模式(它将显示传递给进程的命令行参数):

1
# ps -Alf

显示线程(LWP和NLWP)

#LWP 用户级线程和内核级线程之间的中间层 #NLWP 轻量线程

1
# ps -AlFH

监视进程后的线程

1
# ps -AlLm

打印服务器上的所有进程

1
2
# ps ax
# ps axi

打印进程树

1
2
3
# ps -ejH
# ps axjf
# pstree

获取Linux进程的安全信息

1
2
3
# ps -eo euser,ruser,suser,fuser,f,comm,label
# ps axZ
# ps -eM

让我们打印每个以Vivek用户运行的进程

1
ps -U vivek -u vivek u

配置ps自定义格式的命令输出信息

1
2
3
# ps -eo pid,tid,class,rtprio,ni,pri,psr,pcpu,stat,wchan:14,comm
# ps axo stat,euid,ruid,tty,tpgid,sess,pgrp,ppid,pid,pcpu,comm
# ps -eopid,tt,user,fname,tmout,f,wchan

只显示httpd的进程号

1
2
3
4
5
# ps -C lighttpd -o pid=

$ or

# pgrep -u vivek php-cgi

打印进程55977的名称

1
# ps -p 55977 -o comm=

显示前10个内存消耗进程

1
# ps -auxf | sort -nr -k 4 | head -10

显示前10个CPU消耗进程

1
# ps -auxf | sort -nr -k 3 | head -10

6 free -显示Linux服务器内存使用情况

free命令显示系统中空闲和使用的物理和交换内存的总量,以及内核使用的缓冲区

The free command shows the total amount of free and used physical and swap memory in the system, as well as the buffers used by the kernel.

1
# free

服务器示例:

1
2
3
4
            total       used       free     shared    buffers     cached
Mem: 12302896 9739664 2563232 0 523124 5154740
-/+ buffers/cache: 4061800 8241096
Swap: 1052248 0 1052248

7 iostat—监视Linux平均CPU负载和磁盘活动

我们使用iostat命令报告Linux操作系统下的设备、分区和网络文件系统(NFS)的中央处理单元(CPU)统计数据和输入/输出统计数据。

1
iostat

服务器示例:

1
2
3
4
5
6
7
8
9
10
Linux 2.6.18-128.1.14.el5 (www03.nixcraft.in) 	06/26/2009

avg-cpu: %user %nice %system %iowait %steal %idle
3.50 0.09 0.51 0.03 0.00 95.86

Device: tps Blk_read/s Blk_wrtn/s Blk_read Blk_wrtn
sda 22.04 31.88 512.03 16193351 260102868
sda1 0.00 0.00 0.00 2166 180
sda2 22.04 31.87 512.03 16189010 260102688
sda3 0.00 0.00 0.00 1615 0

8 sar -命令收集和报告Linux系统活动
sar 命令用于收集、上报和保存系统活动信息。要查看网络日志,输入:

1
# sar -n DEV | more

查看24日起日志:

1
# sar -n DEV -f /var/log/sa/sa24 | more

你也可以使用sar显示实时使用情况:

1
# sar 4 5

服务器示例:

1
2
3
4
5
6
7
8
9
Linux 2.6.18-128.1.14.el5 (www03.nixcraft.in) 		06/26/2022

06:45:12 PM CPU %user %nice %system %iowait %steal %idle
06:45:16 PM all 2.00 0.00 0.22 0.00 0.00 97.78
06:45:20 PM all 2.07 0.00 0.38 0.03 0.00 97.52
06:45:24 PM all 0.94 0.00 0.28 0.00 0.00 98.78
06:45:28 PM all 1.56 0.00 0.22 0.00 0.00 98.22
06:45:32 PM all 3.53 0.00 0.25 0.03 0.00 96.19
Average: all 2.02 0.00 0.27 0.01 0.00 97.70

9 mpstat -监视Linux上的多处理器使用情况

Mpstat命令显示每个可用处理器的活动,处理器0是第一个。mpstat -P ALL显示每个处理器的平均CPU利用率:

1
mpstat -P ALL`

服务器示例:

1
2
3
4
5
6
7
8
9
10
11
12
Linux 2.6.18-128.1.14.el5 (www03.nixcraft.in)	 	06/26/2022

06:48:11 PM CPU %user %nice %sys %iowait %irq %soft %steal %idle intr/s
06:48:11 PM all 3.50 0.09 0.34 0.03 0.01 0.17 0.00 95.86 1218.04
06:48:11 PM 0 3.44 0.08 0.31 0.02 0.00 0.12 0.00 96.04 1000.31
06:48:11 PM 1 3.10 0.08 0.32 0.09 0.02 0.11 0.00 96.28 34.93
06:48:11 PM 2 4.16 0.11 0.36 0.02 0.00 0.11 0.00 95.25 0.00
06:48:11 PM 3 3.77 0.11 0.38 0.03 0.01 0.24 0.00 95.46 44.80
06:48:11 PM 4 2.96 0.07 0.29 0.04 0.02 0.10 0.00 96.52 25.91
06:48:11 PM 5 3.26 0.08 0.28 0.03 0.01 0.10 0.00 96.23 14.98
06:48:11 PM 6 4.00 0.10 0.34 0.01 0.00 0.13 0.00 95.42 3.75
06:48:11 PM 7 3.30 0.11 0.39 0.03 0.01 0.46 0.00 95.69 76.89

10 pmap—监视Linux上的进程内存使用情况

Pmap命令报告进程的内存映射。使用此命令查找内存瓶颈的原因。

1
pmap -d PID  

要显示pid 47394的进程内存信息,输入:

1
pmap -d 47394

服务器示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
47394:   /usr/bin/php-cgi
Address Kbytes Mode Offset Device Mapping
0000000000400000 2584 r-x-- 0000000000000000 008:00002 php-cgi
0000000000886000 140 rw--- 0000000000286000 008:00002 php-cgi
00000000008a9000 52 rw--- 00000000008a9000 000:00000 [ anon ]
0000000000aa8000 76 rw--- 00000000002a8000 008:00002 php-cgi
000000000f678000 1980 rw--- 000000000f678000 000:00000 [ anon ]
000000314a600000 112 r-x-- 0000000000000000 008:00002 ld-2.5.so
000000314a81b000 4 r---- 000000000001b000 008:00002 ld-2.5.so
000000314a81c000 4 rw--- 000000000001c000 008:00002 ld-2.5.so
000000314aa00000 1328 r-x-- 0000000000000000 008:00002 libc-2.5.so
000000314ab4c000 2048 ----- 000000000014c000 008:00002 libc-2.5.so
.....
......
..
00002af8d48fd000 4 rw--- 0000000000006000 008:00002 xsl.so
00002af8d490c000 40 r-x-- 0000000000000000 008:00002 libnss_files-2.5.so
00002af8d4916000 2044 ----- 000000000000a000 008:00002 libnss_files-2.5.so
00002af8d4b15000 4 r---- 0000000000009000 008:00002 libnss_files-2.5.so
00002af8d4b16000 4 rw--- 000000000000a000 008:00002 libnss_files-2.5.so
00002af8d4b17000 768000 rw-s- 0000000000000000 000:00009 zero (deleted)
00007fffc95fe000 84 rw--- 00007ffffffea000 000:00000 [ stack ]
ffffffffff600000 8192 ----- 0000000000000000 000:00000 [ anon ]
mapped: 933712K writeable/private: 4304K shared: 768000K