def __init__(self): # Create a new bgpstream instance # and a reusable bgprecord instances self.stream = BGPStream() self.rec = BGPRecord() self.origin_ases = set() self.bgp_lens = defaultdict(lambda: defaultdict(lambda: None))
def filter_play_updates(collectors, start_time_ts, stop_time_ts): stream = BGPStream() is_ripe = False for c in collectors: if 'rrc' in c: is_ripe = True stream.add_filter('collector', c) stream.set_data_interface_option('broker', 'url', 'https://bgpstream-dev.caida.org/broker') stream.add_filter('prefix', '0.0.0.0/0') # Only focus on IPV4 stream.add_filter('record-type', 'ribs') stream.add_filter('record-type', 'updates') # Find the time when the last rib was available (every 2 hurs with routeviews) # 28800 if RIPE RIS, 7200 if RouteViews offset = 7200 if is_ripe: offset = 28880 rib_timestamp = start_time_ts while rib_timestamp % offset != 0: rib_timestamp -= 1 rib_timestamp -= 600 stream.add_interval_filter(rib_timestamp, stop_time_ts) stream.add_rib_period_filter(100000000000000000) return stream
def __init__(self, filters={'collector': ['rrc00']}, rpki_validator="rpki-validator.realmv6.org:8282", db="metasnap.db"): self.stream = BGPStream() self.filters = filters self.route_table = dict() self.i = 0 for filter_type, filter_array in filters.items(): for filter_value in filter_array: self.stream.add_filter(filter_type, filter_value) for collector in filters['collector']: self.route_table[collector] = defaultdict(dict) # self.db = DBConnector(db, read_only=False) rpki = rpki_validator.split(":") self.mgr = RTRManager(rpki[0], rpki[1]) self.mgr.start() self.counter = Counter() start_timestamp = self.get_push_timestamp(datetime.now(timezone.utc)) # self.start_collecting(start_timestamp, int(datetime.now(timezone.utc).strftime("%s"))) self.start_collecting(start_timestamp, start_timestamp)
def get_data_graph(self): stream = BGPStream() rec = BGPRecord() stream.add_filter('record-type', 'ribs') stream.add_interval_filter(self.start, self.end) stream.start() as_graph = nx.Graph() while stream.get_next_record(rec): if rec.status == "valid": elem = rec.get_next_elem() while elem: # the list is a list, which stores an as-path as_path = [ k for k, g in groupby(elem.fields['as-path'].split(" ")) ] peer = str(elem.peer_asn) # judge whether the as-path is legal if len(as_path) > 1 and as_path[0] == peer: if re.match(self.pattern, elem.fields['prefix']): self.as_prefix[as_path[-1]].add( elem.fields['prefix']) # add edges to the graph for i in range(0, len(as_path) - 1): as_graph.add_edge(as_path[i], as_path[i + 1]) elem = rec.get_next_elem() as_graph = self.add_geo_loc(as_graph) return as_graph
def getBGPStream(recordType, AF, collectors, startts, endts): stream = BGPStream() # recordType is supposed to be ribs or updates bgprFilter = "type " + recordType if AF == 6: bgprFilter += " and ipversion 6" else: bgprFilter += " and ipversion 4" for c in collectors: bgprFilter += " and collector %s " % c if isinstance(startts, str): startts = datetime.strptime(startts + "UTC", "%Y-%m-%dT%H:%M:%S%Z") startts = dt2ts(startts) if isinstance(endts, str): endts = datetime.strptime(endts + "UTC", "%Y-%m-%dT%H:%M:%S%Z") endts = dt2ts(endts) currentts = dt2ts(datetime.now()) if endts > currentts: stream.set_live_mode() stream.parse_filter_string(bgprFilter) stream.add_interval_filter(startts, endts) return stream
def create_trie_from_bgpstream_info(self, interval_start=1451692800): stream = BGPStream() rec = BGPRecord() stream.add_filter('collector', self.collector_name) stream.add_filter('record-type', 'ribs') if isinstance(interval_start, datetime.datetime): interval_start_utc = self.convert_interval_to_utc(interval_start) stream.add_interval_filter(interval_start_utc - 300, interval_start_utc + 300) else: stream.add_interval_filter(interval_start - 300, interval_start + 300) stream.start() while (stream.get_next_record(rec)): elem = rec.get_next_elem() while elem: # Get the peer ASN and IP. We then construct a peer_id, since a collector # can establish multiple connections with the same ASN. peer_asn = elem.peer_asn peer_asn_ip = elem.peer_address # make this an unmodifiable tuple peer_id = (peer_asn, peer_asn_ip) peer_route_trie = self.peer_trie_handles[peer_id] trie_node = peer_route_trie.add(elem.fields['prefix']) trie_node.data['as-path'] = elem.fields['as-path'] elem = rec.get_next_elem()
def sendMessageToKafka(self, col_name, col_data): print "lalalala" stream = BGPStream() record = BGPRecord() time_start = int(col_data.get('ribs').get('latestDumpTime')) time_end = time_start + int(col_data.get('ribs').get('dumpPeriod')) stream.add_filter('collector', col_name) stream.add_filter('record-type', 'ribs') stream.add_interval_filter(time_start, time_end) print "Before Start" stream.start() print "After Start" while stream.get_next_record(record): if record.status == "valid": elem = record.get_next_elem() while elem: # print "Record:{}".format(elem) producer = Kafka_producer() producer.send_data(col_name, json.dumps(elem)) elem = record.get_next_elem() else: print "## Current record not valid!" print "One Collector Finished"
def downloader(start_date, duration): """Download BGP paths from Routeviews and RIPE NCC from a start date for a certain duration.""" # Start of UNIX time base = int(datetime.datetime.strptime(start_date, '%m/%d/%Y').strftime('%s')) # Create a new bgpstream instance and a reusable bgprecord instance stream = BGPStream() stream.add_interval_filter(base, base + int(duration)) stream.add_filter('record-type', 'ribs') stream.start() path_set = set() f = open('rib.txt', 'w') while True: rec = stream.get_next_record() if rec is None: return if rec.status != "valid": continue else: elem = rec.get_next_elem() while(elem): path = elem.fields['as-path'] if '{' in path or '(' in path: elem = rec.get_next_elem() continue prefix = elem.fields['prefix'] # Focus on IPv4 prefixes if ":" not in prefix and path not in path_set: f.write(path.replace(' ', '|') + '\n') path_set.add(path) elem = rec.get_next_elem() f.close()
def recv_bgpstream_rib(begin, until, collector): """ Receive and parse BGP RIB records from a given bgpstream collector. """ logging.info("CALL recv_bgpstream_rib") # Create bgpstream stream = BGPStream() rec = BGPRecord() # set filtering stream.add_filter('collector', collector) stream.add_filter('record-type', 'updates') stream.add_interval_filter(begin, until) # Start the stream stream.start() while (stream.get_next_record(rec)): if rec.status == 'valid': elem = rec.get_next_elem() else: logging.warn("stream record invalid, skipping.") continue bgp_message = None while (elem): if (elem.type.upper() == 'A') or (elem.type.upper() == 'R'): bgp_message = BGPmessage(elem.time, 'update') aspath = elem.fields['as-path'].split() for a in aspath: if not '{' in a: # ignore AS-SETs bgp_message.add_as_to_path(a) bgp_message.add_announce(elem.fields['prefix']) output(bgp_message) elem = rec.get_next_elem() # end while (elem) # end while (stream...) output('FLUSH')
def dump2file(type, name, latestDumpTime, dumpDuration, dumpPeriod): print type + ' of ' + name + ': ' csv_header = ['type', 'addr', 'as', 'prefix', 'next_hop', 'as_path'] _file = None stream = BGPStream() rec = BGPRecord() stream.add_filter('collector', c_name) if type == 'ribs': stream.add_filter('record-type', 'ribs') _file = open(c_name + '_ribs.csv', 'w+') elif type == 'updates': stream.add_filter('record-type', 'updates') _file = open(c_name + '_updates.csv', 'w+') stream.add_interval_filter(latestDumpTime, latestDumpTime + dumpPeriod) stream.start() count = 0 useless_c = 0 writer = csv.writer(_file) writer.writerow(csv_header) # Get next record while (stream.get_next_record(rec)): # Print the record information only if it is not a valid record if rec.status != "valid": # print rec.project, rec.collector, rec.type, rec.time, rec.status print 'current rec not valid.' else: elem = rec.get_next_elem() while (elem): useless_c += 1 if useless_c % 1000 == 0: print 'Got ' + str(useless_c) + ' elem totally.' # Print record and elem information if isIPV6(elem): # ipv6 packet ignored elem = rec.get_next_elem() continue count += 1 # print rec.project, rec.collector, rec.type, rec.time, rec.status, # print elem.type, elem.peer_address, elem.peer_asn # elem.fields contains four column: communities, next-hop, prefix, as-path field = elem.fields prefix = field['prefix'] if 'prefix' in field.keys() else '' next_hop = field['next-hop'] if 'next-hop' in field.keys( ) else '' as_path = field['as-path'] if 'as-path' in field.keys() else '' as_path = as_path.replace(' ', '|') writer.writerow([ elem.type, elem.peer_address, elem.peer_asn, prefix, next_hop, as_path ]) elem = rec.get_next_elem() _file.close() print 'count: ' + str(count) return count
def build_sql_db(): conn = sqlite3.connect('bgp_stage.db') c = conn.cursor() # Create a new bgpstream instance and a reusable bgprecord instance stream = BGPStream() rec = BGPRecord() stream.add_filter('collector','rrc11') stream.add_interval_filter(1438417216,1438417316) stream.start() while(stream.get_next_record(rec)): if rec.status != "valid": continue else: elem = rec.get_next_elem() while(elem): if elem.type == "AB": prefix = elem.fields["prefix"] as_path = elem.fields["as-path"].split(" ") origin = as_path[-1] time = elem.time #IP Prefix database ip_min, ip_max = calculate_min_max(prefix) c.execute("SELECT ip_min FROM prefix_as WHERE ip_min = (?) AND ip_max = (?) AND as_o = (?)", (ip_min, ip_max, origin)) row = c.fetchone() if len(row) != 0: c.execute("UPDATE prefix_as SET count = count + 1 WHERE ip_min = (?) AND ip_max = (?) AND as_o = (?)", (ip_min, ip_max, origin)) else: c.execute("INSERT INTO prefix_as VALUES(?,?,?,?,?)", (ip_min, ip_max, origin, 1, time)) #AS link database for as1,as2 in zip(as_path, as_path[1:]) : c.execute("SELECT as_o FROM as_link WHERE as_o = (?) AND as_n = (?)",(as1,as2)) row = c.fetchone() if len(row) != 0: c.execute("UPDATE as_link SET count = count + 1 WHERE as_o = (?) AND as_n = (?)", (as1, as2)) else: c.execute("INSERT INTO as_link VALUES(?,?,?,?)", (as1, as2, 1, 0)) elif elem.type == "WA": prefix = elem.fields["prefix"] time = elem.time #Needs research print(rec.project, rec.collector, rec.type, rec.time, rec.status, elem.type, elem.peer_address, elem.peer_asn, elem.fields) print(prefix,elem.time, "W") print(rec.project, rec.collector, rec.type, rec.time, rec.status, elem.type, elem.peer_address, elem.peer_asn, elem.fields) elem = rec.get_next_elem() conn.commit() conn.close()
def main(): (options, args) = getopts() start = options.start_time end = options.end_time target_prefs = Set() with open('./../../atlas/anchor_prefix.txt', 'rb') as br: for l in br: target_prefs.add(l.strip()) # Create a new bgpstream instance and a reusable bgprecord instance stream = BGPStream() rec = BGPRecord() with open('./data/stream_{0}'.format(start), 'wb') as bw: #for pref in target_prefs: for ptmp in target_prefs: stream.add_filter('prefix', ptmp) # stream.add_filter('prefix','0.0.0.0/0') # Consider RIPE RRC 10 only stream.add_filter('record-type', 'updates') stream.add_filter('collector', 'rrc00') # Consider this time interval: # Sat Aug 1 08:20:11 UTC 2015 # stream.add_interval_filter(1438417216,1438417216) # stream.add_interval_filter(1451606400,1454785264 stream.add_interval_filter(start, end) # Start the stream stream.start() # Get next record cnt = 0 while stream.get_next_record(rec): # Print the record information only if it is not a valid record if rec.status != "valid": pass # print '*', rec.project, rec.collector, rec.type, rec.time, rec.status else: cnt += 1 elem = rec.get_next_elem() while elem: if elem.type == 'S': continue # Print record and elem information # print rec.project, rec.collector, rec.type, rec.time, rec.status, # print elem.type, elem.peer_address, elem.peer_asn, elem.fields, elem.pref bw.write( '{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\t{7}\t{8}\t{9}\n'. format(rec.project, rec.collector, rec.type, rec.time, rec.status, elem.type, elem.fields['prefix'], elem.peer_address, elem.peer_asn, elem.fields)) bw.flush() elem = rec.get_next_elem() print 'Successful termination; Start time: {0}'.format(start)
def initUpdates(seedingCheckOverride, lastBGPUpdateProcessed): # Consider passing in last BGP update processed because of new system. global cursor, startTime, endTime, seeded, stream, rec startTime = lastBGPUpdateProcessed #cursor.execute("""SELECT intVal AS lastBGPUpdateProcessed FROM metadata WHERE name = 'lastBGPUpdateProcessed'""") #result = cursor.fetchone() #if result == None: # print "ERROR: NO Start Time Found in DB (aka lastBGPUpdateProcessed). Recommended snd time is 1489224750 - int(2592000 * 4) (which is 1478856750) because this is a 4 month backlog." # exit() #cursor.execute("""INSERT INTO metadata (name, intVal) VALUES ('lastBGPUpdateProcessed', {0})""".format(1489224750 - int(2592000 * 4))) #conn.commit() #else: # (startTime,) = result cursor.execute("""SELECT intVal AS endTime FROM metadata WHERE name = 'endTime'""") result = cursor.fetchone() if result == None: print "ERROR: NO End Time Found in DB. Recommended end time is 1489224749 because this is the timestamp of the first cert." print "ERROR: Other recommended end time is 1491775893 which is when all certs have been covered." exit() else: (endTime,) = result if not seedingCheckOverride: cursor.execute("""SELECT stringVal AS seeded FROM metadata WHERE name = 'seeded'""") result = cursor.fetchone() if result == None: # We will assume that the DB is not seeded if there is no entry and not throw any errors in this case. seeded = False print "line 59 DB not seeded upon call to initUpdates. DB should be seeded with RIBs before updates can be processed. Call initSeeding first. Exiting." exit() else: strValSeeded = "" (strValSeeded,) = result if strValSeeded == "false": seeded = False print "line 66 DB not seeded upon call to initUpdates. DB should be seeded with RIBs before updates can be processed. Call initSeeding first. Exiting." exit() elif strValSeeded == "true": seeded = True else: print "Invalid value for seeded metadata entry. Exiting." exit() else: seeded = True # create a new bgpstream instance stream = BGPStream() # create a reusable bgprecord instance rec = BGPRecord() stream.add_filter('record-type','updates') # here we collect updates. This could be changed to ribs to instead acquire periodic snapshots of the RIBs. # configure the stream to retrieve Updates from the route-views listener. stream.add_filter('collector', 'route-views2') # getting updates only from one peer gives us only the perferred route of this peer and no rejected routes. # only get updates from level3. Level3 is a provider to ViaWest so their choice is a likely choice of ViaWest. stream.add_filter('peer-asn', '3356') stream.add_interval_filter(startTime, endTime) stream.start()
def __init__(self, route_collector="rrc00", rpki_validator="rpki-validator.realmv6.org:8282"): self.rc = route_collector rpki = rpki_validator.split(":") self.mgr = RTRManager(rpki[0], rpki[1]) # self._start_rtr_manager() self.stream = BGPStream() self.rec = BGPRecord()
def main(): (options, args) = getopts() limit = options.limit start = options.start_time end = options.end_time # Create a new bgpstream instance and a reusable bgprecord instance stream = BGPStream() rec = BGPRecord() # Consider RIPE RRC 10 only stream.add_filter('record-type', 'updates') stream.add_filter('collector', 'rrc00') stream.add_filter('prefix', '0.0.0.0/0') # Consider this time interval: # Sat Aug 1 08:20:11 UTC 2015 # stream.add_interval_filter(1438417216,1438417216) # stream.add_interval_filter(1451606400,1454785264) #stream.add_interval_filter(1454630400, 1454716800) # 1 hour #1454284800 - 1454288400 stream.add_interval_filter(start, end) # Start the stream stream.start() # Get next record prefixes_update = defaultdict(int) prefixes_withdraw = defaultdict(int) while stream.get_next_record(rec): # Print the record information only if it is not a valid record if rec.status != "valid": pass # print '*', rec.project, rec.collector, rec.type, rec.time, rec.status else: elem = rec.get_next_elem() while elem: if elem.type == 'A': #print elem.fields['as-path'] prefixes_update[elem.fields['prefix']] += 1 if elem.type == 'W': prefixes_withdraw[elem.fields['prefix']] += 1 #print rec.project, rec.collector, rec.type, rec.time, rec.status, #print elem.type, elem.peer_address, elem.peer_asn, elem.fields elem = rec.get_next_elem() for k in prefixes_update: if prefixes_update[k] >= limit: print k + "\t" + str(prefixes_update[k]) + "\t" + str( prefixes_withdraw[k])
def init_stream(config_files, start_time, end_time): stream = BGPStream() rec = BGPRecord() stream.add_filter('project', 'ris') stream.add_filter('project', 'routeviews') stream.add_filter('record-type', 'ribs') for exp_id in config_files: config_file = config_files[exp_id] stream.add_filter('prefix', config_file['superprefix']) stream.add_interval_filter(start_time, end_time) return stream, rec
def run(self): stream = BGPStream() rec = BGPRecord() if self.upd_file is None: stream.add_filter('collector', self.collector) stream.add_filter('record-type', self.record_type) stream.add_interval_filter(self.from_date, self.to_date) stream.set_live_mode() else: stream.set_data_interface('singlefile') if self.upd_file: stream.set_data_interface_option('singlefile', 'upd-file', self.upd_file) if self.rib_file: stream.set_data_interface_option('singlefile', 'rib-file', self.rib_file) if self.prefix_filter is not None: for prefix in self.prefix_filter: stream.add_filter('prefix', prefix) if self.peer_as_filter: for asn in self.peer_as_filter: stream.add_filter('peer-asn', str(asn)) if self.communities_filter: for community in self.communities_filter: stream.add_filter('community', community) stream.start() stream.get_next_record(rec) prev = rec.time while (stream.get_next_record(rec)): now = rec.time if rec.status == 'valid': elem = rec.get_next_elem() while (elem): statement = None peer_address = elem.peer_address peer_asn = elem.peer_asn if peer_asn in self.asn_to_nexthop: if elem.type == 'A' or elem.type == 'R': prefix = elem.fields['prefix'] as_path = elem.fields['as-path'] nexthop = elem.fields['next-hop'] if peer_asn in self.asn_to_nexthop: nexthop = self.asn_to_nexthop[peer_asn] statement = 'announce route %s next-hop %s as-path' \ ' [ %s ]' % (prefix, nexthop, as_path) elif elem.type == 'W': prefix = elem.fields['prefix'] statement = 'withdraw route %s' % prefix if statement: sys.stdout.write("%s\n" % statement) sys.stdout.flush() elem = rec.get_next_elem() time.sleep(self.delay + now - prev) prev = now
def main(): parser = argparse.ArgumentParser() parser.formatter_class = argparse.RawDescriptionHelpFormatter parser.description = textwrap.dedent('''\ a proof-of-concept utility for watching updates from BGPstream and then printing out if an unexpected update is heard ''') parser.epilog = textwrap.dedent('''\ Example: watch these route announcements %(prog)s -f routes.yaml ''') required = parser.add_argument_group('required arguments') required.add_argument("-f", "--file", required=True, help="yaml file of prefixes to origin asn") parser.add_argument("-d", "--debug", action='store_true', help="print out all updates containing these prefixes") args = parser.parse_args() routes = pytricia.PyTricia(48) # longest reasonable pfx in dfz with open(args.file, 'r') as f: routesfile = yaml.safe_load(f) for pfx in routesfile: routes[pfx] = routesfile[pfx] stream = BGPStream() rec = BGPRecord() stream.add_filter('record-type', 'updates') stream.add_interval_filter(int(time.time()), 0) stream.set_live_mode() stream.start() while (stream.get_next_record(rec)): if rec.status == 'valid': elem = rec.get_next_elem() while (elem): if 'as-path' in elem.fields: path = elem.fields['as-path'].split() prefix = elem.fields['prefix'] if prefix in routes and (routes[prefix] != path[-1] or args.debug): print('Heard prefix:', elem.fields['prefix'], 'AS-PATH:', elem.fields['as-path'], ' Found by project:', rec.project, 'collector:', rec.collector, 'type:', rec.type, 'at time:', rec.time, 'Type:', elem.type, 'Peer:', elem.peer_address, 'AS', elem.peer_asn) elem = rec.get_next_elem()
def download_data(): peer_state = defaultdict(dict) results = defaultdict(defaultdict_list) current_bin = 0 # create a new bgpstream instance stream = BGPStream() # create a reusable bgprecord instance rec = BGPRecord() bgprFilter = "type updates" bgprFilter += " and project ris " for prefix in prefixes: bgprFilter += " and prefix more %s " % prefix logging.info("Connecting to BGPstream... (%s)" % bgprFilter) logging.info("Timestamps: %s, %s" % (startts, endts)) stream.parse_filter_string(bgprFilter) stream.add_interval_filter(startts, endts) stream.start() while (stream.get_next_record(rec)): if rec.status != "valid": print(rec.project, rec.collector, rec.type, rec.time, rec.status) # from IPython import embed # embed() if current_bin == 0: current_bin = rec.time # slide the time window: if current_bin + bin_size < rec.time: timebins = range(current_bin, rec.time, bin_size) for i in timebins[:-1]: results["other"]["timebin"].append(i) for pfx, p_s in peer_state.items(): for peeras, state in p_s.items(): results[pfx][peeras].append(state) current_bin = timebins[-1] elem = rec.get_next_elem() while (elem): # peerip g= elem.peer_address peeras = elem.peer_asn prefix = elem.fields["prefix"] peer_state[prefix][peeras] = elem.type elem = rec.get_next_elem() return results
def get_stream(self): logging.debug( f"[ZombieRecordFinder-{self.collector}] try to create BGPstream") _start = datetime.datetime(self.year, self.month, 10) _end = datetime.datetime(self.year, self.month, 20) stream = BGPStream() stream.add_interval_filter(dt2ts(_start), dt2ts(_end)) stream.add_filter('collector', self.collector) for _, p in self.zombies: stream.add_filter('prefix-exact', p) return stream
def MOAS_prefixes(): # Create a new bgpstream instance and a reusable bgprecord instance stream = BGPStream() rec = BGPRecord() # Consider Route Views Singapore only stream.add_filter('collector', 'route-views.sg') # Consider RIBs dumps only stream.add_filter('record-type', 'ribs') # Consider this time interval: # Sat, 01 Aug 2015 7:50:00 GMT - 08:10:00 GMT stream.add_interval_filter(1438415400, 1438416600) # Start the stream stream.start() # <prefix, origin-ASns-set > dictionary prefix_origin = defaultdict(set) prefix_origin_dict = defaultdict(list) # Get next record while (stream.get_next_record(rec)): elem = rec.get_next_elem() while (elem): # Get the prefix pfx = elem.fields['prefix'] # Get the list of ASes in the AS path ases = elem.fields['as-path'].split(" ") if len(ases) > 0: # Get the origin ASn (rightmost) origin = ases[-1] if 262857 == origin: #AS262857 - UFRN: AS262857 177.20.128.0/19 UNIVERSIDADE FEDERAL DO RIO GRANDE DO NORTE ''' BI 177.20.128.0/19 198.32.125.84 280 100 0 1916, 262857 IGP E 177.20.128.0/19 213.248.67.117 0 70 0 1299, 2914, 1916, 262857 IGP E 177.20.128.0/19 213.248.98.93 0 70 0 1299, 2914, 1916, 262857 IGP ''' print "Achou UFRN" # Insert the origin ASn in the set of # origins for the prefix prefix_origin[pfx].add(origin) prefix_origin_dict[pfx].append(ases) elem = rec.get_next_elem() # Print the list of MOAS prefix and their origin ASns for pfx in prefix_origin: if len(prefix_origin[pfx]) > 1: pass #print pfx, ",".join(prefix_origin[pfx])
def run_bgpstream(args): (collector, start_time, end_time, data_type) = args # initialize and configure BGPStream stream = BGPStream() rec = BGPRecord() stream.add_filter('collector', collector) # NB: BGPStream uses inclusive/inclusive intervals, so subtract one off the # end time since we are using inclusive/exclusive intervals stream.add_interval_filter(start_time, end_time - 1) stream.add_filter('record-type', data_type) stream.start() # per-peer data peers_data = {} # loop over all records in the stream while stream.get_next_record(rec): elem = rec.get_next_elem() # to track the peers that have elems in this record peer_signatures = set() # loop over all elems in the record while elem: # create a peer signature for this elem sig = peer_signature(rec, elem) peer_signatures.add(sig) # if this is the first time we have ever seen this peer, create # an empty result: (elem_cnt, peer_record_cnt, coll_record_cnt) if sig not in peers_data: peers_data[sig] = [0, 0, 0] peers_data[sig][0] += 1 # increment elem cnt for this peer elem = rec.get_next_elem() # done with elems, increment the 'coll_record_cnt' field for just # one peer that was present in this record (allows a true, per-collector # count of records since each record can contain elems for many peers) if len(peer_signatures): first = True for sig in peer_signatures: # increment peer_record_cnt for all if first: peers_data[sig][2] += 1 # increment the coll_record_cnt first = False peers_data[sig][1] += 1 # the time in the output row is truncated down to a multiple of # RESULT_GRANULARITY so that slices can be merged correctly start_time = \ int(math.floor(start_time/RESULT_GRANULARITY) * RESULT_GRANULARITY) # for each peer that we processed data for, create an output row return [((start_time, collector, p), (peers_data[p])) for p in peers_data]
def initSeeding(lastBGPUpdateProcessed): global cursor, startTime, seeded, stream, rec, endTime #cursor.execute("""SELECT intVal AS lastBGPUpdateProcessed FROM metadata WHERE name = 'lastBGPUpdateProcessed'""") #result = cursor.fetchone() #if result == None: # print "ERROR: NO Start Time Found in DB (aka lastBGPUpdateProcessed). Recommended snd time is 1489224750 - int(2592000 * 4) (which is 1478856750) because this is a 4 month backlog." # exit() #cursor.execute("""INSERT INTO metadata (name, intVal) VALUES ('lastBGPUpdateProcessed', {0})""".format(1489224750 - int(2592000 * 4))) #conn.commit() #else: # (startTime,) = result startTime = lastBGPUpdateProcessed cursor.execute("""SELECT stringVal AS seeded FROM metadata WHERE name = 'seeded'""") result = cursor.fetchone() if result == None: # We will assume that the DB is not seeded if there is no entry and not throw any errors in this case. cursor.execute("""INSERT INTO metadata (name, stringVal) VALUES ('seeded', 'false')""") conn.commit() seeded = False else: strValSeeded = "" (strValSeeded,) = result if strValSeeded == "false": seeded = False elif strValSeeded == "true": seeded = True print "DB seeded already. Calling initUpdates(True)" initUpdates(True, lastBGPUpdateProcessed) return True else: print "Invalid value for seeded metadata entry. Exiting." exit() # create a new bgpstream instance stream = BGPStream() # create a reusable bgprecord instance rec = BGPRecord() stream.add_filter('record-type','ribs') # here we collect updates. This could be changed to ribs to instead acquire periodic snapshots of the RIBs. # configure the stream to retrieve Updates from the route-views listener. stream.add_filter('collector', 'route-views2') # getting updates only from one peer gives us only the perferred route of this peer and no rejected routes. # only get updates from level3 stream.add_filter('peer-asn', '3356') stream.add_rib_period_filter(604800) stream.add_interval_filter(startTime, startTime + 104800) endTime = startTime + 1604801 stream.start() print "bgp stream started from init seeding" return False
def getBGPStream(self): logging.info( f"creating BGPstream {self.collector} {self.record_type} {self.start.year}-{self.start.month} " ) stream = BGPStream() stream.add_filter('collector', self.collector) stream.add_filter('record-type', self.record_type) if self.record_type == "updates": stream.add_interval_filter(dt2ts(self.start), dt2ts(self.end)) elif self.record_type == "ribs": _start = dt2ts(self.start - timedelta(hours=2)) _end = dt2ts(self.start + timedelta(hours=2)) stream.add_interval_filter(_start, _end) return stream
def getting_BGP_update(): stream = BGPStream() rec = BGPRecord() stream.add_filter('collector', 'rrc11') stream.add_interval_filter(1438417216, 1438417216) # Start the stream stream.start() while (stream.get_next_record(rec)): if rec.status != "valid": print rec.project, rec.collector, rec.type, rec.time, rec.status else: elem = rec.get_next_elem() while (elem): print rec.project, rec.collector, rec.type, rec.time, rec.status, print elem.type, elem.peer_address, elem.peer_asn, elem.fields elem = rec.get_next_elem()
def generate_stream(): bs = BGPStream() rec = BGPRecord() #initialize MySql db_writer = MysqlWriter() db_writer.start() #initialize the publisher in port number 12345 publisher = ZmqPublisher(12345) bs.add_interval_filter(calendar.timegm(time.gmtime()) - delay, 0) # bs.add_filter('collector', 'route-views.sg') bs.add_filter('record-type', 'updates') bs.start() print('Beginning to read from stream') input_id = 0 while bs.get_next_record(rec): elem = rec.get_next_elem() while elem is not None: # sleep until it is time to send this record ''' now = calendar.timegm(time.gmtime()) sim_time = now - delay if elem.time > sim_time: time.sleep(elem.time - sim_time) ''' if elem.type not in ['A', 'W']: continue input_id += 1 msg = elem2bgplay(rec, elem) msg['type'] = 'A' msg['id'] = input_id print(msg) # Publish the message publisher.publish(msg) # Write it to DB if elem.type == 'A': db_writer.add(msg) elif elem.type == 'W': db_writer.remove(msg) else: print "Error: Unknown type: " + elem.type elem = rec.get_next_elem()
def get_ribs(collector, start, period): stream = BGPStream() rec = BGPRecord() stream.add_filter('collector', collector) stream.add_interval_filter( int(start.timestamp()) - period, int(start.timestamp())) stream.add_filter('record-type', 'ribs') stream.start() while (stream.get_next_record(rec)): if rec.status == "valid": elem = rec.get_next_elem() while (elem): yield (rec.project, rec.collector, datetime.utcfromtimestamp(rec.time).isoformat(), elem.peer_address, elem.peer_asn, elem.fields['prefix'], elem.fields['as-path']) elem = rec.get_next_elem()
def main(rib, target_prefs): # Create a new bgpstream instance and a reusable bgprecord instance stream = BGPStream() rec = BGPRecord() with open('./data/stream_{0}'.format(start), 'wb') as bw: for ptmp in target_prefs: stream.add_filter('prefix', ptmp) # Consider RIPE RRC 10 only stream.add_filter('record-type', 'updates') stream.add_filter('record-type', 'ribs') #stream.add_filter('collector', 'rrc04') stream.add_filter('project', 'ris') stream.add_filter('project', 'routeviews') stream.add_interval_filter(start-60*60*8, start) stream.add_rib_period_filter(10000000000000) # Start the stream stream.start() while stream.get_next_record(rec): # Print the record information only if it is not a valid record if rec.status != "valid": continue #if rec.time < start: elem = rec.get_next_elem() while elem: if elem.type == 'A' or elem.type == 'R': rib.add_to_rib(rec.collector, elem.peer_address, elem.fields['prefix'], elem.time, elem.fields['as-path']) elem = rec.get_next_elem() #else: rib.flush() print 'Successful termination; Start time: {0}'.format(start)
def count_valid_withdrawals_per_second(t0,num_seconds): # div0 in progress tracking if <100 seconds assert num_seconds>100; # Create a new bgpstream instance stream = BGPStream(); # use Routeviews Saopaulo records stream.add_filter('project','routeviews'); stream.add_filter('collector','route-views.saopaulo'); stream.add_filter('record-type','updates'); # time interval stream.add_interval_filter(t0,t0+tt-1); # progress tracking prog = t0; # Create a reusable bgprecord instance rec = BGPRecord(); # list of zeroes ctr = [0]*tt; # Start the stream stream.start(); # iterate over records while(stream.get_next_record(rec)): if rec.status == "valid": elem = rec.get_next_elem(); while(elem): if elem.type == 'W': ctr[rec.time-t0] += 1; elem = rec.get_next_elem(); # el-cheapo progress indication with dots. comment out if you don't want the dots. # """ if rec.time > prog and (rec.time - t0) % floor(num_seconds/100) == 0: sys.stdout.write('.'); sys.stdout.flush(); prog = rec.time; # """ # print newline after the dots print ""; return ctr;
def __init__( self, filters={"collector": ["rrc00"]}, rpki_validator="rpki-validator.realmv6.org:8282", settings_file="../settings.json", ): self.stream = BGPStream() self.filters = filters self.route_table = dict() self.i = 0 self.metadata_vp = dict() self.metadata_rc = dict() self.peers = Counter() self.prefix4 = Counter() self.prefix6 = Counter() start_timestamp = get_push_timestamp(datetime.now(timezone.utc)) for filter_type, filter_array in filters.items(): for filter_value in filter_array: self.stream.add_filter(filter_type, filter_value) for collector in filters["collector"]: self.route_table[collector] = defaultdict(dict) self.metadata_vp[collector] = defaultdict(list) self.metadata_rc[collector] = RouteCollectorMeta(None, 0, 0, 0, 0) self.peers[collector] = defaultdict(int) self.prefix4[collector] = defaultdict(int) self.prefix6[collector] = defaultdict(int) settings = get_settings(settings_file) settings["db"]["password"] = os.environ["PGPASS"] self.db = DBConnector(settings["db"]) rpki = rpki_validator.split(":") self.mgr = RTRManager(rpki[0], rpki[1]) self.mgr.start() self.start_collecting(start_timestamp)