From b3730387eca2a09ddae7b93ac6bd6e9fd4e8134f Mon Sep 17 00:00:00 2001 From: PhiBo Date: Mon, 16 Dec 2024 09:49:43 +0100 Subject: [PATCH] Move interface status to interface collector --- mktxp/collector/eoip_collector.py | 39 +++++++++++--------------- mktxp/collector/gre_collector.py | 27 ++++++------------ mktxp/collector/interface_collector.py | 21 ++++++++++++-- mktxp/collector/ipip_collector.py | 27 ++++++------------ mktxp/collector/monitor_collector.py | 24 ++++++---------- mktxp/datasource/interface_ds.py | 9 ++++-- 6 files changed, 68 insertions(+), 79 deletions(-) diff --git a/mktxp/collector/eoip_collector.py b/mktxp/collector/eoip_collector.py index 4bbfa74d..c29e55d4 100644 --- a/mktxp/collector/eoip_collector.py +++ b/mktxp/collector/eoip_collector.py @@ -17,40 +17,33 @@ class EOIPCollector(BaseCollector): - ''' EoIP Metrics collector - ''' + """ EoIP Metrics collector + """ @staticmethod def collect(router_entry): if not router_entry.config_entry.eoip: return default_labels = ['name', 'local_address', 'remote_address', 'tunnel_id'] - translation_table = { - 'running': lambda value: '1' if value == 'true' else '0', - 'disabled': lambda value: '1' if value == 'true' else '0' - } monitor_records = InterfaceMetricsDataSource.metric_records( router_entry, kind='eoip', additional_proplist=['actual-mtu', 'l2mtu', 'local-address', 'mtu', 'remote-address', 'tunnel-id'], - translation_table=translation_table ) if monitor_records: - yield BaseCollector.gauge_collector('interface_status', - 'Current status of the interface', - monitor_records, - metric_key='running', - metric_labels=default_labels + ['disabled']) + yield BaseCollector.gauge_collector( + 'interface_l2mtu', + 'Current used layer 2 mtu for this interface', + monitor_records, + metric_key='actual_mtu', + metric_labels=default_labels + ) - yield BaseCollector.gauge_collector('interface_l2mtu', - 'Current used layer 2 mtu for this interface', - monitor_records, - metric_key='actual_mtu', - metric_labels=default_labels) - - yield BaseCollector.gauge_collector('interface_mtu', - 'Current used mut for this interface', - monitor_records, - metric_key='actual_mtu', - metric_labels=default_labels + ['mtu']) + yield BaseCollector.gauge_collector( + 'interface_mtu', + 'Current used mut for this interface', + monitor_records, + metric_key='actual_mtu', + metric_labels=default_labels + ['mtu'] + ) diff --git a/mktxp/collector/gre_collector.py b/mktxp/collector/gre_collector.py index 22bc00b8..13cc8b50 100644 --- a/mktxp/collector/gre_collector.py +++ b/mktxp/collector/gre_collector.py @@ -17,34 +17,25 @@ class GRECollector(BaseCollector): - ''' GRE Metrics collector - ''' + """ GRE Metrics collector + """ @staticmethod def collect(router_entry): if not router_entry.config_entry.gre: return default_labels = ['name', 'local_address', 'remote_address'] - translation_table = { - 'running': lambda value: '1' if value == 'true' else '0', - 'disabled': lambda value: '1' if value == 'true' else '0' - } monitor_records = InterfaceMetricsDataSource.metric_records( router_entry, kind='gre', additional_proplist=['mtu', 'actual-mtu', 'local-address', 'remote-address'], - translation_table=translation_table ) if monitor_records: - yield BaseCollector.gauge_collector('interface_status', - 'Current status of the interface', - monitor_records, - metric_key='running', - metric_labels=default_labels + ['disabled']) - - yield BaseCollector.gauge_collector('interface_mtu', - 'Current used MTU for this interface', - monitor_records, - metric_key='actual_mtu', - metric_labels=default_labels + ['mtu']) + yield BaseCollector.gauge_collector( + 'interface_mtu', + 'Current used MTU for this interface', + monitor_records, + metric_key='actual_mtu', + metric_labels=default_labels + ['mtu'] + ) diff --git a/mktxp/collector/interface_collector.py b/mktxp/collector/interface_collector.py index a8a35100..c7265868 100644 --- a/mktxp/collector/interface_collector.py +++ b/mktxp/collector/interface_collector.py @@ -24,8 +24,17 @@ def collect(router_entry): if not router_entry.config_entry.interface: return - interface_traffic_labels = ['name', 'comment', 'rx_byte', 'tx_byte', 'rx_packet', 'tx_packet', 'rx_error', 'tx_error', 'rx_drop', 'tx_drop', 'link_downs'] - interface_traffic_records = InterfaceTrafficMetricsDataSource.metric_stats_records(router_entry, metric_labels = interface_traffic_labels) + interface_traffic_labels = ['disabled', 'name', 'comment', 'rx_byte', 'tx_byte', 'rx_packet', 'tx_packet', 'rx_error', 'tx_error', 'rx_drop', 'tx_drop', 'link_downs', 'running'] + interface_traffic_translation_table = { + 'running': lambda value: '1' if value == 'true' else '0', + 'disabled': lambda value: '1' if value == 'true' else '0' + } + + interface_traffic_records = InterfaceTrafficMetricsDataSource.metric_stats_records( + router_entry, + metric_labels=interface_traffic_labels, + translation_table=interface_traffic_translation_table, + ) if interface_traffic_records: for interface_traffic_record in interface_traffic_records: @@ -33,6 +42,14 @@ def collect(router_entry): interface_traffic_record['name'] = interface_traffic_record['comment'] if router_entry.config_entry.use_comments_over_names \ else f"{interface_traffic_record['name']} ({interface_traffic_record['comment']})" + yield BaseCollector.gauge_collector( + 'interface_status', + 'Current running status of the interface', + interface_traffic_records, + metric_key='running', + metric_labels=['name', 'disabled'] + ) + rx_byte_metric = BaseCollector.counter_collector('interface_rx_byte', 'Number of received bytes', interface_traffic_records, 'rx_byte', ['name']) yield rx_byte_metric diff --git a/mktxp/collector/ipip_collector.py b/mktxp/collector/ipip_collector.py index dcbabfb5..da7d61b2 100644 --- a/mktxp/collector/ipip_collector.py +++ b/mktxp/collector/ipip_collector.py @@ -17,34 +17,25 @@ class IPIPCollector(BaseCollector): - ''' IPIP Metrics collector - ''' + """ IPIP Metrics collector + """ @staticmethod def collect(router_entry): if not router_entry.config_entry.ipip: return default_labels = ['name', 'local_address', 'remote_address'] - translation_table = { - 'running': lambda value: '1' if value == 'true' else '0', - 'disabled': lambda value: '1' if value == 'true' else '0' - } interface_records = InterfaceMetricsDataSource.metric_records( router_entry, kind='ipip', additional_proplist=['mtu', 'actual-mtu', 'local-address', 'remote-address'], - translation_table=translation_table ) if interface_records: - yield BaseCollector.gauge_collector('interface_status', - 'Current status of the interface', - interface_records, - metric_key='running', - metric_labels=default_labels + ['disabled']) - - yield BaseCollector.gauge_collector('interface_mtu', - 'Current used MTU for this interface', - interface_records, - metric_key='actual_mtu', - metric_labels=default_labels + ['mtu']) + yield BaseCollector.gauge_collector( + 'interface_mtu', + 'Current used MTU for this interface', + interface_records, + metric_key='actual_mtu', + metric_labels=default_labels + ['mtu'] + ) diff --git a/mktxp/collector/monitor_collector.py b/mktxp/collector/monitor_collector.py index c0138746..aa67a6dd 100644 --- a/mktxp/collector/monitor_collector.py +++ b/mktxp/collector/monitor_collector.py @@ -25,21 +25,17 @@ def collect(router_entry): if not router_entry.config_entry.monitor: return - monitor_labels = ['status', 'rate', 'full_duplex', 'name', 'sfp_temperature', 'sfp_module_present', 'sfp_wavelength', 'sfp_tx_power', 'sfp_rx_power'] + monitor_labels = ['rate', 'full_duplex', 'name', 'sfp_temperature', 'sfp_module_present', 'sfp_wavelength', 'sfp_tx_power', 'sfp_rx_power'] translation_table = { - 'status': lambda value: '1' if value=='link-ok' else '0', - 'rate': lambda value: MonitorCollector._rates(value) if value else '0', - 'full_duplex': lambda value: '1' if value=='true' else '0', - 'name': lambda value: value if value else '', - 'sfp_module_present': lambda value: '1' if value=='true' else '0', - 'sfp_temperature': lambda value: value if value else '0' - } + 'rate': lambda value: MonitorCollector._rates(value) if value else '0', + 'full_duplex': lambda value: '1' if value == 'true' else '0', + 'name': lambda value: value if value else '', + 'sfp_module_present': lambda value: '1' if value == 'true' else '0', + 'sfp_temperature': lambda value: value if value else '0' + } monitor_records = InterfaceMonitorMetricsDataSource.metric_records(router_entry, metric_labels = monitor_labels, translation_table=translation_table, include_comments = True) if monitor_records: - monitor_status_metrics = BaseCollector.gauge_collector('interface_status', 'Current interface link status', monitor_records, 'status', ['name']) - yield monitor_status_metrics - # limit records according to the relevant metrics rate_records = [monitor_record for monitor_record in monitor_records if monitor_record.get('rate', None)] monitor_rates_metrics = BaseCollector.gauge_collector('interface_rate', 'Actual interface connection data rate', rate_records, 'rate', ['name']) @@ -60,7 +56,7 @@ def collect(router_entry): @staticmethod def _rates(rate_option): # according mikrotik docs, an interface rate should be one of these - rate_value = { + rate_value = { '10Mbps': '10', '100Mbps': '100', '1Gbps': '1000', @@ -74,7 +70,3 @@ def _rates(rate_option): # ...or just calculate in case it's not return BaseOutputProcessor.parse_interface_rate(rate_option) - - - - diff --git a/mktxp/datasource/interface_ds.py b/mktxp/datasource/interface_ds.py index ad8fcc3d..f5a3a376 100644 --- a/mktxp/datasource/interface_ds.py +++ b/mktxp/datasource/interface_ds.py @@ -66,12 +66,17 @@ def metric_records(router_entry, *, metric_labels = None): ''' Interface Traffic Stats data provider ''' @staticmethod - def metric_stats_records(router_entry, *, metric_labels): + def metric_stats_records(router_entry, *, metric_labels, translation_table=None): metric_labels = metric_labels or [] try: # get stats for all existing interfaces metric_stats_records = router_entry.api_connection.router_api().get_resource('/interface').call('print', {'stats': 'detail'}) - return BaseDSProcessor.trimmed_records(router_entry, router_records = metric_stats_records, metric_labels = metric_labels) + return BaseDSProcessor.trimmed_records( + router_entry, + router_records=metric_stats_records, + metric_labels=metric_labels, + translation_table=translation_table, + ) except Exception as exc: print(f'Error getting interface traffic stats info from router {router_entry.router_name}@{router_entry.config_entry.hostname}: {exc}') return None