一、生産伺服器netstat tcp連接配接狀态................................................................................ 2
1.1生産伺服器某個業務lvs負載均衡上連接配接狀态數量............................................... 2
1.2生産伺服器某個業務web上連接配接狀态數量............................................................ 3
1.3 生産伺服器某個業務db上連接配接狀态數量.............................................................. 3
二、tcp連接配接狀态的描述說明(netstat輸出)........................................................................ 4
2.1 執行netstat -n檢視輸出結果共6列..................................................................... 4
2.2 通過man netstat檢視netstat輸出結果資訊......................................................... 4
2.3netstat第六列state的狀态資訊......................................................................... 5
2.4netstat第六列state狀态資訊轉換圖表.............................................................. 6
2.4.1 狀态參考圖一................................................................................................... 7
2.4.2 狀态參考圖二................................................................................................... 9
2.4.3 狀态參考圖三................................................................................................. 10
2.4.4 用戶端的狀态變化描述............................................................................ 10
2.4.5 伺服器的狀态變化描述............................................................................ 10
2.4.6 其他的狀态變化描述總結......................................................................... 11
更多目錄見博文結尾。
[oldboy@lvs-1-1 ~]$ netstat -n |awk ‘/^tcp/ {++oldboy[$nf]} end {for(a in oldboy) print a, oldboy[a]}‘
time_wait 9137
close_wait 207
fin_wait1 547
established 597
fin_wait2 74
syn_recv 70
closing 55
last_ack 8
[root@lvs_nginx~]#netstat -n |awk ‘/^tcp/ {++oldboy[$nf]} end {for(a in oldboy) print a, oldboy[a]}‘
time_wait 422
close_wait 590
fin_wait1 56
fin_wait2 28
established 1731
[root@web1 ~]# netstat -n |awk ‘/^tcp/ {++oldboy[$nf]} end {for(a in oldboy) print a, oldboy[a]}‘
time_wait 418
fin_wait1 7
fin_wait2 3
established 1097
last_ack 2
[root@web2 ~]# netstat -n |awk ‘/^tcp/ {++oldboy[$nf]} end {for(a in oldboy) print a, oldboy[a]}‘
time_wait 250
fin_wait1 2
established 1032
[root@old_web ~]# netstat -n |awk ‘/^tcp/ {++oldboy[$nf]} end {for(a in oldboy) print a, oldboy[a]}‘
time_wait 342
close_wait 618
fin_wait1 37
established 1681
syn_recv 1
[root@k32_50718 ~]# netstat -n |awk ‘/^tcp/ {++oldboy[$nf]} end {for(a in oldboy) print a, oldboy[a]}‘
time_wait 4146
fin_wait1 352
fin_wait2 1112
established 8451
syn_recv 186
closing 9
last_ack 102
[root@web_slave ~]# netstat -n |awk ‘/^tcp/ {++oldboy[$nf]} end {for(a in oldboy) print a, oldboy[a]}‘
time_wait 263
fin_wait1 1
fin_wait2 48
established 918
[root@oldboy ~]# netstat -n
active internet connections (w/o servers)
proto recv-q send-q local addressforeign addressstate
tcp00 10.0.0.183:5058410.0.0.181:22established
[root@oldboy ~]# man netstat
output
active internet connections (tcp, udp, raw)
proto
the protocol (tcp, udp, raw) used by the socket.
第一列為socket使用的協定。
recv-q
the count of bytes not copied by the user program connected to this socket.
第二列為接到的但是還沒處理的位元組數。
send-q
the count of bytes not acknowledged by the remote host.
第三列為已經發送的但是沒有被遠端主機确認收到的位元組數。
local address
address and port number of the local end of the socket.unless the --numeric(-n)
optionisspecified,thesocketaddress is resolved to its canonical host name
(fqdn), and the port number is translated into the corresponding service name.
第四列為 本地的位址及端口。
foreign address
address and port number of the remote endofthesocket.analogousto"local address."
第五列為外部的位址及端口。
state
thestateofthesocket.sincethere are no states in raw mode and usually no
states used in udp, this column may be left blank. normally this can be one of sev-
eral values:
第六列為socket的狀态,通常僅僅有tcp的狀态,狀态值可能有established,syn_sent,syn_recv fin_wait1,fin_wait2,time_wait等,詳見下文。其中,最重要的是第六列。
如果我們執行man netstat可以找到如下的幫助資訊
established
the socket has an established connection.
socket已經建立連接配接,表示處于連接配接的狀态,一般認為有一個established認為是一個服務的并發連接配接。這個連接配接狀态在生産場景很重要,要重點關注。
syn_sent
the socket is actively attempting to establish a connection.
socket正在積極嘗試建立一個連接配接,即處于發送後連接配接前的一個等待但未比對進入連接配接的狀态。
syn_recv
a connection request has been received from the network.
已經從網絡上收到一個連接配接請求。
fin_wait1
the socket is closed, and the connection is shutting down.
socket已關閉,連接配接正在或正要關閉。
fin_wait2
connectionisclosed,andthesocket is waiting for a shutdown from the remote end.
連接配接已關閉,并且socket正在等待遠端結束。
time_wait
the socket is waiting after close to handle packets still in the network.
socket正在等待關閉處理仍在網絡上的資料包,這個連接配接狀态在生産場景很重要,要重點關注。
closed the socket is not being used.| socket不在被占用了。
close_wait
the remote end has shutdown, waiting for the socket to close.
遠端已經結束,等待socket關閉。
last_ack
the remote end has shut down, and the socket is closed. waiting for acknowl-edgement.|
遠端已經結束,并且socket也已關閉,等待acknowl-edgement。
listen thesocketislisteningforincoming connections.such sockets are not
included in the output unless you specify the --listening (-l) or --all (-a)
option.
socket正在監聽連接配接請求。
closing
both sockets are shut down but we still don’t have all our data sent.
sockets關閉,但是我們仍舊沒有發送資料。
unknown
the state of the socket is unknown
未知的狀态。
tcp的狀态圖,這是一個看起來比較複雜的狀态遷移圖,因為它包含了兩個部分---伺服器的狀态遷移和用戶端的狀态遷移,如果從某一個角度出發來看這個圖,就會清晰許多,這裡面的伺服器和用戶端都不是絕對的,發送資料的就是用戶端,接受資料的就是伺服器。
通過狀态參考圖三,用戶端的狀态可以用如下的流程來表示:
closed->syn_sent->established->fin_wait_1->fin_wait_2->time_wait->closed
以上流程是在程式正常的情況下應該有的流程,在建立連接配接時,當用戶端收到syn封包的ack以後,用戶端就打開了資料互動地連接配接。而結束連接配接則通常是用戶端主動結束的,用戶端結束應用程式以後,需要經曆fin_wait_1,fin_wait_2等狀态,這些狀态的遷移就是前面提到的結束連接配接的四次握手。
通過狀态參考圖三,伺服器的狀态可以用如下的流程來表示:
closed->listen->syn_recvd->established->close_wait->last_ack->closed
在建立連接配接的時候,伺服器端是在第三次握手之後才進入資料互動狀态,而關閉連接配接則是在關閉連接配接的第二次握手以後(注意不是第四次)。而關閉以後還要等待用戶端給出最後的ack包才能進入初始的狀态。
針對伺服器和用戶端兩方面的總結如下
?1)listen->syn_sent,對于這個解釋就很簡單了,伺服器有時候也要打開連接配接的嘛。
?2)syn_sent->syn_recvd,伺服器和用戶端在syn_sent狀态下如果收到syn資料報,則都會發送syn的ack資料報并把自身狀态調整到syn_recvd,準備進入established
?3)syn_sent->closed,在發送逾時的情況下,會傳回到closed狀态。
?4)syn_recvd->listen,如果受到rst包,會傳回到listen狀态。
?5)syn_recvd->fin_wait_1,這個遷移是說,可以不用到established狀态,而可以直接跳轉到fin_wait_1狀态并等待關閉。