Skip to content
Snippets Groups Projects
Commit ae803b41 authored by Pelle Koster's avatar Pelle Koster
Browse files

improve and prettify logging for get-^Cterface-stats

parent c8a84f9e
No related branches found
No related tags found
No related merge requests found
...@@ -3,7 +3,7 @@ import logging.config ...@@ -3,7 +3,7 @@ import logging.config
import os import os
import socket import socket
from datetime import datetime from datetime import datetime
from typing import Iterable, List from typing import Iterable, List, Sequence
import click import click
import jsonschema import jsonschema
...@@ -22,23 +22,18 @@ logger = logging.getLogger(__file__) ...@@ -22,23 +22,18 @@ logger = logging.getLogger(__file__)
LOGGING_DEFAULT_CONFIG = { LOGGING_DEFAULT_CONFIG = {
"version": 1, "version": 1,
"disable_existing_loggers": False, "disable_existing_loggers": False,
"formatters": { "formatters": {"simple": {"format": "%(asctime)s - %(levelname)s - %(message)s"}},
"simple": {
"format": "%(asctime)s - %(name)s "
"(%(lineno)d) - %(levelname)s - %(message)s"
}
},
"handlers": { "handlers": {
"console": { "console": {
"class": "logging.StreamHandler", "class": "logging.StreamHandler",
"level": "DEBUG", "level": "INFO",
"formatter": "simple", "formatter": "simple",
"stream": "ext://sys.stdout", "stream": "ext://sys.stdout",
}, },
}, },
"loggers": { "loggers": {
"brian_polling_manager": { "brian_polling_manager": {
"level": "DEBUG", "level": "INFO",
"handlers": ["console"], "handlers": ["console"],
"propagate": False, "propagate": False,
} }
...@@ -62,6 +57,17 @@ def setup_logging(): ...@@ -62,6 +57,17 @@ def setup_logging():
if LOGGING_CONFIG is defined in the environment, use this for if LOGGING_CONFIG is defined in the environment, use this for
the filename, otherwise use LOGGING_DEFAULT_CONFIG the filename, otherwise use LOGGING_DEFAULT_CONFIG
""" """
# demote ncclient logs
def changeLevel(record):
if record.levelno == logging.INFO:
record.levelno = logging.DEBUG
record.levelname = "DEBUG"
return record
logging.getLogger("ncclient.transport.ssh").addFilter(changeLevel)
logging.getLogger("ncclient.operations.rpc").addFilter(changeLevel)
logging_config = LOGGING_DEFAULT_CONFIG logging_config = LOGGING_DEFAULT_CONFIG
if "LOGGING_CONFIG" in os.environ: if "LOGGING_CONFIG" in os.environ:
filename = os.environ["LOGGING_CONFIG"] filename = os.environ["LOGGING_CONFIG"]
...@@ -141,31 +147,58 @@ def process_juniper_router( ...@@ -141,31 +147,58 @@ def process_juniper_router(
router_fqdn: str, router_fqdn: str,
all_influx_params: dict, all_influx_params: dict,
): ):
logger.info(f"processing Juniper router {router_fqdn}") logger.info(f"Processing Juniper router {router_fqdn}")
document = get_netconf(router_fqdn, vendor=Vendor.JUNIPER) document = get_netconf(router_fqdn, vendor=Vendor.JUNIPER)
timestamp = datetime.now() timestamp = datetime.now()
influx_params = all_influx_params["brian-counters"] influx_params = all_influx_params["brian-counters"]
points = _juniper_brian_points( logger.info("Processing Brian points...")
router_fqdn=router_fqdn, points = list(
netconf_doc=document, _juniper_brian_points(
timestamp=timestamp, router_fqdn=router_fqdn,
measurement_name=influx_params["measurement"], netconf_doc=document,
timestamp=timestamp,
measurement_name=influx_params["measurement"],
)
) )
_log_interface_points_sorted(points)
write_points(points, influx_params=influx_params) write_points(points, influx_params=influx_params)
influx_params = all_influx_params["error-counters"] influx_params = all_influx_params["error-counters"]
points = _juniper_error_points( logger.info("Processing Error points...")
router_fqdn=router_fqdn, points = list(
netconf_doc=document, _juniper_error_points(
timestamp=timestamp, router_fqdn=router_fqdn,
measurement_name=influx_params["measurement"], netconf_doc=document,
timestamp=timestamp,
measurement_name=influx_params["measurement"],
)
) )
_log_interface_points_sorted(points, point_kind='error')
write_points(points, influx_params=influx_params) write_points(points, influx_params=influx_params)
def _log_interface_points_sorted(points: Sequence[dict], point_kind=""):
N_COLUMNS = 5
num_points = len(points)
point_kind = point_kind + " " if point_kind else ""
semicolon = ':' if num_points else ''
logger.info(
f"Found {point_kind}points for {num_points} interfaces{semicolon}"
)
if not points:
return
interfaces = sorted(p["tags"]["interface_name"] for p in points)
longest_ifc = max(len(i) for i in interfaces)
for n in range(len(interfaces) // N_COLUMNS + 1):
ifc_slice = interfaces[n * N_COLUMNS : (n + 1) * N_COLUMNS]
logger.info(" ".join(i.ljust(longest_ifc) for i in ifc_slice))
def _juniper_brian_points(router_fqdn, netconf_doc, timestamp, measurement_name): def _juniper_brian_points(router_fqdn, netconf_doc, timestamp, measurement_name):
interfaces = juniper.interface_counters(netconf_doc) interfaces = juniper.interface_counters(netconf_doc)
yield from vendors.brian_points( yield from vendors.brian_points(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment