From 6cde720737600cf28f1442e727930da2ba2efe07 Mon Sep 17 00:00:00 2001 From: Arseniy Kuznetsov Date: Thu, 2 May 2024 14:19:05 +0200 Subject: [PATCH] stack stack route fixes --- mktxp/collector/route_collector.py | 3 ++- mktxp/datasource/route_ds.py | 21 +++++++++++++++++++-- mktxp/utils/utils.py | 11 +++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/mktxp/collector/route_collector.py b/mktxp/collector/route_collector.py index 75dc5588..20d09e81 100644 --- a/mktxp/collector/route_collector.py +++ b/mktxp/collector/route_collector.py @@ -15,6 +15,7 @@ from mktxp.cli.config.config import MKTXPConfigKeys from mktxp.collector.base_collector import BaseCollector from mktxp.datasource.route_ds import RouteMetricsDataSource +from mktxp.utils.utils import str2bool class RouteCollector(BaseCollector): @@ -42,7 +43,7 @@ def collect(router_entry): routes_per_protocol = {route_label: 0 for route_label in route_labels} for route_record in route_records: for route_label in route_labels: - if route_record.get(route_label): + if str2bool(route_record.get(route_label)): routes_per_protocol[route_label] += 1 # compile route-per-protocol records diff --git a/mktxp/datasource/route_ds.py b/mktxp/datasource/route_ds.py index 57c7639c..2302a2c2 100644 --- a/mktxp/datasource/route_ds.py +++ b/mktxp/datasource/route_ds.py @@ -13,7 +13,7 @@ from mktxp.datasource.base_ds import BaseDSProcessor - +from mktxp.utils.utils import str2bool class RouteMetricsDataSource: ''' Routes Metrics data provider @@ -24,8 +24,25 @@ def metric_records(router_entry, *, metric_labels = None, ipv6 = False): if metric_labels is None: metric_labels = [] try: - route_records = router_entry.api_connection.router_api().get_resource(f'/{ip_stack}/route').get(active='yes') + #route_records = router_entry.api_connection.router_api().get_resource(f'/{ip_stack}/route').get(active='yes') + route_records = router_entry.api_connection.router_api().get_resource(f'/{ip_stack}/route').call('print', {'proplist':'active,connect,dynamic,static,bgp,ospf'}) + + #active_records = [record for record in route_records if record.get('active')] + RouteMetricsDataSource._remove_from_list_of_dict(route_records, 'active') + return BaseDSProcessor.trimmed_records(router_entry, router_records = route_records, metric_labels = metric_labels) except Exception as exc: print(f'Error getting {"IPv6" if ipv6 else "IPv4"} routes info from router{router_entry.router_name}@{router_entry.config_entry.hostname}: {exc}') return None + + # helpers + @staticmethod + def _remove_from_list_of_dict(dict_list, key): + indexes = [] + for index, dict in enumerate(dict_list): + if not str2bool(dict.get(key)): + indexes.append(index) + offset = 0 + for index in indexes: + dict_list.pop(index-offset) + offset += 1 diff --git a/mktxp/utils/utils.py b/mktxp/utils/utils.py index 54e1a902..78b20399 100755 --- a/mktxp/utils/utils.py +++ b/mktxp/utils/utils.py @@ -77,6 +77,17 @@ def parse_mkt_uptime(time): delta = timedelta(**{key: int(value) for key, value in time_dict.items() if value}).total_seconds() return int(delta) if delta else 0 +def str2bool(str_value): + if not str_value or not type(str_value) is str: + return False + str_value = str_value.lower() + if str_value in ('y', 'yes', 't', 'true', 'on', '1'): + return True + elif str_value in ('n', 'no', 'f', 'false', 'off', '0'): + return False + else: + raise ValueError(f'Invalid truth value: {str_value}') + class FSHelper: ''' File System ops helper '''