mirror of
https://github.com/KevinMidboe/mktxp-no-cli.git
synced 2025-12-08 20:38:48 +00:00
firewall/mktxp colllectors, fixes/optimizations
This commit is contained in:
@@ -40,6 +40,7 @@ class MKTXPConfigKeys:
|
||||
FE_DHCP_LEASE_KEY = 'dhcp_lease'
|
||||
FE_DHCP_POOL_KEY = 'pool'
|
||||
FE_INTERFACE_KEY = 'interface'
|
||||
FE_FIREWALL_KEY = 'firewall'
|
||||
FE_MONITOR_KEY = 'monitor'
|
||||
FE_ROUTE_KEY = 'route'
|
||||
FE_WIRELESS_KEY = 'wireless'
|
||||
@@ -75,7 +76,7 @@ class MKTXPConfigKeys:
|
||||
DEFAULT_MKTXP_BANDWIDTH_TEST_INTERVAL = 420
|
||||
|
||||
BOOLEAN_KEYS = (ENABLED_KEY, SSL_KEY, NO_SSL_CERTIFICATE, SSL_CERTIFICATE_VERIFY,
|
||||
FE_DHCP_KEY, FE_DHCP_LEASE_KEY, FE_DHCP_POOL_KEY, FE_INTERFACE_KEY,
|
||||
FE_DHCP_KEY, FE_DHCP_LEASE_KEY, FE_DHCP_POOL_KEY, FE_INTERFACE_KEY, FE_FIREWALL_KEY,
|
||||
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)
|
||||
|
||||
@@ -91,7 +92,7 @@ class ConfigEntry:
|
||||
MKTXPConfigKeys.USER_KEY, MKTXPConfigKeys.PASSWD_KEY,
|
||||
MKTXPConfigKeys.SSL_KEY, MKTXPConfigKeys.NO_SSL_CERTIFICATE, MKTXPConfigKeys.SSL_CERTIFICATE_VERIFY,
|
||||
|
||||
MKTXPConfigKeys.FE_DHCP_KEY, MKTXPConfigKeys.FE_DHCP_LEASE_KEY, MKTXPConfigKeys.FE_DHCP_POOL_KEY, MKTXPConfigKeys.FE_INTERFACE_KEY,
|
||||
MKTXPConfigKeys.FE_DHCP_KEY, MKTXPConfigKeys.FE_DHCP_LEASE_KEY, MKTXPConfigKeys.FE_DHCP_POOL_KEY, MKTXPConfigKeys.FE_INTERFACE_KEY, MKTXPConfigKeys.FE_FIREWALL_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, MKTXPConfigKeys.MKTXP_USE_COMMENTS_OVER_NAMES
|
||||
])
|
||||
@@ -246,9 +247,9 @@ class MKTXPConfigHandler:
|
||||
entry_reader[MKTXPConfigKeys.PORT_KEY] = self._default_value_for_key(MKTXPConfigKeys.SSL_KEY, entry_reader[MKTXPConfigKeys.SSL_KEY])
|
||||
write_needed = True # read from disk next time
|
||||
|
||||
if write_needed:
|
||||
self.config[entry_name] = entry_reader
|
||||
self.config.write()
|
||||
if write_needed:
|
||||
self.config[entry_name] = entry_reader
|
||||
self.config.write()
|
||||
|
||||
return entry_reader
|
||||
|
||||
@@ -263,10 +264,10 @@ class MKTXPConfigHandler:
|
||||
_entry_reader[key] = self._default_value_for_key(key)
|
||||
write_needed = True # read from disk next time
|
||||
|
||||
if write_needed:
|
||||
self._config[entry_name] = _entry_reader
|
||||
self._config.write()
|
||||
|
||||
if write_needed:
|
||||
self._config[entry_name] = _entry_reader
|
||||
self._config.write()
|
||||
|
||||
return _entry_reader
|
||||
|
||||
def _default_value_for_key(self, key, value = None):
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
dhcp_lease = True
|
||||
pool = True
|
||||
interface = True
|
||||
firewall = True
|
||||
monitor = True
|
||||
route = True
|
||||
wireless = True
|
||||
@@ -35,4 +36,4 @@
|
||||
capsman = True
|
||||
capsman_clients = True
|
||||
|
||||
use_comments_over_names = False # when available, use comments instead of interface names
|
||||
use_comments_over_names = False # when available, use comments instead of interfaces names
|
||||
|
||||
@@ -36,21 +36,15 @@ class MKTXPDispatcher:
|
||||
elif args['sub_cmd'] == MKTXPCommands.SHOW:
|
||||
self.show_entries(args)
|
||||
|
||||
elif args['sub_cmd'] == MKTXPCommands.ADD:
|
||||
self.add_entry(args)
|
||||
|
||||
elif args['sub_cmd'] == MKTXPCommands.EDIT:
|
||||
self.edit_entry(args)
|
||||
|
||||
elif args['sub_cmd'] == MKTXPCommands.DELETE:
|
||||
self.delete_entry(args)
|
||||
|
||||
elif args['sub_cmd'] == MKTXPCommands.EXPORT:
|
||||
self.start_export(args)
|
||||
|
||||
elif args['sub_cmd'] == MKTXPCommands.PRINT:
|
||||
self.print(args)
|
||||
|
||||
elif args['sub_cmd'] == MKTXPCommands.EDIT:
|
||||
self.edit_entry(args)
|
||||
|
||||
else:
|
||||
# nothing to dispatch
|
||||
return False
|
||||
@@ -84,10 +78,6 @@ class MKTXPDispatcher:
|
||||
print(f' {field}: {getattr(entry, field)}')
|
||||
print('\n')
|
||||
|
||||
def add_entry(self, args):
|
||||
entry_args = {key: value for key, value in args.items() if key not in set(['sub_cmd', 'entry_name'])}
|
||||
config_handler.register_entry(entry_name = args['entry_name'], entry_args = entry_args)
|
||||
|
||||
def edit_entry(self, args):
|
||||
editor = args['editor']
|
||||
if not editor:
|
||||
@@ -96,10 +86,7 @@ class MKTXPDispatcher:
|
||||
subprocess.check_call([editor, config_handler.mktxp_conf_path])
|
||||
else:
|
||||
subprocess.check_call([editor, config_handler.usr_conf_data_path])
|
||||
|
||||
def delete_entry(self, args):
|
||||
config_handler.unregister_entry(entry_name = args['entry_name'])
|
||||
|
||||
|
||||
def start_export(self, args):
|
||||
MKTXPProcessor.start()
|
||||
|
||||
|
||||
@@ -24,8 +24,6 @@ class MKTXPCommands:
|
||||
EXPORT = 'export'
|
||||
PRINT = 'print'
|
||||
SHOW = 'show'
|
||||
ADD = 'add'
|
||||
DELETE = 'delete'
|
||||
|
||||
@classmethod
|
||||
def commands_meta(cls):
|
||||
@@ -35,8 +33,6 @@ class MKTXPCommands:
|
||||
f'{cls.EXPORT}, ',
|
||||
f'{cls.PRINT}, ',
|
||||
f'{cls.SHOW}, ',
|
||||
f'{cls.ADD}, ',
|
||||
f'{cls.DELETE}',
|
||||
'}'))
|
||||
|
||||
class MKTXPOptionsParser:
|
||||
@@ -102,67 +98,6 @@ Selected metrics info can be printed on the command line. For more information,
|
||||
help = "Shows MKTXP config files paths",
|
||||
action = 'store_true')
|
||||
|
||||
# Add command
|
||||
add_parser = subparsers.add_parser(MKTXPCommands.ADD,
|
||||
description = 'Adds a new MKTXP router entry',
|
||||
formatter_class=MKTXPHelpFormatter)
|
||||
required_args_group = add_parser.add_argument_group('Required Arguments')
|
||||
self._add_entry_name(required_args_group, registered_only = False, help = "Config entry name")
|
||||
required_args_group.add_argument('-host', '--hostname', dest='hostname',
|
||||
help = "IP address of RouterOS device to export metrics from",
|
||||
type = str,
|
||||
required=True)
|
||||
required_args_group.add_argument('-usr', '--username', dest='username',
|
||||
help = "username",
|
||||
type = str,
|
||||
required=True)
|
||||
required_args_group.add_argument('-pwd', '--password', dest='password',
|
||||
help = "password",
|
||||
type = str,
|
||||
required=True)
|
||||
|
||||
optional_args_group = add_parser.add_argument_group('Optional Arguments')
|
||||
optional_args_group.add_argument('-e', dest='enabled',
|
||||
help = "Enables entry for metrics processing",
|
||||
action = 'store_false')
|
||||
|
||||
optional_args_group.add_argument('-port', dest='port',
|
||||
help = "port",
|
||||
default = MKTXPConfigKeys.DEFAULT_API_PORT,
|
||||
type = int)
|
||||
|
||||
optional_args_group.add_argument('-ssl', '--use-ssl', dest='use_ssl',
|
||||
help = "Connect via RouterOS api-ssl service",
|
||||
action = 'store_true')
|
||||
optional_args_group.add_argument('-no-ssl-cert', '--no-ssl-certificate', dest='no_ssl_certificate',
|
||||
help = "Connect with configured RouterOS SSL ceritficate",
|
||||
action = 'store_true')
|
||||
|
||||
optional_args_group.add_argument('-dhcp', '--export_dhcp', dest='dhcp',
|
||||
help = "Export DHCP metrics",
|
||||
action = 'store_true')
|
||||
optional_args_group.add_argument('-dhcp_lease', '--export_dhcp_lease', dest='dhcp_lease',
|
||||
help = "Export DHCP Lease metrics",
|
||||
action = 'store_true')
|
||||
optional_args_group.add_argument('-pool', '--export_pool', dest='pool',
|
||||
help = "Export IP Pool metrics",
|
||||
action = 'store_true')
|
||||
optional_args_group.add_argument('-interface', '--export_interface', dest='interface',
|
||||
help = "Export Interface metrics",
|
||||
action = 'store_true')
|
||||
optional_args_group.add_argument('-monitor', '--export_monitor', dest='monitor',
|
||||
help = "Export Interface Monitor metrics",
|
||||
action = 'store_true')
|
||||
optional_args_group.add_argument('-route', '--export_route', dest='route',
|
||||
help = "Export IP Route metrics",
|
||||
action = 'store_true')
|
||||
optional_args_group.add_argument('-wireless', '--export_wireless', dest='wireless',
|
||||
help = "Export Wireless metrics",
|
||||
action = 'store_true')
|
||||
optional_args_group.add_argument('-capsman', '--export_capsman', dest='capsman',
|
||||
help = "Export CAPsMAN metrics",
|
||||
action = 'store_true')
|
||||
|
||||
# Edit command
|
||||
edit_parser = subparsers.add_parser(MKTXPCommands.EDIT,
|
||||
description = 'Edits an existing MKTXP router entry',
|
||||
@@ -176,21 +111,14 @@ Selected metrics info can be printed on the command line. For more information,
|
||||
help = f"Edit MKTXP internal configuration (advanced)",
|
||||
action = 'store_true')
|
||||
|
||||
# Delete command
|
||||
delete_parser = subparsers.add_parser(MKTXPCommands.DELETE,
|
||||
description = 'Deletes an existing MKTXP router entry',
|
||||
formatter_class=MKTXPHelpFormatter)
|
||||
required_args_group = delete_parser.add_argument_group('Required Arguments')
|
||||
self._add_entry_name(required_args_group, registered_only = True, help = "Name of entry to delete")
|
||||
|
||||
# Start command
|
||||
start_parser = subparsers.add_parser(MKTXPCommands.EXPORT,
|
||||
# Export command
|
||||
export_parser = subparsers.add_parser(MKTXPCommands.EXPORT,
|
||||
description = 'Starts exporting Miktorik Router Metrics to Prometheus',
|
||||
formatter_class=MKTXPHelpFormatter)
|
||||
|
||||
# Print command
|
||||
print_parser = subparsers.add_parser(MKTXPCommands.PRINT,
|
||||
description = 'Displays seleted metrics on the command line',
|
||||
description = 'Displays selected metrics on the command line',
|
||||
formatter_class=MKTXPHelpFormatter)
|
||||
required_args_group = print_parser.add_argument_group('Required Arguments')
|
||||
self._add_entry_name(required_args_group, registered_only = True, help = "Name of config RouterOS entry")
|
||||
@@ -212,17 +140,12 @@ Selected metrics info can be printed on the command line. For more information,
|
||||
# check if there is a cmd to execute
|
||||
self._check_cmd_args(args, parser)
|
||||
|
||||
if args['sub_cmd'] in (MKTXPCommands.DELETE, MKTXPCommands.SHOW, MKTXPCommands.PRINT):
|
||||
if args['sub_cmd'] in (MKTXPCommands.SHOW, MKTXPCommands.PRINT):
|
||||
# Registered Entry name could be a partial match, need to expand
|
||||
if args['entry_name']:
|
||||
args['entry_name'] = UniquePartialMatchList(config_handler.registered_entries()).find(args['entry_name'])
|
||||
|
||||
if args['sub_cmd'] == MKTXPCommands.ADD:
|
||||
if args['entry_name'] in (config_handler.registered_entries()):
|
||||
print(f"{args['entry_name']}: entry name already exists")
|
||||
parser.exit()
|
||||
|
||||
elif args['sub_cmd'] == MKTXPCommands.PRINT:
|
||||
if args['sub_cmd'] == MKTXPCommands.PRINT:
|
||||
if not config_handler.entry(args['entry_name']).enabled:
|
||||
print(f"Can not print metrics for disabled RouterOS entry: {args['entry_name']}\nRun 'mktxp edit' to review and enable it in the configuration file first")
|
||||
parser.exit()
|
||||
|
||||
@@ -97,5 +97,3 @@ class BaseOutputProcessor:
|
||||
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)))
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user