This repository has been archived by the owner on Aug 19, 2021. It is now read-only.
forked from job/irrexplorer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
worker
executable file
·121 lines (80 loc) · 3.86 KB
/
worker
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
#!/usr/bin/env python
"""
Script to update BGP and IRR entries in the IRRExplorer database.
"""
import os
import time
import argparse
import logging
import yaml
from irrexplorer import sqldb, bgpupdate, irrupdate
DEFAULT_DATABASE = 'irrexplorer'
DEFAULT_BGP_SOURCE = 'http://lg01.infra.ring.nlnog.net/table.txt'
DEFAULT_IRR_SPEC = 'irrexplorer_config.yml'
DEFAULT_LOG_FILE = 'irrworker.log'
DEFAULT_UPDATE_INTERVAL = 300 # 5 minutes, the nlnog table is updated at that frequency
INSERT_STM = "SELECT create_route (%s, %s, 'bgp');"
DELETE_STM = "DELETE FROM routes USING sources WHERE routes.route = %s AND routes.asn = %s AND routes.source_id = sources.id AND sources.name = 'bgp';"
def create_parser():
parser = argparse.ArgumentParser(description='Continously update BGP table in IRRExplorer database')
parser.add_argument('-b', '--bgp-source', dest='bgp_source', default=DEFAULT_BGP_SOURCE, help='Source URL for BGP routing table (default: nlnog ring table)')
parser.add_argument('-r', '--irr-spec', dest='irr_spec', default=DEFAULT_IRR_SPEC, help='IRR specifications file (default: %s)' % DEFAULT_IRR_SPEC)
parser.add_argument('-d', '--database', dest='database', default=DEFAULT_DATABASE, help='IRRExplorer database name (default: %s)' % DEFAULT_DATABASE)
parser.add_argument('-i', '--interval', dest='interval', default=DEFAULT_UPDATE_INTERVAL, help='Interval between updates (default: %i seconds)' % DEFAULT_UPDATE_INTERVAL)
parser.add_argument('-l', '--logfile', dest='logfile', default=DEFAULT_LOG_FILE, help='Log file (default: %s)' % DEFAULT_LOG_FILE)
return parser
def parse_irr_spec(irr_spec):
specs = []
if not os.path.exists(irr_spec):
print 'Error: IRR spec file does not exist'
return specs # don't fail on this (bgp update should be allowed to run)
irr_cfg = open(irr_spec)
cfg = yaml.load(irr_cfg)
for db in cfg['databases']:
for db_name, db_info in db.items():
host = port = source = None
for dbe in db_info:
host = dbe.get('nrtmhost', host)
port = dbe.get('nrtmport', port)
source = dbe.get('dbname', source)
if host and source:
specs.append( (host, port, source) )
else:
print 'Could not parse %s for NRTM info' % db_name
return specs
def main():
parser = create_parser()
args = parser.parse_args()
update_interval = int(args.interval)
irr_sources = parse_irr_spec(args.irr_spec)
logging.basicConfig(filename=args.logfile, format='%(asctime)s %(levelname)s: %(message)s', level=logging.DEBUG)
dsn = 'dbname=%s' % args.database
db = sqldb.IRRSQLDatabase(dsn)
logging.info('Worker started')
while True:
t_start = time.time()
# bgp
try:
bgpupdate.updateBGP(args.bgp_source, db)
except Exception as e:
logging.error('Error duing bgp update: %s' % str(e))
# irr update
for host, port, source in irr_sources:
try:
irrupdate.update_irr(host, port, source, db)
except Exception as e:
logging.error('Error during IRR update: %s' % str(e))
if "don't exist!" in str(e):
logging.error(
"ERROR: the above error probably means that the gap between your seed" +
"snapshot and the current SERIAL on the stream source is too large.")
# sleep
t_sleep = update_interval - (time.time() - t_start)
if t_sleep < 0:
logging.info('Update took longer time than sleep interval, skipping sleep')
else:
logging.info('Sleeping for %i seconds' % t_sleep)
time.sleep(t_sleep)
logging.info('--')
if __name__ == '__main__':
main()