expires_after cardinality, bug fixes

This commit is contained in:
Arseniy Kuznetsov
2022-09-17 09:43:46 +01:00
parent 7aeb02ac71
commit d0456eab19
10 changed files with 45 additions and 25 deletions

View File

@@ -21,3 +21,5 @@
bandwidth = True # Turns metrics bandwidth metrics collection on / off bandwidth = True # Turns metrics bandwidth metrics collection on / off
bandwidth_test_interval = 420 # Interval for colllecting bandwidth metrics bandwidth_test_interval = 420 # Interval for colllecting bandwidth metrics
verbose_mode = False # Set it on for troubleshooting

View File

@@ -58,6 +58,7 @@ class MKTXPConfigKeys:
MKTXP_INC_DIV = 'delay_inc_div' MKTXP_INC_DIV = 'delay_inc_div'
MKTXP_BANDWIDTH_KEY = 'bandwidth' MKTXP_BANDWIDTH_KEY = 'bandwidth'
MKTXP_BANDWIDTH_TEST_INTERVAL = 'bandwidth_test_interval' MKTXP_BANDWIDTH_TEST_INTERVAL = 'bandwidth_test_interval'
MKTXP_VERBOSE_MODE = 'verbose_mode'
# UnRegistered entries placeholder # UnRegistered entries placeholder
NO_ENTRIES_REGISTERED = 'NoEntriesRegistered' NO_ENTRIES_REGISTERED = 'NoEntriesRegistered'
@@ -86,7 +87,8 @@ class MKTXPConfigKeys:
FE_MONITOR_KEY, FE_ROUTE_KEY, MKTXP_USE_COMMENTS_OVER_NAMES, FE_MONITOR_KEY, FE_ROUTE_KEY, MKTXP_USE_COMMENTS_OVER_NAMES,
FE_WIRELESS_KEY, FE_WIRELESS_CLIENTS_KEY, FE_CAPSMAN_KEY, FE_CAPSMAN_CLIENTS_KEY, FE_POE_KEY, FE_NETWATCH_KEY} FE_WIRELESS_KEY, FE_WIRELESS_CLIENTS_KEY, FE_CAPSMAN_KEY, FE_CAPSMAN_CLIENTS_KEY, FE_POE_KEY, FE_NETWATCH_KEY}
SYSTEM_BOOLEAN_KEYS_YES = (MKTXP_BANDWIDTH_KEY,) SYSTEM_BOOLEAN_KEYS_YES = {MKTXP_BANDWIDTH_KEY}
SYSTEM_BOOLEAN_KEYS_NO = {MKTXP_VERBOSE_MODE}
STR_KEYS = (HOST_KEY, USER_KEY, PASSWD_KEY) STR_KEYS = (HOST_KEY, USER_KEY, PASSWD_KEY)
MKTXP_INT_KEYS = (PORT_KEY, MKTXP_SOCKET_TIMEOUT, MKTXP_INITIAL_DELAY, MKTXP_MAX_DELAY, MKTXP_INC_DIV, MKTXP_BANDWIDTH_TEST_INTERVAL) MKTXP_INT_KEYS = (PORT_KEY, MKTXP_SOCKET_TIMEOUT, MKTXP_INITIAL_DELAY, MKTXP_MAX_DELAY, MKTXP_INC_DIV, MKTXP_BANDWIDTH_TEST_INTERVAL)
@@ -106,14 +108,15 @@ class ConfigEntry:
]) ])
MKTXPSystemEntry = namedtuple('MKTXPSystemEntry', [MKTXPConfigKeys.PORT_KEY, MKTXPConfigKeys.MKTXP_SOCKET_TIMEOUT, MKTXPSystemEntry = namedtuple('MKTXPSystemEntry', [MKTXPConfigKeys.PORT_KEY, MKTXPConfigKeys.MKTXP_SOCKET_TIMEOUT,
MKTXPConfigKeys.MKTXP_INITIAL_DELAY, MKTXPConfigKeys.MKTXP_MAX_DELAY, MKTXPConfigKeys.MKTXP_INITIAL_DELAY, MKTXPConfigKeys.MKTXP_MAX_DELAY,
MKTXPConfigKeys.MKTXP_INC_DIV, MKTXPConfigKeys.MKTXP_BANDWIDTH_KEY, MKTXPConfigKeys.MKTXP_BANDWIDTH_TEST_INTERVAL]) MKTXPConfigKeys.MKTXP_INC_DIV, MKTXPConfigKeys.MKTXP_BANDWIDTH_KEY,
MKTXPConfigKeys.MKTXP_VERBOSE_MODE, MKTXPConfigKeys.MKTXP_BANDWIDTH_TEST_INTERVAL])
class OSConfig(metaclass = ABCMeta): class OSConfig(metaclass = ABCMeta):
''' OS-related config ''' OS-related config
''' '''
@staticmethod @staticmethod
def os_config(quiet = False): def os_config():
''' Factory method ''' Factory method
''' '''
if sys.platform == 'linux': if sys.platform == 'linux':
@@ -121,8 +124,7 @@ class OSConfig(metaclass = ABCMeta):
elif sys.platform == 'darwin': elif sys.platform == 'darwin':
return OSXConfig() return OSXConfig()
else: else:
if not quiet: print(f'Non-supported platform: {sys.platform}')
print(f'Non-supported platform: {sys.platform}')
return None return None
@property @property
@@ -244,11 +246,11 @@ class MKTXPConfigHandler:
system_entry_reader[key] = self._default_value_for_key(key) system_entry_reader[key] = self._default_value_for_key(key)
write_needed = True # read from disk next time write_needed = True # read from disk next time
for key in MKTXPConfigKeys.SYSTEM_BOOLEAN_KEYS_YES: for key in MKTXPConfigKeys.SYSTEM_BOOLEAN_KEYS_NO.union(MKTXPConfigKeys.SYSTEM_BOOLEAN_KEYS_YES):
if self._config[entry_name].get(key): if self._config[entry_name].get(key):
system_entry_reader[key] = self._config[entry_name].as_bool(key) system_entry_reader[key] = self._config[entry_name].as_bool(key)
else: else:
system_entry_reader[key] = True system_entry_reader[key] = True if key in MKTXPConfigKeys.SYSTEM_BOOLEAN_KEYS_YES else False
write_needed = True # read from disk next time write_needed = True # read from disk next time
if write_needed: if write_needed:

