-
Notifications
You must be signed in to change notification settings - Fork 3
/
mrt2mysql-single.py
70 lines (59 loc) · 1.8 KB
/
mrt2mysql-single.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
#!/usr/bin/env python
import os
import sys
import time
import json
import pymysql
import IPy
from datetime import datetime
import re
# When the parent dies we are seeing continual newlines, so we only access so many before stopping
counter = 0
conn = pymysql.connect(
host='localhost',
user='user',
passwd='password',
db='db',
)
cur = conn.cursor()
def getSourceASfromASPath(aspathstring):
if '{' not in aspathstring:
aspathlist = aspathstring.split(' ')
sourceasn = aspathlist[-1]
else:
aspathstring = re.sub(' \{.*\}$', '', aspathstring)
aspathlist = aspathstring.split(' ')
sourceasn = aspathlist[-1]
return int(sourceasn)
def lineToMysql(oneline):
# TYPE|SUBNETS|AS_PATH|NEXT_HOP|ORIGIN|ATOMIC_AGGREGATE|AGGREGATOR|COMMUNITIES|SOURCE|TIMESTAMP|ASN 32 BIT
(adv_type,subnets,aspath,next_hop,origin,atomic_aggregate,aggregator,communities,source,timestamp,asn32bit) = oneline.split('|')
sourceasn = getSourceASfromASPath(aspath)
ts = int(timestamp)
eventtime = datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
prefixes = subnets.split(' ')
for prefix in prefixes:
sqlinsert = """
INSERT INTO mrt2mysql VALUES (NULL, '%s', '%s', '%s', %d)
""" % (eventtime, prefix, aspath, sourceasn)
#print sqlinsert
conn.ping(reconnect=True)
cur.execute(sqlinsert)
#conn.ping(reconnect=True)
conn.commit()
while True:
try:
line = sys.stdin.readline().strip()
if line == "":
counter += 1
if counter > 100:
break
continue
counter = 0
lineToMysql(line)
#conn.commit()
except KeyboardInterrupt:
pass
except IOError:
# most likely a signal during readline
pass