def append_retrans_rate(ifname): """ Append retransmission rate into file name """ reader = tcplog.TcpLogReader(ifname) retrans_rate = reader.get_retrans_rate() ofname = "%s_retrans_%.3f.log" % (ifname[:-4], retrans_rate * 100.0) shutil.move(ifname, ofname)
def process_connection(ifname): reader = tcplog.TcpLogReader(ifname) reader.read_and_parse() reader.find_lost_packets() data_pkts = [item for item in reader.data if is_sent_data_packet(item)] data_num = len(data_pkts) lost_num = len([item for item in data_pkts \ if tcplog.packet_is_lost(item)]) results = { "data_num": data_num, "lost_num": lost_num, "policing": [], } for cutoff in [0, 2]: policing_params = get_policing_params_for_records(data_pkts, cutoff=cutoff) results["policing"].append({ "cutoff": cutoff, "is_policed": policing_params.result_code == RESULT_OK, "policing_rate": policing_params.policing_rate_bps, "burst_size": policing_params.burst_size, }) return results
def split_connections( iname="tcp-stat.log", oname="tcp-sep.log", cname="connections.dat", odir="./", ): reader = tcplog.TcpLogReader(iname) #reader.check_trace_dir() #reader.oname = oname #reader.cname = cname reader.split_connection( odir=odir, cname=cname, oname=oname, human_readable=False, )
def test(): log_dir = "/Volumes/NT/tcplog/tcplog1/tcp-log-separate" fig_dir = "/Volumes/NT/tcplog/tcplog1/figs" fig_odir = "tcp-log-classified/jitter-test" tcplog.check_dir(fig_odir) total_len = len(os.listdir(log_dir)) proce_num = 0 pbar_widgets = [ "Classify: ", progressbar.Percentage(), progressbar.Bar(), progressbar.AdaptiveETA() ] pbar = progressbar.ProgressBar( max_value=total_len, widgets=pbar_widgets, ) for log_fname in os.listdir(log_dir): proce_num += 1 pbar.update(proce_num) fname_root = os.path.splitext(log_fname)[0] iname = os.path.join(fig_dir, fname_root + ".png") if not os.path.exists(iname): continue log_fname = os.path.join(log_dir, log_fname) reader = tcplog.TcpLogReader(log_fname) irtts = reader.calc_instant_rtt_from_ts() if len(irtts) == 0: continue jv = calc_jitter_values(irtts) oname = "jitter-%d-%dms-%d-%s.png" % ( int(jv["jitter_cv"] * 1000), int(jv["avg_amp"] * 1000), int(jv["avg_amp_incr_ratio"] * 1000), fname_root, ) #oname = "jitter-%dms-%d-%s.png" % ( # int(jv["avg_amp"]*1000), # int(jv["avg_amp_incr_ratio"]*1000), # fname_root, #) oname = os.path.join(fig_odir, oname) shutil.copyfile(iname, oname)
def split_connections_sep_file( tname="tcp-sep.log", cname="connections.dat", odir="tcp-log-separate", ): check_dir(odir) lnum = 0 reader = tcplog.TcpLogReader("/dev/null") with open(tname) as tfp, open(cname) as cfp: for line in cfp: if line.lstrip()[0] == "#": continue conn = tcplog.parse_line_connection(line) if lnum > conn["from"]: logging.error( "Connection data invalid:" + "line number is from %d " % conn["from"] + "but in previous connection, line number is to %d " % lnum) while lnum < conn["from"]: tfp.readline() lnum += 1 conn_data, reader.data = [], [] while lnum < conn["to"]: line = tfp.readline() conn_data.append(line) reader.data.append(tcplog.parse_line(line)) lnum += 1 retrans_rate = reader.get_retrans_rate() ofname = "%s_%d_%s_%d_dur_%.3f-%.3f_retrans_%.3f.log" % ( tcplog.ipaddr_ntos(conn["srcaddr"]), conn["srcport"], tcplog.ipaddr_ntos(conn["dstaddr"]), conn["dstport"], conn["stime"], conn["etime"], retrans_rate * 100, ) ofname = os.path.join(odir, ofname) with open(ofname, "w") as ofp: for line in conn_data: ofp.write(line)