天天看點

linux 監控 TCP狀态數量的推薦方法

示例:點選 -> 性能監控

/proc/net/tcp 第四列 01代表了 TCP_ESTABLISHED 06代表代表time_wait 08代表close_wait

[root@wangzi ~]# cat /proc/net/tcp| awk '{if($4 == '01') print $0}'|wc -l
22
[root@wangzi ~]# netstat -antpl|grep ESTABLISHED|wc -l
22
[root@wangzi ~]#cat /proc/net/tcp| awk '{if($4 == '01' || $4=='06' || $4=='08') print $4}'           

複制

因使用netstat指令有時會占用較多資源,當機器負載較高時,可以用使用上面的指令來檢視tcp正在通信的連接配接數。

附圖一張:

linux 監控 TCP狀态數量的推薦方法

python監控:

#!/usr/bin/env python
# coding=utf-8
# author: brownwang
# mail: [email protected]
# datetime:2019/3/31 1:03 PM
# web: https://www.bthlt.com


def net_tcp():
    ret=Popen("""cat /proc/net/tcp| awk '{if($4 == '01' || $4=='06' || $4=='08') print $4}'""",shell=True,stdout=PIPE)
    established=0
    time_wait=0
    close_wait=0
    for item in ret.stdout.readlines():
       if int(item)==1:
           established+=1
       elif int(item)==6:
           time_wait+=1
       elif int(item)==6:
           close_wait+=1
    insert_sql="""insert into `monitor_net_tcp` (`establish`,`time_wait`,`close_wait`,`flow_time`) values ({0},{1},{2},'{3}')""".format(established,time_wait,close_wait,now_zero)
    cursorUpdate(insert_sql,[])           

複制