forked from plajjan/bgpflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bgpflow.py
executable file
·418 lines (324 loc) · 12.3 KB
/
bgpflow.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#!/usr/bin/python2.4
# vim: et :
import os
import psycopg2
import radix
import re
import socket
import sys
from twisted.internet import abstract, fdesc, interfaces, protocol, reactor
from twisted.python import log
from twisted.protocols import basic
from twistedsnmp import snmpprotocol, agentproxy
from pybgp import speaker, pathattr, proto
import flowd
class BgpListener(speaker.BGP):
def connectionMade(self):
log.msg('connection made!')
speaker.BGP.connectionMade(self)
# notify our factory that someone has connected
self.factory.client_connected(self)
class BgpFactory(protocol.Factory):
protocol = BgpListener
def client_connected(self, proto):
# create a new peer
peer = self.process.peer(proto)
class NfProtocol(protocol.Protocol):
def __init__(self):
log.msg("tjohej NfProtocol init!!")
def dataReceived(self, buf):
print "HEJSAN!!"
def test(self):
print "TJOHEJ!"
class NfFactory(protocol.Factory):
protocol = NfProtocol
def __init__(self, process):
self.i = 0
self.process = process
self.db_cur = self.process.db_conn.cursor()
self.del_query = "DELETE FROM raw WHERE insert_time < NOW() - '30 min'::interval"
self.insert_query = "INSERT INTO raw (flow_start, flow_finish, agent, protocol, src_ip, dst_ip, src_port, dst_port, packets, octets, peer_src_as, peer_dst_as, src_as, dst_as, src_aspath, dst_aspath, ifindex_in, ifindex_out, if_in, if_out, sampling_interval) VALUES ((TIMESTAMP 'epoch' + (%(flow_start)s / 1000::numeric) * '1 seconds'::interval) AT TIME ZONE 'UTC', (TIMESTAMP 'epoch' + (%(flow_finish)s / 1000::numeric)* '1 second'::interval) AT TIME ZONE 'UTC', %(agent)s, %(protocol)s, %(src_ip)s, %(dst_ip)s, %(src_port)s, %(dst_port)s, %(packets)s, %(octets)s, %(peer_src_as)s, %(peer_dst_as)s, %(src_as)s, %(dst_as)s, %(src_aspath)s, %(dst_aspath)s, %(ifindex_in)s, %(ifindex_out)s, %(if_in)s, %(if_out)s, %(sampling_interval)s)"
self.ifs = self.process.ifs
self.rts = self.process.rts
def makeConnection(self, protocol):
log.msg("in makeConnection");
pass
def datagramReceived(self, buf, file):
self.processFlow(buf)
def processFlow(self, flowrec):
flow = flowd.Flow(blob = flowrec)
# these seem to be backwards.. strange
# make sure we get it right, just in case
if flow.flow_start > flow.flow_finish:
large = flow.flow_start
small = flow.flow_finish
else:
large = flow.flow_finish
small = flow.flow_start
# TODO: handle wrapping, ie finish has wrapped but start has not
flow_start_in_ms = (flow.agent_sec*1000 + flow.agent_usec) + (flow.sys_uptime_ms - large)
flow_finish_in_ms = (flow.agent_sec*1000 + flow.agent_usec) + (flow.sys_uptime_ms - small)
try:
if_in = self.ifs[flow.agent_addr][flow.if_ndx_in]['type']
except:
if_in = None
try:
if_out = self.ifs[flow.agent_addr][flow.if_ndx_out]['type']
except:
if_out = None
try:
rt = self.rts[flow.agent_addr]
except:
rt = None
src_rnode = None
dst_rnode = None
if rt is not None:
try:
src_rnode = rt.search_best(flow.src_addr)
except:
src_rnode = None
try:
dst_rnode = rt.search_best(flow.dst_addr)
except:
dst_rnode = None
if src_rnode is not None:
try:
src_as_array = src_rnode.data['aspath'].value[0]
except:
src_as_array = None
try:
src_as = int(src_as_array[-1])
except:
src_as = None
src_aspath = ''
last_asn = ''
if src_as_array is not None:
for asn in src_as_array:
if asn != last_asn:
if src_aspath != '':
src_aspath += ' '
src_aspath += str(asn)
last_asn = asn
else:
src_aspath = None
src_as = None
if dst_rnode is not None:
try:
dst_as_array = dst_rnode.data['aspath'].value[0]
except:
dst_as_array = None
try:
dst_as = int(dst_as_array[-1])
except:
dst_as = None
dst_aspath = ''
last_asn = ''
if dst_as_array is not None:
for asn in dst_as_array:
if asn != last_asn:
if dst_aspath != '':
dst_aspath += ' '
dst_aspath += str(asn)
last_asn = asn
else:
dst_aspath = None
dst_as = None
data = {'flow_start': flow_start_in_ms,
'flow_finish': flow_finish_in_ms,
'agent': flow.agent_addr,
'protocol': flow.protocol,
'src_ip': flow.src_addr,
'dst_ip': flow.dst_addr,
'src_port': flow.src_port,
'dst_port': flow.dst_port,
'packets': flow.packets,
'octets': flow.octets,
'src_as': src_as,
'dst_as': dst_as,
'src_aspath': src_aspath,
'dst_aspath': dst_aspath,
'peer_src_as': flow.src_as,
'peer_dst_as': flow.dst_as,
'ifindex_in': flow.if_ndx_in,
'ifindex_out': flow.if_ndx_out,
'if_in': if_in,
'if_out': if_out,
'sampling_interval': 8192 }
# try:
self.db_cur.execute(self.insert_query, data)
self.process.db_conn.commit()
# except:
# print sys.exc_info()
# import time
# time.sleep(600)
self.i = self.i + 1
if self.i%1000 == 0:
self.i = 0
self.db_cur.execute(self.del_query)
self.process.db_conn.commit()
def write(self, data):
pass
def loseConnection(self):
self.fp.close()
class BgpPeer:
def __init__(self, process, proto):
self.proto = proto
self.peer = self.proto.transport.getPeer()
self.host = self.peer.host
self.process = process
self.i = 0
self.last_aspath = ''
self.ifs = {}
self.rt = radix.Radix()
log.msg("init of peer: ", self.host)
self.reinit()
self.snmp_protocol = snmpprotocol.port()
def reinit(self, reconnect=False):
self.state = 'idle'
if reconnect:
reactor.callLater(5, self.start)
def start(self):
if self.state!='idle':
raise Exception('already connected')
self.state = 'connecting'
def closed(self, reason):
# TODO: we should just remove the whole peer after "a while", since we
# have any configuration on this side but just rely on the other end to
# peer with us
log.msg("connection to", self.host, "lost", reason)
self.reinit(True)
def ok(self, protocol):
self.proto = protocol
self.proto.closed = self.closed
self.state = 'connected'
log.msg("peer connected", self.host)
# hook up the message callback to ourselves
self.proto.handle_msg = self.msg
# send an open
self.proto.open(self.process.asnum, self.process.bgpid)
# get the SNNMP thingy going!
self.update_ifs()
def err(self, reason):
log.msg("connection to", self.host, "failed", reason)
self.reinit()
reactor.callLater(5, self.start)
def msg(self, msg):
if msg.kind=='open':
if self.state!='connected':
raise Exception('open at invalid time')
log.msg("peer opened", self.host)
self.state = 'open'
self.proto.start_timer(msg.holdtime)
reactor.callLater(0.5, self.send_updates)
elif msg.kind=='notification':
log.msg("notification from", self.host, msg)
self.proto.transport.loseConnection()
self.reinit(True)
elif msg.kind=='update':
if self.i % 10000 == 0:
num_prefix = 0
for prefix in self.rt:
num_prefix = num_prefix + 1
print "Number of prefixes: ", num_prefix
self.i = self.i + 1
for prefix in msg.withdraw:
try:
self.rt.delete(str(prefix))
except:
pass
if msg.pathattr.has_key('aspath'):
self.last_aspath = msg.pathattr['aspath']
for prefix in msg.nlri:
rnode = self.rt.search_exact(str(prefix))
if rnode is not None:
try:
self.rt.delete(str(prefix))
except:
pass
rnode = self.rt.add(str(prefix))
rnode.data['aspath'] = self.last_aspath
def send_updates(self):
if self.state!='open':
return
return
def update_ifs(self):
proxy = agentproxy.AgentProxy(
self.host, 161,
community = 'secret',
snmpVersion = 'v2',
protocol = self.snmp_protocol.protocol,
)
df = proxy.getTable([ '.1.3.6.1.2.1.31.1.1.1.18' ], timeout=5, retryCount=3)
df.addCallback(self.snmp_ok)
df.addErrback(self.snmp_fail)
reactor.callLater(300, self.update_ifs)
return df
def snmp_ok(self, results):
for base_oid in results:
for oid in results[base_oid]:
ifindex = int(oid[-1])
# log.msg("ifindex:", ifindex, " value:", results[base_oid][oid])
self.ifs[ifindex] = {}
self.ifs[ifindex]['desc'] = results[base_oid][oid]
# determine if it's a peering interface, ie type = 0
# if other end is a -CORE- machine it's a core interface (type=1)
# otherwise it's a "customer" interface, ie type = 2
#if re.match('ABR:AS[0-9]+;', self.ifs[ifindex]['desc']) is not None:
if re.match('^.*ABR:AS[0-9]+;.*', self.ifs[ifindex]['desc']) is not None:
# log.msg('PEERING interface!')
self.ifs[ifindex]['type'] = 0
elif re.match('^.*Link to .*-CORE-.*', self.ifs[ifindex]['desc']) is not None:
# log.msg('CORE interface!')
self.ifs[ifindex]['type'] = 1
else:
# log.msg('other interface!')
self.ifs[ifindex]['type'] = 2
return results
def snmp_fail(self, err):
print 'ERROR', err.getTraceback()
return err
class BgpProcess:
def __init__(self, asnum, bgpid):
self.asnum = asnum
self.bgpid = bgpid
# our BGP peers
self.peers = {}
# for holding our route tables
self.rts = {}
# for holding router interface info
self.ifs = {}
self.timer = None
self.protocol = speaker.BGP
def peer(self, proto):
log.msg('got new peer!')
p = BgpPeer(self, proto)
p.start()
p.ok(proto)
peer = proto.transport.getPeer()
host = peer.host
self.peers[host] = p
self.rts[host] = p.rt
self.ifs[host] = p.ifs
return p
if __name__=='__main__':
log.FileLogObserver.timeFormat = '%Y-%m-%d %H:%M:%S'
log.startLogging(sys.stderr)
myip = socket.gethostbyname(os.uname()[1])
process = BgpProcess(1257, myip)
factory = BgpFactory()
factory.process = process
try:
process.db_conn = psycopg2.connect("dbname='flow' user='flowd' host='localhost' password='SECRET'");
except:
print "I am unable to connect to the flow database"
named_pipe_file = '/tmp/flowd'
nf_factory = NfFactory(process)
# try:
# os.unlink(named_pipe_file)
# except:
# pass
# os.mkfifo(named_pipe_file)
reactor.listenTCP(179, factory)
reactor.listenUNIXDatagram(named_pipe_file, nf_factory)
reactor.run()
# os.unlink(named_pipe_file)