View the number of TCP connections in Linux

Source: Internet
Author: User
Tags ack server memory

One, see which IP connection native

Netstat-an

Second, view the number of TCP connections

1) Count 80 port connections
Netstat-nat|grep-i "|wc-l"

2) Count httpd protocol connections
Ps-ef|grep httpd|wc-l

3), the statistics are connected, the status is "established
Netstat-na|grep established|wc-l

4), find out which IP address is the most connected, sealed it.
Netstat-na|grep Established|awk {print $5}|awk-f: {print $1}|sort|uniq-c|sort-r +0n

Netstat-na|grep Syn|awk {print $5}|awk-f: {print $1}|sort|uniq-c|sort-r +0n

---------------------------------------------------------------------------------------------

1. View the current number of concurrent accesses to Apache:
Netstat-an | grep established | Wc-l

Compare the number of maxclients in httpd.conf.

2. How many processes are viewed:
PS Aux|grep httpd|wc-l

3, you can use the following parameters to view the data
Server-status?auto

#ps-ef|grep Httpd|wc-l
1388
Count the number of httpd processes, and a single request initiates a process that is used for the Apache server.
Indicates that Apache can handle 1388 concurrent requests, which Apache can automatically adjust according to the load situation.

#netstat-nat|grep-i "|wc-l"
4341
Netstat-an will print the current network link state of the system, while Grep-i "80" is used to extract connections related to port 80, wc-l the number of connections counted.
The final number returned is the total number of requests for all 80 ports currently.

#netstat-na|grep Established|wc-l
376
Netstat-an Prints the current network link state of the system, and grep established extracts information about the established connection. Then wc-l statistics.
The final number returned is the total number of established connections for all current 80 ports.

netstat-nat| | grep ESTABLISHED|WC-To see detailed records of all established connections

To view the number of concurrent requests for Apache and its TCP connection status:
Linux commands:
Netstat-n | awk '/^tcp/{++s[$NF]} END {for (a in S) print A, s[a]} '

Netstat-n | awk '/^tcp/{++s[$NF]} END {for (a in S) print A, s[a]} '
Time_wait 8947 waits enough time to ensure that the remote TCP receives a connection interrupt request acknowledgement
Fin_wait1 15 waiting for a remote TCP connection interrupt request, or confirmation of a previous connection interrupt request
Fin_wait2 1 Waiting for connection interrupt request from remote TCP
Established 55 represents an open connection
Syn_recv 21 after receiving and sending a connection request, wait for the other party to confirm the connection request
CLOSING 2 does not have any connection status
Last_ack 4 Waiting for the original connection interrupt request acknowledgement to remote TCP

TCP Connection Status Detailed
LISTEN: Listening for connection requests from a remote TCP port
Syn-sent: Wait for a matching connection request after sending the connection request
Syn-received: Wait for confirmation of connection request after receiving and sending a connection request
Established: Represents an open connection
Fin-wait-1: Waiting for a remote TCP connection interrupt request, or confirmation of a previous connection interrupt request
Fin-wait-2: Waiting for connection interrupt request from remote TCP
Close-wait: Waiting for a connection interrupt request from a local user
CLOSING: Waiting for remote TCP to confirm connection interruption
Last-ack: Waiting for acknowledgement of the original connection interrupt request to the remote TCP
Time-wait: Wait enough time to ensure that the remote TCP receives a connection interrupt request acknowledgement
CLOSED: No connection status


Last_ack 5
SYN_RECV 30
Established 1597
Fin_wait1 51
Fin_wait2 504
Time_wait 1057
One of the
SYN_RECV indicates the number of requests waiting to be processed;
Established indicates the normal data transmission status;
Time_wait indicates the number of requests that have finished processing and waiting for the timeout to expire.

---------------------------------------------------------------------------------------------

To view the number of Apache concurrent requests and their TCP connection status

View the number of httpd processes (that is, the number of concurrent requests that Apache can handle in prefork mode):
Linux commands:

Ps-ef | grep httpd | Wc-l

Examples of returned results:
1388
Indicates that Apache can handle 1388 concurrent requests, which Apache can automatically adjust according to the load, and the peak of each of my set of servers has reached 2002.

To view the number of concurrent requests for Apache and its TCP connection status:
Linux commands:

Netstat-n | awk '/^tcp/{++s[$NF]} END {for (a in S) print A, s[a]} '
Examples of returned results:
Last_ack 5
SYN_RECV 30
Established 1597
Fin_wait1 51
Fin_wait2 504
Time_wait 1057
Where SYN_RECV indicates the number of requests being processed, established indicates normal data transfer status, time_wait indicates the number of requests that have been processed, waiting for the end of the timeout.
Status: Description

CLOSED: No connection is active or in progress

LISTEN: The server is waiting to enter the call

SYN_RECV: A connection request has arrived, waiting for confirmation

Syn_sent: Application has started, open a connection

Established: Normal data transfer status

FIN_WAIT1: Application says it's done

Fin_wait2: The other side has agreed to release

Itmed_wait: Waiting for all packets to die

CLOSING: Both sides try to close simultaneously

Time_wait: The other side has initialized a release

Last_ack: Waiting for all packets to die



vim/etc/sysctl.conf
Edit the file, add the following:
Net.ipv4.tcp_ syncookies = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_fin_timeout = $
Then execute/ Sbin/sysctl-p let the parameters take effect.

