How to Monitor Active HTTP Connections for Comet Applications in Linux/Windows Servers


4 views

When implementing Comet applications (long-polling, HTTP streaming, or WebSockets), monitoring active HTTP connections becomes crucial for:

  • Server capacity planning
  • Identifying connection leaks
  • Detecting DDoS attacks
  • Performance optimization

Using netstat:

netstat -an | grep :80 | grep ESTABLISHED | wc -l

Using ss (faster alternative):

ss -o state established '( dport = :http or sport = :http )' | wc -l

Apache Server Specific:

apachectl fullstatus | grep -c "ESTABLISHED"

Using PowerShell:

Get-NetTCPConnection -State Established | Where-Object {$_.LocalPort -eq 80 -or $_.RemotePort -eq 80} | Measure-Object | Select-Object -ExpandProperty Count

Using netstat in CMD:

netstat -ano | find ":80" | find "ESTABLISHED" /c

Node.js Implementation:

const net = require('net');
const http = require('http');

const server = http.createServer();
server.getConnections((err, count) => {
    console.log(Active connections: ${count});
});

Python Implementation:

import psutil

def count_http_connections():
    count = 0
    for conn in psutil.net_connections(kind='tcp'):
        if conn.status == 'ESTABLISHED' and (conn.laddr.port == 80 or conn.raddr.port == 80):
            count += 1
    return count

For Nginx with status module enabled:

curl http://localhost/nginx_status

Sample output interpretation:

Active connections: 291 
server accepts handled requests
 16630948 16630948 31070465 
Reading: 6 Writing: 179 Waiting: 106

Consider these tools for continuous monitoring:

  • Prometheus + Grafana with appropriate exporters
  • Netdata for real-time metrics
  • ELK Stack for logging and analysis

For Comet applications, implement these checks:

# Check for TIME_WAIT connections (potential leaks)
netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'

Sample output showing connection states:

ESTABLISHED 25
TIME_WAIT 183

When running comet applications or long-polling services, maintaining visibility into active HTTP connections becomes crucial for:

  • Server resource allocation
  • Identifying connection leaks
  • Scaling decisions
  • Debugging hanging requests

Linux Systems (using netstat):

netstat -anp | grep ':80' | grep ESTABLISHED | wc -l

Windows Systems:

netstat -ano | find ":80" | find "ESTABLISHED" /c

Apache HTTP Server

apachectl fullstatus | grep "GET /comet" | wc -l

Nginx

Enable the stub_status module and query:

location /nginx_status {
    stub_status on;
    allow 127.0.0.1;
    deny all;
}

Node.js implementation example:

const http = require('http');
let connectionCount = 0;

const server = http.createServer((req, res) => {
    connectionCount++;
    req.on('close', () => connectionCount--);
});

// Add status endpoint
server.on('request', (req, res) => {
    if (req.url === '/status') {
        res.end(Active connections: ${connectionCount});
    }
});

For more sophisticated monitoring, consider these approaches:

  • Implementing connection pooling metrics
  • Using APM tools like New Relic or Datadog
  • Building custom middleware for your framework
  • Leverosing WebSocket connection tracking libraries
// Shared memory counter
$shm_id = shmop_open(ftok(__FILE__, 't'), "c", 0644, 100);

function increment_connections() {
    global $shm_id;
    $count = (int)shmop_read($shm_id, 0, 10);
    shmop_write($shm_id, str_pad($count+1, 10), 0);
}

function get_connection_count() {
    global $shm_id;
    return (int)shmop_read($shm_id, 0, 10);
}

Typical connection duration patterns in comet applications:

Connection Type Average Duration
Regular HTTP 2-5 seconds
Long-polling 30-60 seconds
WebSocket Minutes to hours