8

I want to know how many people are connected to my server. Since I'm doing comet applications, this is important

Alex
  • 8,471
  • 26
  • 75
  • 99
  • 3
    What kind of server? What OS? If it's a web server which one? Your choice of tags really don't help at all. – John Gardeniers May 13 '11 at 04:36
  • Bear in mind that HTTP does not commonly maintain connections. So, users will only connect temporarily to pull a web-page, then immediately disconnect. Simply looking at open connections will not give you the number of people viewing your website. – Chris S May 13 '11 at 12:37

4 Answers4

29

There are about a zillion ways to do this but:

netstat | grep http | wc -l

Keep it mind that http is a stateless protocol. Each line can represent one client opening multiple sockets to grab different files (css, images, etc) that will hang out for awhile in a timewait state.

Bart De Vos
  • 17,911
  • 6
  • 63
  • 82
David
  • 501
  • 4
  • 2
7

Below are some commands of netstat using which you can check the number of connections a server has.

To display all active Internet connections to the servers, only established connections are included.

netstat -na

To display only active Internet connections to the server at port 80 and sort the results, allow to recognize many connections coming from one IP

netstat -an | grep :80 | sort

To display the list of the all IP addresses involved instead of just count.

netstat -n -p | grep SYN_REC | sort -u
Nakilon
  • 128
  • 1
  • 1
  • 8
Stephen Lembert
  • 222
  • 2
  • 2
4

If your webserver is apache, you can also use the server-status page (after enabled it).

lg.
  • 4,649
  • 3
  • 21
  • 20
3

I know this is an old question, but I found it while looking for a tidy way to count all incoming connections to a given port. The trouble with a blanket search for a port is that you'll also pick up outgoing connections, skewing your numbers.

Eg:

tcp        0      0 10.224.38.158:8080          10.225.239.6:38194          ESTABLISHED
tcp        0      0 10.224.38.158:8080          10.225.239.6:56825          ESTABLISHED
tcp        0      0 10.224.38.158:51512         10.226.194.28:8080          ESTABLISHED
tcp        0      0 10.224.38.158:51960         10.226.194.28:8080          TIME_WAIT
tcp        0      0 10.224.38.158:46937         10.226.194.28:8080          ESTABLISHED
tcp        0      0 10.224.38.158:51331         10.226.194.28:8080          TIME_WAIT
tcp        0      0 10.224.38.158:8080          10.225.239.6:29634          ESTABLISHED

An indiscriminate grep -c or awk | wc -l would return "7," but there are actually only "3" inbound connections. Anyway, I haven't found a graceful way to do this, and my awk-fu's not so not, but I ultimately opted to netstat -an, use awk to print only the "local address" column, and count the instances of the port I'm looking for.

netstat -an | grep -c 8080

OUTPUT : 91

netstat -an | awk '{print $4}' | grep -c 8080

OUTPUT : 31

So, 31 connected sockets on port 8080. I hope that helps. If anyone knows a more concise way to do this, then please do share it.

Niall
  • 31
  • 1
  • 3
    Whenever you use `awk` and `grep` in quick succession, chances are that `awk` alone will do the job. `netstat -an | awk '$5 ~ /:8080$/ { C++ } END { print C }'` – 200_success Aug 26 '13 at 19:55