Sysstat性能监控工具包中20个实用命令

作者: Kuldeep Sharma 译者: cvsher

| 2014-10-17 10:30:00   评论: 4

在我们上一篇文章中,我们已经学习了如何去安装和更新sysstat,并且了解了包中的一些实用工具。

今天,我们将会通过一些有趣的实例来学习mpstat, pidstat, iostatsar等工具,这些工具可以帮组我们找出系统中的问题。这些工具都包含了不同的选项,这意味着你可以根据不同的工作使用不同的选项,或者根据你的需求来自定义脚本。我们都知道,系统管理员都会有点懒,他们经常去寻找一些更简单的方法来完成他们的工作。

mpstat - 处理器统计信息

1.不带任何参数的使用mpstat命令将会输出所有CPU的平均统计信息

tecmint@tecmint ~ $ mpstat

Linux 3.11.0-23-generic (tecmint.com)   Thursday 04 September 2014  _i686_  (2 CPU)

12:23:57  IST  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
12:23:57  IST  all   37.35    0.01    4.72    2.96    0.00    0.07    0.00    0.00    0.00   54.88

2.使用‘-p’ (处理器编号)和‘ALL’参数将会从0开始独立的输出每个CPU的统计信息,0表示第一个cpu。

tecmint@tecmint ~ $ mpstat -P ALL

Linux 3.11.0-23-generic (tecmint.com)   Thursday 04 September 2014  _i686_  (2 CPU)

12:29:26  IST  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
12:29:26  IST  all   37.33    0.01    4.57    2.58    0.00    0.07    0.00    0.00    0.00   55.44
12:29:26  IST    0   37.90    0.01    4.96    2.62    0.00    0.03    0.00    0.00    0.00   54.48
12:29:26  IST    1   36.75    0.01    4.19    2.54    0.00    0.11    0.00    0.00    0.00   56.40

3.要进行‘N’次,平均每次间隔n秒的输出CPU统计信息,如下所示。

tecmint@tecmint ~ $ mpstat -P ALL 2 5

Linux 3.11.0-23-generic (tecmint.com)   Thursday 04 September 2014  _i686_  (2 CPU)

12:36:21  IST  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
12:36:23  IST  all   53.38    0.00    2.26    0.00    0.00    0.00    0.00    0.00    0.00   44.36
12:36:23  IST    0   46.23    0.00    1.51    0.00    0.00    0.00    0.00    0.00    0.00   52.26
12:36:23  IST    1   60.80    0.00    3.02    0.00    0.00    0.00    0.00    0.00    0.00   36.18

12:36:23  IST  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
12:36:25  IST  all   34.18    0.00    2.30    0.00    0.00    0.00    0.00    0.00    0.00   63.52
12:36:25  IST    0   31.63    0.00    1.53    0.00    0.00    0.00    0.00    0.00    0.00   66.84
12:36:25  IST    1   36.73    0.00    2.55    0.00    0.00    0.00    0.00    0.00    0.00   60.71

12:36:25  IST  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
12:36:27  IST  all   33.42    0.00    5.06    0.25    0.00    0.25    0.00    0.00    0.00   61.01
12:36:27  IST    0   34.34    0.00    4.04    0.00    0.00    0.00    0.00    0.00    0.00   61.62
12:36:27  IST    1   32.82    0.00    6.15    0.51    0.00    0.00    0.00    0.00    0.00   60.51

(LCTT译注: 上面命令中‘2’ 表示每2秒执行一次‘mpstat -P ALL’命令, ‘5’表示共执行5次)

4.使用‘I’参数将会输出每个处理器的中断统计信息

tecmint@tecmint ~ $ mpstat -I

Linux 3.11.0-23-generic (tecmint.com)   Thursday 04 September 2014  _i686_  (2 CPU)

12:39:56  IST  CPU    intr/s
12:39:56  IST  all    651.04

