Skip to content

Instantly share code, notes, and snippets.

@codeout
Created November 20, 2017 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codeout/74ade8e269abd3f4bed66787c63f9c9b to your computer and use it in GitHub Desktop.
Save codeout/74ade8e269abd3f4bed66787c63f9c9b to your computer and use it in GitHub Desktop.
Count BGP updates with pybgpstream
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())
@codeout
Copy link
Author

codeout commented Nov 20, 2017

How to use

In macos for example,

brew install bgpstream
pip install pybgpstream  # requires python2 as of today
python bgpupdate_count.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment