1
0
Fork 1
mirror of https://github.com/thatmattlove/hyperglass.git synced 2026-01-31 14:29:27 +00:00
thatmattlove-hyperglass/hyperglass/execution/drivers/ssh_netmiko.py
Wilhelm Schonfeldt 0398966062
feat: Add structured traceroute support with comprehensive IP enrichment
MAJOR NEW ARCHITECTURE - STRUCTURED TRACEROUTE:
- Complete rewrite of traceroute data processing with structured output
- Dedicated TracerouteResult and TracerouteHop data models
- Platform-specific parsers with unified output format
- Rich metadata including ASN, organization, country, and prefix information
- AS path visualization with organization names in React Flow charts

SUPPORTED PLATFORMS:
- TraceroutePluginMikrotik: Handles MikroTik's complex multi-table format
  * Progressive statistics parsing with deduplication
  * Timeout hop handling and continuation line processing
  * Loss percentage and RTT statistics extraction
- TraceroutePluginHuawei: Unix-style traceroute format parser
  * Standard hop_number ip_address rtt format support
  * Timeout hop detection with * notation
  * Automatic cleanup of excessive trailing timeouts

COMPREHENSIVE IP ENRICHMENT SYSTEM:
- Offline enrichment using BGP.tools bulk data (1.3M+ CIDR entries)
- PeeringDB integration for IXP detection and ASN organization data
- Ultra-fast pickle cache system with combined data files
- Integer-based bitwise IP matching for maximum performance
- Bulk ASN organization lookup capabilities
- Private/reserved IP handling with AS0 fallbacks
- Country code mapping from ASN database
- Graceful fallbacks for missing enrichment data

FRONTEND ENHANCEMENTS:
- New traceroute table components with consistent formatting
- Enhanced AS path visualization with organization names
- Improved copy-to-clipboard functionality with structured data
- Unified table styling across BGP and traceroute results
- Better error handling and loading states

CONCURRENT PROCESSING INFRASTRUCTURE:
- Thread executor implementation for blocking I/O operations
- Query deduplication system to prevent resource conflicts
- Non-blocking Redis cache operations using asyncio executors
- Event coordination for waiting requests
- Background cleanup for completed operations
- Prevents website hangs during long-running queries

PLUGIN ARCHITECTURE IMPROVEMENTS:
- Platform-aware plugin system with proper execution restrictions
- Enhanced MikroTik garbage output cleaning
- IP enrichment plugins for both BGP routes and traceroute
- Conditional plugin execution based on platform detection
- Proper async/sync plugin method handling

CRITICAL BUG FIXES:
- Fixed double AS prefix bug (ASAS123456 → AS123456)
- Resolved TracerouteHop avg_rtt field/property conflicts
- Corrected Huawei traceroute source field validation
- Fixed plugin platform restriction enforcement
- Eliminated blocking I/O causing UI freezes
- Proper timeout and empty response caching prevention
- Enhanced private IP range detection and handling

PERFORMANCE OPTIMIZATIONS:
- Pickle cache system reduces startup time from seconds to milliseconds
- Bulk processing for ASN organization lookups
- Simplified IXP detection using single PeeringDB API call
- Efficient CIDR network sorting and integer-based lookups
- Reduced external API calls by 90%+
- Optimized memory usage for large datasets

API & ROUTING ENHANCEMENTS:
- Enhanced API routes with proper error handling
- Improved middleware for concurrent request processing
- Better state management and event handling
- Enhanced task processing with thread pool execution

This represents a complete transformation of hyperglass traceroute capabilities,
moving from basic text output to rich, structured data with comprehensive
network intelligence and concurrent processing support.
2025-09-28 13:48:04 +02:00

121 lines
4.1 KiB
Python

"""Netmiko-Specific Classes & Utilities.
https://github.com/ktbyers/netmiko
"""
# Standard Library
import math
from typing import Iterable
# Third Party
from netmiko import ( # type: ignore
ConnectHandler,
NetMikoTimeoutException,
NetMikoAuthenticationException,
)
# Project
from hyperglass.log import log
from hyperglass.state import use_state
from hyperglass.exceptions.public import AuthError, DeviceTimeout, ResponseEmpty
# Local
from .ssh import SSHConnection
netmiko_device_globals = {
# Netmiko doesn't currently handle Mikrotik echo verification well,
# see ktbyers/netmiko#1600
"mikrotik_routeros": {"global_cmd_verify": False},
"mikrotik_switchos": {"global_cmd_verify": False},
}
netmiko_device_send_args = {}
class NetmikoConnection(SSHConnection):
"""Handle a device connection via Netmiko."""
async def collect(self, host: str = None, port: int = None) -> Iterable:
"""Connect directly to a device.
Directly connects to the router via Netmiko library, returns the
command output.
"""
params = use_state("params")
_log = log.bind(
device=self.device.name,
address=f"{host}:{port}",
proxy=str(self.device.proxy.address) if self.device.proxy is not None else None,
)
_log.debug("Connecting to device")
global_args = netmiko_device_globals.get(self.device.platform, {})
send_args = netmiko_device_send_args.get(self.device.platform, {})
driver_kwargs = {
"host": host or self.device._target,
"port": port or self.device.port,
"device_type": self.device.get_device_type(),
"username": self.device.credential.username,
"global_delay_factor": 0.1,
"timeout": math.floor(params.request_timeout * 1.25),
"session_timeout": math.ceil(params.request_timeout - 1),
**global_args,
**self.device.driver_config,
}
if "_telnet" in self.device.platform:
# Telnet devices with a low delay factor (default) tend to
# throw login errors.
driver_kwargs["global_delay_factor"] = 2
if self.device.credential._method == "password":
# Use password auth if no key is defined.
driver_kwargs["password"] = self.device.credential.password.get_secret_value()
else:
# Otherwise, use key auth.
driver_kwargs["use_keys"] = True
driver_kwargs["key_file"] = self.device.credential.key
if self.device.credential._method == "encrypted_key":
# If the key is encrypted, use the password field as the
# private key password.
driver_kwargs["passphrase"] = self.device.credential.password.get_secret_value()
# Run blocking netmiko operations in thread executor to prevent blocking the event loop
import asyncio
import functools
def _netmiko_connect():
"""Execute blocking netmiko operations in a separate thread."""
try:
nm_connect_direct = ConnectHandler(**driver_kwargs)
responses = ()
for query in self.query:
raw = nm_connect_direct.send_command_timing(query, **send_args)
responses += (raw,)
nm_connect_direct.disconnect()
return responses
except NetMikoTimeoutException as scrape_error:
raise DeviceTimeout(error=scrape_error, device=self.device) from scrape_error
except NetMikoAuthenticationException as auth_error:
raise AuthError(error=auth_error, device=self.device) from auth_error
try:
# Execute blocking netmiko operations in thread pool
loop = asyncio.get_event_loop()
responses = await loop.run_in_executor(None, _netmiko_connect)
except (DeviceTimeout, AuthError):
# Re-raise our custom exceptions as-is
raise
if not responses:
raise ResponseEmpty(query=self.query_data)
return responses