Initial commit

This commit is contained in:
Arseniy Kuznetsov
2021-01-01 18:35:27 +01:00
parent 31d8a48849
commit 329090b8a4
31 changed files with 1946 additions and 674 deletions

View File

View File

@@ -0,0 +1,58 @@
# coding=utf8
## Copyright (c) 2020 Arseniy Kuznetsov
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
from prometheus_client.core import GaugeMetricFamily, CounterMetricFamily, InfoMetricFamily
from mktxp.cli.config.config import MKTXPConfigKeys
class BaseCollector:
''' Base Collector methods
For use by custom collectors
'''
@staticmethod
def info_collector(name, decription, router_records, metric_labels=[]):
BaseCollector._add_id_labels(metric_labels)
collector = InfoMetricFamily(f'mktxp_{name}', decription)
for router_record in router_records:
label_values = {label: router_record.get(label) if router_record.get(label) else '' for label in metric_labels}
collector.add_metric(metric_labels, label_values)
return collector
@staticmethod
def counter_collector(name, decription, router_records, metric_key, metric_labels=[]):
BaseCollector._add_id_labels(metric_labels)
collector = CounterMetricFamily(f'mktxp_{name}', decription, labels=metric_labels)
for router_record in router_records:
label_values = [router_record.get(label) for label in metric_labels]
collector.add_metric(label_values, router_record.get(metric_key, 0))
return collector
@staticmethod
def gauge_collector(name, decription, router_records, metric_key, metric_labels=[]):
BaseCollector._add_id_labels(metric_labels)
collector = GaugeMetricFamily(f'mktxp_{name}', decription, labels=metric_labels)
for router_record in router_records:
label_values = [router_record.get(label) for label in metric_labels]
collector.add_metric(label_values, router_record.get(metric_key, 0))
return collector
# Helpers
@staticmethod
def _add_id_labels(metric_labels):
metric_labels.append(MKTXPConfigKeys.ROUTERBOARD_NAME)
metric_labels.append(MKTXPConfigKeys.ROUTERBOARD_ADDRESS)

View File

@@ -0,0 +1,33 @@
# coding=utf8
## Copyright (c) 2020 Arseniy Kuznetsov
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
from mktxp.collectors.base_collector import BaseCollector
from mktxp.router_metric import RouterMetric
class CapsmanCollector(BaseCollector):
''' CAPsMAN Metrics collector
'''
@staticmethod
def collect(router_metric):
resource_labels = ['uptime', 'version', 'free-memory', 'total-memory',
'cpu', 'cpu-count', 'cpu-frequency', 'cpu-load',
'free-hdd-space', 'total-hdd-space',
'architecture-name', 'board-name']
resource_records = router_metric.resource_records(resource_labels)
if not resource_records:
return
resource_metrics = BaseCollector.info_collector('resource', 'resource', resource_records, resource_labels)
yield resource_metrics

View File

@@ -0,0 +1,46 @@
# coding=utf8
## Copyright (c) 2020 Arseniy Kuznetsov
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
from mktxp.cli.config.config import MKTXPConfigKeys
from mktxp.collectors.base_collector import BaseCollector
from mktxp.router_metric import RouterMetric
class DHCPCollector(BaseCollector):
''' DHCP Metrics collector
'''
@staticmethod
def collect(router_metric):
dhcp_lease_labels = ['active_address', 'mac_address', 'host_name', 'comment', 'server', 'expires_after']
dhcp_lease_records = router_metric.dhcp_lease_records(dhcp_lease_labels)
if not dhcp_lease_records:
return
# calculate number of leases per DHCP server
dhcp_lease_servers = {}
for dhcp_lease_record in dhcp_lease_records:
dhcp_lease_servers[dhcp_lease_record['server']] = dhcp_lease_servers.get(dhcp_lease_record['server'], 0) + 1
# compile leases-per-server records
dhcp_lease_servers_records = [{ MKTXPConfigKeys.ROUTERBOARD_NAME: router_metric.router_id[MKTXPConfigKeys.ROUTERBOARD_NAME],
MKTXPConfigKeys.ROUTERBOARD_ADDRESS: router_metric.router_id[MKTXPConfigKeys.ROUTERBOARD_ADDRESS],
'server': key, 'count': value} for key, value in dhcp_lease_servers.items()]
# yield lease-per-server metrics
dhcp_lease_server_metrics = BaseCollector.gauge_collector('dhcp_lease_active_count', 'Number of active leases per DHCP server', dhcp_lease_servers_records, 'count', ['server'])
yield dhcp_lease_server_metrics
# active lease metrics
if router_metric.router_entry.dhcp_lease:
dhcp_lease_metrics = BaseCollector.info_collector('dhcp_lease', 'DHCP Active Leases', dhcp_lease_records, dhcp_lease_labels)
yield dhcp_lease_metrics

