#!/usr/bin/python # # Copyright (c) 2007 OLPC # Author: Ricardo Carrano # Version 0.1.2 # # this program is free software; you can redistribute it and/or modify # it under the terms of the gnu general public license as published by # the free software foundation; either version 2 of the license, or # (at your option) any later version. # # this program is distributed in the hope that it will be useful, # but without any warranty; without even the implied warranty of # merchantability or fitness for a particular purpose. see the # gnu general public license for more details. # # you should have received a copy of the gnu general public license # along with this program; if not, write to the free software # foundation, inc., 51 franklin st, fifth floor, boston, ma 02110-1301 usa import sys import commands import math from optparse import OptionParser arguments = OptionParser() arguments.add_option("-f","--pcap-file", dest="pcapfile", help="Capture dump") arguments.add_option("-t","--text-file", dest="textfile", help="Capture already converted/filtered") arguments.add_option("-i","--interval", dest="interval", help="Consolidation interval in seconds") arguments.add_option("-w","--filter", dest="filter", help="Wireshark filter") arguments.add_option("-o","--output-format", dest="output", help="Output Format [csv, lines] ") arguments.add_option("--no-fcs", action="store_false", dest="crc", default=True, help="don't check if frames have bad crc") (options, args) = arguments.parse_args() if not (options.pcapfile or options.textfile) : print "input file is mandatory" sys.exit(0) filter_exp = '' filter = '' if options.crc == True: filter += 'wlan.fcs_good == 1' if options.filter: filter += ' and '+options.filter else: filter += options.filter if options.crc or options.filter: filter_exp = '-R "'+filter+'"' if options.pcapfile: pcapfile = options.pcapfile inputfile = pcapfile if options.textfile: textfile = options.textfile inputfile = textfile else: textfile = pcapfile+'.tmp3' filter_cmd='tshark -r %s %s -T fields -e frame.time_relative -e radiotap.datarate -e frame.len > %s' % (pcapfile, filter_exp, textfile) s, o = commands.getstatusoutput(filter_cmd) if options.interval: interval = float(options.interval) else: interval = 1 timeslot = 0 lastslot = 0 airtime = [0] fd = open(textfile, 'r') cck_datarates = ('2', '4', '11', '22') ofdm_datarates = ('12', '18', '24', '36', '48', '72', '96', '108') for line in fd: time, rate, size = line.split('\t') size = size.strip('\n') if rate in cck_datarates: airsize = 192 + float(size) * 16 / float (rate) elif rate in ofdm_datarates: airsize = 26 + float(size) * 16 / float (rate) else: airsize = 0 timeslot = int(math.floor(float(time) / interval)) if timeslot > lastslot: for slot in range(lastslot, timeslot): airtime.append(0) airtime[timeslot] += airsize / (interval * 1000000) lastslot = timeslot if options.output == "csv": for i in airtime: print str(i)+',', else: for i in range(0, len(airtime)): print "[%s - %s): %.2f%%" % (i*interval, (i+1)*interval, airtime[i] * 100)