View File

@@ -89,18 +89,17 @@ class MKTXPDispatcher:
ExportProcessor.start() ExportProcessor.start()
def print(self, args): def print(self, args):
if not (args['wifi_clients'] or args['capsman_clients']):
print("Select metric option(s) to print out, or run 'mktxp print -h' to find out more")
if args['wifi_clients']: if args['wifi_clients']:
OutputProcessor.wifi_clients(args['entry_name']) OutputProcessor.wifi_clients(args['entry_name'])
if args['capsman_clients']: elif args['capsman_clients']:
OutputProcessor.capsman_clients(args['entry_name']) OutputProcessor.capsman_clients(args['entry_name'])
if args['dhcp_clients']: elif args['dhcp_clients']:
OutputProcessor.dhcp_clients(args['entry_name']) OutputProcessor.dhcp_clients(args['entry_name'])
else:
print("Select metric option(s) to print out, or run 'mktxp print -h' to find out more")
def main(): def main():
MKTXPDispatcher().dispatch() MKTXPDispatcher().dispatch()

View File

@@ -218,8 +218,9 @@ Selected metrics info can be printed on the command line. For more information,
return editor return editor
commands = ['which nano', 'which vi', 'which vim'] commands = ['which nano', 'which vi', 'which vim']
quiet = not config_handler.system_entry().verbose_mode
for command in commands: for command in commands:
editor = run_cmd(command, quiet = True).rstrip() editor = run_cmd(command, quiet = quiet).rstrip()
if editor: if editor:
break break
return editor return editor

View File

@@ -40,11 +40,20 @@ class DHCPCollector(BaseCollector):
'server': key, 'count': value} for key, value in dhcp_lease_servers.items()] 'server': key, 'count': value} for key, value in dhcp_lease_servers.items()]
# yield lease-per-server metrics # 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']) 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 yield dhcp_lease_server_metrics
# active lease metrics # active lease metrics
dhcp_lease_labels.remove('expires_after')
if router_entry.config_entry.dhcp_lease: if router_entry.config_entry.dhcp_lease:
dhcp_lease_metrics = BaseCollector.info_collector('dhcp_lease', 'DHCP Active Leases', dhcp_lease_records, dhcp_lease_labels) dhcp_lease_metrics_gauge = BaseCollector.gauge_collector('dhcp_lease', 'DHCP Active Leases',
yield dhcp_lease_metrics dhcp_lease_records, 'expires_after', dhcp_lease_labels)
yield dhcp_lease_metrics_gauge
# active lease metrics
#if router_entry.config_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

