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_classes.py 5.24 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_classes.py


from os import environ
from time import strftime
thl-cmk's avatar
thl-cmk committed
from typing import Dict, List, Any, NamedTuple
from create_topology_utils import lldp_path, lldp_columns, lldp_label
thl-cmk's avatar
thl-cmk committed


class InventoryColumns(NamedTuple):
    neighbour: str
    local_port: str
    neighbour_port: str


thl-cmk's avatar
thl-cmk committed
class Interface(NamedTuple):
    host: str
    index: str
    name: str
    description: str
    alias: str


class StaticConnection(NamedTuple):
    host: str
    local_port: str
    neighbour_port: str
    neighbour: str
    label: str


thl-cmk's avatar
thl-cmk committed
class Settings:
    def __init__(
            self,
            cli_args: Dict[str, Any],
    ):
thl-cmk's avatar
thl-cmk committed
        self.__omd_root = environ['OMD_ROOT']
thl-cmk's avatar
thl-cmk committed
        self.__topology_save_path = 'var/topology_data'
        self.__topology_file_name = 'network_data.json'
        self.__path_to_if_table = 'networking,interfaces'
        self.__inventory_path = 'var/check_mk/inventory'
        self.__autochecks_path = 'var/check_mk/autochecks'

thl-cmk's avatar
thl-cmk committed
        self.__settings = {
thl-cmk's avatar
thl-cmk committed
            'seed_devices': None,
            'path_in_inventory': 'networking,cdp_cache',
            'inventory_columns': 'device_id,local_port,device_port',
            'data_source': 'inv_CDP',
            'time_format': '%Y-%m-%dT%H:%M:%S.%m',
            'user_data_file': f'{self.__omd_root}/local/bin/topology_data/create_topology_data.toml',
            'output_directory': None,
            'make_default': False,
            'keep_domain': False,
            'lowercase': False,
            'uppercase': False,
            'debug': False,
            'lldp': False,
            'version': False,
thl-cmk's avatar
thl-cmk committed
            'dont_compare': False,
            'keep_max': None,
            'min_age': None,
thl-cmk's avatar
thl-cmk committed
        }
        # args in the form {'s, __seed_devices': 'C9540-7-1', 'p, __path_in_inventory': None, ... }}
        # we will remove 's, __'
        self.__args = ({k.split(',')[-1].strip(' ').strip('_'): v for k, v in cli_args.items() if v})
thl-cmk's avatar
thl-cmk committed
        # first set values
        if self.__args.get('lldp'):
            self.__settings['data_source'] = lldp_label
            self.__settings['path_in_inventory'] = lldp_path
            self.__settings['inventory_columns'] = lldp_columns
thl-cmk's avatar
thl-cmk committed
        # Then update values with cli values
thl-cmk's avatar
thl-cmk committed
        self.__settings.update(self.__args)

    @property
    def version(self) -> bool:
        return self.__settings['version']

thl-cmk's avatar
thl-cmk committed
    @property
    def keep_max(self) -> int | None:
        if self.__settings['keep_max']:
thl-cmk's avatar
thl-cmk committed
            return max(self.__settings['keep_max'], 1)
thl-cmk's avatar
thl-cmk committed

    @property
    def min_age(self) -> int:
        if self.__settings['min_age']:
thl-cmk's avatar
thl-cmk committed
            return max(self.__settings['min_age'], 1)
thl-cmk's avatar
thl-cmk committed
        else:
            return 14

thl-cmk's avatar
thl-cmk committed
    @property
    def lldp(self) -> bool:
        return self.__settings['lldp']

    @property
    def debug(self) -> bool:
        return self.__settings['debug']

thl-cmk's avatar
thl-cmk committed
    @property
    def dont_compare(self) -> bool:
        return self.__settings['dont_compare']

thl-cmk's avatar
thl-cmk committed
    @property
    def make_default(self) -> bool:
        return self.__settings['make_default']

thl-cmk's avatar
thl-cmk committed
    @property
    def keep_domain(self) -> bool:
        return self.__settings['keep_domain']

    @property
    def uppercase(self) -> bool:
        return self.__settings['uppercase']

    @property
    def lowercase(self) -> bool:
        return self.__settings['lowercase']

thl-cmk's avatar
thl-cmk committed
    @property
    def time_format(self) -> str:
        return self.__settings['time_format']

    @property
    def omd_root(self) -> str:
thl-cmk's avatar
thl-cmk committed
        return self.__omd_root
thl-cmk's avatar
thl-cmk committed

    @property
    def inventory_path(self) -> str:
thl-cmk's avatar
thl-cmk committed
        return self.__inventory_path
thl-cmk's avatar
thl-cmk committed

    @property
    def autochecks_path(self) -> str:
thl-cmk's avatar
thl-cmk committed
        return self.__autochecks_path

    @property
    def user_data_file(self) -> str:
        return self.__settings['user_data_file']
thl-cmk's avatar
thl-cmk committed

    @property
    def topology_save_path(self) -> str:
thl-cmk's avatar
thl-cmk committed
        return self.__topology_save_path
thl-cmk's avatar
thl-cmk committed

    @property
    def topology_file_name(self) -> str:
thl-cmk's avatar
thl-cmk committed
        return self.__topology_file_name
thl-cmk's avatar
thl-cmk committed

    @property
    def path_in_inventory(self) -> List[str]:
        path = ('Nodes,' + ',Nodes,'.join(self.__settings['path_in_inventory'].split(',')) + ',Table,Rows').split(',')
        return path

thl-cmk's avatar
thl-cmk committed
    @property
    def path_to_if_table(self) -> List[str]:
        path = ('Nodes,' + ',Nodes,'.join(self.__path_to_if_table.split(',')) + ',Table,Rows').split(',')
        return path

thl-cmk's avatar
thl-cmk committed
    @property
    def inventory_columns(self) -> InventoryColumns:
        columns = self.__settings['inventory_columns'].split(',')
        return InventoryColumns(
            neighbour=columns[0],
            local_port=columns[1],
            neighbour_port=columns[2],
        )

    @property
    def seed_devices(self) -> List[str]:
        if self.__settings['seed_devices']:
thl-cmk's avatar
thl-cmk committed
            return self.__settings['seed_devices']
thl-cmk's avatar
thl-cmk committed
        else:
            return []

    @property
    def data_source(self) -> str:
        return self.__settings['data_source']

    @property
    def output_directory(self) -> str:
        if not self.__settings['output_directory']:
            return f'{strftime(self.__settings["time_format"])}'
        else:
            return self.__settings['output_directory']