mirror of
https://github.com/checktheroads/hyperglass
synced 2024-05-11 05:55:08 +00:00
shorten logger function name
This commit is contained in:
@@ -10,7 +10,7 @@ import re
|
||||
# Third Party Imports
|
||||
import httpx
|
||||
import sshtunnel
|
||||
from logzero import logger
|
||||
from logzero import logger as log
|
||||
from netmiko import ConnectHandler
|
||||
from netmiko import NetMikoAuthenticationException
|
||||
from netmiko import NetmikoAuthError
|
||||
@@ -64,9 +64,7 @@ class Connect:
|
||||
"""
|
||||
device_proxy = getattr(proxies, self.device_config.proxy)
|
||||
|
||||
logger.debug(
|
||||
f"Connecting to {self.device_config.proxy} via sshtunnel library..."
|
||||
)
|
||||
log.debug(f"Connecting to {self.device_config.proxy} via sshtunnel library...")
|
||||
try:
|
||||
tunnel = sshtunnel.open_tunnel(
|
||||
device_proxy.address.compressed,
|
||||
@@ -82,7 +80,7 @@ class Connect:
|
||||
logger=logger,
|
||||
)
|
||||
except sshtunnel.BaseSSHTunnelForwarderError as scrape_proxy_error:
|
||||
logger.error(
|
||||
log.error(
|
||||
f"Error connecting to device {self.device_config.location} via "
|
||||
f"proxy {self.device_config.proxy}"
|
||||
)
|
||||
@@ -93,7 +91,7 @@ class Connect:
|
||||
error=scrape_proxy_error,
|
||||
)
|
||||
with tunnel:
|
||||
logger.debug(f"Established tunnel with {self.device_config.proxy}")
|
||||
log.debug(f"Established tunnel with {self.device_config.proxy}")
|
||||
scrape_host = {
|
||||
"host": "localhost",
|
||||
"port": tunnel.local_bind_port,
|
||||
@@ -103,9 +101,9 @@ class Connect:
|
||||
"global_delay_factor": 0.2,
|
||||
"timeout": params.general.request_timeout - 1,
|
||||
}
|
||||
logger.debug(f"SSH proxy local binding: localhost:{tunnel.local_bind_port}")
|
||||
log.debug(f"SSH proxy local binding: localhost:{tunnel.local_bind_port}")
|
||||
try:
|
||||
logger.debug(
|
||||
log.debug(
|
||||
f"Connecting to {self.device_config.location} "
|
||||
"via Netmiko library..."
|
||||
)
|
||||
@@ -114,12 +112,12 @@ class Connect:
|
||||
for query in self.query:
|
||||
raw = nm_connect_direct.send_command(query)
|
||||
responses.append(raw)
|
||||
logger.debug(f'Raw response for command "{query}":\n{raw}')
|
||||
log.debug(f'Raw response for command "{query}":\n{raw}')
|
||||
response = "\n".join(responses)
|
||||
logger.debug(f"Response type:\n{type(response)}")
|
||||
log.debug(f"Response type:\n{type(response)}")
|
||||
|
||||
except (NetMikoTimeoutException, NetmikoTimeoutError) as scrape_error:
|
||||
logger.error(
|
||||
log.error(
|
||||
f"Timeout connecting to device {self.device_config.location}: "
|
||||
f"{scrape_error}"
|
||||
)
|
||||
@@ -130,7 +128,7 @@ class Connect:
|
||||
error=params.messages.request_timeout,
|
||||
)
|
||||
except (NetMikoAuthenticationException, NetmikoAuthError) as auth_error:
|
||||
logger.error(
|
||||
log.error(
|
||||
f"Error authenticating to device {self.device_config.location}: "
|
||||
f"{auth_error}"
|
||||
)
|
||||
@@ -141,7 +139,7 @@ class Connect:
|
||||
error=params.messages.authentication_error,
|
||||
) from None
|
||||
except sshtunnel.BaseSSHTunnelForwarderError as scrape_error:
|
||||
logger.error(
|
||||
log.error(
|
||||
f"Error connecting to device proxy {self.device_config.proxy}: "
|
||||
f"{scrape_error}"
|
||||
)
|
||||
@@ -152,14 +150,14 @@ class Connect:
|
||||
error=params.messages.general,
|
||||
)
|
||||
if response is None:
|
||||
logger.error(f"No response from device {self.device_config.location}")
|
||||
log.error(f"No response from device {self.device_config.location}")
|
||||
raise ScrapeError(
|
||||
params.messages.connection_error,
|
||||
device_name=self.device_config.display_name,
|
||||
proxy=None,
|
||||
error=params.messages.noresponse_error,
|
||||
)
|
||||
logger.debug(f"Output for query: {self.query}:\n{response}")
|
||||
log.debug(f"Output for query: {self.query}:\n{response}")
|
||||
return response
|
||||
|
||||
async def scrape_direct(self):
|
||||
@@ -168,7 +166,7 @@ class Connect:
|
||||
command output.
|
||||
"""
|
||||
|
||||
logger.debug(f"Connecting directly to {self.device_config.location}...")
|
||||
log.debug(f"Connecting directly to {self.device_config.location}...")
|
||||
|
||||
scrape_host = {
|
||||
"host": self.device_config.address.compressed,
|
||||
@@ -181,17 +179,15 @@ class Connect:
|
||||
}
|
||||
|
||||
try:
|
||||
logger.debug(f"Device Parameters: {scrape_host}")
|
||||
logger.debug(
|
||||
log.debug(f"Device Parameters: {scrape_host}")
|
||||
log.debug(
|
||||
f"Connecting to {self.device_config.location} via Netmiko library"
|
||||
)
|
||||
nm_connect_direct = ConnectHandler(**scrape_host)
|
||||
response = nm_connect_direct.send_command(self.query)
|
||||
except (NetMikoTimeoutException, NetmikoTimeoutError) as scrape_error:
|
||||
logger.error(
|
||||
f"{params.general.request_timeout - 1} second timeout expired."
|
||||
)
|
||||
logger.error(scrape_error)
|
||||
log.error(f"{params.general.request_timeout - 1} second timeout expired.")
|
||||
log.error(scrape_error)
|
||||
raise DeviceTimeout(
|
||||
params.messages.connection_error,
|
||||
device_name=self.device_config.display_name,
|
||||
@@ -199,10 +195,8 @@ class Connect:
|
||||
error=params.messages.request_timeout,
|
||||
)
|
||||
except (NetMikoAuthenticationException, NetmikoAuthError) as auth_error:
|
||||
logger.error(
|
||||
f"Error authenticating to device {self.device_config.location}"
|
||||
)
|
||||
logger.error(auth_error)
|
||||
log.error(f"Error authenticating to device {self.device_config.location}")
|
||||
log.error(auth_error)
|
||||
|
||||
raise AuthError(
|
||||
params.messages.connection_error,
|
||||
@@ -211,19 +205,19 @@ class Connect:
|
||||
error=params.messages.authentication_error,
|
||||
)
|
||||
if not response:
|
||||
logger.error(f"No response from device {self.device_config.location}")
|
||||
log.error(f"No response from device {self.device_config.location}")
|
||||
raise ScrapeError(
|
||||
params.messages.connection_error,
|
||||
device_name=self.device_config.display_name,
|
||||
proxy=None,
|
||||
error=params.messages.noresponse_error,
|
||||
)
|
||||
logger.debug(f"Output for query: {self.query}:\n{response}")
|
||||
log.debug(f"Output for query: {self.query}:\n{response}")
|
||||
return response
|
||||
|
||||
async def rest(self):
|
||||
"""Sends HTTP POST to router running a hyperglass API agent"""
|
||||
logger.debug(f"Query parameters: {self.query}")
|
||||
log.debug(f"Query parameters: {self.query}")
|
||||
|
||||
uri = Supported.map_rest(self.device_config.nos)
|
||||
headers = {
|
||||
@@ -238,8 +232,8 @@ class Connect:
|
||||
uri=uri,
|
||||
)
|
||||
|
||||
logger.debug(f"HTTP Headers: {headers}")
|
||||
logger.debug(f"URL endpoint: {endpoint}")
|
||||
log.debug(f"HTTP Headers: {headers}")
|
||||
log.debug(f"URL endpoint: {endpoint}")
|
||||
|
||||
try:
|
||||
http_client = httpx.AsyncClient()
|
||||
@@ -248,8 +242,8 @@ class Connect:
|
||||
)
|
||||
response = raw_response.text
|
||||
|
||||
logger.debug(f"HTTP status code: {raw_response.status_code}")
|
||||
logger.debug(f"Output for query {self.query}:\n{response}")
|
||||
log.debug(f"HTTP status code: {raw_response.status_code}")
|
||||
log.debug(f"Output for query {self.query}:\n{response}")
|
||||
except (
|
||||
httpx.exceptions.ConnectTimeout,
|
||||
httpx.exceptions.CookieConflict,
|
||||
@@ -270,7 +264,7 @@ class Connect:
|
||||
rest_msg = " ".join(
|
||||
re.findall(r"[A-Z][^A-Z]*", rest_error.__class__.__name__)
|
||||
)
|
||||
logger.error(
|
||||
log.error(
|
||||
f"Error connecting to device {self.device_config.location}: {rest_msg}"
|
||||
)
|
||||
raise RestError(
|
||||
@@ -286,7 +280,7 @@ class Connect:
|
||||
)
|
||||
|
||||
if raw_response.status_code != 200:
|
||||
logger.error(f"Response code is {raw_response.status_code}")
|
||||
log.error(f"Response code is {raw_response.status_code}")
|
||||
raise RestError(
|
||||
params.messages.connection_error,
|
||||
device_name=self.device_config.display_name,
|
||||
@@ -294,14 +288,14 @@ class Connect:
|
||||
)
|
||||
|
||||
if not response:
|
||||
logger.error(f"No response from device {self.device_config.location}")
|
||||
log.error(f"No response from device {self.device_config.location}")
|
||||
raise RestError(
|
||||
params.messages.connection_error,
|
||||
device_name=self.device_config.display_name,
|
||||
error=params.messages.noresponse_error,
|
||||
)
|
||||
|
||||
logger.debug(f"Output for query: {self.query}:\n{response}")
|
||||
log.debug(f"Output for query: {self.query}:\n{response}")
|
||||
return response
|
||||
|
||||
|
||||
@@ -325,14 +319,14 @@ class Execute:
|
||||
"""
|
||||
device_config = getattr(devices, self.query_location)
|
||||
|
||||
logger.debug(f"Received query for {self.query_data}")
|
||||
logger.debug(f"Matched device config: {device_config}")
|
||||
log.debug(f"Received query for {self.query_data}")
|
||||
log.debug(f"Matched device config: {device_config}")
|
||||
|
||||
# Run query parameters through validity checks
|
||||
validation = Validate(device_config, self.query_type, self.query_target)
|
||||
valid_input = validation.validate_query()
|
||||
if valid_input:
|
||||
logger.debug(f"Validation passed for query: {self.query_data}")
|
||||
log.debug(f"Validation passed for query: {self.query_data}")
|
||||
pass
|
||||
|
||||
connect = None
|
||||
|
Reference in New Issue
Block a user