mirror of
				https://github.com/KevinMidboe/mktxp-no-cli.git
				synced 2025-10-29 17:50:23 +00:00 
			
		
		
		
	Dual-CAPsMAN support, routerOS 7.13
This commit is contained in:
		| @@ -14,7 +14,7 @@ | |||||||
|  |  | ||||||
| from mktxp.flow.processor.output import BaseOutputProcessor | from mktxp.flow.processor.output import BaseOutputProcessor | ||||||
| from mktxp.datasource.wireless_ds import WirelessMetricsDataSource | from mktxp.datasource.wireless_ds import WirelessMetricsDataSource | ||||||
|  | from mktxp.flow.router_entry import RouterEntryWirelessType | ||||||
|  |  | ||||||
| class WirelessOutput: | class WirelessOutput: | ||||||
|     ''' Wireless Clients CLI Output |     ''' Wireless Clients CLI Output | ||||||
| @@ -42,8 +42,8 @@ class WirelessOutput: | |||||||
|  |  | ||||||
|         output_records = 0 |         output_records = 0 | ||||||
|         registration_records = len(registration_records)                 |         registration_records = len(registration_records)                 | ||||||
|         output_entry = BaseOutputProcessor.OutputWiFiEntry \ |         output_entry = BaseOutputProcessor.OutputWirelessEntry \ | ||||||
|                         if not WirelessMetricsDataSource.is_legacy(router_entry) else BaseOutputProcessor.OutputWirelessEntry |                         if router_entry.wireless_type in (RouterEntryWirelessType.DUAL, RouterEntryWirelessType.WIRELESS) else BaseOutputProcessor.OutputWiFiEntry | ||||||
|         output_table = BaseOutputProcessor.output_table(output_entry) |         output_table = BaseOutputProcessor.output_table(output_entry) | ||||||
|          |          | ||||||
|         for key in dhcp_rt_by_interface.keys(): |         for key in dhcp_rt_by_interface.keys(): | ||||||
|   | |||||||
| @@ -14,24 +14,29 @@ | |||||||
|  |  | ||||||
| from mktxp.datasource.base_ds import BaseDSProcessor | from mktxp.datasource.base_ds import BaseDSProcessor | ||||||
| from mktxp.datasource.wireless_ds import WirelessMetricsDataSource | from mktxp.datasource.wireless_ds import WirelessMetricsDataSource | ||||||
|  | from mktxp.flow.router_entry import RouterEntryWirelessType | ||||||
|  |  | ||||||
| class CapsmanInfo: | class CapsmanInfo: | ||||||
|     @staticmethod |     @staticmethod | ||||||
|     def capsman_path(router_entry): |     def capsman_paths(router_entry): | ||||||
|         if WirelessMetricsDataSource.is_legacy(router_entry): |         if router_entry.wireless_type == RouterEntryWirelessType.DUAL: | ||||||
|             return '/caps-man' |             return ['/caps-man', f'/interface/wifi/capsman'] | ||||||
|  |         elif router_entry.wireless_type == RouterEntryWirelessType.WIRELESS: | ||||||
|  |             return ['/caps-man'] | ||||||
|         else:     |         else:     | ||||||
|             wireless_package = WirelessMetricsDataSource.wireless_package(router_entry) |             wireless_package = WirelessMetricsDataSource.wireless_package(router_entry) | ||||||
|             return f'/interface/{wireless_package}/capsman' |             return [f'/interface/{wireless_package}/capsman'] | ||||||
|  |  | ||||||
|     @staticmethod |     @staticmethod | ||||||
|     def registration_table_path(router_entry): |     def registration_table_paths(router_entry): | ||||||
|         if WirelessMetricsDataSource.is_legacy(router_entry): |         if router_entry.wireless_type == RouterEntryWirelessType.DUAL: | ||||||
|             return '/caps-man/registration-table' |             return ['/caps-man/registration-table', f'/interface/wifi/registration-table'] | ||||||
|  |         elif router_entry.wireless_type == RouterEntryWirelessType.WIRELESS: | ||||||
|  |             return ['/caps-man/registration-table'] | ||||||
|         else:     |         else:     | ||||||
|             wireless_package = WirelessMetricsDataSource.wireless_package(router_entry) |             wireless_package = WirelessMetricsDataSource.wireless_package(router_entry) | ||||||
|             return f'/interface/{wireless_package}/registration-table' |             return [f'/interface/{wireless_package}/registration-table'] | ||||||
|  |  | ||||||
|  |  | ||||||
| class CapsmanCapsMetricsDataSource: | class CapsmanCapsMetricsDataSource: | ||||||
|     ''' Caps Metrics data provider |     ''' Caps Metrics data provider | ||||||
| @@ -41,8 +46,9 @@ class CapsmanCapsMetricsDataSource: | |||||||
|         if metric_labels is None: |         if metric_labels is None: | ||||||
|             metric_labels = []                 |             metric_labels = []                 | ||||||
|         try: |         try: | ||||||
|             capsman_path = CapsmanInfo.capsman_path(router_entry) |             remote_caps_records = [] | ||||||
|             remote_caps_records = router_entry.api_connection.router_api().get_resource(f'{capsman_path}/remote-cap').get() |             for capsman_path in CapsmanInfo.capsman_paths(router_entry): | ||||||
|  |                 remote_caps_records.extend(router_entry.api_connection.router_api().get_resource(f'{capsman_path}/remote-cap').get()) | ||||||
|             return BaseDSProcessor.trimmed_records(router_entry, router_records = remote_caps_records, metric_labels = metric_labels) |             return BaseDSProcessor.trimmed_records(router_entry, router_records = remote_caps_records, metric_labels = metric_labels) | ||||||
|         except Exception as exc: |         except Exception as exc: | ||||||
|             print(f'Error getting CAPsMAN remote caps info from router{router_entry.router_name}@{router_entry.config_entry.hostname}: {exc}') |             print(f'Error getting CAPsMAN remote caps info from router{router_entry.router_name}@{router_entry.config_entry.hostname}: {exc}') | ||||||
| @@ -57,8 +63,9 @@ class CapsmanRegistrationsMetricsDataSource: | |||||||
|         if metric_labels is None: |         if metric_labels is None: | ||||||
|             metric_labels = []                 |             metric_labels = []                 | ||||||
|         try: |         try: | ||||||
|             registration_table_path = CapsmanInfo.registration_table_path(router_entry) |             registration_table_records = [] | ||||||
|             registration_table_records = router_entry.api_connection.router_api().get_resource(f'{registration_table_path}').get() |             for registration_table_path in CapsmanInfo.registration_table_paths(router_entry): | ||||||
|  |                 registration_table_records.extend(router_entry.api_connection.router_api().get_resource(f'{registration_table_path}').get()) | ||||||
|              |              | ||||||
|             # With wifiwave2, Mikrotik renamed the field 'rx-signal' to 'signal'  |             # With wifiwave2, Mikrotik renamed the field 'rx-signal' to 'signal'  | ||||||
|             # For backward compatibility, including both variants |             # For backward compatibility, including both variants | ||||||
| @@ -75,10 +82,9 @@ class CapsmanRegistrationsMetricsDataSource: | |||||||
| class CapsmanInterfacesDatasource: | class CapsmanInterfacesDatasource: | ||||||
|     ''' Data provider for CAPsMaN interfaces |     ''' Data provider for CAPsMaN interfaces | ||||||
|     ''' |     ''' | ||||||
|  |  | ||||||
|     @staticmethod |     @staticmethod | ||||||
|     def metric_records(router_entry, *, metric_labels = None): |     def metric_records(router_entry, *, metric_labels = None): | ||||||
|         if not WirelessMetricsDataSource.is_legacy(router_entry): |         if not router_entry.wireless_type in (RouterEntryWirelessType.DUAL, RouterEntryWirelessType.WIRELESS): | ||||||
|             return None             |             return None             | ||||||
|         if metric_labels is None: |         if metric_labels is None: | ||||||
|             metric_labels = []                 |             metric_labels = []                 | ||||||
|   | |||||||
| @@ -29,3 +29,25 @@ class RouterboardMetricsDataSource: | |||||||
|             print(f'Error getting system routerboard info from router{router_entry.router_name}@{router_entry.config_entry.hostname}: {exc}') |             print(f'Error getting system routerboard info from router{router_entry.router_name}@{router_entry.config_entry.hostname}: {exc}') | ||||||
|             return None |             return None | ||||||
|  |  | ||||||
|  |     @staticmethod | ||||||
|  |     def firmware_version(router_entry): | ||||||
|  |         try: | ||||||
|  |             version_st = router_entry.api_connection.router_api().get_resource('/system/routerboard').call('print', {'proplist':'current-firmware'})[0] | ||||||
|  |             if version_st.get('current-firmware'): | ||||||
|  |                 return version_st['current-firmware'] | ||||||
|  |             return None | ||||||
|  |         except Exception as exc: | ||||||
|  |             print(f'Error getting routerboard current-firmware from router{router_entry.router_name}@{router_entry.config_entry.hostname}: {exc}') | ||||||
|  |             return None | ||||||
|  |  | ||||||
|  |     @staticmethod | ||||||
|  |     def firmware_version(router_entry): | ||||||
|  |         try: | ||||||
|  |             version_st = router_entry.api_connection.router_api().get_resource('/system/routerboard').call('print', {'proplist':'upgrade-firmware'})[0] | ||||||
|  |             if version_st.get('upgrade-firmware'): | ||||||
|  |                 return version_st['upgrade-firmware'] | ||||||
|  |             return None | ||||||
|  |         except Exception as exc: | ||||||
|  |             print(f'Error getting routerboard upgrade-firmware from router{router_entry.router_name}@{router_entry.config_entry.hostname}: {exc}') | ||||||
|  |             return None | ||||||
|  |  | ||||||
|   | |||||||
| @@ -11,21 +11,17 @@ | |||||||
| ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | ||||||
| ## GNU General Public License for more details. | ## GNU General Public License for more details. | ||||||
|  |  | ||||||
|  |  | ||||||
| from mktxp.datasource.base_ds import BaseDSProcessor | from mktxp.datasource.base_ds import BaseDSProcessor | ||||||
| from mktxp.datasource.package_ds import PackageMetricsDataSource | from mktxp.datasource.package_ds import PackageMetricsDataSource | ||||||
|  | from mktxp.flow.router_entry import RouterEntryWirelessType | ||||||
|  |  | ||||||
| class WirelessMetricsDataSource: | class WirelessMetricsDataSource: | ||||||
|     ''' Wireless Metrics data provider |     ''' Wireless Metrics data provider | ||||||
|     '''              |     '''              | ||||||
|     WIFIWAVE2 = 'wifiwave2' |  | ||||||
|     WIRELESS = 'wireless' |     WIRELESS = 'wireless' | ||||||
|  |     WIFIWAVE2 = 'wifiwave2' | ||||||
|     WIFI = 'wifi' |     WIFI = 'wifi' | ||||||
|  |  | ||||||
|     WIFI_PACKAGE = 'wifi-qcom' |  | ||||||
|     WIFI_AC_PACKAGE = 'wifi-qcom-ac' |  | ||||||
|  |  | ||||||
|     @staticmethod |     @staticmethod | ||||||
|     def metric_records(router_entry, *, metric_labels = None, add_router_id = True): |     def metric_records(router_entry, *, metric_labels = None, add_router_id = True): | ||||||
|         if metric_labels is None: |         if metric_labels is None: | ||||||
| @@ -45,20 +41,13 @@ class WirelessMetricsDataSource: | |||||||
|             print(f'Error getting wireless registration table info from router{router_entry.router_name}@{router_entry.config_entry.hostname}: {exc}') |             print(f'Error getting wireless registration table info from router{router_entry.router_name}@{router_entry.config_entry.hostname}: {exc}') | ||||||
|             return None |             return None | ||||||
|  |  | ||||||
|  |  | ||||||
|     @staticmethod |     @staticmethod | ||||||
|     def wireless_package(router_entry): |     def wireless_package(router_entry): | ||||||
|         if not router_entry.wifi_package: |         if router_entry.wireless_type in (RouterEntryWirelessType.DUAL, RouterEntryWirelessType.WIRELESS): | ||||||
|             if PackageMetricsDataSource.is_package_installed(router_entry, package_name = WirelessMetricsDataSource.WIFI_PACKAGE): |             return WirelessMetricsDataSource.WIRELESS | ||||||
|               router_entry.wifi_package = WirelessMetricsDataSource.WIFI |         elif router_entry.wireless_type == RouterEntryWirelessType.WIFIWAVE2: | ||||||
|             elif PackageMetricsDataSource.is_package_installed(router_entry, package_name = WirelessMetricsDataSource.WIFI_AC_PACKAGE): |             return WirelessMetricsDataSource.WIFIWAVE2 | ||||||
|               router_entry.wifi_package = WirelessMetricsDataSource.WIFI |  | ||||||
|             elif PackageMetricsDataSource.is_package_installed(router_entry, package_name = WirelessMetricsDataSource.WIFIWAVE2): |  | ||||||
|               router_entry.wifi_package = WirelessMetricsDataSource.WIFIWAVE2 |  | ||||||
|         else: |         else: | ||||||
|               router_entry.wifi_package = WirelessMetricsDataSource.WIRELESS |             return WirelessMetricsDataSource.WIFI | ||||||
|         return router_entry.wifi_package |  | ||||||
|  |  | ||||||
|     @staticmethod |  | ||||||
|     def is_legacy(router_entry): |  | ||||||
|         return WirelessMetricsDataSource.wireless_package(router_entry) == WirelessMetricsDataSource.WIRELESS |  | ||||||
|   | |||||||
| @@ -50,13 +50,10 @@ class BaseOutputProcessor: | |||||||
|             registration_record['rx_bytes'] = registration_record['bytes'].split(',')[1] |             registration_record['rx_bytes'] = registration_record['bytes'].split(',')[1] | ||||||
|             del registration_record['bytes'] |             del registration_record['bytes'] | ||||||
|  |  | ||||||
|         is_legacy = WirelessMetricsDataSource.is_legacy(router_entry) |  | ||||||
|         if registration_record.get('tx_rate'): |         if registration_record.get('tx_rate'): | ||||||
|             registration_record['tx_rate'] = BaseOutputProcessor.parse_bitrates(registration_record['tx_rate']) \ |             registration_record['tx_rate'] = BaseOutputProcessor.parse_bitrates(registration_record['tx_rate']) | ||||||
|                                                 if not is_legacy else BaseOutputProcessor.parse_rates(registration_record['tx_rate']) |  | ||||||
|         if registration_record.get('rx_rate'): |         if registration_record.get('rx_rate'): | ||||||
|             registration_record['rx_rate'] = BaseOutputProcessor.parse_bitrates(registration_record['rx_rate']) \ |             registration_record['rx_rate'] = BaseOutputProcessor.parse_bitrates(registration_record['rx_rate']) | ||||||
|                                                 if not is_legacy else BaseOutputProcessor.parse_rates(registration_record['rx_rate']) |  | ||||||
|         if registration_record.get('uptime'): |         if registration_record.get('uptime'): | ||||||
|             registration_record['uptime'] = naturaldelta(BaseOutputProcessor.parse_timedelta_seconds(registration_record['uptime']), months=True, minimum_unit='seconds') |             registration_record['uptime'] = naturaldelta(BaseOutputProcessor.parse_timedelta_seconds(registration_record['uptime']), months=True, minimum_unit='seconds') | ||||||
|  |  | ||||||
|   | |||||||
| @@ -12,11 +12,26 @@ | |||||||
| ## GNU General Public License for more details. | ## GNU General Public License for more details. | ||||||
|  |  | ||||||
|  |  | ||||||
|  | from enum import IntEnum | ||||||
| from collections import namedtuple | from collections import namedtuple | ||||||
| from mktxp.cli.config.config import config_handler, MKTXPConfigKeys, CollectorKeys | from mktxp.cli.config.config import config_handler, MKTXPConfigKeys, CollectorKeys | ||||||
| from mktxp.flow.router_connection import RouterAPIConnection | from mktxp.flow.router_connection import RouterAPIConnection | ||||||
|  | from mktxp.datasource.package_ds import PackageMetricsDataSource | ||||||
|  |  | ||||||
|  |  | ||||||
|  | class RouterEntryWirelessType(IntEnum): | ||||||
|  |     NONE = 0 | ||||||
|  |     WIRELESS = 1 | ||||||
|  |     WIFIWAVE2 = 2 | ||||||
|  |     WIFI = 3 | ||||||
|  |     DUAL = 4 | ||||||
|  |  | ||||||
|  | class RouterEntryWirelessPackage: | ||||||
|  |     WIFI_PACKAGE = 'wifi-qcom' | ||||||
|  |     WIFI_AC_PACKAGE = 'wifi-qcom-ac' | ||||||
|  |     WIFIWAVE2_PACKAGE = 'wifiwave2' | ||||||
|  |     WIRELESS_PACKAGE = 'wireless' | ||||||
|  |  | ||||||
| class RouterEntry: | class RouterEntry: | ||||||
|     ''' RouterOS Entry |     ''' RouterOS Entry | ||||||
|     '''                  |     '''                  | ||||||
| @@ -29,7 +44,6 @@ class RouterEntry: | |||||||
|             MKTXPConfigKeys.ROUTERBOARD_ADDRESS: self.config_entry.hostname |             MKTXPConfigKeys.ROUTERBOARD_ADDRESS: self.config_entry.hostname | ||||||
|             } |             } | ||||||
|          |          | ||||||
|         self.wifi_package = None |  | ||||||
|         self.time_spent =  { CollectorKeys.IDENTITY_COLLECTOR: 0, |         self.time_spent =  { CollectorKeys.IDENTITY_COLLECTOR: 0, | ||||||
|                             CollectorKeys.SYSTEM_RESOURCE_COLLECTOR: 0, |                             CollectorKeys.SYSTEM_RESOURCE_COLLECTOR: 0, | ||||||
|                             CollectorKeys.HEALTH_COLLECTOR: 0, |                             CollectorKeys.HEALTH_COLLECTOR: 0, | ||||||
| @@ -55,6 +69,23 @@ class RouterEntry: | |||||||
|                             }          |                             }          | ||||||
|         self._dhcp_entry = None |         self._dhcp_entry = None | ||||||
|         self._dhcp_records = {} |         self._dhcp_records = {} | ||||||
|  |         self._wireless_type = RouterEntryWirelessType.NONE                                     | ||||||
|  |  | ||||||
|  |     @property | ||||||
|  |     def wireless_type(self): | ||||||
|  |         router_entry = self | ||||||
|  |         if self._wireless_type == RouterEntryWirelessType.NONE: | ||||||
|  |             if PackageMetricsDataSource.is_package_installed(router_entry, package_name = RouterEntryWirelessPackage.WIFI_PACKAGE): | ||||||
|  |               self._wireless_type = RouterEntryWirelessType.WIFI | ||||||
|  |             elif PackageMetricsDataSource.is_package_installed(router_entry, package_name = RouterEntryWirelessPackage.WIFI_AC_PACKAGE): | ||||||
|  |               self._wireless_type = RouterEntryWirelessType.WIFI | ||||||
|  |             elif PackageMetricsDataSource.is_package_installed(router_entry, package_name = RouterEntryWirelessPackage.WIFIWAVE2_PACKAGE): | ||||||
|  |               self._wireless_type = RouterEntryWirelessType.WIFIWAVE2 | ||||||
|  |             elif PackageMetricsDataSource.is_package_installed(router_entry, package_name = RouterEntryWirelessPackage.WIRELESS_PACKAGE): | ||||||
|  |               self._wireless_type = RouterEntryWirelessType.DUAL | ||||||
|  |             else: | ||||||
|  |               self._wireless_type = RouterEntryWirelessType.WIRELESS | ||||||
|  |         return self._wireless_type | ||||||
|  |  | ||||||
|     @property |     @property | ||||||
|     def dhcp_entry(self): |     def dhcp_entry(self): | ||||||
| @@ -95,7 +126,7 @@ class RouterEntry: | |||||||
|         return is_ready |         return is_ready | ||||||
|  |  | ||||||
|     def is_done(self): |     def is_done(self): | ||||||
|         self.wifi_package = None  |  | ||||||
|         self._dhcp_records = {} |         self._dhcp_records = {} | ||||||
|  |         self._wireless_type = RouterEntryWirelessType.NONE | ||||||
|  |  | ||||||
| DHCPCacheEntry = namedtuple('DHCPCacheEntry', ['type', 'record']) | DHCPCacheEntry = namedtuple('DHCPCacheEntry', ['type', 'record']) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user