Linux系統狀態查看命令2

10月24日任務linux

10.6 監控io性能ios

10.7 free命令centos

10.8 ps命令緩存

10.9 查看網絡狀態服務器

10.10 linux下抓包網絡

 

監控io性能

磁盤io狀態監控(sysstat包內)多線程

isstatdom

[root@centos7 ~]# iostat
Linux 3.10.0-693.2.2.el7.x86_64 (centos7) 	... 	_x86_64_	(2 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.27    0.00    0.81    1.66    0.00   97.27

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda               6.46       136.94        10.12      99168       7330
scd0              0.02         1.42         0.00       1028          0

[root@centos7 ~]# iostat 1 4
Linux 3.10.0-693.2.2.el7.x86_64 (centos7) 	... 	_x86_64_	(2 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.26    0.00    0.79    1.62    0.00   97.33

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda               6.31       133.58         9.92      99176       7364
scd0              0.02         1.38         0.00       1028          0

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.00    0.00    0.00    0.00    0.00  100.00

Device:            tps    kB_read/s    kB_wrtn/s    kB_read    kB_wrtn
sda               0.00         0.00         0.00          0          0
scd0              0.00         0.00         0.00          0          0

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.00    0.00    0.50    0.00    0.00   99.50



[root@centos7 ~]# iostat -x
Linux 3.10.0-693.2.2.el7.x86_64 (centos7) 	... 	_x86_64_	(2 CPU)

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           0.26    0.00    0.77    1.59    0.00   97.38

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.01     0.06    5.48    0.71   130.95     9.72    45.48     0.10   16.63   13.73   39.25   6.17   3.82
scd0              0.00     0.00    0.02    0.00     1.36     0.00   114.22     0.00   73.44   73.44    0.00  63.17   0.15

-x參數比較重要,查看的重點指標是%util:表明io等待佔比(讀寫時);數值大,磁盤io性能較差,可能須要更換磁盤

讀寫2列(r/s,w/s)不大,%util很大,可能磁盤有故障

iotop(yum install -y iotop)ssh

相似於top命令,iotop命令會動態顯示系統當前的磁盤總的讀寫速率和各進程的讀寫狀態以及io佔比等信息。socket

iotop.png

內存使用狀況查看:free

說明:centos6上的free顯示效果跟centos7略有

不加參數,默認是以KB爲單位
[root@centos7 ~]# free
              total        used        free      shared  buff/cache   available
Mem:         999720      123384      587972        6840      288364      691412
Swap:       4194300           0     4194300

根據磁盤大小自動換算單位
[root@centos7 ~]# free -h
              total        used        free      shared  buff/cache   available
Mem:           976M        120M        573M        6.7M        281M        675M
Swap:          4.0G          0B        4.0G

以Mb爲單位顯示
[root@centos7 ~]# free -m
              total        used        free      shared  buff/cache   available
Mem:            976         120         573           6         281         675
Swap:          4095           0        4095

使用Gb爲單位顯示
[root@centos7 ~]# free -g
              total        used        free      shared  buff/cache   available
Mem:              0           0           0           0           0           0
Swap:             3           0           3

總的磁盤大小total = used + free + buff/cache

  • used 已經使用的內存大小
  • free 剩餘未使用的內存大小
  • buff/cache 緩衝/緩存
  • available = free + buff/cache

查看剩餘內存應該看available值

數據處理:數據(磁盤) --> 內存(cache) --> CPU

處理返回:CPU --> 內存(buffer) --> 數據(磁盤)

系統進程狀態查看: ps

一次性顯示當前系統進程
[root@localhost ~]# ps aux 
USER        PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root          1  0.0  0.4 128108  6708 ?        Ss   19:27   0:02 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
root          2  0.0  0.0      0     0 ?        S    19:27   0:00 [kthreadd]
root          3  0.0  0.0      0     0 ?        S    19:27   0:00 [ksoftirqd/0]
root          6  0.0  0.0      0     0 ?        S    19:27   0:02 [kworker/u256:0]
root          7  0.0  0.0      0     0 ?        S    19:27   0:00 [migration/0]
root          8  0.0  0.0      0     0 ?        S    19:27   0:00 [rcu_bh]
root          9  0.0  0.0      0     0 ?        R    19:27   0:00 [rcu_sched]
root         10  0.0  0.0      0     0 ?        S    19:27   0:00 [watchdog/0]
root         12  0.0  0.0      0     0 ?        S<   19:27   0:00 [khelper]
root         13  0.0  0.0      0     0 ?        S    19:27   0:00 [kdevtmpfs]
root         14  0.0  0.0      0     0 ?        S<   19:27   0:00 [netns]
root         15  0.0  0.0      0     0 ?        S    19:27   0:00 [khungtaskd]
root         16  0.0  0.0      0     0 ?        S<   19:27   0:00 [writeback]
root         17  0.0  0.0      0     0 ?        S<   19:27   0:00 [kintegrityd]
root         18  0.0  0.0      0     0 ?        S<   19:27   0:00 [bioset]
root         19  0.0  0.0      0     0 ?        S<   19:27   0:00 [kblockd]
root         20  0.0  0.0      0     0 ?        S<   19:27   0:00 [md]
root         26  0.0  0.0      0     0 ?        S    19:27   0:00 [kswapd0]
...
  • 第一列:運行的用戶USER
  • 第二列:進程ID
  • 第三列:佔CPU資源百分比
  • 第四列:佔內存的百分比
  • 第八列:進程狀態STAT
  • 第九列:進程啓動的時間
  • 第十列:運行的命令

STAT 表示進程運行狀態

  • D 不能中斷的進程
  • R 運行狀態的進程
  • S 休眠狀態的進程
  • T 暫停的進程(好比執行了ctrl + Z命令)
  • Z 殭屍進程(看到儘可能關閉)
  • < 高優先級的進程
  • N 低優先級的進程
  • L 內存中被鎖了內存分頁
  • 主進程(Ss)
  • l 多線程進程(Sl)
  • + 前臺進程(S+)

與aux選項相似,-elf選項也能夠一次性顯示系統進程,只是顯示的內容有些不一樣。 一種用法是找到進程pid,使用kill命令關閉。

[root@localhost ~]# 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 - 32027 ep_pol 19:27 ?        00:00:02 /usr/lib/systemd/systemd --switched-root --system --dese
1 S root          2      0  0  80   0 -     0 kthrea 19:27 ?        00:00:00 [kthreadd]
1 S root          3      2  0  80   0 -     0 smpboo 19:27 ?        00:00:00 [ksoftirqd/0]
1 S root          6      2  0  80   0 -     0 worker 19:27 ?        00:00:02 [kworker/u256:0]
1 S root          7      2  0 -40   - -     0 smpboo 19:27 ?        00:00:00 [migration/0]
1 S root          8      2  0  80   0 -     0 rcu_gp 19:27 ?        00:00:00 [rcu_bh]
1 R root          9      2  0  80   0 -     0 -      19:27 ?        00:00:00 [rcu_sched]
5 S root         10      2  0 -40   - -     0 smpboo 19:27 ?        00:00:00 [watchdog/0]
1 S root         12      2  0  60 -20 -     0 rescue 19:27 ?        00:00:00 [khelper]
5 S root         13      2  0  80   0 -     0 devtmp 19:27 ?        00:00:00 [kdevtmpfs]
1 S root         14      2  0  60 -20 -     0 rescue 19:27 ?        00:00:00 [netns]
1 S root         15      2  0  80   0 -     0 watchd 19:27 ?        00:00:00 [khungtaskd]
...

查看網絡狀態:netstat

  • -lnp選項
能夠查看監聽的端口,包含端口和socket文件信息
[root@centos7 ~]# netstat -lnp
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      889/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      978/master          
tcp6       0      0 :::22                   :::*                    LISTEN      889/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      978/master          
udp        0      0 127.0.0.1:323           0.0.0.0:*                           549/chronyd         
udp6       0      0 ::1:323                 :::*                                549/chronyd         
raw6       0      0 :::58                   :::*                    7           584/NetworkManager  
Active UNIX domain sockets (only servers)
Proto RefCnt Flags       Type       State         I-Node   PID/Program name     Path
unix  2      [ ACC ]     STREAM     LISTENING     19087    978/master           public/pickup
unix  2      [ ACC ]     STREAM     LISTENING     1580     1/systemd            /run/systemd/journal/stdout
unix  2      [ ACC ]     STREAM     LISTENING     15152    1/systemd            /var/run/dbus/system_bus_socket
unix  2      [ ACC ]     STREAM     LISTENING     15926    529/VGAuthService    /var/run/vmware/guestServicePipe
...
  • -t / -u選項
只查看系統的tcp/udp鏈接端口信息;-t tcp/-u udp;加上-t/-u後將再也不顯示socket文件信息
[root@centos7 ~]# netstat -tulnp
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      889/sshd            
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      978/master          
tcp6       0      0 :::22                   :::*                    LISTEN      889/sshd            
tcp6       0      0 ::1:25                  :::*                    LISTEN      978/master          
udp        0      0 127.0.0.1:323           0.0.0.0:*                           549/chronyd         
udp6       0      0 ::1:323                 :::*                                549/chronyd
  • -an選項:查看全部的鏈接狀態
還有一個參數方法 -an 查看系統的網絡鏈接情況
[root@centos7 ~]# netstat -an
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN     
tcp        0     52 192.168.65.130:22       192.168.65.1:49891      ESTABLISHED
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 ::1:25                  :::*                    LISTEN     
udp        0      0 127.0.0.1:323           0.0.0.0:*                          
udp6       0      0 ::1:323                 :::*                               
raw6       0      0 :::58                   :::*                    7          
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags       Type       State         I-Node   Path
unix  2      [ ]         DGRAM                    11009    /run/systemd/shutdownd
unix  2      [ ACC ]     STREAM     LISTENING     19087    public/pickup
unix  2      [ ]         DGRAM                    1566     /run/systemd/notify
unix  2      [ ]         DGRAM                    1568     /run/systemd/cgroups-agent
unix  2      [ ACC ]     STREAM     LISTENING     1580     /run/systemd/journal/stdout
...

小技巧:使用awk命令彙總netstat -an 中的各鏈接狀態

[root@centos7 ~]# netstat -an | awk '/^tcp/ {++sta[$NF]} END {for (key in sta) print key,"\t",sta[key]}'
LISTEN 	 4
ESTABLISHED 	 1

有個與netstat相似的命令:ss -an

linux下抓包:tcpdump

安裝:yum install -y tcpdump

用法:tcpdump

  • -nn 使用ip、端口代替主機名、服務顯示 localhost.localdomain.ssh --> 192.168.65.133.22
  • -i 網卡名
  • [not] port 端口號
  • host IP
  • -c 抓包數量
  • -w *.cap (將數據保存到文件,文件格式爲tcpdump抓包文件)這個文件是真實網絡上傳輸的數據包,沒法使用cat查看內容!
  1. 基本使用
[root@centos7 ~]# tcpdump -n -i ens33 -c 100
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
22:23:48.015504 IP 192.168.65.130.ssh > 192.168.65.1.49891: Flags [P.], seq 714412558:714412770, ack 3922939984, win 318, length 212
22:23:48.015802 IP 192.168.65.130.ssh > 192.168.65.1.49891: Flags [P.], seq 212:408, ack 1, win 318, length 196
...
22:23:48.031997 IP 192.168.65.130.ssh > 192.168.65.1.49891: Flags [P.], seq 16784:16964, ack 1, win 318, length 180
22:23:48.032137 IP 192.168.65.130.ssh > 192.168.65.1.49891: Flags [P.], seq 16964:17144, ack 1, win 318, length 180
100 packets captured
100 packets received by filter
0 packets dropped by kernel
  1. port指定抓取特定端口的包
[root@localhost ~]# tcpdump -nn -i ens33 -c 5 port 22
tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
21:32:57.500685 IP 192.168.65.133.22 > 192.168.65.1.50775: Flags [P.], seq 3945596256:3945596468, ack 661489939, win 251, length 212
21:32:57.501660 IP 192.168.65.1.50775 > 192.168.65.133.22: Flags [.], ack 212, win 256, length 0
21:32:57.502907 IP 192.168.65.133.22 > 192.168.65.1.50775: Flags [P.], seq 212:504, ack 1, win 251, length 292
21:32:57.504053 IP 192.168.65.133.22 > 192.168.65.1.50775: Flags [P.], seq 504:668, ack 1, win 251, length 164
21:32:57.505222 IP 192.168.65.133.22 > 192.168.65.1.50775: Flags [P.], seq 668:832, ack 1, win 251, length 164
5 packets captured
6 packets received by filter
0 packets dropped by kernel
  1. -w 參數將抓包存入指定文件
# 保存的文件沒法直接查看
[root@centos7 ~]# tcpdump -n -i ens33 -c 5 -w ./test.cap
tcpdump: listening on ens33, link-type EN10MB (Ethernet), capture size 262144 bytes
5 packets captured
5 packets received by filter
0 packets dropped by kernel
  1. 讀取保存的抓包文件
[root@centos7 ~]# tcpdump -r test.cap 
reading from file test.cap, link-type EN10MB (Ethernet)
22:25:52.971050 IP centos7.ssh > 192.168.65.1.49891: Flags [P.], seq 714434958:714435106, ack 3922943564, win 318, length 148
22:25:52.971518 IP 192.168.65.1.49891 > centos7.ssh: Flags [.], ack 148, win 254, length 0
22:25:54.100199 ARP, Request who-has gateway tell 192.168.65.1, length 46
22:25:54.872750 ARP, Request who-has gateway tell 192.168.65.1, length 46
22:25:55.874164 ARP, Request who-has gateway tell 192.168.65.1, length 46

可使用tcpdump -r *.cap 來查看抓取的包文件

tshark

安裝: yum install -y wireshark

實用命令:

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"

簡單的來講TCP的三次握手創建鏈接的過程:

  1. 客戶端發送一個數據包給服務器,其中包含一個SYN位,其值爲J
  2. 服務器端接收到這個包,解包後發送一個確認數據包給客戶端,其中的SYN位爲K,ACK位爲J+1
  3. 客戶端查看返回的包,知道服務器贊成創建鏈接(ACK)後發送確認數據包,ACK位爲k+1

一樣的TCP的四次揮手斷開鏈接的過程:

  1. 客戶端須要斷開鏈接,因此發送一個包含FIN位的數據包給服務器,FIN值爲M;
  2. 服務器接收到這個包,知道客戶端想要斷開鏈接,因此發送一個確認數據包,包含ACK位,其值爲M+1;
  3. 同時服務器端也須要發送一個數據包,包含FIN位,去告訴客戶端斷開鏈接,其值爲N;
  4. 客戶端接收到包含FIN位的數據包後,就發送確認數據包返回給服務器,其內ACK值設爲N+1(代表收到上個斷開請求包)。

至此一個雙向的斷開鏈接請求發送接收完畢,鏈接斷開。

相關文章
相關標籤/搜索