View File

@@ -0,0 +1,33 @@
# coding=utf8
## Copyright (c) 2020 Arseniy Kuznetsov
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
from mktxp.collectors.base_collector import BaseCollector
from mktxp.router_metric import RouterMetric
class HealthCollector(BaseCollector):
''' System Health Metrics collector
'''
@staticmethod
def collect(router_metric):
health_labels = ['voltage', 'temperature']
health_records = router_metric.health_records(health_labels)
if not health_records:
return
voltage_metrics = BaseCollector.gauge_collector('routerboard_voltage', 'Supplied routerboard voltage', health_records, 'voltage')
yield voltage_metrics
temperature_metrics = BaseCollector.gauge_collector('routerboard_temperature', ' Routerboard current temperature', health_records, 'temperature')
yield temperature_metrics

View File

@@ -0,0 +1,30 @@
# coding=utf8
## Copyright (c) 2020 Arseniy Kuznetsov
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
from mktxp.collectors.base_collector import BaseCollector
from mktxp.router_metric import RouterMetric
class IdentityCollector(BaseCollector):
''' System Identity Metrics collector
'''
@staticmethod
def collect(router_metric):
identity_labels = ['name']
identity_records = router_metric.identity_records(identity_labels)
if not identity_records:
return
identity_metrics = BaseCollector.info_collector('identity', 'System identity', identity_records, identity_labels)
yield identity_metrics

View File

@@ -0,0 +1,52 @@
# coding=utf8
## Copyright (c) 2020 Arseniy Kuznetsov
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
from mktxp.collectors.base_collector import BaseCollector
from mktxp.router_metric import RouterMetric
class InterfaceCollector(BaseCollector):
''' Router Interface Metrics collector
'''
@staticmethod
def collect(router_metric):
interface_traffic_labels = ['name', 'comment', 'rx_byte', 'tx_byte', 'rx_packet', 'tx_packet', 'rx_error', 'tx_error', 'rx_drop', 'tx_drop']
interface_traffic_records = router_metric.interface_traffic_records(interface_traffic_labels)
if not interface_traffic_records:
return
rx_byte_metric = BaseCollector.counter_collector('rx_byte', 'Number of received bytes', interface_traffic_records, 'rx_byte', ['name'])
yield rx_byte_metric
tx_byte_metric = BaseCollector.counter_collector('tx_byte', 'Number of transmitted bytes', interface_traffic_records, 'tx_byte', ['name'])
yield tx_byte_metric
rx_packet_metric = BaseCollector.counter_collector('rx_packet', 'Number of packets received', interface_traffic_records, 'rx_packet', ['name'])
yield rx_packet_metric
tx_packet_metric = BaseCollector.counter_collector('tx_packet', 'Number of transmitted packets', interface_traffic_records, 'tx_packet', ['name'])
yield tx_packet_metric
rx_error_metric = BaseCollector.counter_collector('rx_error', 'Number of packets received with an error', interface_traffic_records, 'rx_error', ['name'])
yield rx_error_metric
tx_error_metric = BaseCollector.counter_collector('tx_error', 'Number of packets transmitted with an error', interface_traffic_records, 'tx_error', ['name'])
yield tx_error_metric
rx_drop_metric = BaseCollector.counter_collector('rx_drop', 'Number of received packets being dropped', interface_traffic_records, 'rx_drop', ['name'])
yield rx_drop_metric
tx_drop_metric = BaseCollector.counter_collector('tx_drop', 'Number of transmitted packets being dropped', interface_traffic_records, 'tx_drop', ['name'])
yield tx_drop_metric

View File

