5.5-9监控Linux系统状态5-9

vmstat命令监控系统状态-综合工具、判定瓶颈点

监控网卡流量

查看进程

查看网络链接情况

抓包工具

vmstat命令监控系统状态-综合工具、判定瓶颈点

[root@www ~]# vmstat
procs -----------memory---------- ---swap-- -----io---- -system-- ------cpu-----
 r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
 2  0      0 557116   2076 304764    0    0    50    18   41   89  0  1 98  1  0

使用w命令查看的是系统总体上的负载,没法判断具体是哪些硬件负载压力过大,这就用到了vmstat命令,vmstat显示的内容一共分为六部分php

  • procs显示进程的相关信息
  • r(run):表示运行或等待CPU时间片的进程数。该数值若是长期大于CPU核数就意味着CPU不够用了
  • b(block):表示等待资源的进程数,这里的资源指的是I/O、内存等。好比说有个数据CPU很快处理完了,可是硬盘在频繁的读写当中,该数据等待写入硬盘时,这个进程就是b的状态。该数据长时间大于1就须要找找问题了。
  • memory显示内存的相关信息
  • swpd:表示切换到交换分区中的内存数量,单位为KB
  • free:表示当前空闲的内存大小,单位KB
  • buff:表示(即将写入磁盘的)缓冲大小,单位KB
  • cache:表示(从磁盘中读取的)缓存大小,单位KB
  • swap显示内存的交换状况
  • si:表示由交换分区写内存的数据量,单位KB
  • so:表示由内存写入交换分区的数据量
  • io显示磁盘的使用状况
  • bi:表示从块设备读取数据的量(读磁盘)
  • bo:表示从块设备写入数据的量(写磁盘)
  • system显示采集间隔内发生的中断次数
  • in:表示在某一时间间隔内观测到的每秒设备的中断次数
  • cs:表示每秒产生的上下文切换次数
  • CPU显示cpu的使用状态
  • us:显示用户下所花费CPU的时间百分比
  • sy:显示系统花费CPU的时间百分比
  • id:表示CPU处于空闲状态的时间百分比
  • wa:表示I/O等待所占用的CPU时间百分比
  • st:表示被偷走的CPU所占的百分比(通常为0,不用关注)
  • vmstat 2 10 意思是每两秒输出一次状态,输出10次

监控网卡流量

  • netstat列出端口信息
  • netstat -lnp列出系统监听端口
  • netstat -an查看系统的网络链接情况
  • netstat -lntp 列出监听端口,只看tcp的,不包括socket
  • ss -an 跟netstat -an做用同样
[root@www ~]# netstat -lntp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 127.0.0.1:9000          0.0.0.0:*               LISTEN      6017/php-fpm: maste 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      5913/sshd           
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      6014/master         
tcp6       0      0 :::22                   :::*                    LISTEN      5913/sshd           
tcp6       0      0 ::1:25                  :::*                    LISTEN      6014/master

sar命令

  • sar命令是Linux系统中特别强大的一个命令,经过sar命令能够全面获取系统的CPU、运行队列、磁盘I/O、交换区、内存、cpu中断、网络等性能数据
  • sar命令和iostat命令都是由sysstat安装包安装的

sar命令行的经常使用格式:

  • sar [options] [-A] [-o file] t [n] 在命令行中, n 和 t 两个参数组合起来定义采样间隔和次数, t 为采样间隔, 是必须有的参数, n 为采样次数,是可选的,默认值是 1, -o file 表示将命令结果以二进制格式存放在文件中, file 在此处不是关键字,是文件名。 options 为命令行选项:
  • -A:全部报告的总和
  • -u:CPU利用率
  • -v:进程、节点、文件和锁表状态
  • -p:当前系统中指定CPU使用信息
  • -d:硬盘使用报告
  • -r:显示系统内存的使用状况
  • -n:显示网络运行状态。参数后面可跟DEV、EDEV、SOCK和FULL。DEV显示网络接口信息,EDEV显示网络错误的统计数据,SOCK显示套接字信息,FULL显示前三参数全部信息
  • -q:显示运行队列的大小,它与系统当前的平均负载有关
  • -B:内存分页状况
  • -R:显示进程在采样时间内的活动状况
  • -g:串口I/O的状况
  • -b:缓冲区使用状况
  • -a:文件读写状况
  • -c:系统调用状况
  • -R:进程的活动状况
  • -y:终端设备活动状况
  • -W:系统交换活动

