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 5.62 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
thl-cmk's avatar
thl-cmk committed
# -b --backend
thl-cmk's avatar
thl-cmk committed
# -d --default
thl-cmk's avatar
thl-cmk committed
# -l --layer
thl-cmk's avatar
thl-cmk committed
# -o --output-directory
# -s --seed-devices
# -u --user-data-file
# -v --version
# --check-user-data-only
# --debug
# --dont-compare
# --keep-domain
# --keep
# --lowercase
# --min-age
# --time-format
# --uppercase

from argparse import (
    Namespace as arg_Namespace,
    ArgumentParser,
    RawTextHelpFormatter,
)
thl-cmk's avatar
thl-cmk committed
from create_topology_utils import (
thl-cmk's avatar
thl-cmk committed
    CREATE_TOPOLOGY_VERSION,
    SCRIPT,
    SAMPLE_SEEDS,
    USER_DATA_FILE,
)


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'
    )
    command_group = parser.add_mutually_exclusive_group()

thl-cmk's avatar
thl-cmk committed
    parser.add_argument(
        '-b', '--backend',
        # nargs='+',
        choices=['LIVESTATUS', 'FILESYSTEM', 'MULTISITE'],
        default='LIVESTATUS',
        help=f'Backend used to retrieve the topology data\n'
             f'LIVESTATUS fetches the data only from the local site. In distributed environments use FILESYSTEM.\n'
             f'For performance reasons LIVESTATUS is the default.',
    )

thl-cmk's avatar
thl-cmk committed
    parser.add_argument(
        '-d', '--default', default=False, action='store_const', const=True,
        help='Set the created topology data as default',
    )
thl-cmk's avatar
thl-cmk committed

thl-cmk's avatar
thl-cmk committed
    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}',
    )
thl-cmk's avatar
thl-cmk committed
    parser.add_argument(
        '-l', '--layers',
        nargs='+',
        choices=['CDP', 'CUSTOM', 'LLDP', 'STATIC'],
        default=['CDP', 'STATIC'],
        help=f'Layers with least significant layer first. Listed layers\n'
             f'will be merged automatically. Default is "-l CDP STATIC"',
    )
thl-cmk's avatar
thl-cmk committed
    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(
        '--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(
        '--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'
thl-cmk's avatar
thl-cmk committed
             f'max will be deleted.\n'
thl-cmk's avatar
thl-cmk committed
             f'NOTE: The default topologies will be always kept.\n'
    )
    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,
thl-cmk's avatar
thl-cmk committed
        help=f'The minimum number of days before a topology is deleted by "--keep".'
thl-cmk's avatar
thl-cmk committed
    )
    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()