mirror of
https://github.com/KevinMidboe/mktxp-no-cli.git
synced 2025-10-29 17:50:23 +00:00
cli metrics, fixes
This commit is contained in:
0
mktxp/cli/output/__init__.py
Normal file
0
mktxp/cli/output/__init__.py
Normal file
101
mktxp/cli/output/base_out.py
Normal file
101
mktxp/cli/output/base_out.py
Normal file
@@ -0,0 +1,101 @@
|
||||
# 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 collections import namedtuple
|
||||
from humanize import naturaldelta
|
||||
from mktxp.cli.config.config import config_handler
|
||||
|
||||
|
||||
class BaseOutputProcessor:
|
||||
OutputCapsmanEntry = namedtuple('OutputCapsmanEntry', ['dhcp_name', 'dhcp_address', 'mac_address', 'rx_signal', 'interface', 'ssid', 'tx_rate', 'rx_rate', 'uptime'])
|
||||
OutputWiFiEntry = namedtuple('OutputWiFiEntry', ['dhcp_name', 'dhcp_address', 'mac_address', 'signal_strength', 'signal_to_noise', 'interface', 'tx_rate', 'rx_rate', 'uptime'])
|
||||
|
||||
@staticmethod
|
||||
def augment_record(router_metric, registration_record, dhcp_lease_records):
|
||||
try:
|
||||
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']))
|
||||
dhcp_name = dhcp_lease_record.get('host_name')
|
||||
dhcp_comment = dhcp_lease_record.get('comment')
|
||||
|
||||
if dhcp_name and dhcp_comment:
|
||||
dhcp_name = f'{dhcp_name[0:20]} ({dhcp_comment[0:20]})' if not router_metric.router_entry.use_comments_over_names else dhcp_comment
|
||||
elif dhcp_comment:
|
||||
dhcp_name = dhcp_comment
|
||||
else:
|
||||
dhcp_name = dhcp_lease_record.get('mac_address') if not dhcp_name else dhcp_name
|
||||
dhcp_address = dhcp_lease_record.get('address', '')
|
||||
except StopIteration:
|
||||
dhcp_name = registration_record['mac_address']
|
||||
dhcp_address = 'No DHCP Record'
|
||||
|
||||
registration_record['dhcp_name'] = dhcp_name
|
||||
registration_record['dhcp_address'] = dhcp_address
|
||||
|
||||
# split out tx/rx bytes
|
||||
if registration_record.get('bytes'):
|
||||
registration_record['tx_bytes'] = registration_record['bytes'].split(',')[0]
|
||||
registration_record['rx_bytes'] = registration_record['bytes'].split(',')[1]
|
||||
del registration_record['bytes']
|
||||
|
||||
registration_record['tx_rate'] = BaseOutputProcessor.parse_rates(registration_record['tx_rate'])
|
||||
registration_record['rx_rate'] = BaseOutputProcessor.parse_rates(registration_record['rx_rate'])
|
||||
registration_record['uptime'] = naturaldelta(BaseOutputProcessor.parse_timedelta_seconds(registration_record['uptime']), months=True, minimum_unit='seconds', when=None)
|
||||
|
||||
if registration_record.get('signal_strength'):
|
||||
registration_record['signal_strength'] = BaseOutputProcessor.parse_signal_strength(registration_record['signal_strength'])
|
||||
if registration_record.get('rx_signal'):
|
||||
registration_record['rx_signal'] = BaseOutputProcessor.parse_signal_strength(registration_record['rx_signal'])
|
||||
|
||||
@staticmethod
|
||||
def parse_rates(rate):
|
||||
wifi_rates_rgx = config_handler.re_compiled.get('wifi_rates_rgx')
|
||||
if not wifi_rates_rgx:
|
||||
wifi_rates_rgx = re.compile(r'(\d*(?:\.\d*)?)([GgMmKk]bps?)')
|
||||
config_handler.re_compiled['wifi_rates_rgx'] = wifi_rates_rgx
|
||||
rc = wifi_rates_rgx.search(rate)
|
||||
return f'{int(float(rc[1]))} {rc[2]}'
|
||||
|
||||
@staticmethod
|
||||
def parse_timedelta(time):
|
||||
duration_interval_rgx = config_handler.re_compiled.get('duration_interval_rgx')
|
||||
if not duration_interval_rgx:
|
||||
duration_interval_rgx = re.compile(r'((?P<weeks>\d+)w)?((?P<days>\d+)d)?((?P<hours>\d+)h)?((?P<minutes>\d+)m)?((?P<seconds>\d+)s)?')
|
||||
config_handler.re_compiled['duration_interval_rgx'] = duration_interval_rgx
|
||||
time_dict = duration_interval_rgx.match(time).groupdict()
|
||||
return timedelta(**{key: int(value) for key, value in time_dict.items() if value})
|
||||
|
||||
@staticmethod
|
||||
def parse_timedelta_seconds(time):
|
||||
return BaseOutputProcessor.parse_timedelta(time).total_seconds()
|
||||
|
||||
@staticmethod
|
||||
def parse_signal_strength(signal_strength):
|
||||
wifi_signal_strength_rgx = config_handler.re_compiled.get('wifi_signal_strength_rgx')
|
||||
if not wifi_signal_strength_rgx:
|
||||
# wifi_signal_strength_rgx = re.compile(r'(-?\d+(?:\.\d+)?)(dBm)?')
|
||||
wifi_signal_strength_rgx = re.compile(r'(-?\d+(?:\.\d+)?)')
|
||||
config_handler.re_compiled['wifi_signal_strength_rgx'] = wifi_signal_strength_rgx
|
||||
return wifi_signal_strength_rgx.search(signal_strength).group()
|
||||
|
||||
@staticmethod
|
||||
def parse_interface_rate(interface_rate):
|
||||
interface_rate_rgx = config_handler.re_compiled.get('interface_rate_rgx')
|
||||
if not interface_rate_rgx:
|
||||
interface_rate_rgx = re.compile(r'[^.\-\d]')
|
||||
config_handler.re_compiled['interface_rate_rgx'] = interface_rate_rgx
|
||||
rate = lambda interface_rate: 1000 if interface_rate.find('Mbps') < 0 else 1
|
||||
return(int(float(interface_rate_rgx.sub('', interface_rate)) * rate(interface_rate)))
|
||||
|
||||
|
||||
52
mktxp/cli/output/capsman_out.py
Normal file
52
mktxp/cli/output/capsman_out.py
Normal 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 tabulate import tabulate
|
||||
from mktxp.cli.output.base_out import BaseOutputProcessor
|
||||
|
||||
class CapsmanOutput:
|
||||
''' CAPsMAN CLI Output
|
||||
'''
|
||||
@staticmethod
|
||||
def clients_summary(router_metric):
|
||||
registration_labels = ['interface', 'ssid', 'mac_address', 'rx_signal', 'uptime', 'tx_rate', 'rx_rate']
|
||||
registration_records = router_metric.capsman_registration_table_records(registration_labels, False)
|
||||
if not registration_records:
|
||||
print('No CAPsMAN registration records')
|
||||
return
|
||||
|
||||
# translate / trim / augment registration records
|
||||
dhcp_lease_labels = ['host_name', 'comment', 'address', 'mac_address']
|
||||
dhcp_lease_records = router_metric.dhcp_lease_records(dhcp_lease_labels, False)
|
||||
|
||||
dhcp_rt_by_interface = {}
|
||||
for registration_record in sorted(registration_records, key = lambda rt_record: rt_record['rx_signal'], reverse=True):
|
||||
BaseOutputProcessor.augment_record(router_metric, registration_record, dhcp_lease_records)
|
||||
|
||||
interface = registration_record['interface']
|
||||
if interface in dhcp_rt_by_interface.keys():
|
||||
dhcp_rt_by_interface[interface].append(registration_record)
|
||||
else:
|
||||
dhcp_rt_by_interface[interface] = [registration_record]
|
||||
|
||||
num_records = 0
|
||||
output_table = []
|
||||
for key in dhcp_rt_by_interface.keys():
|
||||
for record in dhcp_rt_by_interface[key]:
|
||||
output_table.append(BaseOutputProcessor.OutputCapsmanEntry(**record))
|
||||
num_records += 1
|
||||
output_table.append({})
|
||||
print()
|
||||
print(tabulate(output_table, headers = "keys", tablefmt="github"))
|
||||
print(tabulate([{0:'Connected Wifi Devices:', 1:num_records}], tablefmt="text"))
|
||||
|
||||
51
mktxp/cli/output/wifi_out.py
Normal file
51
mktxp/cli/output/wifi_out.py
Normal file
@@ -0,0 +1,51 @@
|
||||
# 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 tabulate import tabulate
|
||||
from mktxp.cli.output.base_out import BaseOutputProcessor
|
||||
|
||||
class WirelessOutput:
|
||||
''' Wireless Clients CLI Output
|
||||
'''
|
||||
@staticmethod
|
||||
def clients_summary(router_metric):
|
||||
registration_labels = ['interface', 'mac_address', 'signal_strength', 'uptime', 'tx_rate', 'rx_rate', 'signal_to_noise']
|
||||
registration_records = router_metric.wireless_registration_table_records(registration_labels, False)
|
||||
if not registration_records:
|
||||
print('No wireless registration records')
|
||||
return
|
||||
|
||||
# translate / trim / augment registration records
|
||||
dhcp_lease_labels = ['host_name', 'comment', 'address', 'mac_address']
|
||||
dhcp_lease_records = router_metric.dhcp_lease_records(dhcp_lease_labels, False)
|
||||
|
||||
dhcp_rt_by_interface = {}
|
||||
for registration_record in sorted(registration_records, key = lambda rt_record: rt_record['signal_strength'], reverse=True):
|
||||
BaseOutputProcessor.augment_record(router_metric, registration_record, dhcp_lease_records)
|
||||
|
||||
interface = registration_record['interface']
|
||||
if interface in dhcp_rt_by_interface.keys():
|
||||
dhcp_rt_by_interface[interface].append(registration_record)
|
||||
else:
|
||||
dhcp_rt_by_interface[interface] = [registration_record]
|
||||
|
||||
num_records = 0
|
||||
output_table = []
|
||||
for key in dhcp_rt_by_interface.keys():
|
||||
for record in dhcp_rt_by_interface[key]:
|
||||
output_table.append(BaseOutputProcessor.OutputWiFiEntry(**record))
|
||||
num_records += 1
|
||||
output_table.append({})
|
||||
print()
|
||||
print(tabulate(output_table, headers = "keys", tablefmt="github"))
|
||||
print(tabulate([{0:'Connected Wifi Devices:', 1:num_records}], tablefmt="text"))
|
||||
Reference in New Issue
Block a user