Exemple #1
0
    def __exec_save_commands(self, ssh_type: SshTypes,
                             command_list: List[SshCommand]) -> None:
        """Helper method, executes and saves all commands via ssh for all clients of the given type.

        Used cause of the individual save of the results + the verbose print.
        This functionality is not integrated in MethodUtils cause of the missing Influxclient in static context.

        Arguments:
            ssh_type {SshTypes} -- all clients of this type are going to be queried
            command_list {List[SshCommand]} -- list of commands to be executed on all clients.
        """
        result_tuples = MethodUtils.ssh_execute_commands(
            ssh_clients=self.__ssh_clients,
            ssh_type=ssh_type,
            command_list=command_list)
        for (table_name, insert_list) in result_tuples:
            if (self.__verbose):
                MethodUtils.my_print(insert_list)

            self.__influx_client.insert_dicts_to_buffer(
                table_name=table_name, list_with_dicts=insert_list)
    def test_connection(influx_client: InfluxClient, rest_client: RestClient,
                        config_file: Dict[str, Any]):
        if (not config_file):
            raise ValueError("SPPmon does not work without a config file")

        LOGGER.info("Testing all connections required for SPPMon to work")
        working: bool = True  # SPPMon itself will finish sucessfull (no critical errors)
        no_warnings: bool = True  # SPPMon will finish without any warnings (no errors at all)

        # ## InfluxDB ##

        LOGGER.info("> Testing and configuring InfluxDB")
        try:
            influx_client.connect()
            influx_client.disconnect()
            if (not influx_client.use_ssl):
                ExceptionUtils.error_message(
                    "> WARNING: Mandatory SSL is disabled. We hightly recommend to enable it!"
                )
                no_warnings = False

            LOGGER.info("InfluxDB is ready for use")
        except ValueError as error:
            ExceptionUtils.exception_info(
                error,
                extra_message=
                "> Testing of the InfluxDB failed. This is a crictial component of SPPMon."
            )
            working = False

        # ## REST-API ##

        LOGGER.info("> Testing REST-API of SPP.")
        try:
            rest_client.login()
            (version_nr, build_nr) = rest_client.get_spp_version_build()
            LOGGER.info(
                f">> Sucessfully connected to SPP V{version_nr}, build {build_nr}."
            )
            rest_client.logout()
            LOGGER.info("> REST-API is ready for use")
        except ValueError as error:
            ExceptionUtils.exception_info(
                error,
                extra_message=
                "> Testing of the REST-API failed. This is a crictial component of SPPMon."
            )
            working = False

        # ## SSH-CLIENTS ##

        LOGGER.info(
            "> Testing all types of SSH-Clients: Server, VAPDs, vSnaps, Cloudproxy and others"
        )
        ssh_working = True  # The arg --ssh will finish without any error at all

        # Count of clients checks
        ssh_clients: List[SshClient] = SshMethods.setup_ssh_clients(
            config_file)
        if (not ssh_clients):
            ExceptionUtils.error_message(
                ">> No SSH-clients detected at all. At least the server itself should be added for process-statistics."
            )
            ssh_working = False
        else:
            for type in SshTypes:
                if (not list(
                        filter(lambda client: client.client_type == type,
                               ssh_clients))):
                    LOGGER.info(f">> No {type.name} client detected.")

                    if (type == SshTypes.SERVER):
                        ExceptionUtils.error_message(
                            ">> Critical: Without Server as ssh client you wont have any process statistics available. These are a key part of SPPMon."
                        )
                        ssh_working = False  # No error, but still critical

                    if (type == SshTypes.VSNAP):
                        LOGGER.info(
                            ">> WARNING: Without vSnap as ssh client you have no access to storage information. You may add vSnap's for additional monitoring and alerts."
                        )
                        no_warnings = False  # ssh will still work, but thats definitly a warning

            ssh_methods: SshMethods = SshMethods(influx_client, config_file,
                                                 False)
            # Connection check
            LOGGER.info(
                f">> Testing now connection and commands of {len(ssh_clients)} registered ssh-clients."
            )
            for client in ssh_clients:
                try:
                    client.connect()
                    client.disconnect()

                    error_count: int = len(ExceptionUtils.stored_errors)
                    MethodUtils.ssh_execute_commands(
                        ssh_clients=[client],
                        ssh_type=client.client_type,
                        command_list=ssh_methods.client_commands[
                            client.client_type] + ssh_methods.all_command_list)
                    if (len(ExceptionUtils.stored_errors) != error_count):
                        ssh_working = False
                        ExceptionUtils.error_message(
                            f"Not all commands available for client {client.host_name} with type: {client.client_type}.\n"
                            +
                            "Please check manually if the commands are installed and their output."
                        )

                except ValueError as error:
                    ExceptionUtils.exception_info(
                        error,
                        extra_message=
                        f"Connection failed for client {client.host_name} with type: {client.client_type}."
                    )
                    ssh_working = False

        if (ssh_working):
            LOGGER.info("> Testing of SSH-clients sucessfull.")
        else:
            LOGGER.info(
                "> Testing of SSH-clients failed! SPPMon will still work, not all informations are available."
            )
            no_warnings = False

        # #### Conclusion ####

        if (working and no_warnings):
            LOGGER.info(
                "> All components tested sucessfully. SPPMon is ready to be used!"
            )
        elif (working):
            LOGGER.info(
                "> Testing partially sucessful. SPPMon will run, but please check the warnings."
            )
        else:
            LOGGER.info(
                "> Testing failed. SPPMon is not ready to be used. Please fix the connection issues."
            )