使用sar命令查看网卡流量状况,下面这个状况是查看网卡流量状况,一秒读取一次,一共读取两次

[root@www ~]# sar -n DEV 1 2
Linux 3.10.0-957.el7.x86_64 (www.aaa.com) 	2019年03月09日 	_x86_64_	(1 CPU)

00时56分26秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
00时56分27秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
00时56分27秒     ens33      0.00      0.00      0.00      0.00      0.00      0.00      0.00

00时56分27秒     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
00时56分28秒        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
00时56分28秒     ens33      1.03      1.03      0.06      0.39      0.00      0.00      0.00

平均时间:     IFACE   rxpck/s   txpck/s    rxkB/s    txkB/s   rxcmp/s   txcmp/s  rxmcst/s
平均时间:        lo      0.00      0.00      0.00      0.00      0.00      0.00      0.00
平均时间:     ens33      0.51      0.51      0.03      0.19      0.00      0.00      0.00
  • 上面输出结果中,IFACE是网卡名字
  • rxpck/s是网卡每秒接收数据包的数量
  • txpck/s是网卡每秒发送数据包的数量
  • rxkB/s是网卡每秒接收数据的大小
  • txkB/s是网卡每秒发送数据的大小
  • 其他内容不用太关注

nload命令

  • nload命令是动态查看网卡流量状况
  • nload安装包是由扩展源提供的,须要先安装扩展源,yum install -y epel-release而后再安装yum install nload -y
  • 直接运行nload就能够查看网卡流量,按上下箭头能够切换不一样的网卡
  • 或者使用nload eth0查看指定网卡流量

ethtool和mii-tool命令

  • ethtool eth0这个命令查看网卡的信息,以及有无网线链接
  • mii-tool eth0查看网口有无网线链接
[root@www ~]# ethtool ens33
Settings for ens33:
	Supported ports: [ TP ]
	Supported link modes:   10baseT/Half 10baseT/Full 
	                        100baseT/Half 100baseT/Full 
	                        1000baseT/Full 
	Supported pause frame use: No
	Supports auto-negotiation: Yes
	Supported FEC modes: Not reported
	Advertised link modes:  10baseT/Half 10baseT/Full 
	                        100baseT/Half 100baseT/Full 
	                        1000baseT/Full 
	Advertised pause frame use: No
	Advertised auto-negotiation: Yes
	Advertised FEC modes: Not reported
	Speed: 1000Mb/s
	Duplex: Full
	Port: Twisted Pair
	PHYAD: 0
	Transceiver: internal
	Auto-negotiation: on
	MDI-X: off (auto)
	Supports Wake-on: d
	Wake-on: d
	Current message level: 0x00000007 (7)
			       drv probe link
	Link detected: yes
[root@www ~]# mii-tool ens33
ens33: negotiated 1000baseT-FD flow-control, link ok

查看进程

ps命令

  • 直接运行ps命令是查看当前终端下的进程
[root@www ~]# ps
  PID TTY          TIME CMD
 6202 pts/0    00:00:00 bash
 8173 pts/0    00:00:00 ps
  • ps-aux查看系统中全部用户的进程
