2018-06-08 (第十三课)系统性能监测实战

2018-06-11  本文已影响0人  chocolee911

目录

  1. 系统负载监测
    1.1 查看系统平均负载:wuptime
    1.2 查看 CPU 的硬件信息:/proc/cpuinfo
    1.3 查看更全面的系统负载信息:vmstat
    1.4 系统监控神器:sar
  2. 硬盘监测
    2.1 iostat
    2.2 iotop
  3. 内存监测
    3.1 free
  4. 进程监测
    4.1 静态地查看系统进程:ps
    4.2 动态排行系统进程:top
  5. 网络监测
    5.1 实时监控网卡流量查看:nload
    5.2 查看服务端口状态:netstat
  6. 网络抓包
    6.1 tcpdump
    6.2 tshark

1. 系统负载监测

1.1 查看系统平均负载:wuptime

命令很简单, 直接输入 w 或 uptime 即可, 不需要输入什么选项

1.1.1 w 显示信息解释

$ w
 12:13:37 up  1:43,  2 users,  load average: 0.00, 0.01, 0.05
USER     TTY        LOGIN@   IDLE   JCPU   PCPU WHAT
root     tty1      12:12    1:21   0.00s  0.00s -bash
root     pts/0     12:13    1.00s  0.02s  0.01s w

1.1.2 uptime显示信息解释(其实就是 w 命令显示的头信息)

$ uptime
 12:34:54 up 9 min,  1 user,  load average: 0.00, 0.01, 0.02

1.2 查看 CPU 的硬件信息:/proc/cpuinfo

$ cat /proc/cpuinfo
processor   : 0
vendor_id   : GenuineIntel
cpu family  : 6
model       : 60
model name  : Intel(R) Core(TM) i5-4570S CPU @ 2.90GHz
stepping    : 3
microcode   : 0x24
cpu MHz     : 2893.090
cache size  : 6144 KB
physical id : 0
siblings    : 1
core id     : 0
cpu cores   : 1
apicid      : 0
initial apicid  : 0
fpu     : yes
fpu_exception   : yes
cpuid level : 13
wp      : yes
bogomips    : 5786.18
clflush size    : 64
cache_alignment : 64
address sizes   : 43 bits physical, 48 bits virtual
power management:

processor   : 1
vendor_id   : GenuineIntel
cpu family  : 6
model       : 60
model name  : Intel(R) Core(TM) i5-4570S CPU @ 2.90GHz
stepping    : 3
microcode   : 0x24
cpu MHz     : 2893.090
cache size  : 6144 KB
physical id : 2
siblings    : 1
core id     : 0
cpu cores   : 1
apicid      : 2
initial apicid  : 2
fpu     : yes
fpu_exception   : yes
cpuid level : 13
wp      : yes
bogomips    : 5786.18
clflush size    : 64
cache_alignment : 64
address sizes   : 43 bits physical, 48 bits virtual
power management:

1.3 查看更全面的系统负载信息:vmstat

不需要什么复杂的选项或参数,直接查看结果即可

1.3.1 习惯用法

vmstat <frequency> <times>
如:vmstat 1 5:表示每 1 秒执行一次 vmstat,总共执行 5 次

1.3.2 vmstat 显示信息解释

$ vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 1  0      0 758876    692  77624    0    0    35     2   20   30  0  0 100  0  0

1.4 系统监控神器:sar

如需使用,提前安装 yum -y install sysstat
功能相当强大,可谓 Linux 下的瑞士军刀
由于 sar 会自动保存日志,所以往往用来查看历史的系统状态

1.4.1 sar 的日志

  • sar 一旦安装成功,便会每隔10分钟抓一次系统状态,放到 /var/log/sa/sa## 中,日志以 sa 加当天的日期命名,保留1个月,但是该日志为二进制文件,只能用 sar -f <log_name> 来进行查看
  • sar 运行到第二天时,会生成昨日的 /var/log/sa/sar## 日志文件,该文件可以直接使用 cat 查看

1.4.2 Options

1.4.3 使用方法

1. sar -n DEV 1 1查看网卡流量

$ sar -n DEV 1 1
Linux 3.10.0-123.el7.x86_64 (localhost.localdomain)     2018年06月11日     _x86_64_    (2 CPU)

