DS refactor, fixes/optimizations

This commit is contained in:
Arseniy Kuznetsov
2021-02-07 15:40:49 +01:00
parent 88984a74b3
commit cb2ff3c1a5
20 changed files with 87 additions and 54 deletions

0
mktxp/flow/__init__.py Normal file
View File

View File

@@ -0,0 +1,104 @@
# 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 timeit import default_timer
from mktxp.collector.dhcp_collector import DHCPCollector
from mktxp.collector.interface_collector import InterfaceCollector
from mktxp.collector.health_collector import HealthCollector
from mktxp.collector.identity_collector import IdentityCollector
from mktxp.collector.monitor_collector import MonitorCollector
from mktxp.collector.pool_collector import PoolCollector
from mktxp.collector.resource_collector import SystemResourceCollector
from mktxp.collector.route_collector import RouteCollector
from mktxp.collector.wlan_collector import WLANCollector
from mktxp.collector.capsman_collector import CapsmanCollector
from mktxp.collector.bandwidth_collector import BandwidthCollector
from mktxp.collector.firewall_collector import FirewallCollector
from mktxp.collector.mktxp_collector import MKTXPCollector
class CollectorsHandler:
''' MKTXP Collectors Handler
'''
def __init__(self, entries_handler):
self.entries_handler = entries_handler
self.bandwidthCollector = BandwidthCollector()
def collect(self):
# process mktxp internal metrics
yield from self.bandwidthCollector.collect()
for router_entry in self.entries_handler.router_entries:
if not router_entry.api_connection.is_connected():
# let's pick up on things in the next run
router_entry.api_connection.connect()
continue
start = default_timer()
yield from IdentityCollector.collect(router_entry)
router_entry.time_spent['IdentityCollector'] += default_timer() - start
start = default_timer()
yield from SystemResourceCollector.collect(router_entry)
router_entry.time_spent['SystemResourceCollector'] += default_timer() - start
start = default_timer()
yield from HealthCollector.collect(router_entry)
router_entry.time_spent['HealthCollector'] += default_timer() - start
if router_entry.config_entry.dhcp:
start = default_timer()
yield from DHCPCollector.collect(router_entry)
router_entry.time_spent['DHCPCollector'] += default_timer() - start
if router_entry.config_entry.pool:
start = default_timer()
yield from PoolCollector.collect(router_entry)
router_entry.time_spent['PoolCollector'] += default_timer() - start
if router_entry.config_entry.interface:
start = default_timer()
yield from InterfaceCollector.collect(router_entry)
router_entry.time_spent['InterfaceCollector'] += default_timer() - start
if router_entry.config_entry.firewall:
start = default_timer()
yield from FirewallCollector.collect(router_entry)
router_entry.time_spent['FirewallCollector'] += default_timer() - start
if router_entry.config_entry.monitor:
start = default_timer()
yield from MonitorCollector.collect(router_entry)
router_entry.time_spent['MonitorCollector'] += default_timer() - start
if router_entry.config_entry.route:
start = default_timer()
yield from RouteCollector.collect(router_entry)
router_entry.time_spent['RouteCollector'] += default_timer() - start
if router_entry.config_entry.wireless:
start = default_timer()
yield from WLANCollector.collect(router_entry)
router_entry.time_spent['WLANCollector'] += default_timer() - start
if router_entry.config_entry.capsman:
start = default_timer()
yield from CapsmanCollector.collect(router_entry)
router_entry.time_spent['CapsmanCollector'] += default_timer() - start
yield from MKTXPCollector.collect(router_entry)

View File

