def compute_dest_based_graphs(msms):
    path_dict = {}
    for msm in msms:
        info = parse.mmt_info(msm)
        # Start time should not be < Jan 1st 2016
        #if info['start_time'] < 1451606400: continue
        if info['type']['af'] != 4: continue
        dst_asn = info['dst_asn']
        if not dst_asn:
            continue
        if not info['is_oneoff']:
            period = int(info['interval'])
            if info['stop_time']:
                stop_time = int(info['stop_time'])
            else:
                stop_time = int(time.time())
            start = stop_time - 5 * period
            end = stop_time
            try:
                data = parse.parse_msm_trcrt(msm,
                                             start=start,
                                             end=end,
                                             count=500)
            except urllib2.HTTPError:
                continue
        else:
            data = parse.parse_msm_trcrt(msm)
        for d in data:
            src_asn = prb.get_probe_asn(d['prb_id'])
            stop_ts = d['endtime']
            if not src_asn:
                continue
            aslinks = asp.traceroute_to_aspath(d)
            if not aslinks['_links']: continue
            aslinks = ixp.remove_ixps(aslinks)
            path = []
            prev = None
            contiguous = True
            for link in aslinks:
                if prev:
                    if not link['src'] == prev['dst']:
                        contiguous = False
                prev = link
            if not contiguous: continue
            for link in aslinks:
                if link['src'] == dst_asn:
                    break
                path.append(int(link['src']))
            path.append(int(link['dst']))
            if path[-1] != dst_asn:
                continue
            path = tuple(path)
            if "%s-%s" % (src_asn, dst_asn) in path_dict:
                path_dict["%s-%s" % (src_asn, dst_asn)][stop_ts] = path
            else:
                path_dict["%s-%s" % (src_asn, dst_asn)] = {stop_ts: path}

    print path_dict.keys()
    return path_dict
def compute_dest_based_graphs(msms):
    dest_based_graphs = {}
    for msm in msms:
        info = parse.mmt_info(msm)
        # Start time should not be < Jan 1st 2016
        if info['start_time'] < 1451606400: continue
        if info['type']['af'] != 4: continue
        dst_asn = info['dst_asn']
        if not dst_asn:
            continue
        if not info['is_oneoff']:
            period = int(info['interval'])
            if info['stop_time']:
                stop_time = int(info['stop_time'])
            else:
                stop_time = int(time.time())
            start = stop_time - 5 * period
            end = stop_time
            try:
                data = parse.parse_msm_trcrt(msm,
                                             start=start,
                                             end=end,
                                             count=500)
            except urllib2.HTTPError:
                continue
        else:
            data = parse.parse_msm_trcrt(msm)
        if dst_asn in dest_based_graphs:
            G = dest_based_graphs[dst_asn]
        #elif graph_on_disk(dst_asn):
        #    G = get_graph_on_disk(dst_asn)
        else:
            G = Graph()
            vprop_asn = G.new_vertex_property("int")
            G.vp.asn = vprop_asn
            eprop_type = G.new_edge_property("short")
            G.ep.type = eprop_type
            eprop_msm = G.new_edge_property("long")
            G.ep.msm = eprop_msm
            eprop_probe = G.new_edge_property("int")
            G.ep.probe = eprop_probe
            eprop_dict = G.new_edge_property("object")
            G.ep.origin = eprop_dict

        assert 'asn' in G.vp
        assert 'type' in G.ep
        assert 'msm' in G.ep
        assert 'probe' in G.ep
        assert 'origin' in G.ep
        for d in data:
            src_asn = prb.get_probe_asn(d['prb_id'])
            if not src_asn:
                continue
            aslinks = asp.traceroute_to_aspath(d)
            if not aslinks['_links']: continue
            aslinks = ixp.remove_ixps(aslinks)
            for link in aslinks:
                if link['src'] == dst_asn:
                    break
                v1 = find_vertex(G, G.vp.asn, link['src'])
                if not v1:
                    v1 = G.add_vertex()
                    G.vp.asn[v1] = int(link['src'])
                else:
                    assert len(v1) == 1
                    v1 = v1[0]

                v2 = find_vertex(G, G.vp.asn, link['dst'])
                if not v2:
                    v2 = G.add_vertex()
                    G.vp.asn[v2] = int(link['dst'])
                else:
                    assert len(v2) == 1
                    v2 = v2[0]

                assert G.vp.asn[v1] == int(link['src'])
                assert G.vp.asn[v2] == int(link['dst'])
                ed = G.edge(v1, v2)
                if not ed:
                    ed = G.add_edge(v1, v2)
                if not G.ep.origin[ed]:
                    G.ep.origin[ed] = {src_asn: 1}
                elif src_asn in G.ep.origin[ed]:
                    G.ep.origin[ed][src_asn] += 1
                else:
                    G.ep.origin[ed][src_asn] = 1
                if link['type'] == 'i':
                    G.ep.type[ed] = 1
                else:
                    G.ep.type[ed] = 0
                G.ep.msm[ed] = msm
                G.ep.probe[ed] = int(d['prb_id'])
        dest_based_graphs[dst_asn] = G

    print dest_based_graphs.keys()
    return dest_based_graphs
