Created
November 20, 2017 13:43
-
-
Save codeout/74ade8e269abd3f4bed66787c63f9c9b to your computer and use it in GitHub Desktop.
Count BGP updates with pybgpstream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import unicode_literals, print_function, absolute_import | |
from _pybgpstream import BGPStream, BGPRecord, BGPElem | |
import re | |
class BgpUpdate(object): | |
def __init__(self, collector=None, nexthop=None, aspath=None, start=None, stop=None): | |
self.collector = collector | |
self.nexthop = nexthop | |
self.aspath = aspath | |
self.start = start | |
self.stop = stop | |
self.stream = BGPStream() | |
self.stream.add_interval_filter(start, stop) | |
self.stream.add_filter('record-type', 'updates') | |
if collector: | |
self.stream.add_filter('collector', collector) | |
self.stream.start() | |
def count(self): | |
nlri = [0] * 24 * 60 | |
withdraw = [0] * 24 * 60 | |
rec = BGPRecord() | |
while(self.stream.get_next_record(rec)): | |
if rec.status != 'valid': | |
continue | |
while True: | |
elem = rec.get_next_elem() | |
if elem == None: | |
break | |
if 'next-hop' in elem.fields and self.nexthop and elem.fields['next-hop'] != self.nexthop: | |
continue | |
if self.aspath and \ | |
not ('as-path' in elem.fields and re.match(self.aspath, elem.fields['as-path'])): | |
continue | |
print(rec.time) | |
if elem.type == 'A': | |
self.__increment(nlri, rec.time) | |
elif elem.type == 'W': | |
self.__increment(withdraw, rec.time) | |
return (nlri, withdraw) | |
def __increment(self, minutes, time): | |
minutes[(time - self.start) / 60] += 1 | |
update = BgpUpdate(collector='route-views.eqix', | |
nexthop='206.126.236.47', | |
aspath='.* 2914 3356 .*', | |
start=1509926400, stop=1510012800) | |
print(update.count()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How to use
In macos for example,