Number of connections in the linux comet model

Source: Internet
Author: User
Tags get ip hex to decimal port number

The comet model hosts maintain persistent connections, and the number of connections to a single host can reach one million (currently, the maximum number of connections to a single host can reach about 0.6 million ), netstat does not respond for a long time when calculating the network connection status, but the SS command statistics are relatively fast, but the output is not beautiful enough. This article mainly summarizes the common statistical methods for the number of connections in the comet model. The tcp connection host collects statistics by reading/proc/net/tcp files. The meanings of each item value are shown in the figure below (click to view the larger image)

 

 

I. Number of connections in C ++ code

[Root @ 361way netstat] # cat ss ++. cc
// Code from www.361way.com
# Include <iostream>
# Include <fstream>
# Include <cstdlib>
# Include <sstream>
Using namespace std;
// Struct: Property of a tcp connection
Struct ConnectionProperty
{
String local_addr;
String remote_addr;
String state;
};
// Change hexadecimal number to decimal number
Int HexToInt (char h)
{
If (h> = '0' & h <= '9 ')
    {
Return h-'0 ';
    }
Else if (h> = 'A' & h <= 'F ')
    {
Return h-'A' + 10;
    }
Else
    {
Cerr <"Error: Illegal Hex Number! "<Endl;
Return-1;
    }
}
// Get Ip address and port number from string XXXXXXXX: XXXX
String GetIpAddress (char * str)
{
Int a, B, c, d, e;
A = HexToInt (str [0]) * 16 + HexToInt (str [1]);
B = HexToInt (str [2]) * 16 + HexToInt (str [3]);
C = HexToInt (str [4]) * 16 + HexToInt (str [5]);
D = HexToInt (str [6]) * 16 + HexToInt (str [7]);
E = HexToInt (str [9]) * 16*16*16 +
HexToInt (str [10]) * 16*16 +
HexToInt (str [11]) x 16 +
HexToInt (str [12]);
// Change int to string
String sa, sb, SC, sd, se;
Ostringstream oss;
Oss <;
Sa = oss. str ();
Oss. str (""); // clear the content in oss
Oss <B;
Sb = oss. str ();
Oss. str ("");
Oss <c;
SC = oss. str ();
Oss. str ("");
Oss <d;
Sd = oss. str ();
Oss. str ("");
Oss <e;
Se = oss. str ();
Oss. str ("");
// Return by order: d. c. B. a: e
Return sd + '.' + SC + '.' + sb + '.' + sa + ':' + se;
}
// Get tcp connection state
String GetConnectionState (char * str)
{
If (str [0] = '0' & str [1] = '0') return "ERROR_STATUS ";
If (str [0] = '0' & str [1] = '1') return "TCP_ESTABLISHED ";
If (str [0] = '0' & str [1] = '2') return "TCP_SYN_SENT ";
If (str [0] = '0' & str [1] = '3') return "TCP_SYN_RECV ";
If (str [0] = '0' & str [1] = '4') return "TCP_FIN_WAIT1 ";
If (str [0] = '0' & str [1] = '5') return "TCP_FIN_WAIT2 ";
If (str [0] = '0' & str [1] = '6') return "TCP_TIME_WAIT ";
If (str [0] = '0' & str [1] = '7') return "TCP_CLOSE ";
If (str [0] = '0' & str [1] = '8') return "TCP_CLOSE_WAIT ";
If (str [0] = '0' & str [1] = '9') return "TCP_LAST_ACK ";
If (str [0] = '0' & str [1] = 'a') return "TCP_LISTEN ";
If (str [0] = '0' & str [1] = 'B') return "TCP_CLOSING ";
Return "UNKNOWN_STATE ";
}
Int main ()
{
// Read from file/proc/net/tcp
Ifstream infile ("/proc/net/tcp", ios: in );
If (! Infile)
    {
Cerr <"open error! "<Endl;
Exit (1 );
    }
String s;
Char s_local_addr [20], s_remote_addr [20], s_state [20];
Getline (infile, s); // title: every column's name
While (getline (infile, s ))
    {
Sscanf (s. c_str (), "% * s % s", s_local_addr, s_remote_addr, s_state );
// Printf ("% s \ t % s \ n", s_local_addr, s_remote_addr, s_state );
String ip_local = GetIpAddress (s_local_addr );
String ip_remote = GetIpAddress (s_remote_addr );
String conn_state = GetConnectionState (s_state );
Cout <ip_local <"\ t" <ip_remote <"\ t" <conn_state <endl;
    }
Return 0;
}
Compile the following code to generate an executable file:

