I have a web app that is using
ActiveResource to talk to another server that has a rate limit on the
connection. I’m curious how I can best monitor that from the host my web app is running on – that is, from a bash prompt on linux from my server, how can I measure the outbound requests per second my machine is making to another?
I’m looking for a linux one-liner that given an interface, a hostname,
and/or some combination thereof, tells me the connection rate I’m
making to that server. I’ve gotten close with tools like tc and iftop, but those are reporting the amount of data transferred, not the connections made… so its not quite what I’m looking for. I’d love to see something
like:
$awesometool –host thetargethost.com –interface eth0 –interval 5
gathering statistics…
Requests per second report for thetargethost.com over interface eth0
avg: 23 req/sec min: 12 req/sec max 39 req/sec
5 samples taken
Can anyone point me to one?
tcpdump(8)can provide something very similar; search for TCP packets with theSYNflag set to catch the first packet in the three-way handshake that are destined for your other peer:You could use
/usr/bin/timeor your shell’stimebuilt-in, or do some arithmetic with the timestamps in the output, to get your average rate per second. (Use more than ten packets — this was just for demonstration.)Update
I wrote a little program to run
tcpdump(8), count the packets, and report on how many packets were sent within the interval specified:Because it asks
tcpdump(8)to use line-buffered output, I’m a little afraid that it might not scale beyond 200-300 requests per second, at least on my hardware. But without the line-buffered output,tcpdump(8)would wait until its output buffer (seesetvbuf(3)for details) would be full before sending any output, causing extremely jittery results.But if your rate of connections isn’t that high, this will probably do what you need. If your rate of connections is higher, then this little hack is probably best ignored — it strikes me that probably
iptables(8)can count streams.