Linux view the number of connections for a port

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

grep "  the  WC -L

2) Count httpd protocol connections

PS grep WC -L

3) The statistics are connected, the status is "established

grep WC -L

4), find out which IP address is the most connected, sealed it

grep awk {print $5}| awk -F: {print $1sortuniqsort -r +grepawk {print $5}| awk -F: {print $1sortuniqsort -R +0n

Instance:

1. View the current number of concurrent accesses to Apache:

grep WC -L

Compare the number of maxclients in httpd.conf.

2. How many processes are viewed:

PS grep WC -L

3, you can use the following parameters to view the data

PS grep WC -l1388

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.

grep "  the " WC -l4341

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.

grep WC -l376

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.

grep WC

To view detailed records of all established connections

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

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
    • 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.

4, if the discovery system has a large number of time_wait state connection, by adjusting the kernel parameters to solve

Vim/etc/sysctl.conf

Edit the file and add the following:

11 1

And then execute

Let the parameters take effect.

Attach the meaning of the TIME_WAIT state:

    • Net.ipv4.tcp_syncookies = 1 means that Syn cookies are turned on. When there is a SYN wait queue overflow, 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. Allows time-wait sockets to be re-used for new TCP connections, which defaults to 0, which means shutdown;
    • Net.ipv4.tcp_tw_recycle = 1 means a fast recycle of time-wait sockets in the TCP connection is turned on, and the default is 0, which means shutdown.
    • Net.ipv4.tcp_fin_timeout Modify the default timeout time for the system

5, the client and the server to establish a TCP/IP connection after shutting down the socket, the server-side connection port status of Time_wait is not all the active shutdown of the socket will enter the TIME_WAIT state? Is there a situation where the active shut-down socket goes directly into the closed state?

The active closed side after sending the last Ack, will enter the TIME_WAIT state to stay 2MSL (max segment lifetime) time, this is the TCP/IP is essential, that is, "solve" not. That's what TCP/IP designers were designed to do.

There are two main reasons:

    • 1, prevent the last connection in the package, lost after the re-appearance, affecting the new connection (after 2MSL, all the duplicates in the last connection will disappear)
    • 2, the reliable shutdown TCP connection in the active shutdown of the last ACK (FIN) sent, it is possible to lose, then the passive side will be re-hair fin, if the active side in the closed state, will respond to RST instead of ACK. So the active side should be in the TIME_WAIT state, but not 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.

6, how to reasonably set the maximum number of Apache httpd connection?

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:

inch 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                                  maxclients                                        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 grep WC -L

Or:

Pgrep httpd| WC -L

Calculate the average number of httpd occupied memory:

PS aux| grep 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                                  serverlimit                     5500                 MaxClients                                     maxrequestsperchild               </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:

21.5

7, real-time detection httpd connection number:

1 " pgrep httpd|wc-l "

Reference:

http://blog.csdn.net/he_jian1/article/details/40787269 (the above content is transferred from this article)

Linux view the number of connections for a port

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.