[root@www ~]# ps -aux
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.6 128040  6620 ?        Ss   3月08   0:03 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
root         2  0.0  0.0      0     0 ?        S    3月08   0:00 [kthreadd]
root         3  0.0  0.0      0     0 ?        S    3月08   0:00 [ksoftirqd/0]
root         5  0.0  0.0      0     0 ?        S<   3月08   0:00 [kworker/0:0H]
root         7  0.0  0.0      0     0 ?        S    3月08   0:00 [migration/0]
root         8  0.0  0.0      0     0 ?        S    3月08   0:00 [rcu_bh]
root         9  0.0  0.0      0     0 ?        R    3月08   0:02 [rcu_sched]
root        10  0.0  0.0      0     0 ?        S<   3月08   0:00 [lru-add-drain]
root        11  0.0  0.0      0     0 ?        S    3月08   0:00 [watchdog/0]
root        13  0.0  0.0      0     0 ?        S    3月08   0:00 [kdevtmpfs]
root        14  0.0  0.0      0     0 ?        S<   3月08   0:00 [netns]
root        15  0.0  0.0      0     0 ?        S    3月08   0:00 [khungtaskd]
root        16  0.0  0.0      0     0 ?        S<   3月08   0:00 [writeback]
root        17  0.0  0.0      0     0 ?        S<   3月08   0:00 [kintegrityd]
root        18  0.0  0.0      0     0 ?        S<   3月08   0:00 [bioset]
root        19  0.0  0.0      0     0 ?        S<   3月08   0:00 [bioset]
root        20  0.0  0.0      0     0 ?        S<   3月08   0:00 [bioset]
root        21  0.0  0.0      0     0 ?        S<   3月08   0:00 [kblockd]
root        22  0.0  0.0      0     0 ?        S<   3月08   0:00 [md]
root        23  0.0  0.0      0     0 ?        S<   3月08   0:00 [edac-poller]
root        24  0.0  0.0      0     0 ?        S<   3月08   0:00 [watchdogd]
root        30  0.0  0.0      0     0 ?        S    3月08   0:00 [kswapd0]
root        31  0.0  0.0      0     0 ?        SN   3月08   0:00 [ksmd]
root        32  0.0  0.0      0     0 ?        SN   3月08   0:00 [khugepaged]
root        33  0.0  0.0      0     0 ?        S<   3月08   0:00 [crypto]
root        41  0.0  0.0      0     0 ?        S<   3月08   0:00 [kthrotld]
root        43  0.0  0.0      0     0 ?        S<   3月08   0:00 [kmpath_rdacd]
root        44  0.0  0.0      0     0 ?        S<   3月08   0:00 [kaluad]
root        45  0.0  0.0      0     0 ?        S<   3月08   0:00 [kpsmoused]
root        47  0.0  0.0      0     0 ?        S<   3月08   0:00 [ipv6_addrconf]
root        60  0.0  0.0      0     0 ?        S<   3月08   0:00 [deferwq]
root        92  0.0  0.0      0     0 ?        S    3月08   0:00 [kauditd]
root       684  0.0  0.0      0     0 ?        S<   3月08   0:00 [ata_sff]
root       700  0.0  0.0      0     0 ?        S    3月08   0:00 [scsi_eh_0]
root       704  0.0  0.0      0     0 ?        S<   3月08   0:00 [scsi_tmf_0]
root       710  0.0  0.0      0     0 ?        S    3月08   0:00 [scsi_eh_1]
root       712  0.0  0.0      0     0 ?        S<   3月08   0:00 [scsi_tmf_1]
root      1587  0.0  0.0      0     0 ?        S<   3月08   0:00 [mpt_poll_0]
root      1590  0.0  0.0      0     0 ?        S<   3月08   0:00 [mpt/0]
root      1626  0.0  0.0      0     0 ?        S    3月08   0:00 [scsi_eh_2]
root      1631  0.0  0.0      0     0 ?        S<   3月08   0:00 [scsi_tmf_2]
root      1857  0.0  0.0      0     0 ?        S<   3月08   0:00 [ttm_swap]
root      1859  0.0  0.0      0     0 ?        S    3月08   0:00 [irq/16-vmwgfx]
root      2509  0.0  0.0      0     0 ?        S<   3月08   0:00 [kworker/0:1H]
root      2512  0.0  0.0      0     0 ?        S<   3月08   0:00 [bioset]
root      2517  0.0  0.0      0     0 ?        S<   3月08   0:00 [xfsalloc]
root      2522  0.0  0.0      0     0 ?        S<   3月08   0:00 [xfs_mru_cache]
root      2527  0.0  0.0      0     0 ?        S<   3月08   0:00 [xfs-buf/sda3]
root      2529  0.0  0.0      0     0 ?        S<   3月08   0:00 [xfs-data/sda3]
root      2530  0.0  0.0      0     0 ?        S<   3月08   0:00 [xfs-conv/sda3]
root      2532  0.0  0.0      0     0 ?        S<   3月08   0:00 [xfs-cil/sda3]
root      2533  0.0  0.0      0     0 ?        S<   3月08   0:00 [xfs-reclaim/sda]
root      2534  0.0  0.0      0     0 ?        S<   3月08   0:00 [xfs-log/sda3]
root      2535  0.0  0.0      0     0 ?        S<   3月08   0:00 [xfs-eofblocks/s]
root      2536  0.0  0.0      0     0 ?        S    3月08   0:03 [xfsaild/sda3]
root      2602  0.0  0.3  37172  3396 ?        Ss   3月08   0:01 /usr/lib/systemd/systemd-journald
root      2631  0.0  0.5  48472  5996 ?        Ss   3月08   0:01 /usr/lib/systemd/systemd-udevd
root      4464  0.0  0.0      0     0 ?        S<   3月08   0:00 [kworker/u129:0]
root      4466  0.0  0.0      0     0 ?        S<   3月08   0:00 [hci0]
root      4467  0.0  0.0      0     0 ?        S<   3月08   0:00 [hci0]
root      4469  0.0  0.0      0     0 ?        S<   3月08   0:00 [kworker/u129:2]
root      4531  0.0  0.0      0     0 ?        S<   3月08   0:00 [xfs-buf/sda1]
root      4532  0.0  0.0      0     0 ?        S<   3月08   0:00 [xfs-data/sda1]
root      4536  0.0  0.0      0     0 ?        S<   3月08   0:00 [xfs-conv/sda1]
root      4543  0.0  0.0      0     0 ?        S<   3月08   0:00 [xfs-cil/sda1]
root      4544  0.0  0.0      0     0 ?        S<   3月08   0:00 [xfs-reclaim/sda]
root      4546  0.0  0.0      0     0 ?        S<   3月08   0:00 [xfs-log/sda1]
root      4547  0.0  0.0      0     0 ?        S<   3月08   0:00 [xfs-eofblocks/s]
root      4598  0.0  0.0      0     0 ?        S    3月08   0:00 [xfsaild/sda1]
root      5321  0.0  0.0  55520   896 ?        S<sl 3月08   0:00 /sbin/auditd
root      5344  0.0  0.1  26376  1752 ?        Ss   3月08   0:00 /usr/lib/systemd/systemd-logind
polkitd   5345  0.0  1.4 612996 14988 ?        Ssl  3月08   0:00 /usr/lib/polkit-1/polkitd --no-debug
root      5347  0.0  0.6  99664  6104 ?        Ss   3月08   0:00 /usr/bin/VGAuthService -s
root      5348  0.1  0.6 301088  6536 ?        Ssl  3月08   1:48 /usr/bin/vmtoolsd
dbus      5352  0.0  0.2  66468  2588 ?        Ssl  3月08   0:01 /usr/bin/dbus-daemon --system --address=systemd: --nofork --nopidfi
root      5401  0.0  0.1 126320  1736 ?        Ss   3月08   0:00 /usr/sbin/crond -n
root      5405  0.0  0.0 110092   856 tty1     Ss+  3月08   0:00 /sbin/agetty --noclear tty1 linux
root      5417  0.0  2.9 358564 29316 ?        Ssl  3月08   0:01 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid
root      5446  0.0  1.0 555968 10804 ?        Ssl  3月08   0:02 /usr/sbin/NetworkManager --no-daemon
root      5913  0.0  0.4 112856  4304 ?        Ss   3月08   0:00 /usr/sbin/sshd -D
root      5914  0.0  0.4 214504  5008 ?        Ssl  3月08   0:07 /usr/sbin/rsyslogd -n
root      5917  0.0  1.9 573920 19172 ?        Ssl  3月08   0:11 /usr/bin/python2 -Es /usr/sbin/tuned -l -P
root      6014  0.0  0.2  89644  2160 ?        Ss   3月08   0:00 /usr/libexec/postfix/master -w
postfix   6016  0.0  0.4  89816  4088 ?        S    3月08   0:00 qmgr -l -t unix -u
root      6017  0.0  0.6 230976  6352 ?        Ss   3月08   0:06 php-fpm: master process (/usr/local/php-fpm/etc/php-fpm.conf)
php-fpm   6018  0.0  0.6 230976  6104 ?        S    3月08   0:00 php-fpm: pool www
php-fpm   6019  0.0  0.6 230976  6104 ?        S    3月08   0:00 php-fpm: pool www
root      6199  0.0  0.5 158860  5600 ?        Ss   3月08   0:01 sshd: root@pts/0
root      6202  0.0  0.2 115544  2116 pts/0    Ss   3月08   0:00 -bash
root      6254  0.0  0.0      0     0 ?        S    3月08   0:01 [kworker/u128:2]
root      7978  0.0  0.0      0     0 ?        S    3月08   0:00 [kworker/u128:0]
root      8096  0.0  0.0      0     0 ?        S    00:50   0:00 [kworker/0:1]
root      8100  0.0  0.0      0     0 ?        S    00:55   0:00 [kworker/0:0]
root      8125  0.0  0.0      0     0 ?        R    01:01   0:00 [kworker/0:2]
root      8168  0.0  0.0      0     0 ?        S    01:05   0:00 [kworker/0:3]
postfix   8172  0.0  0.4  89748  4060 ?        S    01:07   0:00 pickup -l -t unix -u
root      8180  0.0  0.1 155360  1892 pts/0    R+   01:10   0:00 ps -aux
  •  输出结果:
  1. USER:用户
  2. PID:进程号
  3. %CPU:CPU使用率
  4. %MEM:内存使用率
  5. VSZ:虚拟内存大小
  6. RSS:物理内存大小
  7. TTY,显示?的tty都是在后台运行的
  8. STAT:进程状态
  9. START:进程启动的时间
  10. TIME:进程空闲的时间
  11. COMMAND:对应的指令
  • 上面输出的结果中STAT一列显示的内容有:
  1. S Sleep
  2. R Running
  3. s 父进程
  4. N 低优先级
  5. < 高优先级
  6. +前台进程
  7. l多线程进程
  8. Z僵尸进程
  • ps-elf跟aux相似,也是查看系统中全部的进程
