Skip to content

Commit

Permalink
Move interface status to interface collector
Browse files Browse the repository at this point in the history
  • Loading branch information
phibos committed Dec 23, 2024
1 parent c2e1092 commit b373038
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 79 deletions.
39 changes: 16 additions & 23 deletions mktxp/collector/eoip_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
)
27 changes: 9 additions & 18 deletions mktxp/collector/gre_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
)
21 changes: 19 additions & 2 deletions mktxp/collector/interface_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,32 @@ 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:
if interface_traffic_record.get('comment'):
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

Expand Down
27 changes: 9 additions & 18 deletions mktxp/collector/ipip_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']
)
24 changes: 8 additions & 16 deletions mktxp/collector/monitor_collector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand All @@ -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',
Expand All @@ -74,7 +70,3 @@ def _rates(rate_option):

# ...or just calculate in case it's not
return BaseOutputProcessor.parse_interface_rate(rate_option)




9 changes: 7 additions & 2 deletions mktxp/datasource/interface_ds.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b373038

Please sign in to comment.