13时56分28秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
13时56分29秒 eno16777736      0.00      0.00      0.00      0.00      0.00      0.00      0.00
13时56分29秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00

平均时间:     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
平均时间: eno16777736      0.00      0.00      0.00      0.00      0.00      0.00      0.00
平均时间:        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00

2. sar -q 1 1查看系统负载

其实也是主要看 ldavg-1、ldavg-5、ldavg-15,与 w 基本相同
$ sar -q -f /var/log/sa/sa11
Linux 3.10.0-123.el7.x86_64 (localhost.localdomain) 2018年06月11日 x86_64 (2 CPU)

14时00分01秒   runq-sz  plist-sz   ldavg-1   ldavg-5  ldavg-15   blocked
14时10分02秒         1       364      0.00      0.01      0.03         0
平均时间:         1       364      0.00      0.01      0.03         0

3 sar -b 1 1查看硬盘信息

$ sar -b 1 1
Linux 3.10.0-123.el7.x86_64 (localhost.localdomain)     2018年06月11日     _x86_64_    (2 CPU)

14时21分48秒       tps      rtps      wtps   bread/s   bwrtn/s
14时21分49秒      0.00      0.00      0.00      0.00      0.00
平均时间:      0.00      0.00      0.00      0.00      0.00

2. 硬盘监测

当通过观察系统负载时发现 block 或 wait 的进程较多时,需要更具体地查看硬盘的 io 信息

2.1 iostat

用法:iostat <frequency> <times>,与 vmstat、sar 类似

  1. 直接 iostat

查看的信息跟 sar -b看到的基本没差