[root@www ~]# ps -elf
F S UID        PID  PPID  C PRI  NI ADDR SZ WCHAN  STIME TTY          TIME CMD
4 S root         1     0  0  80   0 - 32010 ep_pol 3月08 ?       00:00:03 /usr/lib/systemd/systemd --switched-root --system --deseri
1 S root         2     0  0  80   0 -     0 kthrea 3月08 ?       00:00:00 [kthreadd]
1 S root         3     2  0  80   0 -     0 smpboo 3月08 ?       00:00:00 [ksoftirqd/0]
1 S root         5     2  0  60 -20 -     0 worker 3月08 ?       00:00:00 [kworker/0:0H]
1 S root         7     2  0 -40   - -     0 smpboo 3月08 ?       00:00:00 [migration/0]

1 S root      8125     2  0  80   0 -     0 worker 01:01 ?        00:00:00 [kworker/0:2]
4 S postfix   8172  6014  0  80   0 - 22437 ep_pol 01:07 ?        00:00:00 pickup -l -t unix -u
1 S root      8181     2  0  80   0 -     0 worker 01:15 ?        00:00:00 [kworker/0:0]
0 R root      8182  6202  0  80   0 - 38840 -      01:18 pts/0    00:00:00 ps -elf
  • ps-eLf查看线程