@@ -0,0 +1,117 @@
# 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 ssl
import socket
from datetime import datetime
from routeros_api import RouterOsApiPool
from mktxp.cli.config.config import config_handler
class RouterAPIConnectionError(Exception):
pass
class RouterAPIConnection:
''' Base wrapper interface for the routeros_api library
'''
def __init__(self, router_name, config_entry):
self.router_name = router_name
self.config_entry = config_entry
self.last_failure_timestamp = self.successive_failure_count = 0
ctx = None
if self.config_entry.use_ssl and self.config_entry.no_ssl_certificate:
ctx = ssl.create_default_context()
ctx.set_ciphers('ADH:@SECLEVEL=0')
self.connection = RouterOsApiPool(
host = self.config_entry.hostname,
username = self.config_entry.username,
password = self.config_entry.password,
port = self.config_entry.port,
plaintext_login = True,
use_ssl = self.config_entry.use_ssl,
ssl_verify = self.config_entry.ssl_certificate_verify,
ssl_context = ctx)
self.connection.socket_timeout = config_handler._entry().socket_timeout
self.api = None
def is_connected(self):
if not (self.connection and self.connection.connected and self.api):
return False
try:
self.api.get_resource('/system/identity').get()
return True
except (socket.error, socket.timeout, Exception) as exc:
self._set_connect_state(success = False, exc = exc)
return False
def connect(self):
connect_time = datetime.now()
if self.is_connected() or self._in_connect_timeout(connect_time.timestamp()):
return
try:
print(f'Connecting to router {self.router_name}@{self.config_entry.hostname}')
self.api = self.connection.get_api()
self._set_connect_state(success = True, connect_time = connect_time)
except (socket.error, socket.timeout, Exception) as exc:
self._set_connect_state(success = False, connect_time = connect_time, exc = exc)
#raise RouterAPIConnectionError
def router_api(self):
if not self.is_connected():
self.connect()
return self.api
def _in_connect_timeout(self, connect_timestamp, quiet = True):
connect_delay = self._connect_delay()
if (connect_timestamp - self.last_failure_timestamp) < connect_delay:
if not quiet:
print(f'{self.router_name}@{self.config_entry.hostname}: in connect timeout, {int(connect_delay - (connect_timestamp - self.last_failure_timestamp))}secs remaining')
print(f'Successive failure count: {self.successive_failure_count}')
return True
if not quiet:
print(f'{self.router_name}@{self.config_entry.hostname}: OK to connect')
if self.last_failure_timestamp > 0:
print(f'Seconds since last failure: {connect_timestamp - self.last_failure_timestamp}')
print(f'Prior successive failure count: {self.successive_failure_count}')
return False
def _connect_delay(self):
mktxp_entry = config_handler._entry()
connect_delay = (1 + self.successive_failure_count / mktxp_entry.delay_inc_div) * mktxp_entry.initial_delay_on_failure
return connect_delay if connect_delay < mktxp_entry.max_delay_on_failure else mktxp_entry.max_delay_on_failure
def _set_connect_state(self, success = False, connect_time = datetime.now(), exc = None):
if success:
self.last_failure_timestamp = 0
self.successive_failure_count = 0
print(f'{connect_time.strftime("%Y-%m-%d %H:%M:%S")} Connection to router {self.router_name}@{self.config_entry.hostname} has been established')
else:
self.api = None
self.successive_failure_count += 1
self.last_failure_timestamp = connect_time.timestamp()
print(f'{connect_time.strftime("%Y-%m-%d %H:%M:%S")} Connection to router {self.router_name}@{self.config_entry.hostname} has failed: {exc}')

View File

@@ -0,0 +1,40 @@
# 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 config_handler
from mktxp.flow.router_entry import RouterEntry
class RouterEntriesHandler:
''' Handles RouterOS entries defined in MKTXP config
'''
def __init__(self):
self.router_entries = []
for router_name in config_handler.registered_entries():
entry = config_handler.entry(router_name)
if entry.enabled:
self.router_entries.append(RouterEntry(router_name))
@staticmethod
def router_entry(entry_name, enabled_only = False):
router_entry = None
for router_name in config_handler.registered_entries():
if router_name == entry_name:
if enabled_only:
entry = config_handler.entry(router_name)
if not entry.enabled:
break
router_entry = RouterEntry(router_name)
break
return router_entry

View File

@@ -0,0 +1,41 @@
# 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 config_handler, MKTXPConfigKeys
from mktxp.flow.router_connection import RouterAPIConnection
class RouterEntry:
''' RouterOS Entry
'''
def __init__(self, router_name):
self.router_name = router_name
self.config_entry = config_handler.entry(router_name)
self.api_connection = RouterAPIConnection(router_name, self.config_entry)
self.router_id = {
MKTXPConfigKeys.ROUTERBOARD_NAME: self.router_name,
MKTXPConfigKeys.ROUTERBOARD_ADDRESS: self.config_entry.hostname
}
self.time_spent = { 'IdentityCollector': 0,
'SystemResourceCollector': 0,
'HealthCollector': 0,
'DHCPCollector': 0,
'PoolCollector': 0,
'InterfaceCollector': 0,
'FirewallCollector': 0,
'MonitorCollector': 0,
'RouteCollector': 0,
'WLANCollector': 0,
'CapsmanCollector': 0
}