@@ -0,0 +1,77 @@
# coding=utf8
## Copyright (c) 2020 Arseniy Kuznetsov
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
import re
from mktxp.collectors.base_collector import BaseCollector
from mktxp.router_metric import RouterMetric
class MonitorCollector(BaseCollector):
''' Ethernet Interface Monitor Metrics collector
'''
@staticmethod
def collect(router_metric):
monitor_labels = ['status', 'rate', 'full_duplex', 'name']
monitor_records = router_metric.interface_monitor_records(monitor_labels)
if not monitor_records:
return
# translate records to appropriate values
for monitor_record in monitor_records:
for monitor_label in monitor_labels:
value = monitor_record.get(monitor_label, None)
if value:
monitor_record[monitor_label] = MonitorCollector._translated_values(monitor_label, value)
monitor_status_metrics = BaseCollector.gauge_collector('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('rate', 'Actual interface connection data rate', rate_records, 'rate', ['name'])
yield monitor_rates_metrics
full_duplex_records = [monitor_record for monitor_record in monitor_records if monitor_record.get('full_duplex', None)]
monitor_rates_metrics = BaseCollector.gauge_collector('full_duplex', 'Full duplex data transmission', full_duplex_records, 'full_duplex', ['name'])
yield monitor_rates_metrics
# Helpers
@staticmethod
def _translated_values(monitor_label, value):
return {
'status': lambda value: '1' if value=='link-ok' else '0',
'rate': lambda value: MonitorCollector._rates(value),
'full_duplex': lambda value: '1' if value=='true' else '0',
'name': lambda value: value
}[monitor_label](value)
@staticmethod
def _rates(rate_option):
# according mikrotik docs, an interface rate should be one of these
rate_value = {
'10Mbps': '10',
'100Mbps': '100',
'1Gbps': '1000',
'2.5Gbps': '2500',
'5Gbps': '5000',
'10Gbps': '10000',
'40Gbps': '40000'
}.get(rate_option, None)
if rate_value:
return rate_value
# ...or just calculate if it's not
rate = lambda rate_option: 1000 if rate_option.find('Mbps') < 0 else 1
return(int(float(re.sub('[^.\-\d]', '', rate_option)) * rate(rate_option)))

View File

@@ -0,0 +1,45 @@
# coding=utf8
## Copyright (c) 2020 Arseniy Kuznetsov
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
from mktxp.cli.config.config import MKTXPConfigKeys
from mktxp.collectors.base_collector import BaseCollector
from mktxp.router_metric import RouterMetric
class PoolCollector(BaseCollector):
''' IP Pool Metrics collector
'''
@staticmethod
def collect(router_metric):
# initialize all pool counts, including those currently not used
pool_records = router_metric.pool_records(['name'])
if not pool_records:
return
pool_used_labels = ['pool']
pool_used_counts = {pool_record['name']: 0 for pool_record in pool_records}
# for pools in usage, calculate the current numbers
pool_used_records = router_metric.pool_used_records(pool_used_labels)
for pool_used_record in pool_used_records:
pool_used_counts[pool_used_record['pool']] = pool_used_counts.get(pool_used_record['pool'], 0) + 1
# compile used-per-pool records
used_per_pool_records = [{ MKTXPConfigKeys.ROUTERBOARD_NAME: router_metric.router_id[MKTXPConfigKeys.ROUTERBOARD_NAME],
MKTXPConfigKeys.ROUTERBOARD_ADDRESS: router_metric.router_id[MKTXPConfigKeys.ROUTERBOARD_ADDRESS],
'pool': key, 'count': value} for key, value in pool_used_counts.items()]
# yield used-per-pool metrics
used_per_pool_metrics = BaseCollector.gauge_collector('ip_pool_used', 'Number of used addresses per IP pool', used_per_pool_records, 'count', ['pool'])
yield used_per_pool_metrics

View File

@@ -0,0 +1,77 @@
# coding=utf8
## Copyright (c) 2020 Arseniy Kuznetsov
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
import re
from datetime import timedelta
from mktxp.collectors.base_collector import BaseCollector
from mktxp.router_metric import RouterMetric
class SystemResourceCollector(BaseCollector):
''' System Resource Metrics collector
'''
@staticmethod
def collect(router_metric):
resource_labels = ['uptime', 'version', 'free_memory', 'total_memory',
'cpu', 'cpu_count', 'cpu_frequency', 'cpu_load',
'free_hdd_space', 'total_hdd_space',
'architecture_name', 'board_name']
resource_records = router_metric.system_resource_records(resource_labels)
if not resource_records:
return
# translate records to appropriate values
translated_fields = ['uptime']
for resource_record in resource_records:
for translated_field in translated_fields:
value = resource_record.get(translated_field, None)
if value:
resource_record[translated_field] = SystemResourceCollector._translated_values(translated_field, value)
uptime_metrics = BaseCollector.gauge_collector('uptime', 'Time interval since boot-up', resource_records, 'uptime', ['version', 'board_name', 'cpu', 'architecture_name'])
yield uptime_metrics
free_memory_metrics = BaseCollector.gauge_collector('free_memory', 'Unused amount of RAM', resource_records, 'free_memory', ['version', 'board_name', 'cpu', 'architecture_name'])
yield free_memory_metrics
total_memory_metrics = BaseCollector.gauge_collector('total_memory', 'Amount of installed RAM', resource_records, 'total_memory', ['version', 'board_name', 'cpu', 'architecture_name'])
yield total_memory_metrics
free_hdd_metrics = BaseCollector.gauge_collector('free_hdd_space', 'Free space on hard drive or NAND', resource_records, 'free_hdd_space', ['version', 'board_name', 'cpu', 'architecture_name'])
yield free_hdd_metrics
total_hdd_metrics = BaseCollector.gauge_collector('total_hdd_space', 'Size of the hard drive or NAND', resource_records, 'total_hdd_space', ['version', 'board_name', 'cpu', 'architecture_name'])
yield total_hdd_metrics
cpu_load_metrics = BaseCollector.gauge_collector('cpu_load', 'Percentage of used CPU resources', resource_records, 'cpu_load', ['version', 'board_name', 'cpu', 'architecture_name'])
yield cpu_load_metrics
cpu_count_metrics = BaseCollector.gauge_collector('cpu_count', 'Number of CPUs present on the system', resource_records, 'cpu_count', ['version', 'board_name', 'cpu', 'architecture_name'])
yield cpu_count_metrics
cpu_frequency_metrics = BaseCollector.gauge_collector('cpu_frequency', 'Current CPU frequency', resource_records, 'cpu_frequency', ['version', 'board_name', 'cpu', 'architecture_name'])
yield cpu_frequency_metrics
# Helpers
@staticmethod
def _translated_values(translated_field, value):
return {
'uptime': lambda value: SystemResourceCollector._parse_uptime(value)
}[translated_field](value)
@staticmethod
def _parse_uptime(time):
time_dict = re.match(r'((?P<weeks>\d+)w)?((?P<days>\d+)d)?((?P<hours>\d+)h)?((?P<minutes>\d+)m)?((?P<seconds>\d+)s)?', time).groupdict()
return timedelta(**{key: int(value) for key, value in time_dict.items() if value}).total_seconds()

View File

@@ -0,0 +1,54 @@
# coding=utf8
## Copyright (c) 2020 Arseniy Kuznetsov
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
from mktxp.cli.config.config import MKTXPConfigKeys
from mktxp.collectors.base_collector import BaseCollector
from mktxp.router_metric import RouterMetric
class RouteCollector(BaseCollector):
''' IP Route Metrics collector
'''
@staticmethod
def collect(router_metric):
route_labels = ['connect', 'dynamic', 'static', 'bgp', 'ospf']
route_records = router_metric.route_records(route_labels)
if not route_records:
return
# compile total routes records
total_routes = len(route_records)
total_routes_records = [{ MKTXPConfigKeys.ROUTERBOARD_NAME: router_metric.router_id[MKTXPConfigKeys.ROUTERBOARD_NAME],
MKTXPConfigKeys.ROUTERBOARD_ADDRESS: router_metric.router_id[MKTXPConfigKeys.ROUTERBOARD_ADDRESS],
'count': total_routes
}]
total_routes_metrics = BaseCollector.gauge_collector('total_routes', 'Overall number of routes in RIB', total_routes_records, 'count')
yield total_routes_metrics
# init routes per protocol (with 0)
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):
routes_per_protocol[route_label] += 1
# compile route-per-protocol records
route_per_protocol_records = [{ MKTXPConfigKeys.ROUTERBOARD_NAME: router_metric.router_id[MKTXPConfigKeys.ROUTERBOARD_NAME],
MKTXPConfigKeys.ROUTERBOARD_ADDRESS: router_metric.router_id[MKTXPConfigKeys.ROUTERBOARD_ADDRESS],
'protocol': key, 'count': value} for key, value in routes_per_protocol.items()]
# yield route-per-protocol metrics
route_per_protocol_metrics = BaseCollector.gauge_collector('routes_protocol_count', 'Number of routes per protocol in RIB', route_per_protocol_records, 'count', ['protocol'])
yield route_per_protocol_metrics

