Thursday, February 23, 2012

measure bandwidth for a port or a process on Linux

I would like to know how much data goes between my application and the Oracle database, in order to assess a WAN link

I came up with the idea of capturing all the traffic towards the database:

nohup tcpdump -p  -s0 -w ./t.cap -C1 host and port 1521 &

The C1 options will create a new capture file as soon as it hits 1MB:

[root@godzilla test]# ls -ltr
total 16260
-rw-r--r-- 1 pcap pcap 1000105 Feb 21 08:42 t.cap
-rw-r--r-- 1 pcap pcap 1000210 Feb 21 09:08 t.cap1
-rw-r--r-- 1 pcap pcap 1000214 Feb 21 09:31 t.cap2
-rw-r--r-- 1 pcap pcap 1000058 Feb 21 10:12 t.cap3
-rw-r--r-- 1 pcap pcap 1000544 Feb 21 10:27 t.cap4

Then I can use the date and produce a nice chart with the script at the bottom:

Result is MB per minutes. Not perfect for peaks but at least I get an idea...
(This this on a test DB, with only a few developers and QA accessing. )

These days I force myself to use Python, here is the script I used:

import datetime
import os
d=os.popen("ls -l | grep t.cap ")
lsTuples=[ x.split() for x in lsRaw]
tstampString=[ "%s %s %s" % (mon,day,time) for (perm,c,owner,group,size,mon,day,time,fname) in lsTuples ]

monthValues={ 'Jan':1, 'Feb':2, 'Mar':3 }
tstamps=[ datetime.datetime(2012, monthValues[mon], int(day), int(time.split(':')[0]),int(time.split(':')[1]) ) for (perm,c,owner,group,size,mon,day,time,fname) in lsTuples ]


for x in tstampString:
if not mbPerDuration.has_key(x):

for t in mbPerDuration.keys():
print "%s, %d" % (t, mbPerDuration[t])