G ++-o netinfo ss ++. cc
The execution output is as follows:

[Root @ comethost ~] #./Netinfo
115.28.174.118: 5224 121.31.251.8: 13351 TCP_ESTABLISHED
115.28.174.118: 5229 223.104.23.84: 62815 TCP_ESTABLISHED
115.28.174.118: 5225 117.136.79.70: 45116 TCP_ESTABLISHED
115.28.174.118: 5224 112.17.243.208: 33236 TCP_ESTABLISHED
115.28.174.118: 5224 27.204.14.135: 44976 TCP_ESTABLISHED
115.28.174.118: 5228 58.18.233.29: 60135 TCP_ESTABLISHED
115.28.174.118: 5226 112.17.246.84: 26055 TCP_ESTABLISHED
II. python tcp connection statistics

#! /Usr/bin/env python
# Code from www.361way.com
# Coding = UTF-8
Import pwd
Import OS
Import re
Import glob
PROC_TCP = "/proc/net/tcp"
STATE = {
'01': 'established ',
'02': 'syn _ SENT ',
'03 ': 'syn _ recv ',
'04 ': 'Fin _ WAIT1 ',
'05 ': 'Fin _ WAIT2 ',
'06': 'Time _ WAIT ',
'07 ': 'close ',
'08 ': 'Close _ wait ',
'09 ': 'Last _ ack ',
'0a': 'listen ',
'0b': 'closing'
        }
Def _ load ():
'''READ the table of tcp connections & remove header '''
With open (PROC_TCP, 'r') as f:
Content = f. readlines ()
Content. pop (0)
Return content
Def _ hex2dec (s ):
Return str (int (s, 16 ))
Def _ ip (s ):
Ip = [(_ hex2dec (s []), (_ hex2dec (s []), (_ hex2dec (s []), (_ hex2dec (s [0: 2])]
Return '.'. join (ip)
Def _ remove_empty (array ):
Return [x for x in array if x! = '']
Def _ convert_ip_port (array ):
Host, port = array. split (':')
Return _ ip (host), _ hex2dec (port)
Def netstat ():
'''
Function to return a list with status of tcp connections at linux systems
To get pid of all network process running on system, you must run this script
As superuser
'''
Content = _ load ()
Result = []
For line in content:
Line_array = _ remove_empty (line. split ('') # Split lines and remove empty spaces.
Rochelle host, l_port = _ convert_ip_port (line_array [1]) # Convert ipaddress and port from hex to decimal.
R_host, r_port = _ convert_ip_port (line_array [2])
State = STATE [line_array [3]
Nline = [l_host + ':' + l_port, r_host + ':' + r_port, state]
Result. append (nline)
Return result
If _ name _ = '_ main __':
For conn in netstat ():
Print conn
III. ss statistics

After the iproute2 package is installed by default, it contains the ss tool, but you want to view the ss source code can be viewed in the https://github.com/shemminger/iproute2/blob/master/misc/ss.c.

IV. Statistical rate statistics

Here, only one comet server with a connection number of more than 0.2 million is found for testing. The execution result is as follows:

Fastest ss-nt execution speed

The netinfo of c ++ is close to the ss (a little slower than the ss of c)

Python is five to six times slower than ss and netinfo.

Netstat has not been tested. According to previous knowledge, it is estimated that it is close to or worse than the python version.

Therefore, in the future, there will be a simple demand for statistics on the number of connections on the current network. The c ++ version of ss will be preferred (with beautiful output) and can be customized on this basis.

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.