def get_path_to_netbox(netbox):
"""Returns a likely path from netbox to its apparent gateway/router.
If any switches on the path, or the router itself is down,
no current path exists and a False value is returned. However,
if there is insufficient information for NAV to find a likely path,
a True value is returned.
"""
prefix = netbox.get_prefix()
if not prefix:
_logger.warning("couldn't find prefix for %s", netbox)
return True
router_ports = prefix.get_router_ports()
if router_ports:
router_port = router_ports[0]
else:
_logger.warning("couldn't find router ports for %s", prefix)
return True
router = router_port.interface.netbox
_logger.debug("reachability check for %s on %s (router: %s)",
netbox, prefix, router)
graph = get_graph_for_vlan(prefix.vlan)
try:
netbox.add_to_graph(graph)
except AttributeError:
pass
# first, see if any path exists
if not _path_exists(graph, netbox, router):
_logger.warning("cannot find a path between %s and %s on VLAN %s",
netbox, router, prefix.vlan)
return True
# now, remove nodes that are down and see if a path still exists
strip_down_nodes_from_graph(graph, keep=netbox)
if netbox not in graph or router not in graph:
if router.up == router.UP_UP:
_logger.warning("%(netbox)s topology problem: router %(router)s "
"is up, but not in VLAN graph for %(prefix)r. "
"Defaulting to 'reachable' status.", locals())
return True
_logger.debug("%s not reachable, router or box not in graph: %r",
netbox, graph.edges())
return False
try:
path = networkx.shortest_path(graph, netbox, router)
except NetworkXException as error:
_logger.debug("an internal networkx exception was raised in "
"shortest_path, assuming no path was found: %s", error)
path = []
else:
_logger.debug("path to %s: %r", netbox, path)
return path
评论列表
文章目录