12:39:56  IST  CPU        0/s        1/s        6/s        8/s        9/s       12/s       16/s       17/s       20/s       21/s       22/s       23/s       45/s       46/s       47/s      NMI/s      LOC/s      SPU/s      PMI/s      IWI/s      RTR/s      RES/s      CAL/s      TLB/s      TRM/s      THR/s      MCE/s      MCP/s      ERR/s      MIS/s
12:39:56  IST    0      76.27       1.73       0.00       0.00       0.42       0.33       0.00       0.06      11.46       0.00       0.00       0.01       7.62       1.87       0.05       0.33     182.26       0.00       0.33       3.03       0.00      22.66       0.16       5.14       0.00       0.00       0.00       0.00       0.00       0.00
12:39:56  IST    1      70.88       1.44       0.00       0.00       0.41       0.33       0.00      27.91      10.33       0.00       0.00       0.01       7.27       1.79       0.05       0.32     184.11       0.00       0.32       5.17       0.00      22.09       0.13       4.73       0.00       0.00       0.00       0.00       0.00       0.00

12:39:56  IST  CPU       HI/s    TIMER/s   NET_TX/s   NET_RX/s    BLOCK/s BLOCK_IOPOLL/s  TASKLET/s    SCHED/s  HRTIMER/s      RCU/s
12:39:56  IST    0       0.00     116.49       0.05       0.27       7.33       0.00       1.22      10.44       0.13      37.47
12:39:56  IST    1       0.00     111.65       0.05       0.41       7.07       0.00      56.36       9.97       0.13      41.38

5.使用‘A’参数将会输出上面提到的所有信息,等同于‘-u -I All -p ALL’。

tecmint@tecmint ~ $ mpstat -A

Linux 3.11.0-23-generic (tecmint.com)   Thursday 04 September 2014  _i686_  (2 CPU)

12:41:39  IST  CPU    %usr   %nice    %sys %iowait    %irq   %soft  %steal  %guest  %gnice   %idle
12:41:39  IST  all   38.70    0.01    4.47    2.01    0.00    0.06    0.00    0.00    0.00   54.76
12:41:39  IST    0   39.15    0.01    4.82    2.05    0.00    0.02    0.00    0.00    0.00   53.95
12:41:39  IST    1   38.24    0.01    4.12    1.98    0.00    0.09    0.00    0.00    0.00   55.57

12:41:39  IST  CPU    intr/s
12:41:39  IST  all    651.73
12:41:39  IST    0    173.16
12:41:39  IST    1    225.89

12:41:39  IST  CPU        0/s        1/s        6/s        8/s        9/s       12/s       16/s       17/s       20/s       21/s       22/s       23/s       45/s       46/s       47/s      NMI/s      LOC/s      SPU/s      PMI/s      IWI/s      RTR/s      RES/s      CAL/s      TLB/s      TRM/s      THR/s      MCE/s      MCP/s      ERR/s      MIS/s
12:41:39  IST    0      76.04       1.77       0.00       0.00       0.41       0.36       0.00       0.06      11.60       0.00       0.00       0.01       7.42       1.83       0.05       0.34     182.89       0.00       0.34       2.97       0.00      22.69       0.16       5.22       0.00       0.00       0.00       0.00       0.00       0.00
12:41:39  IST    1      70.70       1.48       0.00       0.00       0.40       0.36       0.00      27.47      10.46       0.00       0.00       0.01       7.08       1.75       0.05       0.32     184.83       0.00       0.32       5.10       0.00      22.19       0.13       4.91       0.00       0.00       0.00       0.00       0.00       0.00

12:41:39  IST  CPU       HI/s    TIMER/s   NET_TX/s   NET_RX/s    BLOCK/s BLOCK_IOPOLL/s  TASKLET/s    SCHED/s  HRTIMER/s      RCU/s
12:41:39  IST    0       0.00     116.96       0.05       0.26       7.12       0.00       1.24      10.42       0.12      36.99
12:41:39  IST    1       0.00     112.25       0.05       0.40       6.88       0.00      55.05       9.93       0.13      41.20

pidstat - 进程和内核线程的统计信息

该命令是用于监控进程和当前受内核管理的线程。pidstat还可以检查子进程和线程的状态。

语法

# pidstat <OPTIONS> [INTERVAL] [COUNT]

6.不带任何参数使用pidstat将会输出所有活跃的任务。

tecmint@tecmint ~ $ pidstat

Linux 3.11.0-23-generic (tecmint.com)   Thursday 04 September 2014  _i686_  (2 CPU)

