From 76aa9acd9f346cd07da02680cdfe5c9597be21ad Mon Sep 17 00:00:00 2001 From: Arseniy Kuznetsov Date: Sun, 2 Oct 2022 11:24:42 +0100 Subject: [PATCH] bug fixes --- mktxp/collector/connection_collector.py | 2 +- mktxp/datasource/base_ds.py | 8 +++++++- mktxp/datasource/capsman_ds.py | 8 ++++++-- mktxp/datasource/connection_ds.py | 4 +++- mktxp/datasource/dhcp_ds.py | 4 +++- mktxp/datasource/firewall_ds.py | 4 +++- mktxp/datasource/health_ds.py | 4 +++- mktxp/datasource/identity_ds.py | 4 +++- mktxp/datasource/interface_ds.py | 8 ++++++-- mktxp/datasource/netwatch_ds.py | 4 +++- mktxp/datasource/poe_ds.py | 4 +++- mktxp/datasource/pool_ds.py | 8 ++++++-- mktxp/datasource/route_ds.py | 4 +++- mktxp/datasource/routerboard_ds.py | 4 +++- mktxp/datasource/system_resource_ds.py | 4 +++- mktxp/datasource/wireless_ds.py | 4 +++- setup.py | 2 +- 17 files changed, 60 insertions(+), 20 deletions(-) diff --git a/mktxp/collector/connection_collector.py b/mktxp/collector/connection_collector.py index 5b1646f..c9d112f 100644 --- a/mktxp/collector/connection_collector.py +++ b/mktxp/collector/connection_collector.py @@ -24,7 +24,7 @@ class IPConnectionCollector(BaseCollector): if not router_entry.config_entry.connections: return - connection_records = IPConnectionDatasource.metric_records(router_entry, metric_labels = []) + connection_records = IPConnectionDatasource.metric_records(router_entry) if connection_records: connection_metrics = BaseCollector.gauge_collector('ip_connections_total', 'Number of IP connections', connection_records, 'count',) diff --git a/mktxp/datasource/base_ds.py b/mktxp/datasource/base_ds.py index 17fdd7c..a656e42 100644 --- a/mktxp/datasource/base_ds.py +++ b/mktxp/datasource/base_ds.py @@ -17,7 +17,13 @@ class BaseDSProcessor: ''' @staticmethod - def trimmed_records(router_entry, *, router_records = [], metric_labels = [], add_router_id = True, translation_table = {}): + def trimmed_records(router_entry, *, router_records = None, metric_labels = None, add_router_id = True, translation_table = None): + if router_records is None: + router_records = [] + if metric_labels is None: + metric_labels = [] + if translation_table is None: + translation_table = {} dash2_ = lambda x : x.replace('-', '_') if len(metric_labels) == 0 and len(router_records) > 0: metric_labels = [dash2_(key) for key in router_records[0].keys()] diff --git a/mktxp/datasource/capsman_ds.py b/mktxp/datasource/capsman_ds.py index 0a93a6a..2fc8759 100644 --- a/mktxp/datasource/capsman_ds.py +++ b/mktxp/datasource/capsman_ds.py @@ -19,7 +19,9 @@ class CapsmanCapsMetricsDataSource: ''' Caps Metrics data provider ''' @staticmethod - def metric_records(router_entry, *, metric_labels = []): + def metric_records(router_entry, *, metric_labels = None): + if metric_labels is None: + metric_labels = [] try: remote_caps_records = router_entry.api_connection.router_api().get_resource('/caps-man/remote-cap').get() return BaseDSProcessor.trimmed_records(router_entry, router_records = remote_caps_records, metric_labels = metric_labels) @@ -32,7 +34,9 @@ class CapsmanRegistrationsMetricsDataSource: ''' Capsman Registrations Metrics data provider ''' @staticmethod - def metric_records(router_entry, *, metric_labels = [], add_router_id = True): + def metric_records(router_entry, *, metric_labels = None, add_router_id = True): + if metric_labels is None: + metric_labels = [] try: registration_table_records = router_entry.api_connection.router_api().get_resource('/caps-man/registration-table').get() return BaseDSProcessor.trimmed_records(router_entry, router_records = registration_table_records, metric_labels = metric_labels, add_router_id = add_router_id) diff --git a/mktxp/datasource/connection_ds.py b/mktxp/datasource/connection_ds.py index 3f8800e..f1a84d6 100644 --- a/mktxp/datasource/connection_ds.py +++ b/mktxp/datasource/connection_ds.py @@ -19,7 +19,9 @@ class IPConnectionDatasource: ''' IP connections data provider ''' @staticmethod - def metric_records(router_entry, *, metric_labels = []): + def metric_records(router_entry, *, metric_labels = None): + if metric_labels is None: + metric_labels = [] try: answer = router_entry.api_connection.router_api().get_binary_resource('/ip/firewall/connection/').call('print', {'count-only': b''}) # answer looks and feels like an empty list: [], but it has a special attribute `done_message` diff --git a/mktxp/datasource/dhcp_ds.py b/mktxp/datasource/dhcp_ds.py index 1ff86e4..215f3d4 100644 --- a/mktxp/datasource/dhcp_ds.py +++ b/mktxp/datasource/dhcp_ds.py @@ -20,7 +20,9 @@ class DHCPMetricsDataSource: ''' DHCP Metrics data provider ''' @staticmethod - def metric_records(router_entry, *, metric_labels = [], add_router_id = True): + def metric_records(router_entry, *, metric_labels = None, add_router_id = True): + if metric_labels is None: + metric_labels = [] try: #dhcp_lease_records = router_entry.api_connection.router_api().get_resource('/ip/dhcp-server/lease').get(status='bound') dhcp_lease_records = router_entry.api_connection.router_api().get_resource('/ip/dhcp-server/lease').call('print', {'active':''}) diff --git a/mktxp/datasource/firewall_ds.py b/mktxp/datasource/firewall_ds.py index b18fa06..4ac456f 100644 --- a/mktxp/datasource/firewall_ds.py +++ b/mktxp/datasource/firewall_ds.py @@ -19,7 +19,9 @@ class FirewallMetricsDataSource: ''' Firewall Metrics data provider ''' @staticmethod - def metric_records(router_entry, *, metric_labels = [], raw = False, matching_only = True): + def metric_records(router_entry, *, metric_labels = None, raw = False, matching_only = True): + if metric_labels is None: + metric_labels = [] try: filter_path = '/ip/firewall/filter' if not raw else '/ip/firewall/raw' firewall_records = router_entry.api_connection.router_api().get_resource(filter_path).call('print', {'stats':'', 'all':''}) diff --git a/mktxp/datasource/health_ds.py b/mktxp/datasource/health_ds.py index 5a3b7e5..cbee111 100644 --- a/mktxp/datasource/health_ds.py +++ b/mktxp/datasource/health_ds.py @@ -19,7 +19,9 @@ class HealthMetricsDataSource: ''' Health Metrics data provider ''' @staticmethod - def metric_records(router_entry, *, metric_labels = []): + def metric_records(router_entry, *, metric_labels = None): + if metric_labels is None: + metric_labels = [] try: health_records = router_entry.api_connection.router_api().get_resource('/system/health').get() for record in health_records: diff --git a/mktxp/datasource/identity_ds.py b/mktxp/datasource/identity_ds.py index ae2d73e..ba7446f 100644 --- a/mktxp/datasource/identity_ds.py +++ b/mktxp/datasource/identity_ds.py @@ -19,7 +19,9 @@ class IdentityMetricsDataSource: ''' Identity Metrics data provider ''' @staticmethod - def metric_records(router_entry, *, metric_labels = []): + def metric_records(router_entry, *, metric_labels = None): + if metric_labels is None: + metric_labels = [] try: identity_records = router_entry.api_connection.router_api().get_resource('/system/identity').get() return BaseDSProcessor.trimmed_records(router_entry, router_records = identity_records, metric_labels = metric_labels) diff --git a/mktxp/datasource/interface_ds.py b/mktxp/datasource/interface_ds.py index dd6b36a..a49f2ea 100644 --- a/mktxp/datasource/interface_ds.py +++ b/mktxp/datasource/interface_ds.py @@ -19,7 +19,9 @@ class InterfaceTrafficMetricsDataSource: ''' Interface Traffic Metrics data provider ''' @staticmethod - def metric_records(router_entry, *, metric_labels = []): + def metric_records(router_entry, *, metric_labels = None): + if metric_labels is None: + metric_labels = [] try: traffic_records = router_entry.api_connection.router_api().get_resource('/interface').get(running='yes', disabled='no') return BaseDSProcessor.trimmed_records(router_entry, router_records = traffic_records, metric_labels = metric_labels) @@ -32,7 +34,9 @@ class InterfaceMonitorMetricsDataSource: ''' Interface Monitor Metrics data provider ''' @staticmethod - def metric_records(router_entry, *, metric_labels = [], kind = 'ethernet', include_comments = False, running_only = True): + def metric_records(router_entry, *, metric_labels = None, kind = 'ethernet', include_comments = False, running_only = True): + if metric_labels is None: + metric_labels = [] try: interfaces = router_entry.api_connection.router_api().get_resource(f'/interface/{kind}').get() interface_names = [(interface['name'], interface.get('comment'), interface.get('running')) for interface in interfaces] diff --git a/mktxp/datasource/netwatch_ds.py b/mktxp/datasource/netwatch_ds.py index 5aa168e..7101bd5 100644 --- a/mktxp/datasource/netwatch_ds.py +++ b/mktxp/datasource/netwatch_ds.py @@ -19,7 +19,9 @@ class NetwatchMetricsDataSource: ''' Netwatch Metrics data provider ''' @staticmethod - def metric_records(router_entry, *, metric_labels = []): + def metric_records(router_entry, *, metric_labels = None): + if metric_labels is None: + metric_labels = [] try: netwatch_records = router_entry.api_connection.router_api().get_resource('/tool/netwatch').get(disabled='false') if 'name' in metric_labels: diff --git a/mktxp/datasource/poe_ds.py b/mktxp/datasource/poe_ds.py index 612eb22..a93064a 100644 --- a/mktxp/datasource/poe_ds.py +++ b/mktxp/datasource/poe_ds.py @@ -19,7 +19,9 @@ class POEMetricsDataSource: ''' POE Metrics data provider ''' @staticmethod - def metric_records(router_entry, *, include_comments = False, metric_labels = []): + def metric_records(router_entry, *, include_comments = False, metric_labels = None): + if metric_labels is None: + metric_labels = [] try: poe_records = router_entry.api_connection.router_api().get_resource('/interface/ethernet/poe').get() for int_num, poe_record in enumerate(poe_records): diff --git a/mktxp/datasource/pool_ds.py b/mktxp/datasource/pool_ds.py index 69f39ef..b820c32 100644 --- a/mktxp/datasource/pool_ds.py +++ b/mktxp/datasource/pool_ds.py @@ -19,7 +19,9 @@ class PoolMetricsDataSource: ''' Pool Metrics data provider ''' @staticmethod - def metric_records(router_entry, *, metric_labels = []): + def metric_records(router_entry, *, metric_labels = None): + if metric_labels is None: + metric_labels = [] try: pool_records = router_entry.api_connection.router_api().get_resource('/ip/pool').get() return BaseDSProcessor.trimmed_records(router_entry, router_records = pool_records, metric_labels = metric_labels) @@ -32,7 +34,9 @@ class PoolUsedMetricsDataSource: ''' Pool/Used Metrics data provider ''' @staticmethod - def metric_records(router_entry, *, metric_labels = []): + def metric_records(router_entry, *, metric_labels = None): + if metric_labels is None: + metric_labels = [] try: pool_used_records = router_entry.api_connection.router_api().get_resource('/ip/pool/used').get() return BaseDSProcessor.trimmed_records(router_entry, router_records = pool_used_records, metric_labels = metric_labels) diff --git a/mktxp/datasource/route_ds.py b/mktxp/datasource/route_ds.py index 90444d2..c704d28 100644 --- a/mktxp/datasource/route_ds.py +++ b/mktxp/datasource/route_ds.py @@ -19,7 +19,9 @@ class RouteMetricsDataSource: ''' Routes Metrics data provider ''' @staticmethod - def metric_records(router_entry, *, metric_labels = []): + def metric_records(router_entry, *, metric_labels = None): + if metric_labels is None: + metric_labels = [] try: route_records = router_entry.api_connection.router_api().get_resource('/ip/route').get(active='yes') return BaseDSProcessor.trimmed_records(router_entry, router_records = route_records, metric_labels = metric_labels) diff --git a/mktxp/datasource/routerboard_ds.py b/mktxp/datasource/routerboard_ds.py index b4ae3f5..805ccd3 100644 --- a/mktxp/datasource/routerboard_ds.py +++ b/mktxp/datasource/routerboard_ds.py @@ -19,7 +19,9 @@ class RouterboardMetricsDataSource: ''' Routerboard Metrics data provider ''' @staticmethod - def metric_records(router_entry, *, metric_labels = []): + def metric_records(router_entry, *, metric_labels = None): + if metric_labels is None: + metric_labels = [] try: routerboard_records = router_entry.api_connection.router_api().get_resource('/system/routerboard').get() return BaseDSProcessor.trimmed_records(router_entry, router_records = routerboard_records, metric_labels = metric_labels) diff --git a/mktxp/datasource/system_resource_ds.py b/mktxp/datasource/system_resource_ds.py index 4f462ff..3ebbba6 100644 --- a/mktxp/datasource/system_resource_ds.py +++ b/mktxp/datasource/system_resource_ds.py @@ -19,7 +19,9 @@ class SystemResourceMetricsDataSource: ''' System Resource Metrics data provider ''' @staticmethod - def metric_records(router_entry, *, metric_labels = []): + def metric_records(router_entry, *, metric_labels = None): + if metric_labels is None: + metric_labels = [] try: system_resource_records = router_entry.api_connection.router_api().get_resource('/system/resource').get() return BaseDSProcessor.trimmed_records(router_entry, router_records = system_resource_records, metric_labels = metric_labels) diff --git a/mktxp/datasource/wireless_ds.py b/mktxp/datasource/wireless_ds.py index faa5a4a..086ff69 100644 --- a/mktxp/datasource/wireless_ds.py +++ b/mktxp/datasource/wireless_ds.py @@ -19,7 +19,9 @@ class WirelessMetricsDataSource: ''' Wireless Metrics data provider ''' @staticmethod - def metric_records(router_entry, *, metric_labels = [], add_router_id = True): + def metric_records(router_entry, *, metric_labels = None, add_router_id = True): + if metric_labels is None: + metric_labels = [] try: registration_table_records = router_entry.api_connection.router_api().get_resource('/interface/wireless/registration-table').get() return BaseDSProcessor.trimmed_records(router_entry, router_records = registration_table_records, metric_labels = metric_labels, add_router_id = add_router_id) diff --git a/setup.py b/setup.py index 7a59337..e477b45 100755 --- a/setup.py +++ b/setup.py @@ -20,7 +20,7 @@ with open(path.join(pkg_dir, 'README.md'), encoding='utf-8') as f: setup( name='mktxp', - version='0.35', + version='0.36', url='https://github.com/akpw/mktxp',