I'm doing some comet benchmarks and would like to see how many open connections I have.

Actually I use netstat:

netstat -ant | grep 8080 | grep EST | wc -l

But it needs around 4-6 minutes to list the number, is there any tool that can do show it in real time? The number of open connections is between 100'000 - 250'000.

link|improve this question

45% accept rate
Based on what you're trying to accomplish, have you considered using NetFlow and an analyzer tool? – SpacemanSpiff Dec 13 '11 at 22:15
@SpacemanSpiff I hope there is some easy solution, but I will take a look on NetFlow as I'm not sure if is working with a HP switch. – Nenad Dec 13 '11 at 22:20
You know, I wonder if a perfmon or WMI query might bring this data back from the TCP stack faster... still... are you after open ports or active data transfers? – SpacemanSpiff Dec 13 '11 at 22:22
@SpacemanSpiff I'm after open ports looking – Nenad Dec 13 '11 at 22:27
Maybe an SNMP query?... just throwing out ideas for you. – SpacemanSpiff Dec 13 '11 at 22:41
show 1 more comment
feedback

2 Answers

up vote 4 down vote accepted

don't know if lsof is better, but give this a try:

lsof-ni:8080 -sTCP:ESTABLISHED | wc -l
link|improve this answer
actually lsof take 5 sec, but I have 20'000 connections and counting :-) will let you know how long after pass 100K – Nenad Dec 13 '11 at 22:28
update: have some issues with our comet clients, but lsof is much faster 10 sec for 30'000 connections, will update after I fixed the comet issues and was able to check above 100K connections – Nenad Dec 13 '11 at 22:35
update: 15sec to show 130'000 connections - thank you! This do the job for me for more detailed solution a network analyzer must be used. – Nenad Dec 13 '11 at 22:51
wonderfull! You could also use the switch -t - perhaps this will give you also a little boost. – ThorstenS Dec 14 '11 at 6:07
feedback

Another option would be to read /proc/net/tcp directly. To see all established TCP connections on, 8080, you would do something like

$ printf %04X 8080
1F90
$ grep :1F90 /proc/net/tcp | grep ' 01 ' | wc -l

If you want to do it in a single process (less IO overhead) and handle corner cases, the following tells you how many ESTABLISHED TCP connections have local port 8080:

$ perl -anle '
          $F[1] =~ /:1F90/ and $F[3] =~ /01/ and $cnt++;
          END { print 0+$cnt }
         '  /proc/net/tcp

If the software on your machine listening on 8080 has IPv6 support, you'll need to read /proc/net/tcp6 also; if the program's using IPv6 sockets, connections will show up there even if they're using IPv4.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.