12:47:24  IST   UID       PID    %usr %system  %guest    %CPU   CPU  Command
12:47:24  IST     0         1    0.01    0.12    0.00    0.13     1  init
12:47:24  IST     0         3    0.00    0.01    0.00    0.01     0  ksoftirqd/0
12:47:24  IST     0         9    0.00    0.04    0.00    0.04     0  rcu_sched
12:47:24  IST     0        10    0.00    0.00    0.00    0.00     0  watchdog/0
12:47:24  IST     0        11    0.00    0.00    0.00    0.00     1  watchdog/1
12:47:24  IST     0        12    0.00    0.00    0.00    0.00     1  migration/1
12:47:24  IST     0        13    0.00    0.01    0.00    0.01     1  ksoftirqd/1
12:47:24  IST     0        23    0.00    0.00    0.00    0.00     0  kworker/u9:0
12:47:24  IST     0        29    0.00    0.61    0.00    0.61     0  kworker/0:1
12:47:24  IST     0        30    0.00    0.06    0.00    0.06     1  kworker/1:1
12:47:24  IST     0       224    0.00    0.01    0.00    0.01     1  jbd2/sda1-8
12:47:24  IST     0       360    0.00    0.00    0.00    0.00     1  upstart-udev-br
12:47:24  IST     0       365    0.01    0.00    0.00    0.01     0  systemd-udevd
12:47:24  IST     0       476    0.00    0.00    0.00    0.00     0  kworker/u9:1

7.使用‘-p’(进程)参数输出所有活跃和非活跃的任务。

tecmint@tecmint ~ $ pidstat -p ALL

Linux 3.11.0-23-generic (tecmint.com)   Thursday 04 September 2014  _i686_  (2 CPU)

12:51:55  IST   UID       PID    %usr %system  %guest    %CPU   CPU  Command
12:51:55  IST     0         1    0.01    0.11    0.00    0.12     1  init
12:51:55  IST     0         2    0.00    0.00    0.00    0.00     0  kthreadd
12:51:55  IST     0         3    0.00    0.01    0.00    0.01     0  ksoftirqd/0
12:51:55  IST     0         5    0.00    0.00    0.00    0.00     0  kworker/0:0H
12:51:55  IST     0         7    0.00    0.00    0.00    0.00     0  migration/0
12:51:55  IST     0         8    0.00    0.00    0.00    0.00     0  rcu_bh
12:51:55  IST     0         9    0.00    0.04    0.00    0.04     1  rcu_sched
12:51:55  IST     0        10    0.00    0.00    0.00    0.00     0  watchdog/0
12:51:55  IST     0        11    0.00    0.00    0.00    0.00     1  watchdog/1
12:51:55  IST     0        12    0.00    0.00    0.00    0.00     1  migration/1
12:51:55  IST     0        13    0.00    0.01    0.00    0.01     1  ksoftirqd/1
12:51:55  IST     0        15    0.00    0.00    0.00    0.00     1  kworker/1:0H
12:51:55  IST     0        16    0.00    0.00    0.00    0.00     1  khelper
12:51:55  IST     0        17    0.00    0.00    0.00    0.00     0  kdevtmpfs
12:51:55  IST     0        18    0.00    0.00    0.00    0.00     0  netns
12:51:55  IST     0        19    0.00    0.00    0.00    0.00     0  writeback
12:51:55  IST     0        20    0.00    0.00    0.00    0.00     1  kintegrityd

8.使用‘-d 2’参数,我们可以看到I/O统计信息,2表示以秒为单位对统计信息进行刷新。这个参数可以方便的知道当系统在进行繁重的I/O时,那些进行占用大量的资源的进程。

tecmint@tecmint ~ $ pidstat -d 2

Linux 3.11.0-23-generic (tecmint.com)   Thursday 04 September 2014  _i686_  (2 CPU)

03:26:53  EDT       PID   kB_rd/s   kB_wr/s kB_ccwr/s  Command

03:26:55  EDT       PID   kB_rd/s   kB_wr/s kB_ccwr/s  Command
03:26:57  EDT       574      0.00    148.00      2.00  miniserv.pl

03:27:01  EDT       PID   kB_rd/s   kB_wr/s kB_ccwr/s  Command
03:27:03  EDT         1      0.00      8.00      2.00  init
03:27:03  EDT       450      0.00      2.00      0.00  rsyslogd
03:27:03  EDT       534    138.00     10.00      4.00  crond
03:27:03  EDT     25100      0.00      6.00      0.00  sendmail
03:27:03  EDT     30829      0.00      6.00      0.00  java

