Commit 93e459ac authored by Tomas Hlavacek's avatar Tomas Hlavacek

Add AS names to BGP graph nodes.

Implement arbitrary data into sessions.
Implement finishHook to TextCommand.
Refactor graph generation of Cisco by the abovementoined mechanisms.
Add whois client.
Refactor whois client calls in ulg.py in action whois.
Add simple annotation by AS names in BIRD BGP graph.
parent 5e3ffef6
......@@ -61,6 +61,7 @@ STRING_SOCKET_TIMEOUT = "Socket communication timed out. See log."
STRING_PEERID = "Peer ID"
STRING_RTABLE = "Routing Table"
STRING_DETAILS = "Details of"
STRING_UNKNOWN = "(unknown)"
# URL generator functions
def getASNURL(asn):
......
......@@ -32,12 +32,12 @@ import urllib
import md5
import time
import random
import subprocess
import config
import defaults
import ulgmodel
import whois
IPV4_ANNOTATE_REGEXP = '([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}(/[0-9]{1,2})?)'
ipv4_annotate_regexp = re.compile(IPV4_ANNOTATE_REGEXP)
......@@ -55,6 +55,7 @@ class Session(object):
self.finished=copy.finished
self.range=copy.range
self.resultlen=copy.resultlen
self.data=copy.data
else:
if(sessionid == None):
......@@ -69,6 +70,7 @@ class Session(object):
self.finished=finished
self.range=resrange
self.resultlen=0
self.data=None
self.save()
......@@ -239,6 +241,13 @@ class Session(object):
def getMaxRange(self):
return self.resultlines
def getData(self):
return self.data
def setData(self,data):
self.data = data
self.save()
class DecoratorHelper:
def __init__(self):
......@@ -450,6 +459,7 @@ class ULGCgi:
ulgmodel.debug("Running command: "+session.getCommand().getName())
try:
session.getRouter().runAsyncCommand(session.getCommand(),session.getParameters(),FakeSessionFile(session))
session.getCommand().finishHook(session)
except Exception as e:
ulgmodel.log("ERROR: Exception occured while running a command:" + traceback.format_exc())
session.setResult("ERROR in commandThreadBody:\n"+traceback.format_exc())
......@@ -652,18 +662,7 @@ class ULGCgi:
template = self.loader.load(defaults.whois_template_file)
s = subprocess.Popen([defaults.bin_whois,
'-H',
key], stdout=subprocess.PIPE)
res=''
begin = False
for l in s.stdout.readlines():
if(re.match('^\s*$',l) and not begin):
continue
if(l[0] != '%'):
res=res+l
begin = True
res = whois.lookup(key)
return template.generate(result=Markup(res),
url=url,
url_caption=urlc,
......
......@@ -77,7 +77,7 @@ def bird_parse_sh_route_all(text,prependas):
m = bird_sh_route_all_ases_regexp.match(l)
if(m):
ases = ["AS"+str(asn) for asn in [prependas] + split_ases(m.group(2))]
ases = [ulgmodel.annotateAS("AS"+str(asn)) for asn in [prependas] + split_ases(m.group(2))]
res.append((ases,params))
params = dict(DEFAULT_PARAMS)
continue
......
......@@ -105,7 +105,7 @@ def cisco_parse_sh_bgp_uni(lines,prependas):
if(table_started):
m = regex_sh_bgp_uni_asline.match(l)
if(m):
ases = ["AS"+str(asn) for asn in [prependas] + split_ases(m.group(2))]
ases = [ulgmodel.annotateAS("AS"+str(asn)) for asn in [prependas] + split_ases(m.group(2))]
infotext = m.group(3)
if(infotext):
paths.append((ases,get_info(infotext)))
......@@ -600,11 +600,17 @@ class CiscoCommandGraphShowBgpIPv46Uni(ulgmodel.TextCommand):
name=name)
def decorateResult(self,session,decorator_helper=None):
return (decorator_helper.img(decorator_helper.getSpecialContentURL(session.getSessionId()),"BGP graph"),1)
if(session.isFinished()):
return (decorator_helper.img(decorator_helper.getSpecialContentURL(session.getSessionId()),"BGP graph"),1)
else:
return ('',0)
def finishHook(self,session):
session.setData(cisco_parse_sh_bgp_uni(session.getResult(),str(session.getRouter().getASN())))
def getSpecialContent(self,session,**params):
paths = session.getData()
print "Content-type: image/png\n"
paths = cisco_parse_sh_bgp_uni(session.getResult(),str(session.getRouter().getASN()))
ulggraph.bgp_graph_gen(reduce_bgp_paths(paths),start=session.getRouter().getName(),
end=session.getParameters()[0])
......
File mode changed from 100755 to 100644
......@@ -28,6 +28,7 @@ import pickle
import fcntl
import StringIO
import whois
import defaults
......@@ -44,6 +45,9 @@ def debug(message):
log('DEBUG:' + message)
def annotateAS(asn):
return asn+' | '+whois.lookup_as_name(asn)
class PersistentStorage(object):
def __init__(self):
......@@ -265,6 +269,9 @@ class TextCommand(object):
def showRange(self):
return True
def finishHook(self,session):
pass
class AnyCommand(TextCommand):
def __init__(self):
......
#!/usr/bin/env python
#
# ULG - Universal Looking Glass
# by Tomas Hlavacek (tomas.hlavacek@nic.cz)
# last udate: June 21 2012
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Imports
import os, sys
import re
import subprocess
import defaults
ASNAME_REGEX = '^\s*(as-name|ASName):\s*([^\s].*)\s*'
asname_regex = re.compile(ASNAME_REGEX)
asname_cache = {}
def lookup(key):
s = subprocess.Popen([defaults.bin_whois,
'-H',
key], stdout=subprocess.PIPE)
res=''
begin = False
for l in s.stdout.readlines():
if(re.match('^\s*$',l) and not begin):
continue
if(l[0] != '%'):
res=res+l
begin = True
return res
def lookup_as_name(asn):
if asn in asname_cache:
return asname_cache[asn]
s = subprocess.Popen([defaults.bin_whois,
'-H',
asn], stdout=subprocess.PIPE)
for l in s.stdout.readlines():
m = asname_regex.match(l)
if(m):
asname_cache[asn] = m.group(2)
return m.group(2)
return defaults.STRING_UNKNOWN
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment