赞
踩
linux获取TCP连接数
- 方法一:
- [admin@zabbix ~]$ ss -ant | awk 'NR>1 {a[$1]++} END {for (b in a) print b,a[b]}'
- ESTAB 535
- TIME-WAIT 80
- LISTEN 13
-
- 方法二:
- [admin@zabbix ~]$ netstat -an | awk '/^tcp/ {a[$NF]++} END {for (b in a) print b,a[b]}'
- TIME_WAIT 91
- SYN_SENT 7
- ESTABLISHED 535
- LISTEN 13
-
- 方法三:
- [admin@zabbix ~]$ cat /proc/net/snmp
- Ip: Forwarding DefaultTTL InReceives InHdrErrors InAddrErrors ForwDatagrams InUnknownProtos InDiscards InDelivers OutRequests OutDiscards OutNoRoutes ReasmTimeout ReasmReqds ReasmOKs ReasmFails FragOKs FragFails FragCreates
- Ip: 1 64 12503948767 0 0 0 0 0 12503948767 12822437575 0 0 0 0 0 0 0 0 0
- Icmp: InMsgs InErrors InDestUnreachs InTimeExcds InParmProbs InSrcQuenchs InRedirects InEchos InEchoReps InTimestamps InTimestampReps InAddrMasks InAddrMaskReps OutMsgs OutErrors OutDestUnreachs OutTimeExcds OutParmProbs OutSrcQuenchs OutRedirects OutEchos OutEchoReps OutTimestamps OutTimestampReps OutAddrMasks OutAddrMaskReps
- Icmp: 985733 389 3779 69 0 0 0 490950 490935 0 0 0 0 984433 0 2548 0 0 0 0 490935 490950 0 0 0 0
- IcmpMsg: InType0 InType3 InType8 InType11 OutType0 OutType3 OutType8
- IcmpMsg: 490935 3779 490950 69 490950 2548 490935
- Tcp: RtoAlgorithm RtoMin RtoMax MaxConn ActiveOpens PassiveOpens AttemptFails EstabResets CurrEstab InSegs OutSegs RetransSegs InErrs OutRsts
- Tcp: 1 200 120000 -1 1585243925 55029885 290059 15370 535 12500488405 12818246605 731883 25103 3821760
- Udp: InDatagrams NoPorts InErrors OutDatagrams RcvbufErrors SndbufErrors
- Udp: 2546210 3436 0 2657997 0 0
- UdpLite: InDatagrams NoPorts InErrors OutDatagrams RcvbufErrors SndbufErrors
- UdpLite: 0 0 0 0 0 0
-
- 注:通过CurrEstab获取tcp连接数
- [admin@zabbix ~]$ cat /proc/net/snmp | grep Tcp | tail -1 | awk '{print $10}'
- 535
-
- 扩展:如何获取每秒新增的TCP连接呢?
- 通过cat /proc/net/snmp得到最近240秒内PassiveOpens的增量,除以240可以得到每秒的tcp连接增量。
-
- 详情参考我的另一篇文章:https://blog.csdn.net/m0_37814112/article/details/80774935
-
- 如果想知道cat /proc/net/snmp输出选项各个参数的意义,可以参考
- http://perthcharles.github.io/2015/11/10/wiki-netstat-proc/
- Linux下 查看tcp连接数及状态
- netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'
- LISTEN: 侦听来自远方的TCP端口的连接请求
- SYN-SENT: 再发送连接请求后等待匹配的连接请求
- SYN-RECEIVED:再收到和发送一个连接请求后等待对方对连接请求的确认
- ESTABLISHED: 代表一个打开的连接
- FIN-WAIT-1: 等待远程TCP连接中断请求,或先前的连接中断请求的确认
- FIN-WAIT-2: 从远程TCP等待连接中断请求
- CLOSE-WAIT: 等待从本地用户发来的连接中断请求
- CLOSING: 等待远程TCP对连接中断的确认
- LAST-ACK: 等待原来的发向远程TCP的连接中断请求的确认
- TIME-WAIT: 等待足够的时间以确保远程TCP接收到连接中断请求的确认
- CLOSED: 没有任何连接状态
linux作为服务器系统,当socket运行高并发TCP程序时,通常会出现连接建立到一定个数后不能再建立连接的情况
本人在工作时,测试高并发tcp程序(GPS服务器端程序),多次测试,发现每次连接建立到1000左右时,再也不能建立tcp连接,最总上网搜索,linux系统默认ulimit为1024个访问 用户最多可开启的程序数目。一般一个端口的最高连接为2的16次方65535
- soft nofile 65535
-
- hard nofile 65535
session required /lib/security/pam_limits.so
如果是64bit系统的话,应该为 :
session required /lib64/security/pam_limits.so
net.ipv4.ip_local_port_range = 1024 65535 net.core.rmem_max=16777216 net.core.wmem_max=16777216 net.ipv4.tcp_rmem=4096 87380 16777216 net.ipv4.tcp_wmem=4096 65536 16777216 net.ipv4.tcp_fin_timeout = 10 net.ipv4.tcp_tw_recycle = 1 net.ipv4.tcp_timestamps = 0 net.ipv4.tcp_window_scaling = 0 net.ipv4.tcp_sack = 0 net.core.netdev_max_backlog = 30000 net.ipv4.tcp_no_metrics_save=1 net.core.somaxconn = 262144 net.ipv4.tcp_syncookies = 0 net.ipv4.tcp_max_orphans = 262144 net.ipv4.tcp_max_syn_backlog = 262144 net.ipv4.tcp_synack_retries = 2 net.ipv4.tcp_syn_retries = 2
- /sbin/sysctl -p /etc/sysctl.conf
-
- /sbin/sysctl -w net.ipv4.route.flush=1
- echo ulimit -HSn 65536 >> /etc/rc.local
-
- echo ulimit -HSn 65536 >>/root/.bash_profile
-
- ulimit -HSn 65536
通过修改,tcp可以达到20000个连接完全没有问题
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。