Commit f8dce06b authored by Tomas Hlavacek's avatar Tomas Hlavacek

Basic support for BIRD show protocols added.

BIRD show protocols command parser and table formater added.
parent 7adba2f2
......@@ -74,6 +74,8 @@ server_got_reply(char *x)
BIRD_SOCK_HEADER_REGEXP='^([0-9]+)[-\s](.+)$'
BIRD_SOCK_REPLY_END_REGEXP='^([0-9]+)\s*$'
BIRD_SHOW_PROTO_LINE_REGEXP='^\s*([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)(\s+([^\s].+)){0,1}\s*$'
BIRD_SHOW_PROTO_HEADER_REGEXP='^\s*(name)\s+(proto)\s+(table)\s+(state)\s+(since)\s+(info)\s*$'
bird_sock_header_regexp = re.compile(BIRD_SOCK_HEADER_REGEXP)
bird_sock_reply_end_regexp = re.compile(BIRD_SOCK_REPLY_END_REGEXP)
......@@ -130,7 +132,6 @@ def isBirdSockReplyCont(line):
else:
return False
def normalizeBirdSockLine(line):
ulgmodel.debug("normalizeBirdSockLine: "+line)
if(isBirdSockAsyncReply(line)):
......@@ -150,8 +151,62 @@ def normalizeBirdSockLine(line):
raise Exception("Can not normalize line: "+line)
def parseShowProtocols(line):
sh_proto_line_regexp = re.compile(BIRD_SHOW_PROTO_LINE_REGEXP)
m = sh_proto_line_regexp.match(line)
if(m):
ulgmodel.debug("bird.parseShowProtocols matched line: "+line)
ulgmodel.debug("bird.parseShowProtocols match results: "+str(m.groups()[0:-2]))
res = list(m.groups()[0:5])
if(m.group(6)):
res.append(m.group(6))
return res
else:
ulgmodel.debug("bird.parseShowProtocols failed to match line: "+line)
return None
# classes
class BirdShowProtocolsCommand(ulgmodel.TextCommand):
COMMAND_TEXT = 'show protocols'
def __init__(self,name=None):
ulgmodel.TextCommand.__init__(self,self.COMMAND_TEXT,param_specs=[],name=name)
def _decorateTableLine(self,line,router,decorator_helper):
lg = parseShowProtocols(line)
ulgmodel.debug("bird._decorateTableLine lg="+str(lg))
if(lg):
return [(lgm,ulgmodel.TableDecorator.GREEN) for lgm in lg]
else:
ulgmodel.log("BirdShowProtocolsCommand._decorateTableLine(): Skipping unparsable line: "+line)
return None
def decorateResult(self,result,router=None,decorator_helper=None):
if((not router) or (not decorator_helper)):
return "<pre>\n%s\n</pre>" % result
else:
table_header = []
table = []
for l in str.splitlines(result):
if(re.match('^\s*$',l)):
continue
hm = re.match(BIRD_SHOW_PROTO_HEADER_REGEXP,l)
if(hm):
table_header = hm.groups()
else:
tl = self._decorateTableLine(l,router,decorator_helper)
if(tl):
table.append(tl)
return ulgmodel.TableDecorator(table,table_header).decorate()
class BirdRouterLocal(ulgmodel.LocalRouter):
DefaultCommands = [ulgmodel.TextCommand('show protocols %s', [ulgmodel.TextParameter('.*')])]
DefaultCommands = [BirdShowProtocolsCommand()]
def __init__(self,sock=defaults.default_bird_sock,commands=None):
super(self.__class__,self).__init__()
......@@ -205,3 +260,62 @@ class BirdRouterLocal(ulgmodel.LocalRouter):
result = STRING_SOCKET_TIMEOUT
return result
#################################
def getForkNeeded(self):
return False
"""
def rescanBGPPeers(self,command,regexp,ipv6=True):
table = self.runRawCommand(command)
peers = []
rlr = re.compile(regexp)
if ipv6:
lines = normalizeBGPIPv6SumSplitLines(str.splitlines(table))
else:
lines = str.splitlines(table)
for tl in lines:
rlrm = rlr.match(tl)
if(rlrm):
peers.append(rlrm.group(1))
return peers
def rescanBGPIPv4Peers(self):
self.bgp_ipv4_peers = self.rescanBGPPeers(RESCAN_BGP_IPv4_COMMAND,BGP_IPV4_TABLE_LINE_REGEXP,False)
def rescanBGPIPv6Peers(self):
self.bgp_ipv6_peers = self.rescanBGPPeers(RESCAN_BGP_IPv6_COMMAND,BGP_IPV6_TABLE_LINE_REGEXP,True)
def rescanHook(self):
self.rescanBGPIPv4Peers()
self.rescanBGPIPv6Peers()
self.saveBGPPeers()
def getBGPIPv4Peers(self):
return self.bgp_ipv4_peers
def getBGPIPv6Peers(self):
return self.bgp_ipv6_peers
def saveBGPPeers(self):
key4 = self.getHost() + self.PS_KEY_BGPV4
key6 = self.getHost() + self.PS_KEY_BGPV6
ps = ulgmodel.PersistentStorage.load()
ps.set(key4,self.getBGPIPv4Peers())
ps.set(key6,self.getBGPIPv6Peers())
ps.save()
def loadBGPPeers(self):
key4 = self.getHost() + self.PS_KEY_BGPV4
key6 = self.getHost() + self.PS_KEY_BGPV6
ps = ulgmodel.PersistentStorage.load()
self.bgp_ipv4_peers = ps.get(key4)
self.bgp_ipv6_peers = ps.get(key6)
if(not self.getBGPIPv4Peers()) or (not self.getBGPIPv6Peers()):
self.rescanHook()
"""
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