def set_critial_configs(self, config_file: Dict[str, Any]) -> None:
        """Sets up any critical infrastructure, to be called within the init.

        Be aware not everything may be initalized on call time.
        Add config here if the system should abort if it is missing.

        Arguments:
            config_file {Dict[str, Any]} -- Opened Config file
        """
        if (not config_file):
            ExceptionUtils.error_message(
                "missing or empty config file, aborting")
            self.exit(error_code=ERROR_CODE_CMD_LINE)
        try:
            # critical components only

            auth_influx = SppUtils.get_cfg_params(param_dict=config_file,
                                                  param_name="influxDB")
            if (not isinstance(auth_influx, dict)):
                raise ValueError("influx config need to be dict")
            self.influx_client = InfluxClient(auth_influx=auth_influx)
            self.influx_client.connect()

        except ValueError as err:
            ExceptionUtils.exception_info(
                error=err,
                extra_message="error while setting up critical config. Aborting"
            )
            self.influx_client = None  # set none cause it does not work.
            self.exit(error_code=ERROR_CODE)
Esempio n. 2
0
    def setup_ssh_clients(config_file: Dict[str, Any]) -> List[SshClient]:
        auth_ssh = SppUtils.get_cfg_params(param_dict=config_file,
                                           param_name="sshclients")

        if (not isinstance(auth_ssh, list)):
            raise ValueError("not a list of sshconfig given", auth_ssh)

        ssh_clients: List[SshClient] = []
        for client_ssh in auth_ssh:
            try:
                ssh_clients.append(SshClient(client_ssh))
            except ValueError as error:
                ExceptionUtils.exception_info(
                    error=error,
                    extra_message=
                    f"Setting up one ssh-client failed, skipping it. Client: \
                    {client_ssh.get('name', 'ERROR WHEN GETTING NAME')}")
        return ssh_clients
Esempio n. 3
0
    def __init__(self, config_file: Dict[str, Any]):
        """Initalize the influx client from a config dict. Call `connect` before using the client.

        Arguments:
            auth_influx {dictonary} -- Dictionary with required parameters.

        Raises:
            ValueError: Raises a ValueError if any important parameters are missing within the file
        """
        if (not config_file):
            raise ValueError(
                "A config file is required to setup the InfluxDB client.")

        auth_influx = SppUtils.get_cfg_params(param_dict=config_file,
                                              param_name="influxDB")
        if (not isinstance(auth_influx, dict)):
            raise ValueError(
                "The InfluxDB config is corrupted within the file: Needs to be a dictionary."
            )

        try:
            self.__user: str = auth_influx["username"]
            self.__password: str = auth_influx["password"]
            self.__use_ssl: bool = auth_influx["ssl"]
            if (self.__use_ssl):
                self.__verify_ssl: bool = auth_influx["verify_ssl"]
            else:
                self.__verify_ssl = False
            self.__port: int = auth_influx["srv_port"]
            self.__address: str = auth_influx["srv_address"]
            self.__database: Database = Database(auth_influx["dbName"])

            # Create table definitions in code
            Definitions.add_table_definitions(self.database)

            self.__metrics_table: Table = self.database['influx_metrics']
        except KeyError as key_error:
            ExceptionUtils.exception_info(error=key_error)
            raise ValueError("Missing Influx-Config arg", str(key_error))

        # declare for later
        self.__client: InfluxDBClient
        self.__version: str
Esempio n. 4
0
    def __init__(self, config_file: Dict[str, Any],
                 initial_connection_timeout: float,
                 pref_send_time: int,
                 request_timeout: int | None,
                 send_retries: int,
                 starting_page_size: int,
                 min_page_size: int,
                 verbose: bool):

        if(not config_file):
            raise ValueError("A config file is required to setup the InfluxDB client.")

        auth_rest = SppUtils.get_cfg_params(
            param_dict=config_file,
            param_name="sppServer")
        if(not isinstance(auth_rest, dict)):
            raise ValueError("The REST-API config is corrupted within the file: Needs to be a dictionary.")

        self.__timeout = request_timeout
        self.__initial_connection_timeout = initial_connection_timeout

        self.__preferred_time = pref_send_time
        self.__page_size = starting_page_size
        self.__min_page_size = min_page_size
        self.__send_retries = send_retries

        self.__verbose = verbose
        try:
            self.__username: str = auth_rest["username"]
            self.__password: str = auth_rest["password"]
            self.__srv_address: str = auth_rest["srv_address"]
            self.__srv_port: int = auth_rest["srv_port"]
        except KeyError as error:
            raise ValueError("Not all REST-API Parameters are given", auth_rest) from error

        self.__sessionid: str = ""
        self.__srv_url: str = ""