[root@www ~]# ps -eLf
UID        PID  PPID   LWP  C NLWP STIME TTY          TIME CMD
root         1     0     1  0    1 3月08 ?       00:00:03 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
root         2     0     2  0    1 3月08 ?       00:00:00 [kthreadd]
root         3     2     3  0    1 3月08 ?       00:00:00 [ksoftirqd/0]
root         5     2     5  0    1 3月08 ?       00:00:00 [kworker/0:0H]
root         7     2     7  0    1 3月08 ?       00:00:00 [migration/0]
root         8     2     8  0    1 3月08 ?       00:00:00 [rcu_bh]

root       684     2   684  0    1 3月08 ?       00:00:00 [ata_sff]
root       700     2   700  0    1 3月08 ?       00:00:00 [scsi_eh_0]
root       704     2   704  0    1 3月08 ?       00:00:00 [scsi_tmf_0]
root       710     2   710  0    1 3月08 ?       00:00:00 [scsi_eh_1]
root       712     2   712  0    1 3月08 ?       00:00:00 [scsi_tmf_1]

root      8125     2  8125  0    1 01:01 ?        00:00:00 [kworker/0:2]
postfix   8172  6014  8172  0    1 01:07 ?        00:00:00 pickup -l -t unix -u
root      8181     2  8181  0    1 01:15 ?        00:00:00 [kworker/0:0]
root      8184  6202  8184  0    1 01:19 pts/0    00:00:00 ps -eLf
  • ls /proc/pid(具体进程数)可查看一个进程的xian详细信息