$ iostat
Linux 3.10.0-123.el7.x86_64 (localhost.localdomain)     2018年06月11日     _x86_64_    (2 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
       0.04    0.00    0.14    0.02    0.00   99.80

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda               0.82        17.78         9.48     148921      79409
sdb               0.04         0.17         0.00       1449          0
  1. iostat -x

重点看%util,该字段大致含义为:在系统负载中, CPU 等待硬盘 IO 的次数的占比,若较大则硬盘性能已经成为系统瓶颈

$ iostat -x
Linux 3.10.0-123.el7.x86_64 (localhost.localdomain)     2018年06月11日     _x86_64_    (2 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
       0.04    0.00    0.14    0.02    0.00   99.80

Device:  rrqm/s   wrqm/s     r/s     w/s    rkB/s    wkB/s avgrq-sz avgqu-sz   await r_await w_await  svctm  %util
sda      0.07     0.02    0.59    0.24    17.78     9.48    66.24     0.00    1.88    1.50    2.81   0.76   0.06
sdb      0.13     0.00    0.04    0.00     0.17     0.00     8.09     0.00    0.13    0.13    0.00   0.13   0.00

2.2 iotop

如需使用,提前安装yum -y install iotop
与 top 命令类似,查看哪些进程占用 IO 比较狠

$ iotop
Total DISK READ :   0.00 B/s | Total DISK WRITE :       0.00 B/s
Actual DISK READ:   0.00 B/s | Actual DISK WRITE:       0.00 B/s
   TID  PRIO  USER     DISK READ  DISK WRITE  SWAPIN     IO>    COMMAND
     1 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % systemd --switched-root --system --deserialize 23
     2 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [kthreadd]
     3 be/4 root        0.00 B/s    0.00 B/s  0.00 %  0.00 % [ksoftirqd/0]

3. 内存监测

3.1 free

3.1.1 Options

3.1.2 示例

$ free -h
                       total       used       free     shared    buffers     cachde
Mem:                    979M       372M       607M       6.6M       692K       198M
-/+ buffers/cache:      173M       805M
Swap:                   2.0G         0B       2.0G

3.1.3 内存小常识

4. 进程监测

4.1 静态地查看系统进程:ps

何为静态?就是该命令仅借取某个时间点的 process 状态

4.1.1 Options

4.1.2 习惯用法

ps auxps -elf

4.1.3 显示信息解释

  1. 常用的 aux 参数显示信息
$ ps aux
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          2  0.0  0.0      0     0 ?        S    09:53   0:00 [kthreadd]
root          3  0.0  0.0      0     0 ?        S    09:53   0:00 [ksoftirqd/0]
root          5  0.0  0.0      0     0 ?        S<   09:53   0:00 [kworker/0:0H]
root          6  0.0  0.0      0     0 ?        S    09:53   0:00 [kworker/u256:0]
  1. -l能够显示更全面的信息
$ ps ax -l
F S   UID    PID   PPID  C PRI  NI ADDR SZ WCHAN  TTY        TIME CMD
1 S     0      2      0  0  80   0 -     0 kthrea ?          0:00 [kthreadd]
1 S     0      3      2  0  80   0 -     0 smpboo ?          0:00 [ksoftirqd/0]
1 S     0      5      2  0  60 -20 -     0 worker ?          0:00 [kworker/0:0H]

4.2 动态排行系统进程:top

很综合的一款工具,包含了 w、vmstat 中的信息,并能够动态地显示进程所占用的进程

4.2.1 Options

4.2.2 top显示信息解释

top - 13:31:21 up  1:06,  1 user,  load average: 0.00, 0.01, 0.03
Tasks: 346 total,   1 running, 345 sleeping,   0 stopped,   0 zombie
%Cpu(s):  0.0 us,  0.2 sy,  0.0 ni, 99.8 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem:   1003456 total,   246128 used,   757328 free,      692 buffers
KiB Swap:  2097148 total,        0 used,  2097148 free.    78060 cached Mem

   PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
     1 root      20   0   49864   6384   3708 S   0.0  0.6   0:01.73 systemd
     2 root      20   0       0      0      0 S   0.0  0.0   0:00.04 kthreadd
     3 root      20   0       0      0      0 S   0.0  0.0   0:00.00 ksoftirqd/0
     5 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   0:00.06 kworker/u256:0
     7 root      rt   0       0      0      0 S   0.0  0.0   0:00.56 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   0:00.00 rcuob/0
    10 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcuob/1
    11 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcuob/2
    12 root      20   0       0      0      0 S   0.0  0.0   0:00.00 rcuob/3

4.2.3 使用技巧

top - 13:37:31 up  1:12,  1 user,  load average: 0.00, 0.01, 0.03
Tasks: 346 total,   2 running, 344 sleeping,   0 stopped,   0 zombie
%Cpu0  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
%Cpu1  :  0.0 us,  0.0 sy,  0.0 ni,100.0 id,  0.0 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem:   1003456 total,   246240 used,   757216 free,      692 buffers
KiB Swap:  2097148 total,        0 used,  2097148 free.    78060 cached Mem

5. 网络监测

5.1 实时监控网卡流量查看:nload

若需使用,提前安装yum -y install nload

5.1.1 使用方式

5.1.2 示例

Device eno16777736 [172.16.166.254] (1/2):
===================================================================================
Incoming:

                                                      Curr: 1.02 kBit/s
                                                      Avg: 1.07 kBit/s
                                                      Min: 1.01 kBit/s
                                                      Max: 2.03 kBit/s
                                                      Ttl: 5.18 MByte
Outgoing:

                                                      Curr: 9.34 kBit/s
                                                      Avg: 9.23 kBit/s
                                                      Min: 3.89 kBit/s
                                                      Max: 9.86 kBit/s
                                                      Ttl: 1.24 MByte

5.2 查看服务端口状态:netstat

5.2.1 Options

5.2.2 习惯用法

netstat -lnp
netstat -lntp
netstat -an

5.2.3 显示信息解释

$ netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      886/sshd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1168/master
tcp6       0      0 :::22                   :::*                    LISTEN      886/sshd
tcp6       0      0 ::1:25                  :::*                    LISTEN      1168/master

5.2.4 技巧与注意事项

  1. 技巧 - 查看各种链接的总数

netstat -an | awk '/^tcp/ {++sta[$NF]} END {for (key in sta) print key,"\t",sta[key]}'

6. 网络抓包

6.1 tcpdump

若需使用,提前安装yum -y install tcpdump
Linux 下的抓包工具,类似 Win 下的 wireshark

6.1.1 Options

6.1.2 用法示例

6.1.3 注意事项

6.2 tshark

若需使用,提前安装yum -y install wireshark
其实这就是 WireShark 的 Linux 版本
在此不做过多介绍,仅记住一条命令

tshark -n -t a -R http.request -T fields\
-e "frame.time" -e "ip.src" -e "http.host"\
-e "http.request.method" -e "http.request.uri"
上一篇 下一篇

猜你喜欢

热点阅读