Esempio n. 5
0
    def set_optional_configs(self, config_file: Dict[str, Any]) -> None:
        """Sets up any optional infrastructure, to be called within the init.

        Be aware not everything may be initalized on call time.
        Add config here if the system should not abort if it is missing.

        Arguments:
            config_file {Dict[str, Any]} -- Opened Config file
        """

        if (not config_file):
            ExceptionUtils.error_message(
                "missing or empty config file, aborting.")
            self.exit(error_code=ERROR_CODE_START_ERROR)
        if (not self.influx_client):
            ExceptionUtils.error_message(
                "Influx client is somehow missing. aborting")
            self.exit(error_code=ERROR_CODE)

        # ############################ REST-API #####################################
        try:
            ConnectionUtils.verbose = ARGS.verbose
            # ### Loaded Systems part 1/2 ### #
            if (ARGS.minimumLogs or ARGS.loadedSystem):
                # Setting pagesize scaling settings
                ConnectionUtils.timeout_reduction = self.loaded_timeout_reduction
                ConnectionUtils.allowed_send_delta = self.loaded_allowed_send_delta
                ConnectionUtils.max_scaling_factor = self.loaded_max_scaling_factor

                # Setting RestClient request settings.
                self.rest_client = RestClient(
                    config_file=config_file,
                    initial_connection_timeout=self.initial_connection_timeout,
                    pref_send_time=self.loaded_pref_send_time,
                    request_timeout=self.loaded_request_timeout,
                    max_send_retries=self.loaded_max_send_retries,
                    starting_page_size=self.loaded_starting_page_size,
                    min_page_size=self.loaded_min_page_size,
                    verbose=ARGS.verbose)
            else:
                ConnectionUtils.timeout_reduction = self.timeout_reduction
                ConnectionUtils.allowed_send_delta = self.allowed_send_delta
                ConnectionUtils.max_scaling_factor = self.max_scaling_factor

                # Setting RestClient request settings.
                self.rest_client = RestClient(
                    config_file=config_file,
                    initial_connection_timeout=self.initial_connection_timeout,
                    pref_send_time=self.pref_send_time,
                    request_timeout=self.request_timeout,
                    max_send_retries=self.max_send_retries,
                    starting_page_size=self.starting_page_size,
                    min_page_size=self.min_page_size,
                    verbose=ARGS.verbose)

            self.api_queries = ApiQueries(self.rest_client)
            if (not self.ignore_setup):
                # delay the connect into the testing phase
                self.rest_client.login()

        except ValueError as error:
            ExceptionUtils.exception_info(
                error=error,
                extra_message="REST-API is not available due Config error")
            # Required to declare variable
            self.rest_client = None
            self.api_queries = None

        # ######################## System, Job and Hypervisor Methods ##################
        try:
            # explicit ahead due dependency
            self.system_methods = SystemMethods(self.influx_client,
                                                self.api_queries, ARGS.verbose)
        except ValueError as error:
            ExceptionUtils.exception_info(error=error)

        # ### Full Logs ### #
        if (ARGS.fullLogs):
            given_log_types = self.full_joblog_types
        else:
            given_log_types = self.joblog_types

        try:
            auth_rest: Dict[str, Any] = SppUtils.get_cfg_params(
                param_dict=config_file, param_name="sppServer")  # type: ignore
            # TODO DEPRECATED TO BE REMOVED IN 1.1
            self.job_log_retention_time = auth_rest.get(
                "jobLog_rentation",
                auth_rest.get("jobLog_retention", self.job_log_retention_time))
            # TODO New once 1.1 is live
            #self.job_log_retention_time = auth_rest.get("jobLog_retention", self.job_log_retention_time)

            self.job_methods = JobMethods(self.influx_client, self.api_queries,
                                          self.job_log_retention_time,
                                          given_log_types, ARGS.verbose)
        except ValueError as error:
            ExceptionUtils.exception_info(error=error)

        try:
            # dependen on system methods
            self.protection_methods = ProtectionMethods(
                self.system_methods, self.influx_client, self.api_queries,
                ARGS.verbose)
        except ValueError as error:
            ExceptionUtils.exception_info(error=error)

        # ############################### SSH #####################################
        if (self.ssh and not self.ignore_setup):
            try:
                # set from None to methods once finished
                self.ssh_methods = SshMethods(influx_client=self.influx_client,
                                              config_file=config_file,
                                              verbose=ARGS.verbose)

            except ValueError as error:
                ExceptionUtils.exception_info(
                    error=error,
                    extra_message=
                    "SSH-Commands are not available due Config error")
                # Variable needs to be declared
                self.ssh_methods = None
        else:
            # Variable needs to be declared
            self.ssh_methods = None
    def set_optional_configs(self, config_file: Dict[str, Any]) -> None:
        """Sets up any optional infrastructure, to be called within the init.

        Be aware not everything may be initalized on call time.
        Add config here if the system should not abort if it is missing.

        Arguments:
            config_file {Dict[str, Any]} -- Opened Config file
        """

        if (not config_file):
            ExceptionUtils.error_message(
                "missing or empty config file, aborting.")
            self.exit(error_code=ERROR_CODE_CMD_LINE)

        # ############################ REST-API #####################################
        try:
            auth_rest = SppUtils.get_cfg_params(param_dict=config_file,
                                                param_name="sppServer")

            if (not isinstance(auth_rest, dict)):
                raise ValueError("sppServer config need to be dict")

            self.job_log_retention_time = auth_rest.get(
                "jobLog_rentation", "60d")

            ConnectionUtils.verbose = OPTIONS.verbose
            ConnectionUtils.timeout_reduction = self.timeout_reduction
            ConnectionUtils.allowed_time_diff_quota = self.allowed_time_diff_quota
            ConnectionUtils.maximum_increase_pagesize = self.maximum_increase_pagesize

            if (OPTIONS.minimumLogs):
                rest_time_out = self.minimum_timeout
                rest_preferred_time = self.loaded_preferred_time
            else:
                rest_time_out = self.default_timeout
                rest_preferred_time = self.preferred_time

            self.rest_client = RestClient(auth_rest, rest_time_out,
                                          rest_preferred_time, self.page_size,
                                          self.min_page_size,
                                          self.send_retries, OPTIONS.verbose)

            self.api_queries = ApiQueries(self.rest_client)
            self.rest_client.login()

        except ValueError as error:
            ExceptionUtils.exception_info(
                error=error,
                extra_message="REST-API is not available due Config error")
            self.rest_client = None
            self.api_queries = None

        # ######################## System, Job and Hypervisor Methods ##################
        try:
            # explicit ahead due dependency
            self.system_methods = SystemMethods(self.influx_client,
                                                self.api_queries,
                                                OPTIONS.verbose)
        except ValueError as error:
            ExceptionUtils.exception_info(error=error)

        try:
            self.job_methods = JobMethods(self.influx_client, self.api_queries,
                                          self.job_log_retention_time,
                                          self.minLogs_joblog_type,
                                          self.default_joblog_type,
                                          OPTIONS.verbose, OPTIONS.minimumLogs)
        except ValueError as error:
            ExceptionUtils.exception_info(error=error)

        try:
            # dependen on system methods
            self.hypervisor_methods = ProtectionMethods(
                self.system_methods, self.influx_client, self.api_queries,
                OPTIONS.verbose)
        except ValueError as error:
            ExceptionUtils.exception_info(error=error)

        # ############################### SSH #####################################
        if (self.ssh or self.process_stats):
            try:

                auth_ssh = SppUtils.get_cfg_params(param_dict=config_file,
                                                   param_name="sshclients")

                ssh_clients: List[SshClient] = []
                if (not isinstance(auth_ssh, list)):
                    raise ValueError("not a list of sshconfig given", auth_ssh)

                for client_ssh in auth_ssh:
                    try:
                        ssh_clients.append(SshClient(client_ssh))
                    except ValueError as error:
                        ExceptionUtils.exception_info(
                            error=error,
                            extra_message=
                            f"Setting up one client failed, skipping it. Client: \
                            {client_ssh.get('name', 'ERROR WHEN GETTING NAME')}"
                        )

                # set from None to methods once finished
                self.ssh_methods = SshMethods(influx_client=self.influx_client,
                                              ssh_clients=ssh_clients,
                                              verbose=OPTIONS.verbose)

            except ValueError as error:
                ExceptionUtils.exception_info(
                    error=error,
                    extra_message=
                    "SSH-Commands are not available due Config error")