View File

@@ -0,0 +1,38 @@
# coding=utf8
## Copyright (c) 2020 Arseniy Kuznetsov
##
## This program is free software; you can redistribute it and/or
## modify it under the terms of the GNU General Public License
## as published by the Free Software Foundation; either version 2
## of the License, or (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
from mktxp.collectors.base_collector import BaseCollector
from mktxp.router_metric import RouterMetric
class WLANCollector(BaseCollector):
''' Wireless Metrics collector
'''
@staticmethod
def collect(router_metric):
monitor_labels = ['channel', 'noise_floor', 'overall_tx_ccq']
monitor_records = router_metric.interface_monitor_records(monitor_labels, 'wireless')
if not monitor_records:
return
# sanitize records for relevant labels
noise_floor_records = [monitor_record for monitor_record in monitor_records if monitor_record.get('noise_floor')]
tx_ccq_records = [monitor_record for monitor_record in monitor_records if monitor_record.get('overall_tx_ccq')]
if noise_floor_records:
noise_floor_metrics = BaseCollector.gauge_collector('noise_floor', 'Noise floor threshold', noise_floor_records, 'noise_floor', ['channel'])
yield noise_floor_metrics
if tx_ccq_records:
overall_tx_ccq_metrics = BaseCollector.gauge_collector('overall_tx_ccq', ' Client Connection Quality for transmitting', tx_ccq_records, 'overall_tx_ccq', ['channel'])
yield overall_tx_ccq_metrics