mirror of
https://github.com/KevinMidboe/mktxp-no-cli.git
synced 2025-10-29 17:50:23 +00:00
Initial commit
This commit is contained in:
0
mktxp/cli/__init__.py
Normal file
0
mktxp/cli/__init__.py
Normal file
0
mktxp/cli/checks/__init__.py
Normal file
0
mktxp/cli/checks/__init__.py
Normal file
48
mktxp/cli/checks/chk_pv.py
Executable file
48
mktxp/cli/checks/chk_pv.py
Executable file
@@ -0,0 +1,48 @@
|
||||
#!/usr/bin/env python
|
||||
# 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.
|
||||
|
||||
''' Python version check
|
||||
'''
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
import sys
|
||||
|
||||
def check_version():
|
||||
if sys.version_info.major < 3:
|
||||
print(\
|
||||
'''
|
||||
Mikrotik Prometheus Exporter requires
|
||||
Python version 3.6 or later.
|
||||
|
||||
You can create an isolated Python 3.6 environment
|
||||
with the virtualenv tool:
|
||||
http://docs.python-guide.org/en/latest/dev/virtualenvs
|
||||
|
||||
''')
|
||||
sys.exit(0)
|
||||
elif sys.version_info.major == 3 and sys.version_info.minor < 6:
|
||||
print(\
|
||||
'''
|
||||
|
||||
Mikrotik Prometheus Exporter requires
|
||||
Python version 3.6 or later.
|
||||
|
||||
Please upgrade to the latest Python 3.x version.
|
||||
|
||||
''')
|
||||
sys.exit(0)
|
||||
|
||||
# check
|
||||
check_version()
|
||||
0
mktxp/cli/config/__init__.py
Normal file
0
mktxp/cli/config/__init__.py
Normal file
209
mktxp/cli/config/config.py
Executable file
209
mktxp/cli/config/config.py
Executable file
@@ -0,0 +1,209 @@
|
||||
# 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 os, sys, shutil
|
||||
from collections import namedtuple
|
||||
from configobj import ConfigObj
|
||||
from abc import ABCMeta, abstractmethod
|
||||
from pkg_resources import Requirement, resource_filename
|
||||
from mktxp.utils.utils import FSHelper
|
||||
|
||||
|
||||
''' MKTXP conf file handling
|
||||
'''
|
||||
|
||||
class MKTXPConfigKeys:
|
||||
''' MKTXP config file keys
|
||||
'''
|
||||
# Section Keys
|
||||
ENABLED_KEY = 'enabled'
|
||||
HOST_KEY = 'hostname'
|
||||
PORT_KEY = 'port'
|
||||
USER_KEY = 'username'
|
||||
PASSWD_KEY = 'password'
|
||||
|
||||
SSL_KEY = 'use_ssl'
|
||||
SSL_CERTIFICATE = 'ssl_certificate'
|
||||
|
||||
FE_DHCP_KEY = 'dhcp'
|
||||
FE_DHCP_LEASE_KEY = 'dhcp_lease'
|
||||
FE_DHCP_POOL_KEY = 'pool'
|
||||
FE_INTERFACE_KEY = 'interface'
|
||||
FE_MONITOR_KEY = 'monitor'
|
||||
FE_ROUTE_KEY = 'route'
|
||||
FE_WIRELESS_KEY = 'wireless'
|
||||
FE_CAPSMAN_KEY = 'capsman'
|
||||
|
||||
|
||||
# UnRegistered entries placeholder
|
||||
NO_ENTRIES_REGISTERED = 'NoEntriesRegistered'
|
||||
|
||||
# Base router id labels
|
||||
ROUTERBOARD_NAME = 'routerboard_name'
|
||||
ROUTERBOARD_ADDRESS = 'routerboard_address'
|
||||
|
||||
# Default ports
|
||||
DEFAULT_API_PORT = 8728
|
||||
DEFAULT_API_SSL_PORT = 8729
|
||||
|
||||
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]
|
||||
STR_KEYS = [HOST_KEY, USER_KEY, PASSWD_KEY]
|
||||
|
||||
|
||||
class ConfigEntry:
|
||||
MKTXPEntry = namedtuple('MKTXPEntry', [MKTXPConfigKeys.ENABLED_KEY, MKTXPConfigKeys.HOST_KEY, MKTXPConfigKeys.PORT_KEY,
|
||||
MKTXPConfigKeys.USER_KEY, MKTXPConfigKeys.PASSWD_KEY,
|
||||
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
|
||||
])
|
||||
|
||||
class OSConfig(metaclass = ABCMeta):
|
||||
''' OS-related config
|
||||
'''
|
||||
@staticmethod
|
||||
def os_config(quiet = False):
|
||||
''' Factory method
|
||||
'''
|
||||
if sys.platform == 'linux':
|
||||
return LinuxConfig()
|
||||
elif sys.platform == 'darwin':
|
||||
return OSXConfig()
|
||||
else:
|
||||
if not quiet:
|
||||
print('Non-supported platform: {}'.format(sys.platform))
|
||||
return None
|
||||
|
||||
@property
|
||||
@abstractmethod
|
||||
def mktxp_user_dir_path(self):
|
||||
pass
|
||||
|
||||
|
||||
class OSXConfig(OSConfig):
|
||||
''' OSX-related config
|
||||
'''
|
||||
@property
|
||||
def mktxp_user_dir_path(self):
|
||||
return FSHelper.full_path('~/mktxp')
|
||||
|
||||
|
||||
class LinuxConfig(OSConfig):
|
||||
''' Linux-related config
|
||||
'''
|
||||
@property
|
||||
def mktxp_user_dir_path(self):
|
||||
return FSHelper.full_path('/etc/mktxp')
|
||||
|
||||
|
||||
class MKTXPConfigHandler:
|
||||
def __init__(self):
|
||||
self.os_config = OSConfig.os_config()
|
||||
if not self.os_config:
|
||||
sys.exit(1)
|
||||
|
||||
# mktxp user config folder
|
||||
if not os.path.exists(self.os_config.mktxp_user_dir_path):
|
||||
os.makedirs(self.os_config.mktxp_user_dir_path)
|
||||
|
||||
# if needed, stage the user config data
|
||||
self.usr_conf_data_path = os.path.join(self.os_config.mktxp_user_dir_path, 'mktxp.conf')
|
||||
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)
|
||||
|
||||
self.read_from_disk()
|
||||
|
||||
def read_from_disk(self):
|
||||
''' (Force-)Read conf data from disk
|
||||
'''
|
||||
self.config = ConfigObj(self.usr_conf_data_path)
|
||||
|
||||
|
||||
# MKTXP entries
|
||||
##############
|
||||
def register_entry(self, entry_name, entry_info, quiet = False):
|
||||
''' Registers MKTXP conf entry
|
||||
'''
|
||||
if entry_name in self.registered_entries():
|
||||
if not quiet:
|
||||
print('"{0}": entry name already registered'.format(entry_name))
|
||||
return False
|
||||
else:
|
||||
self.config[entry_name] = dict(entry_info._asdict())
|
||||
print(f'adding entry: {self.config[entry_name]}')
|
||||
self.config.write()
|
||||
if not quiet:
|
||||
print('Entry registered: {0}'.format(entry_name))
|
||||
return True
|
||||
|
||||
def unregister_entry(self, entry_name, quiet = False):
|
||||
''' Un-registers MKTXP conf entry
|
||||
'''
|
||||
if self.config[entry_name]:
|
||||
del(self.config[entry_name])
|
||||
self.config.write()
|
||||
if not quiet:
|
||||
print('Unregistered entry: {}'.format(entry_name))
|
||||
return True
|
||||
else:
|
||||
if not quiet:
|
||||
print('Entry is not registered: {}'.format(entry_name))
|
||||
return False
|
||||
|
||||
def registered_entries(self):
|
||||
''' All MKTXP registered entries
|
||||
'''
|
||||
registered_entries = [entry_name for entry_name in self.config.keys()]
|
||||
if not registered_entries:
|
||||
registered_entries = [MKTXPConfigKeys.NO_ENTRIES_REGISTERED]
|
||||
|
||||
return registered_entries
|
||||
|
||||
def entry(self, entry_name):
|
||||
''' Given an entry name, reads and returns the entry info
|
||||
'''
|
||||
entry_reader = self._entry_reader(entry_name)
|
||||
return ConfigEntry.MKTXPEntry(**entry_reader)
|
||||
|
||||
# Helpers
|
||||
def _entry_reader(self, entry_name):
|
||||
entry = {}
|
||||
for key in MKTXPConfigKeys.BOOLEAN_KEYS:
|
||||
if self.config[entry_name].get(key):
|
||||
entry[key] = self.config[entry_name].as_bool(key)
|
||||
else:
|
||||
entry[key] = False
|
||||
|
||||
for key in MKTXPConfigKeys.STR_KEYS:
|
||||
entry[key] = self.config[entry_name][key]
|
||||
|
||||
# port
|
||||
if self.config[entry_name].get(MKTXPConfigKeys.PORT_KEY):
|
||||
entry[MKTXPConfigKeys.PORT_KEY] = self.config[entry_name].as_int(MKTXPConfigKeys.PORT_KEY)
|
||||
else:
|
||||
if entry[MKTXPConfigKeys.SSL_KEY]:
|
||||
entry[MKTXPConfigKeys.PORT_KEY] = MKTXPConfigKeys.DEFAULT_API_SSL_PORT
|
||||
else:
|
||||
entry[MKTXPConfigKeys.PORT_KEY] = MKTXPConfigKeys.DEFAULT_API_PORT
|
||||
|
||||
return entry
|
||||
|
||||
|
||||
# Simplest possible Singleton impl
|
||||
config_handler = MKTXPConfigHandler()
|
||||
|
||||
36
mktxp/cli/config/mktxp.conf
Normal file
36
mktxp/cli/config/mktxp.conf
Normal file
@@ -0,0 +1,36 @@
|
||||
## 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.
|
||||
|
||||
|
||||
[Sample_RouterBoard]
|
||||
enabled = False
|
||||
|
||||
hostname = localhost
|
||||
port = 8728
|
||||
|
||||
username = user
|
||||
password = password
|
||||
|
||||
use_ssl = False
|
||||
ssl_certificate = False
|
||||
|
||||
identity = True
|
||||
resource = True
|
||||
health = True
|
||||
dhcp = True
|
||||
dhcp_lease = True
|
||||
pool = True
|
||||
interface = True
|
||||
monitor = True
|
||||
route = True
|
||||
wireless = True
|
||||
caps_man = True
|
||||
107
mktxp/cli/dispatch.py
Executable file
107
mktxp/cli/dispatch.py
Executable file
@@ -0,0 +1,107 @@
|
||||
#!/usr/bin/env python
|
||||
# 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 sys
|
||||
import pkg_resources
|
||||
import mktxp.cli.checks.chk_pv
|
||||
from mktxp.cli.options import MKTXPOptionsParser, MKTXPCommands
|
||||
from mktxp.cli.config.config import config_handler, ConfigEntry
|
||||
from mktxp.basep import MKTXPProcessor
|
||||
|
||||
class MKTXPDispatcher:
|
||||
''' Base MKTXP Commands Dispatcher
|
||||
'''
|
||||
def __init__(self):
|
||||
self.option_parser = MKTXPOptionsParser()
|
||||
|
||||
# Dispatcher
|
||||
def dispatch(self):
|
||||
args = self.option_parser.parse_options()
|
||||
|
||||
if args['sub_cmd'] == MKTXPCommands.VERSION:
|
||||
self.print_version()
|
||||
|
||||
elif args['sub_cmd'] == MKTXPCommands.INFO:
|
||||
self.print_info()
|
||||
|
||||
elif args['sub_cmd'] == MKTXPCommands.SHOW:
|
||||
self.show_entries()
|
||||
|
||||
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.START:
|
||||
self.start_export(args)
|
||||
|
||||
else:
|
||||
# nothing to dispatch
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
# Dispatched methods
|
||||
def print_version(self):
|
||||
''' Prints MKTXP version info
|
||||
'''
|
||||
version = pkg_resources.require("mktxp")[0].version
|
||||
print('Mikrotik RouterOS Prometheus Exporter version {}'.format(version))
|
||||
|
||||
def print_info(self):
|
||||
''' Prints MKTXP general info
|
||||
'''
|
||||
print('Mikrotik RouterOS Prometheus Exporter: {}'.format(self.option_parser.script_name))
|
||||
print(self.option_parser.description)
|
||||
|
||||
|
||||
def show_entries(self):
|
||||
for entryname in config_handler.registered_entries():
|
||||
entry = config_handler.entry(entryname)
|
||||
|
||||
print('[{}]'.format(entryname))
|
||||
for field in entry._fields:
|
||||
print(' {}: {}'.format(field, getattr(entry, field)))
|
||||
print()
|
||||
|
||||
def add_entry(self, args):
|
||||
args.pop('sub_cmd', None)
|
||||
entry_name = args['entry_name']
|
||||
args.pop('entry_name', None)
|
||||
|
||||
entry_info = ConfigEntry.MKTXPEntry(**args)
|
||||
config_handler.register_entry(entry_name = entry_name, entry_info = entry_info)
|
||||
|
||||
|
||||
def edit_entry(self, args):
|
||||
pass
|
||||
|
||||
def delete_entry(self, args):
|
||||
config_handler.unregister_entry(entry_name = args['entry_name'])
|
||||
|
||||
|
||||
def start_export(self, args):
|
||||
MKTXPProcessor.start()
|
||||
|
||||
|
||||
def main():
|
||||
MKTXPDispatcher().dispatch()
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
287
mktxp/cli/options.py
Executable file
287
mktxp/cli/options.py
Executable file
@@ -0,0 +1,287 @@
|
||||
# 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 os
|
||||
from argparse import ArgumentParser, HelpFormatter
|
||||
from mktxp.cli.config.config import config_handler, MKTXPConfigKeys
|
||||
from mktxp.utils.utils import FSHelper, UniquePartialMatchList
|
||||
|
||||
|
||||
class MKTXPCommands:
|
||||
INFO = 'info'
|
||||
VERSION = 'version'
|
||||
SHOW = 'show'
|
||||
ADD = 'add'
|
||||
EDIT = 'edit'
|
||||
DELETE = 'delete'
|
||||
START = 'start'
|
||||
|
||||
@classmethod
|
||||
def commands_meta(cls):
|
||||
return ''.join(('{',
|
||||
'{}, '.format(cls.INFO),
|
||||
'{}'.format(cls.VERSION),
|
||||
'{}'.format(cls.SHOW),
|
||||
'{}'.format(cls.ADD),
|
||||
'{}'.format(cls.EDIT),
|
||||
'{}'.format(cls.DELETE),
|
||||
'{}'.format(cls.START),
|
||||
'}'))
|
||||
|
||||
class MKTXPOptionsParser:
|
||||
''' Base MKTXP Options Parser
|
||||
'''
|
||||
def __init__(self):
|
||||
self._script_name = 'MKTXP'
|
||||
self._description = \
|
||||
'''
|
||||
Prometheus Exporter for Mikrotik RouterOS Devices
|
||||
'''
|
||||
|
||||
@property
|
||||
def description(self):
|
||||
return self._description
|
||||
|
||||
@property
|
||||
def script_name(self):
|
||||
return self._script_name
|
||||
|
||||
# Options Parsing Workflow
|
||||
def parse_options(self):
|
||||
''' General Options parsing workflow
|
||||
'''
|
||||
parser = ArgumentParser(prog = self._script_name,
|
||||
description = self._description,
|
||||
formatter_class=MKTXPHelpFormatter)
|
||||
|
||||
self.parse_global_options(parser)
|
||||
self.parse_commands(parser)
|
||||
args = vars(parser.parse_args())
|
||||
|
||||
self._check_args(args, parser)
|
||||
|
||||
return args
|
||||
|
||||
def parse_global_options(self, parser):
|
||||
''' Parses global options
|
||||
'''
|
||||
pass
|
||||
|
||||
def parse_commands(self, parser):
|
||||
''' Commands parsing
|
||||
'''
|
||||
subparsers = parser.add_subparsers(dest = 'sub_cmd',
|
||||
title = 'MKTXP commands',
|
||||
metavar = MKTXPCommands.commands_meta())
|
||||
|
||||
# Info command
|
||||
subparsers.add_parser(MKTXPCommands.INFO,
|
||||
description = 'Displays MKTXP info',
|
||||
formatter_class=MKTXPHelpFormatter)
|
||||
# Version command
|
||||
subparsers.add_parser(MKTXPCommands.VERSION,
|
||||
description = 'Displays MKTXP version info',
|
||||
formatter_class=MKTXPHelpFormatter)
|
||||
|
||||
# Show command
|
||||
subparsers.add_parser(MKTXPCommands.SHOW,
|
||||
description = 'Displays MKTXP router entries',
|
||||
formatter_class=MKTXPHelpFormatter)
|
||||
|
||||
# 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('-ssl-cert', '--use-ssl-certificate', dest='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')
|
||||
|
||||
#'hostname', 'port', 'username', 'password', 'use_ssl', 'ssl_certificate', 'dhcp', 'dhcp_lease', 'pool', 'interface', 'monitor', 'route', 'wireless', and 'capsman'
|
||||
|
||||
# Edit command
|
||||
edit_parser = subparsers.add_parser(MKTXPCommands.EDIT,
|
||||
description = 'Edits an existing MKTXP router entry',
|
||||
formatter_class=MKTXPHelpFormatter)
|
||||
required_args_group = edit_parser.add_argument_group('Required Arguments')
|
||||
self._add_entry_name(required_args_group, registered_only = True, help = "Name of entry to edit")
|
||||
|
||||
|
||||
# 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.START,
|
||||
description = 'Starts exporting Miktorik Router Metrics',
|
||||
formatter_class=MKTXPHelpFormatter)
|
||||
|
||||
|
||||
# Options checking
|
||||
def _check_args(self, args, parser):
|
||||
''' Validation of supplied CLI arguments
|
||||
'''
|
||||
# check if there is a cmd to execute
|
||||
self._check_cmd_args(args, parser)
|
||||
|
||||
if args['sub_cmd'] in (MKTXPCommands.EDIT, MKTXPCommands.DELETE):
|
||||
# Registered Entry name could be a partial match, need to expand
|
||||
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('"{0}": entry name already exists'.format(args['entry_name']))
|
||||
parser.exit()
|
||||
|
||||
def _check_cmd_args(self, args, parser):
|
||||
''' Validation of supplied CLI commands
|
||||
'''
|
||||
# base command check
|
||||
if 'sub_cmd' not in args or not args['sub_cmd']:
|
||||
# If no command was specified, check for the default one
|
||||
cmd = self._default_command
|
||||
if cmd:
|
||||
args['sub_cmd'] = cmd
|
||||
else:
|
||||
# no appropriate default either
|
||||
parser.print_help()
|
||||
parser.exit()
|
||||
|
||||
|
||||
@property
|
||||
def _default_command(self):
|
||||
''' If no command was specified, print INFO by default
|
||||
'''
|
||||
return MKTXPCommands.START
|
||||
|
||||
|
||||
# Internal helpers
|
||||
@staticmethod
|
||||
def _is_valid_dir_path(parser, path_arg):
|
||||
''' Checks if path_arg is a valid dir path
|
||||
'''
|
||||
path_arg = FSHelper.full_path(path_arg)
|
||||
if not (os.path.exists(path_arg) and os.path.isdir(path_arg)):
|
||||
parser.error('"{}" does not seem to be an existing directory path'.format(path_arg))
|
||||
else:
|
||||
return path_arg
|
||||
|
||||
@staticmethod
|
||||
def _is_valid_file_path(parser, path_arg):
|
||||
''' Checks if path_arg is a valid file path
|
||||
'''
|
||||
path_arg = FSHelper.full_path(path_arg)
|
||||
if not (os.path.exists(path_arg) and os.path.isfile(path_arg)):
|
||||
parser.error('"{}" does not seem to be an existing file path'.format(path_arg))
|
||||
else:
|
||||
return path_arg
|
||||
|
||||
@staticmethod
|
||||
def _add_entry_name(parser, registered_only = False, help = 'MKTXP Entry name'):
|
||||
parser.add_argument('-en', '--entry-name', dest = 'entry_name',
|
||||
type = str,
|
||||
metavar = config_handler.registered_entries() if registered_only else None,
|
||||
required = True,
|
||||
choices = UniquePartialMatchList(config_handler.registered_entries())if registered_only else None,
|
||||
help = help)
|
||||
|
||||
@staticmethod
|
||||
def _add_entry_groups(parser):
|
||||
required_args_group = parser.add_argument_group('Required Arguments')
|
||||
MKTXPOptionsParser._add_entry_name(required_args_group)
|
||||
|
||||
|
||||
class MKTXPHelpFormatter(HelpFormatter):
|
||||
''' Custom formatter for ArgumentParser
|
||||
Disables double metavar display, showing only for long-named options
|
||||
'''
|
||||
def _format_action_invocation(self, action):
|
||||
if not action.option_strings:
|
||||
metavar, = self._metavar_formatter(action, action.dest)(1)
|
||||
return metavar
|
||||
else:
|
||||
parts = []
|
||||
# if the Optional doesn't take a value, format is:
|
||||
# -s, --long
|
||||
if action.nargs == 0:
|
||||
parts.extend(action.option_strings)
|
||||
|
||||
# if the Optional takes a value, format is:
|
||||
# -s ARGS, --long ARGS
|
||||
# change to
|
||||
# -s, --long ARGS
|
||||
else:
|
||||
default = action.dest.upper()
|
||||
args_string = self._format_args(action, default)
|
||||
for option_string in action.option_strings:
|
||||
#parts.append('%s %s' % (option_string, args_string))
|
||||
parts.append('%s' % option_string)
|
||||
parts[-1] += ' %s'%args_string
|
||||
return ', '.join(parts)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user