@@ -13,6 +13,7 @@
from mktxp.datasource.base_ds import BaseDSProcessor from mktxp.datasource.base_ds import BaseDSProcessor
from mktxp.utils.utils import parse_mkt_uptime
class DHCPMetricsDataSource: class DHCPMetricsDataSource:
@@ -31,7 +32,7 @@ class DHCPMetricsDataSource:
if 'host_name' in metric_labels: if 'host_name' in metric_labels:
translation_table['host_name'] = lambda c: c if c else '' translation_table['host_name'] = lambda c: c if c else ''
if 'expires_after' in metric_labels: if 'expires_after' in metric_labels:
translation_table['expires_after'] = lambda c: c if c else '' translation_table['expires_after'] = lambda c: parse_mkt_uptime(c)
if 'active_address' in metric_labels: if 'active_address' in metric_labels:
translation_table['active_address'] = lambda c: c if c else '' translation_table['active_address'] = lambda c: c if c else ''

View File

@@ -85,7 +85,7 @@ class BaseOutputProcessor:
wifi_rates_rgx = re.compile(r'(\d*(?:\.\d*)?)([GgMmKk]bps?)') wifi_rates_rgx = re.compile(r'(\d*(?:\.\d*)?)([GgMmKk]bps?)')
config_handler.re_compiled['wifi_rates_rgx'] = wifi_rates_rgx config_handler.re_compiled['wifi_rates_rgx'] = wifi_rates_rgx
rc = wifi_rates_rgx.search(rate) rc = wifi_rates_rgx.search(rate)
return f'{int(float(rc[1]))} {rc[2]}' if rc else '' return f'{int(float(rc[1]))} {rc[2]}' if rc and len(rc.groups()) == 2 else rate
@staticmethod @staticmethod
def parse_timedelta(time): def parse_timedelta(time):

View File

@@ -76,14 +76,14 @@ class RouterAPIConnection:
self.connect() self.connect()
return self.api return self.api
def _in_connect_timeout(self, connect_timestamp, quiet = True): def _in_connect_timeout(self, connect_timestamp):
connect_delay = self._connect_delay() connect_delay = self._connect_delay()
if (connect_timestamp - self.last_failure_timestamp) < connect_delay: if (connect_timestamp - self.last_failure_timestamp) < connect_delay:
if not quiet: if config_handler.system_entry().verbose_mode:
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'{self.router_name}@{self.E.hostname}: in connect timeout, {int(connect_delay - (connect_timestamp - self.last_failure_timestamp))}secs remaining')
print(f'Successive failure count: {self.successive_failure_count}') print(f'Successive failure count: {self.successive_failure_count}')
return True return True
if not quiet: if config_handler.system_entry().verbose_mode:
print(f'{self.router_name}@{self.config_entry.hostname}: OK to connect') print(f'{self.router_name}@{self.config_entry.hostname}: OK to connect')
if self.last_failure_timestamp > 0: if self.last_failure_timestamp > 0:
print(f'Seconds since last failure: {connect_timestamp - self.last_failure_timestamp}') print(f'Seconds since last failure: {connect_timestamp - self.last_failure_timestamp}')

View File

@@ -18,6 +18,7 @@ from timeit import default_timer
from collections.abc import Iterable from collections.abc import Iterable
from contextlib import contextmanager from contextlib import contextmanager
from multiprocessing import Process, Event from multiprocessing import Process, Event
from datetime import timedelta
''' Utilities / Helpers ''' Utilities / Helpers
@@ -69,6 +70,11 @@ def get_last_digit(str_to_search):
else: else:
return -1 return -1
def parse_mkt_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()
delta = timedelta(**{key: int(value) for key, value in time_dict.items() if value}).total_seconds()
return int(delta) if delta else 0
class FSHelper: class FSHelper:
''' File System ops helper ''' File System ops helper
''' '''

View File

@@ -20,7 +20,7 @@ with open(path.join(pkg_dir, 'README.md'), encoding='utf-8') as f:
setup( setup(
name='mktxp', name='mktxp',
version='0.34', version='0.35',
url='https://github.com/akpw/mktxp', url='https://github.com/akpw/mktxp',