Net.ipv4.tcp_syncookies = 1 means that Syn cookies are turned on. When a SYN wait queue overflow occurs, cookies are enabled to protect against a small number of SYN attacks, the default is 0, which means close;
Net.ipv4.tcp_tw_reuse = 1 means turn on reuse. Allow time-wait sockets to be reused for new TCP connections, which is 0 by default, and
Net.ipv4.tcp_tw_recycle = 1 for fast recycling of time-wait sockets on TCP connections, which defaults to 0. Indicates close.
Net.ipv4.tcp_fin_timeout Modify the default timeout time

The meanings of the Time_wait status are attached below:

The port on the server-side connection after the client establishes a TCP/IP connection to the server and closes the socket
Status is Time_wait

Are all sockets that perform an active shutdown enter the TIME_WAIT state?
Is there a situation where the active shut-down socket goes directly into the closed state?

A party that is actively closed after sending the last ACK
Will enter the TIME_WAIT state to stay 2MSL (max segment lifetime) time
This is necessary for TCP/IP, which means "solution".

That's what TCP/IP designers were designed to do.
There are two main reasons
1. Prevent packages in the last connection, re-emerge after getting lost, affect new connections
(After 2MSL, all duplicate packets in the last connection will disappear)
2. Reliable shutdown of TCP connections
The last ACK (FIN) sent at the active shutdown may be lost, when the passive side will resend
Fin, if the active side is in the CLOSED state at this point, will respond to RST instead of ACK. So
The active side should be in a time_wait state, not a CLOSED.

Time_wait does not occupy a significant amount of resources unless it is under attack.

Also, if a party send or recv timeout, it will go directly into the CLOSED state


How to reasonably set the maximum number of connections for Apache httpd?

There is an increase in the number of online sites on hand and very slow access. Initially think that the server resources are insufficient, but after repeated testing, once connected, and constantly click on the same page on the different links, can be opened quickly, this phenomenon is that the maximum number of Apache connection is full, new visitors can only wait in line to have free links, and if once connected, in Keeyalive Lifetime (KeepAliveTimeout, default 5 seconds) does not have to reopen the connection, so the solution is to increase the maximum number of Apache connections.

1. Where to set up?
Apache 2.24, using the default configuration (FreeBSD does not load the custom MPM configuration by default), the default maximum number of connections is 250

Load the MPM configuration in/usr/local/etc/apache22/httpd.conf (remove the previous comment):
# Server-pool Management (MPM specific)
Include etc/apache22/extra/httpd-mpm.conf

The visible MPM configuration in/usr/local/etc/apache22/extra/httpd-mpm.conf, but the inside according to httpd work mode of a lot of blocks, which is the current httpd mode of work? You can view it by executing apachectl-l:
Compiled in Modules:
Core.c
Prefork.c
Http_core.c
Mod_so.c

See the Prefork word, so the current httpd should be working in Prefork mode, the default configuration for Prefork mode is:
<ifmodule mpm_prefork_module>
Startservers 5
Minspareservers 5
Maxspareservers 10
MaxClients 150
Maxrequestsperchild 0
</IfModule>

2. How much do you want to add?

The number of connections in theory is, of course, the larger the better, but within the capabilities of the server, this is related to the server's CPU, memory, bandwidth, and so on.

To view the current number of connections, you can use:
PS aux | grep httpd | Wc-l

Or:
Pgrep httpd|wc-l

Calculate the average number of httpd occupied memory:
PS aux|grep-v Grep|awk '/httpd/{sum+=$6;n++}; End{print sum/n} '

Because the basic is a static page, CPU consumption is very low, each process takes up memory is not much, about 200K.

Server memory 2G, except for the general start of the service needs about 500M (conservative estimate), and the remaining 1.5G available, then theoretically support 1.5*1024*1024*1024/200000 = 8053.06368

About 8K processes, support 2W people at the same time access should be no problem (can guarantee that 8 k people access quickly, others may need to wait 1, 2 seconds to connect, and once the connection will be very smooth)

MaxClients that control the maximum number of connections, so you can try to configure:
<ifmodule mpm_prefork_module>
Startservers 5
Minspareservers 5
Maxspareservers 10
Serverlimit 5500
MaxClients 5000
Maxrequestsperchild 100
</IfModule>

Note that the default maximum of MaxClients is 250, if you want to set Serverlimit explicitly, and serverlimit to be placed before maxclients, the value is either less than maxclients, or you will be prompted when you restart httpd.

After restarting httpd, by repeatedly executing the pgrep httpd|wc-l to observe the number of connections, you can see the number of connections in the maxclients to achieve the value of no longer increase, but at this time to visit the site is also very smooth, then no greedy and set higher value, Otherwise, if the site visit sudden increase will consume light server memory, according to the future access pressure trend and memory occupancy changes and then gradually adjust until an optimal setting value is found.

(Maxrequestsperchild cannot be set to 0 and may cause server crashes due to memory leaks)

Formula for better maximum value calculation:

Apache_max_process_with_good_perfermance < (total_hardware_memory/apache_memory_per_process) * 2
apache_max_process = apache_max_process_with_good_perfermance * 1.5

Report:

Real-time detection of httpd connections:
Watch-n 1-d "Pgrep httpd|wc-l"

View the number of TCP connections in Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.