Skip to content

Commit

Permalink
stack stack route fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
akpw committed May 2, 2024
1 parent 4a370ff commit 6cde720
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
3 changes: 2 additions & 1 deletion mktxp/collector/route_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down
21 changes: 19 additions & 2 deletions mktxp/datasource/route_ds.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@


from mktxp.datasource.base_ds import BaseDSProcessor

from mktxp.utils.utils import str2bool

class RouteMetricsDataSource:
''' Routes Metrics data provider
Expand All @@ -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
11 changes: 11 additions & 0 deletions mktxp/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
'''
Expand Down

0 comments on commit 6cde720

Please sign in to comment.