9.想要每间隔2秒对进程4164的cpu统计信息输出3次,则使用如下带参数‘-t’(输出某个选定进程的统计信息)的命令。

tecmint@tecmint ~ $ pidstat -t -p 4164 2 3

Linux 3.11.0-23-generic (tecmint.com)   Thursday 04 September 2014  _i686_  (2 CPU)

01:09:06  IST   UID      TGID       TID    %usr %system  %guest    %CPU   CPU  Command
01:09:08  IST  1000      4164         -   22.00    1.00    0.00   23.00     1  firefox
01:09:08  IST  1000         -      4164   20.00    0.50    0.00   20.50     1  |__firefox
01:09:08  IST  1000         -      4171    0.00    0.00    0.00    0.00     0  |__Gecko_IOThread
01:09:08  IST  1000         -      4172    0.00    0.00    0.00    0.00     0  |__Socket
01:09:08  IST  1000         -      4173    0.00    0.00    0.00    0.00     0  |__JS
01:09:08  IST  1000         -      4174    0.00    0.00    0.00    0.00     0  |__JS
01:09:08  IST  1000         -      4175    0.00    0.00    0.00    0.00     0  |__Hang
01:09:08  IST  1000         -      4176    0.00    0.00    0.00    0.00     1  |__gdbus
01:09:08  IST  1000         -      4177    0.00    0.00    0.00    0.00     1  |__gmain

10.使用‘-rh’参数,将会输出进程的内存使用情况。如下命令每隔2秒刷新经常的内存使用情况。

tecmint@tecmint ~ $ pidstat -rh 2 3

Linux 3.11.0-23-generic (tecmint.com)   Thursday 04 September 2014  _i686_  (2 CPU)

#      Time   UID       PID  minflt/s  majflt/s     VSZ    RSS   %MEM  Command
 1409816695  1000      3958   3378.22      0.00  707420 215972   5.32  cinnamon
 1409816695  1000      4164    406.93      0.00 1252024 461404  11.36  firefox
 1409816695  1000      6676    168.81      0.00    4436    984   0.02  pidstat

#      Time   UID       PID  minflt/s  majflt/s     VSZ    RSS   %MEM  Command
 1409816697     0      1601    644.00      0.00  506728 316788   7.80  Xorg
 1409816697  1000      3958   3412.00      0.00  707420 215972   5.32  cinnamon
 1409816697  1000      4164   2667.00      0.00 1259576 471724  11.62  firefox
 1409816697  1000      6676    172.50      0.00    4436   1020   0.03  pidstat

#      Time   UID       PID  minflt/s  majflt/s     VSZ    RSS   %MEM  Command
 1409816699     0      1601    644.00      0.00  506728 316788   7.80  Xorg
 1409816699  1000      3958   4094.00      0.00  710148 218700   5.39  cinnamon
 1409816699  1000      4164    599.00      0.00 1261944 476664  11.74  firefox
 1409816699  1000      6676    168.00      0.00    4436   1020   0.03  pidstat

11.要使用‘-G’参数可以输出包含某个特定字符串的进程信息。如下命令输出所有包含‘VB’字符串的进程的统计信息,使用‘-t’参数将线程的信息也进行输出。

tecmint@tecmint ~ $ pidstat -G VB

Linux 3.11.0-23-generic (tecmint.com)   Thursday 04 September 2014  _i686_  (2 CPU)

01:09:06  IST   UID      PID      %usr  %system  %guest    %CPU   CPU   Command
01:09:08  IST  1000    1492     22.00     1.00       0.00        23.00     1        VBoxService
01:09:08  IST  1000    1902     4164      20.00      0.50        0.00       20.50       VBoxClient
01:09:08  IST  1000    1922     4171      0.00       0.00        0.00       0.00        VBoxClient


最新评论

骑车去首都  2015-03-18 14:04
好强大的工具
netb2c  2014-10-19 19:37
另:ubuntu 通过软件源安装 sa1/sa2 位置 在 /usr/lib/sysstat 下面
netb2c  2014-10-19 19:34
感谢LCTT,感谢楼主分享... 学到不少!
微博评论  2014-10-17 12:33
[good]

友情链接
返回顶部