From a173229fe83e0722768dcbc9fa4d61bea55953b9 Mon Sep 17 00:00:00 2001 From: Arseniy Kuznetsov Date: Mon, 4 Jan 2021 19:58:38 +0100 Subject: [PATCH] cli options --- README.md | 52 +++++++++++++++++++++-- mktxp/basep.py | 4 +- mktxp/cli/config/config.py | 20 +++------ mktxp/cli/config/mktxp.conf | 7 ++-- mktxp/cli/options.py | 4 +- mktxp/collectors/capsman_collector.py | 59 +++++++++++++++++---------- mktxp/collectors/wlan_collector.py | 6 +++ 7 files changed, 107 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index b6bda8a..dc6226c 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,5 @@ + ![License](https://img.shields.io/badge/License-GNU%20GPL-blue.svg) ![Language](https://img.shields.io/badge/python-v3.6-blue) ![License](https://img.shields.io/badge/mikrotik-routeros-orange) @@ -7,8 +8,53 @@ ![capsman](http://www.akpdev.com/images/capsman.png) - -# mktxp -Prometheus Exporter for Mikrotik RouterOS devices +#### Requirements: +- [Python 3.6.x](https://www.python.org/downloads/release/python-360/) or later + + +- OSs: + * Linux + * Mac OSX + * Windows: TBD / maybe + +#### Install: +- from [PyPI](https://pypi.python.org/pypi/batchmp): `$ pip install mktxp` +- latest from source repository: `$ pip install git+https://github.com/akpw/mktxp` + + +## Description +Prometheus Exporter for Mikrotik RouterOS. +MKTXP enables gathering metrics across multiple RouterOS devices, all easily configurable via built-in CLI interface. +Comes along with a dedicated [Grafana dashboard](https://grafana.com/grafana/dashboards/13679) + + +## Getting started + Usage: $ mktxp [-h] + {info, version, show, add, edit, delete, start} +Commands: + {info, version, show, add, edit, delete, start} + + $ mktxp {command} -h #run this for detailed help on individual commands + + +## Full description of CLI Commands +### mktxp + . action commands: + .. start Starts collecting metrics for all enabled RouterOS configuration entries + .. add Adds MKTXP RouterOS configuration entry + .. show Shows MKTXP configuration entries + .. delete Deletes a MKTXP RouterOS configuration entry + .. edit Open MKTXP configuration file in your editor of choice + .. info Shows base MKTXP info + .. version Shows MKTXP version + + +## Installing Development version +- Clone the repo, then run: `$ python setup.py develop` + +**Running Tests** +- TDB +- Run via: `$ python setup.py test` + diff --git a/mktxp/basep.py b/mktxp/basep.py index 4d70517..e26fb3a 100644 --- a/mktxp/basep.py +++ b/mktxp/basep.py @@ -13,6 +13,7 @@ from http.server import HTTPServer +from datetime import datetime from prometheus_client.core import REGISTRY from prometheus_client import MetricsHandler, start_http_server from mktxp.cli.config.config import config_handler @@ -34,5 +35,6 @@ class MKTXPProcessor: def run(server_class=HTTPServer, handler_class=MetricsHandler, port = None): server_address = ('', port) httpd = server_class(server_address, handler_class) - print(f'Running HTTP collector server on port {port}') + current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S") + print(f'{current_time} Running metrics HTTP server on port {port}') httpd.serve_forever() diff --git a/mktxp/cli/config/config.py b/mktxp/cli/config/config.py index 4db1326..e42f808 100755 --- a/mktxp/cli/config/config.py +++ b/mktxp/cli/config/config.py @@ -42,7 +42,9 @@ class MKTXPConfigKeys: FE_MONITOR_KEY = 'monitor' FE_ROUTE_KEY = 'route' FE_WIRELESS_KEY = 'wireless' + FE_WIRELESS_CLIENTS_KEY = 'wireless_clients' FE_CAPSMAN_KEY = 'capsman' + FE_CAPSMAN_CLIENTS_KEY = 'capsman_clients' # UnRegistered entries placeholder NO_ENTRIES_REGISTERED = 'NoEntriesRegistered' @@ -58,7 +60,7 @@ class MKTXPConfigKeys: BOOLEAN_KEYS = [ENABLED_KEY, SSL_KEY, SSL_CERTIFICATE, FE_DHCP_KEY, FE_DHCP_LEASE_KEY, FE_DHCP_POOL_KEY, FE_INTERFACE_KEY, - FE_MONITOR_KEY, FE_ROUTE_KEY, FE_WIRELESS_KEY, FE_CAPSMAN_KEY] + FE_MONITOR_KEY, FE_ROUTE_KEY, FE_WIRELESS_KEY, FE_WIRELESS_CLIENTS_KEY, FE_CAPSMAN_KEY, FE_CAPSMAN_CLIENTS_KEY] STR_KEYS = [HOST_KEY, USER_KEY, PASSWD_KEY] # MKTXP config entry nane @@ -71,7 +73,8 @@ class ConfigEntry: MKTXPConfigKeys.SSL_KEY, MKTXPConfigKeys.SSL_CERTIFICATE, MKTXPConfigKeys.FE_DHCP_KEY, MKTXPConfigKeys.FE_DHCP_LEASE_KEY, MKTXPConfigKeys.FE_DHCP_POOL_KEY, MKTXPConfigKeys.FE_INTERFACE_KEY, - MKTXPConfigKeys.FE_MONITOR_KEY, MKTXPConfigKeys.FE_ROUTE_KEY, MKTXPConfigKeys.FE_WIRELESS_KEY, MKTXPConfigKeys.FE_CAPSMAN_KEY + MKTXPConfigKeys.FE_MONITOR_KEY, MKTXPConfigKeys.FE_ROUTE_KEY, MKTXPConfigKeys.FE_WIRELESS_KEY, MKTXPConfigKeys.FE_WIRELESS_CLIENTS_KEY, + MKTXPConfigKeys.FE_CAPSMAN_KEY, MKTXPConfigKeys.FE_CAPSMAN_CLIENTS_KEY ]) class OSConfig(metaclass = ABCMeta): @@ -221,16 +224,3 @@ class MKTXPConfigHandler: # Simplest possible Singleton impl config_handler = MKTXPConfigHandler() - - -''' if not os.path.exists(self.usr_conf_data_path): - # stage from the mktxp conf template - lookup_path = resource_filename(Requirement.parse("mktxp"), "mktxp/cli/config/mktxp.conf") - shutil.copy(lookup_path, self.usr_conf_data_path) - - if not os.path.exists(self.mktxp_conf_path): - # stage from the mktxp conf template - lookup_path = resource_filename(Requirement.parse("mktxp"), "mktxp/cli/config/.mktxp.conf") - shutil.copy(lookup_path, self.mktxp_conf_path) - -''' diff --git a/mktxp/cli/config/mktxp.conf b/mktxp/cli/config/mktxp.conf index 325f070..664934b 100644 --- a/mktxp/cli/config/mktxp.conf +++ b/mktxp/cli/config/mktxp.conf @@ -23,9 +23,6 @@ use_ssl = False ssl_certificate = False - identity = True - resource = True - health = True dhcp = True dhcp_lease = True pool = True @@ -33,4 +30,6 @@ monitor = True route = True wireless = True - capsman = True \ No newline at end of file + wireless_clients = True + capsman = True + capsman_clients = True \ No newline at end of file diff --git a/mktxp/cli/options.py b/mktxp/cli/options.py index 6383c8c..a1de267 100755 --- a/mktxp/cli/options.py +++ b/mktxp/cli/options.py @@ -45,7 +45,9 @@ class MKTXPOptionsParser: self._script_name = 'MKTXP' self._description = \ ''' - Prometheus Exporter for Mikrotik RouterOS Devices. + Prometheus Exporter for Mikrotik RouterOS. + Supports gathering metrics across multiple RouterOS devices, all easily configurable via built-in CLI interface. + Comes along with a dedicated Grafana dashboard(https://grafana.com/grafana/dashboards/13679) ''' diff --git a/mktxp/collectors/capsman_collector.py b/mktxp/collectors/capsman_collector.py index b7d0f24..bcdfab8 100644 --- a/mktxp/collectors/capsman_collector.py +++ b/mktxp/collectors/capsman_collector.py @@ -47,29 +47,46 @@ class CapsmanCollector(BaseCollector): registration_per_interface_metrics = BaseCollector.gauge_collector('capsman_registrations_count', 'Number of active registration per CAPsMAN interface', registration_per_interface_records, 'count', ['interface']) yield registration_per_interface_metrics - # translate / trim / augment registration records - dhcp_lease_labels = ['mac_address', 'host_name', 'comment'] - dhcp_lease_records = router_metric.dhcp_lease_records(dhcp_lease_labels) - for registration_record in registration_records: - dhcp_lease_record = next((dhcp_lease_record for dhcp_lease_record in dhcp_lease_records if dhcp_lease_record['mac_address']==registration_record['mac_address'])) - if dhcp_lease_record: - registration_record['name'] = dhcp_lease_record.get('comment', dhcp_lease_record.get('host_name', dhcp_lease_record.get('mac_address'))) - else: - registration_record['name'] = registration_record['mac_address'] - - # split out tx/rx bytes - registration_record['tx_bytes'] = registration_record['bytes'].split(',')[0] - registration_record['rx_bytes'] = registration_record['bytes'].split(',')[1] - del registration_record['bytes'] + # the client info metrics + if router_metric.router_entry.capsman_clients: + # translate / trim / augment registration records + dhcp_lease_labels = ['mac_address', 'host_name', 'comment'] + dhcp_lease_records = router_metric.dhcp_lease_records(dhcp_lease_labels) + for registration_record in registration_records: + dhcp_lease_record = next((dhcp_lease_record for dhcp_lease_record in dhcp_lease_records if dhcp_lease_record['mac_address']==registration_record['mac_address'])) + if dhcp_lease_record: + registration_record['name'] = dhcp_lease_record.get('comment', dhcp_lease_record.get('host_name', dhcp_lease_record.get('mac_address'))) + else: + registration_record['name'] = registration_record['mac_address'] + + # split out tx/rx bytes + registration_record['tx_bytes'] = registration_record['bytes'].split(',')[0] + registration_record['rx_bytes'] = registration_record['bytes'].split(',')[1] + del registration_record['bytes'] + + tx_byte_metrics = BaseCollector.counter_collector('capsman_traffic_tx_bytes', 'Number of sent packet bytes', registration_records, 'tx_bytes', ['name']) + yield tx_byte_metrics + + rx_byte_metrics = BaseCollector.counter_collector('capsman_traffic_rx_bytes', 'Number of received packet bytes', registration_records, 'rx_bytes', ['name']) + yield rx_byte_metrics + + signal_strength_metrics = BaseCollector.gauge_collector('capsman_registered_signal_strength', 'Registered devices signal strength', registration_records, 'rx_signal', ['name']) + yield signal_strength_metrics + + registration_metrics = BaseCollector.info_collector('capsman_registered_devices', 'Registered devices info', + registration_records, ['name', 'rx_signal', 'ssid', 'tx_rate', 'rx_rate', 'interface', 'mac_address', 'uptime']) + yield registration_metrics + + + + + + + + + - tx_byte_metrics = BaseCollector.counter_collector('capsman_traffic_tx_bytes', 'Number of sent packet bytes', registration_records, 'tx_bytes', ['name']) - yield tx_byte_metrics - rx_byte_metrics = BaseCollector.counter_collector('capsman_traffic_rx_bytes', 'Number of received packet bytes', registration_records, 'rx_bytes', ['name']) - yield rx_byte_metrics - registration_metrics = BaseCollector.info_collector('capsman_registered_devices', 'Registered devices info', - registration_records, ['name', 'rx_signal', 'ssid', 'tx_rate', 'rx_rate', 'interface', 'mac_address', 'uptime']) - yield registration_metrics diff --git a/mktxp/collectors/wlan_collector.py b/mktxp/collectors/wlan_collector.py index 244bf23..615f049 100644 --- a/mktxp/collectors/wlan_collector.py +++ b/mktxp/collectors/wlan_collector.py @@ -36,3 +36,9 @@ class WLANCollector(BaseCollector): if tx_ccq_records: overall_tx_ccq_metrics = BaseCollector.gauge_collector('wlan_overall_tx_ccq', ' Client Connection Quality for transmitting', tx_ccq_records, 'overall_tx_ccq', ['channel']) yield overall_tx_ccq_metrics + + + # the client info metrics + if router_metric.router_entry.wireless_clients: + # TBD + pass \ No newline at end of file