path_comp = []
for tup in msms:
    msm = tup[-1]
    src_asn = tup[0]
    dst_asn = tup[1]
    try:
        data = parse.parse_msm_trcrt(msm)
    except:
        pdb.set_trace()
        continue
    for d in data:
        src_asn = int(ripeprobes.get_probe_asn(d['prb_id']))
        dst_asn = int(ip2asn.ip2asn_bgp(d['dst_addr']))
        if not path_in_cache(src_asn, dst_asn): continue
            
        aslinks = asp.traceroute_to_aspath(d)
        if not aslinks['_links']: continue
        aslinks = ixp.remove_ixps(aslinks)
        hops = []
        for link in aslinks:
            hops.append(int(link['src']))
        hops.append(int(link['dst']))
        pc_path = get_most_probable_path(src_asn, dst_asn, threshold=5)
        path_comp.append([pc_path, hops])
pdb.set_trace()
accuracy = 0
for p in path_comp:
    if p[-1] in p[0]:
        accuracy += 1

print accuracy, len(path_comp)
Beispiel #4
0
def compute_dest_based_graphs(msms):
    dest_based_graphs = {}
    for msm in msms:
        info = parse.mmt_info(msm)
        # Start time should not be < Jan 1st 2016
        if info['start_time'] < 1451606400: continue
        if info['type']['af'] != 4: continue
        dst_asn = info['dst_asn']
        dst_addr = info['dst_addr']
        if not dst_addr or not dst_asn:
            continue
        rnode = ip2asn.ip_to_pref(dst_addr)
        if rnode:
            dst_prefix = rnode.prefix.replace('/', '_')
        else:
            continue
        #if not dst_asn:
        #    continue
        if not info['is_oneoff']:
            period = int(info['interval'])
            if info['stop_time']:
                stop_time = int(info['stop_time'])
            else:
                stop_time = int(time.time())
            start = stop_time - 5 * period
            end = stop_time
            try:
                data = parse.parse_msm_trcrt(msm,
                                             start=start,
                                             end=end,
                                             count=500)
            except urllib2.HTTPError:
                continue
        else:
            data = parse.parse_msm_trcrt(msm)

        if dst_prefix in dest_based_graphs:
            G = dest_based_graphs[dst_prefix]
            root_node = find_vertex(G, G.vp.prefix, dst_prefix)
            assert len(root_node) == 1
            root_node = root_node[0]
            assert G.vp.asn[root_node] == dst_asn
        else:
            G = Graph()
            vprop_asn = G.new_vertex_property("int")
            G.vp.asn = vprop_asn
            eprop_type = G.new_edge_property("short")
            G.ep.type = eprop_type
            eprop_msm = G.new_edge_property("long")
            G.ep.msm = eprop_msm
            eprop_probe = G.new_edge_property("int")
            G.ep.probe = eprop_probe
            eprop_dict = G.new_edge_property("object")
            G.ep.origin = eprop_dict
            vprop_prefix = G.new_vertex_property("string")
            G.vp.prefix = vprop_prefix
            eprop_ts = G.new_edge_property("int64_t")
            G.ep.ts = eprop_ts
            root_node = G.add_vertex()
            G.vp.prefix[root_node] = dst_prefix
            G.vp.asn[root_node] = dst_asn

        assert 'asn' in G.vp
        assert 'type' in G.ep
        assert 'msm' in G.ep
        assert 'probe' in G.ep
        assert 'origin' in G.ep
        assert 'ts' in G.ep
        assert 'prefix' in G.vp

        for d in data:
            src_asn = prb.get_probe_asn(d['prb_id'])
            if not src_asn:
                continue
            aslinks = asp.traceroute_to_aspath(d)
            if not aslinks['_links']: continue
            if str(dst_asn) not in ixp.IXPs:
                aslinks = ixp.remove_ixps(aslinks)
            else:
                aslinks = aslinks['_links']
            for link in aslinks:
                if int(link['src']) == dst_asn:
                    break
                v1 = find_vertex(G, G.vp.asn, link['src'])
                if not v1:
                    v1 = G.add_vertex()
                    G.vp.asn[v1] = int(link['src'])
                else:
                    assert len(v1) == 1
                    v1 = v1[0]

                v2 = find_vertex(G, G.vp.asn, link['dst'])
                if not v2:
                    v2 = G.add_vertex()
                    G.vp.asn[v2] = int(link['dst'])
                else:
                    assert len(v2) == 1
                    v2 = v2[0]

                assert G.vp.asn[v1] == int(link['src'])
                assert G.vp.asn[v2] == int(link['dst'])
                ed = G.edge(v1, v2)
                if not ed:
                    ed = G.add_edge(v1, v2)
                if not G.ep.origin[ed]:
                    G.ep.origin[ed] = {src_asn: 1}
                elif src_asn in G.ep.origin[ed]:
                    G.ep.origin[ed][src_asn] += 1
                else:
                    G.ep.origin[ed][src_asn] = 1
                if link['type'] == 'i':
                    G.ep.type[ed] = 1
                else:
                    G.ep.type[ed] = 0
                G.ep.msm[ed] = msm
                G.ep.ts[ed] = d['endtime']
                G.ep.probe[ed] = int(d['prb_id'])

        assert root_node.out_degree() == 0
        #if not root_node.in_degree() > 0:
        #    pdb.set_trace()
        dest_based_graphs[dst_prefix] = G

    print dest_based_graphs.keys()
    return dest_based_graphs