Collection of CheckMK checks (see https://checkmk.com/). All checks and plugins are provided as is. Absolutely no warranty. Send any comments to thl-cmk[at]outlook[dot]com

Skip to content
Snippets Groups Projects
create_topology_args.py 6.74 KiB
Newer Older
thl-cmk's avatar
thl-cmk committed
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# License: GNU General Public License v2
# Author: thl-cmk[at]outlook[dot]com
# URL   : https://thl-cmk.hopto.org
# Date  : 2023-10-12
# File  : create_topology_utils.py

#
# options used
# -d --default
# -o --output-directory
# -s --seed-devices
# -u --user-data-file
# -v --version
# --check-user-data-only
# --data-source
# --debug
# --dont-compare
# --inventory-columns
# --keep-domain
# --keep
# --lldp
# --lowercase
# --min-age
# --path-in-inventory
# --time-format
# --uppercase

from argparse import (
    Namespace as arg_Namespace,
    ArgumentParser,
    RawTextHelpFormatter,
)
from create_topology_utils import(
    CREATE_TOPOLOGY_VERSION,
    SCRIPT,
    SAMPLE_SEEDS,
    USER_DATA_FILE,
    LABEL_CDP,
    COLUMNS_CDP,
    LABEL_LLDP,
    PATH_LLDP,
    COLUMNS_LLDP,
    PATH_CDP
)


def parse_arguments() -> arg_Namespace:
    parser = ArgumentParser(
        prog='create_topology_data.py',
        description='This script creates the topology data file needed for the Checkmk "network_visualization"\n'
                    'plugin by Andreas Boesl and schnetz. For more information see\n'
                    'the announcement from schnetz: https://forum.checkmk.com/t/network-visualization/41680\n'
                    'and the plugin on the Exchange: https://exchange.checkmk.com/p/network-visualization .\n'
                    '\n'
                    'The required inventory data can be created with my inventory plugins:\n'
                    'CDP: https://thl-cmk.hopto.org/gitlab/checkmk/vendor-independent/inventory/inv_cdp_cache\n'
                    'LLDP: https://thl-cmk.hopto.org/gitlab/checkmk/vendor-independent/inventory/inv_lldp_cache\n'
                    '\n'
                    f'\nVersion: {CREATE_TOPOLOGY_VERSION} | Written by: thl-cmk\n'
                    f'for more information see: https://thl-cmk.hopto.org',
        formatter_class=RawTextHelpFormatter,
        epilog='Usage:\n'
               'for CDP (the default):\n'
               f'{SCRIPT} -s {SAMPLE_SEEDS} -d\n'
               'for LLDP:\n'
               f'{SCRIPT} -s {SAMPLE_SEEDS} -d --lldp\n',
    )
    command_group = parser.add_mutually_exclusive_group()

    parser.add_argument(
        '-d', '--default', default=False, action='store_const', const=True,
        help='Set the created topology data as default',
    )
    parser.add_argument(
        '-m', '--merge',
        nargs=2,
        choices=['CDP', 'LLDP'],
        help=f'Merge topologies. This runs the topology creation for CDP and LLDP.\n'
             f'The topologies are then merged in the specified order.\n'
             f'I.e. -m CDP LLDP merges the CDP topology into the LLDP topology, overwriting\n'
             f'the LLDP data in case there are conflicts.\n'
             f'NOTE: static connection data from the user file will always merged with the\n'
             f' highest priority',
    )
    parser.add_argument(
        '-o', '--output-directory', type=str,
        help='Directory name where to save the topology data.\n'
             'I.e.: my_topology. Default is the actual date/time\n'
             'in "--time-format" format.\n'
             'NOTE: the directory is a sub directory under "~/var/topology_data/"',
    )
    parser.add_argument(
        '-s', '--seed-devices', type=str, nargs='+',
        help=f'List of devices to start the topology discovery from.\n'
             f'I.e. {SAMPLE_SEEDS}',
    )
    parser.add_argument(
        '-u', '--user-data-file', type=str,
        help='Set the name uf the user provided data file\n'
             'Default is ~local/bin/topology_data/create_topology_data.toml\n',
    )
    parser.add_argument(
        '-v', '--version', default=False, action='store_const', const=True,
        help='Print version of this script and exit',
    )
    parser.add_argument(
        '--check-user-data-only', default=False, action='store_const', const=True,
        help=f'Only tries to read/parse the user data from {USER_DATA_FILE} and exits.',
    )
    parser.add_argument(
        '--data-source', type=str,
        help=f'The source from which the topology data originates.\n'
             f'I.e. {LABEL_CDP} for CDP data from the inventory.\n'
             'NOTE: right now this only an unused label.',
    )
    parser.add_argument(
        '--debug', default=False, action='store_const', const=True,
        help='Print debug information',
    )
    parser.add_argument(
        '--dont-compare', default=False, action='store_const', const=True,
        help='Do not compare the actual topology data with the default topology\n'
             'data. By default, the actual topology is compared with the default\n'
             'topology. If the data matches, the actual topology is not saved.\n'
             'So, if you run this tool in a cron job, a new topology will be\n'
             'created only if there was a change, unless you use "--dont-compare".'
    )
    parser.add_argument(
        '--inventory-columns', type=str,
        help=f'Columns used from the inventory data.\n'
             f'I.e. "{COLUMNS_CDP}"\n'
             'NOTE: the columns must be in the order: neighbour, local_port,\n'
             'neighbour_port',
    )
    parser.add_argument(
        '--keep-domain', default=False, action='store_const', const=True,
        help='Do not remove the domain name from the neighbor name',
    )
    parser.add_argument(
        '--keep', type=int,
        help=f'Number of topologies to keep. The oldest topologies above keep\n'
             f'max will be deleted. The minimum value for --keep is 1.\n'
             f'NOTE: The default topologies will be always kept.\n'
    )
    parser.add_argument(
        '--lldp', default=False, action='store_const', const=True,
        help=f'Sets data source to {LABEL_LLDP}, inventory path \n'
             f'to "{PATH_LLDP}" and columns\n'
             f'to "{COLUMNS_LLDP}"',
    )
    command_group.add_argument(
        '--lowercase', default=False, action='store_const', const=True,
        help='Change neighbour names to all lower case',
    )
    parser.add_argument(
        '--min-age', type=int,
        help=f'The minimum number of days before a topology is deleted\n'
             f'by "--keep".\n'
             f'NOTE: Topologies that are not older than 1 days are always kept.',
    )
    parser.add_argument(
        '--path-in-inventory', type=str,
        help=f'Checkmk inventory path to the topology data.\n'
             f'I.e. "{PATH_CDP}"',
    )
    parser.add_argument(
        '--time-format', type=str,
        help='Format string to render the time. (default: %%Y-%%m-%%dT%%H:%%M:%%S.%%m)',
    )
    command_group.add_argument(
        '--uppercase', default=False, action='store_const', const=True,
        help='Change neighbour names to all upper case',
    )
    return parser.parse_args()