lib/batman: refactor gateway handling

influxdb
Martin Weinelt 2015-03-25 14:11:00 +01:00
parent e3e5ae1615
commit 8d4856db56
1 changed files with 20 additions and 18 deletions

View File

@ -12,6 +12,9 @@ class Batman(object):
self.mesh_interface = mesh_interface self.mesh_interface = mesh_interface
self.alfred_sock = alfred_sockpath self.alfred_sock = alfred_sockpath
# compile regular expressions only once on startup
self.mac_addr_pattern = re.compile(r'(([a-z0-9]{2}:){5}[a-z0-9]{2})')
def vis_data(self, batadv_vis=False): def vis_data(self, batadv_vis=False):
vds = self.vis_data_batctl_legacy() vds = self.vis_data_batctl_legacy()
if batadv_vis: if batadv_vis:
@ -60,36 +63,35 @@ class Batman(object):
output = subprocess.check_output( output = subprocess.check_output(
['batctl', '-m', self.mesh_interface, 'gwl', '-n']) ['batctl', '-m', self.mesh_interface, 'gwl', '-n'])
output_utf8 = output.decode('utf-8') output_utf8 = output.decode('utf-8')
lines = output_utf8.splitlines() rows = output_utf8.splitlines()
own_mac = re.match(r"^.*MainIF/MAC: [^/]+/([0-9a-f:]+).*$",
lines[0]).group(1)
gateways = [] gateways = []
gw_mode = self.gateway_mode()
if gw_mode['mode'] == 'server':
gateways.append(own_mac)
for line in lines: # local gateway
gw_line = re.match(r"^(?:=>)? +([0-9a-f:]+) ", line) header = rows.pop(0)
if gw_line: mode, bandwidth = self.gateway_mode()
gateways.append(gw_line.group(1)) if mode == 'server':
local_gw_mac = self.mac_addr_pattern.search(header).group(0)
gateways.append(local_gw_mac)
# remote gateway(s)
for row in rows:
match = self.mac_addr_pattern.search(row)
if match:
gateways.append(match.group(1))
return gateways return gateways
def gateway_mode(self): def gateway_mode(self):
""" """
Parse "batctl -m <mesh_interface> gw" Parse "batctl -m <mesh_interface> gw"
return: tuple mode, bandwidth, if mode != server then bandwidth is None
""" """
output = subprocess.check_output( output = subprocess.check_output(
['batctl', '-m', self.mesh_interface, 'gw']) ['batctl', '-m', self.mesh_interface, 'gw'])
elements = output.decode("utf-8").split() chunks = output.decode("utf-8").split()
mode = elements[0]
if mode == 'server': return chunks[0], chunks[3] if 3 in chunks else None
return {'mode': 'server',
'bandwidth': elements[3]}
else:
return {'mode': mode}
if __name__ == "__main__": if __name__ == "__main__":
bc = Batman() bc = Batman()