查看网络链接情况

  • netstat -lnp(查看监听端口)
  • netstat -lntp(只查看TCP协议相关)
  • netstat -lntup(查看TCP,UDP协议相关)
  • netstat -an(查看全部网络链接情况) #呈现的内容中关于State字段的含义:LISTEN 监听 TIME_WAIT已链接等待中 ESTABLISHED链接已创建(正在通讯)
  • 查看netstat-an有什么用?能够排查问题,是否有syn工具,ack攻击
  • ss -an跟netstat -an输入结果相似,可是相应速度比它快

抓包工具

tcpdump(安装:yum install -y tcpdump)

tcpdump -nn -i 网卡名称 port 端口号python

tcpdump -nn -i 网卡名称 -c 100 -w 1.caplinux

tcpdump -i 网卡名称 -nn not port 22 and not  host IP地址 ios

tcpdump -nn -r 1.cap 数组

参数解释:-nn    以数字形式表示连接缓存

                 -i    指定网卡bash

                 -c    加数字指定输出多少条记录网络

                 -w    将内容写到指定文件多线程

                 -r    读取指定文件的内容(约定以.cap为后缀)    .cap文件比直接使用命令输出的结果更详细ssh

                 port    指定端口

                 host    指定IP              

                多个筛选条件之间使用and链接

wireshark分析抓包工具(安装:yum install -y wireshark)

tshark -i 网卡名称  -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"

#tshark是wireshark包的命令,带有分析功能

 输出结果相似于访问日志 #能够看来源IP 时间 域名 URL 

 小常识:

bit 比特(带宽单位)
Byte 字节 (速度传输单位) 迅雷 磁盘存储也是B单位
8b=1B
相关文章
相关标签/搜索