diff --git a/tests/resources/Broker.py b/tests/resources/Broker.py index 460dd601473..abac5b0f370 100755 --- a/tests/resources/Broker.py +++ b/tests/resources/Broker.py @@ -420,6 +420,13 @@ def _apply_conf(name, callback): def config_broker(name, poller_inst: int = 1): + """ + Configure a broker instance for test. Write the configuration files. + + Args: + name (str): name of the conf broker wanted + poller_inst (int, optional): Defaults to 1. + """ makedirs(ETC_ROOT, mode=0o777, exist_ok=True) makedirs(VAR_ROOT, mode=0o777, exist_ok=True) makedirs(ETC_ROOT + "/centreon-broker", mode=0o777, exist_ok=True) @@ -470,6 +477,12 @@ def config_broker(name, poller_inst: int = 1): def change_broker_tcp_output_to_grpc(name: str): + """ + Update broker configuration to use a gRPC output instead of a TCP one. + + Args: + name (str): name of the conf broker wanted to be changed + """ def output_to_grpc(conf): output_dict = conf["centreonBroker"]["output"] for i, v in enumerate(output_dict): @@ -479,6 +492,14 @@ def output_to_grpc(conf): def add_path_to_rrd_output(name: str, path: str): + """ + Set the path for the rrd output. If no rrd output is defined, this function + does nothing. + + Args: + name (str): The broker instance name among central, rrd and module%d. + path (str): path to the rrd output. + """ def rrd_output(conf): output_dict = conf["centreonBroker"]["output"] for i, v in enumerate(output_dict): @@ -488,6 +509,13 @@ def rrd_output(conf): def change_broker_tcp_input_to_grpc(name: str): + """ + Update the broker configuration to use gRPC input instead of a TCP one. + If no tcp input is found, no replacement is done. + + Args: + name: The broker instance name among central, rrd and module%d. + """ def input_to_grpc(conf): input_dict = conf["centreonBroker"]["input"] for i, v in enumerate(input_dict): @@ -506,6 +534,18 @@ def add_broker_crypto(json_dict, add_cert: bool, only_ca_cert: bool): def add_broker_tcp_input_grpc_crypto(name: str, add_cert: bool, reversed: bool): + """ + Add some crypto to broker gRPC input. + + Args: + name: The broker instance name among central, rrd and module%d. + add_cert (bool): True to add a certificate, False otherwise. + reversed (bool): True if only a CA certificate is provided. + + *Example:* + + | Add Broker Tcp Input Grpc Crypto | central | ${True} | ${False} | + """ def crypto_modifier(conf): input_dict = conf["centreonBroker"]["input"] for i, v in enumerate(input_dict): @@ -515,6 +555,18 @@ def crypto_modifier(conf): def add_broker_tcp_output_grpc_crypto(name: str, add_cert: bool, reversed: bool): + """ + Add grpc crypto to broker tcp output + + Args: + name: The broker instance name among central, rrd and module%d. + add_cert (bool): True to add a certificate, False otherwise. + reversed (bool): False if only a we just want a CA certificate. + + *Example:* + + | Add Broker Tcp Output Grpc Crypto | module0 | ${True} | ${False} | + """ def crypto_modifier(conf): input_dict = conf["centreonBroker"]["output"] for i, v in enumerate(input_dict): @@ -524,6 +576,19 @@ def crypto_modifier(conf): def add_host_to_broker_output(name: str, output_name: str, host_ip: str): + """ + Add a host to some broker output. This is useful for a grpc or tcp client + where we want where to connect to. + + Args: + name (str): The broker instance name among central, rrd and module%d. + output_name (str): The name of the output to modify. + host_ip (str): the host address to set. + + *Example:* + + | Add Host To Broker Output | module0 | central-module-master-output | localhost | + """ def modifier(conf): input_dict = conf["centreonBroker"]["output"] for i, v in enumerate(input_dict): @@ -533,6 +598,19 @@ def modifier(conf): def add_host_to_broker_input(name: str, input_name: str, host_ip: str): + """ + Add host to some broker input. This is useful for a grpc or tcp client + where we want to set where to connect to. + + Args: + name: The broker instance name among central, rrd and module%d. + input_name (str): the name of the input to modify. + host_ip (str): the host address to set. + + *Example:* + + | Add Host To Broker Input | central | central-broker-master-input | localhost | + """ def modifier(conf): input_dict = conf["centreonBroker"]["input"] for i, v in enumerate(input_dict): @@ -542,6 +620,17 @@ def modifier(conf): def remove_host_from_broker_output(name: str, output_name: str): + """ + Remove the host entry from a broker output given by its name. + + Args: + name: The broker instance name among central, rrd and module%d. + output_name (str): The name of the output containing a host entry. + + *Example:* + + | Remove Host From Broker Output | module0 | central-module-master-output | + """ def modifier(conf): input_dict = conf["centreonBroker"]["output"] for i, v in enumerate(input_dict): @@ -551,6 +640,17 @@ def modifier(conf): def remove_host_from_broker_input(name: str, input_name: str): + """ + Remove the host entry from a broker input given by its name. + + Args: + name: The broker instance name among central, rrd and module%d. + input_name (str): The name of the input containing a host entry. + + *Example:* + + | Remove Host From Broker Input | central | central-broker-master-input | + """ def modifier(conf): input_dict = conf["centreonBroker"]["input"] for i, v in enumerate(input_dict): @@ -560,6 +660,18 @@ def modifier(conf): def change_broker_compression_output(config_name: str, output_name: str, compression_value: str): + """ + Change the compression option of a broker output. + + Args: + config_name (str): The broker instance name among central, rrd and module%d. + output_name (str): The output name to modify. + compression_value (str): The compression value. "yes/no", "1/0" or "true/false". + + *Example:* + + | Change Broker Compression Output | module0 | central-module-master-output | yes | + """ def compression_modifier(conf): output_dict = conf["centreonBroker"]["output"] for i, v in enumerate(output_dict): @@ -569,6 +681,18 @@ def compression_modifier(conf): def change_broker_compression_input(config_name: str, input_name: str, compression_value: str): + """ + Change the compression option of a broker input. + + Args: + config_name (str): The broker instance name among central, rrd and module%d. + input_name (str): The input name to modify. + compression_value (str): The compression value: "yes/no", "1/0" or "true/false". + + *Example:* + + | Change Broker Compression Input | central | central-broker-master-input | yes | + """ def compression_modifier(conf): input_dict = conf["centreonBroker"]["input"] for i, v in enumerate(input_dict): @@ -578,6 +702,23 @@ def compression_modifier(conf): def config_broker_bbdo_input(name, stream, port, proto, host=None): + """ + Configure Broker BBDO input. It can be a client or a server. We provide a + port number and a protocol that is grpc or tcp. + + Args: + name: The broker instance name among central, rrd and module%d. + stream: The type of stream among [bbdo_server, bbdo_client]. + port: A port number. + proto: grpc or tcp. + host (str, optional): Defaults to None. Used to provide a host, needed + in the case of bbdo_client. + + *Example:* + + | Config Broker Bbdo Input | central | bbdo_server | 5669 | grpc | | + | Config Broker Bbdo Input | rrd | bbdo_client | 5670 | tcp | localhost | + """ if stream != "bbdo_server" and stream != "bbdo_client": raise Exception( "config_broker_bbdo_input_output() function only accepts stream in ('bbdo_server', 'bbdo_client')") @@ -614,6 +755,22 @@ def config_broker_bbdo_input(name, stream, port, proto, host=None): def config_broker_bbdo_output(name, stream, port, proto, host=None): + """ + Configure Broker BBDO output. It can be a client or a server. We provide a + port number and a protocol that is grpc or tcp. + + Args: + name: The broker instance name among central, rrd and module%d. + stream (str): The type of stream among [bbdo_server, bbdo_client]. + port (int): A port number. + proto (str): grpc or tcp. + host (str, optional): Defaults to None. Used to provide a host to connect, + needed in the case of bbdo_client. + + *Example:* + + | Config Broker Bbdo Output | central | bbdo_client | 5670 | tcp | localhost | + """ if stream != "bbdo_server" and stream != "bbdo_client": raise Exception( "config_broker_bbdo_output() function only accepts stream in ('bbdo_server', 'bbdo_client')") @@ -650,6 +807,14 @@ def config_broker_bbdo_output(name, stream, port, proto, host=None): def config_broker_sql_output(name, output, queries_per_transaction: int = 20000): + """ + Configure the broker sql output. + + Args: + name (str): The broker instance name among central, rrd and module%d. + output (str): One string among "unified_sql" and "sql/perfdata". + queries_per_transaction (int, optional): Defaults to 20000. + """ if name == 'central': filename = "central-broker.json" elif name.startswith('module'): @@ -729,6 +894,18 @@ def config_broker_sql_output(name, output, queries_per_transaction: int = 20000) def broker_config_clear_outputs_except(name, ex: list): + """ + Remove all the outputs of the broker configuration except those of types given + in the ex list. + + Args: + name: The broker instance name among central, rrd and module%d. + ex (list): A list of type. + + *Example:* + + | Broker Config Clear Outputs Except | central | ["sql", "storage"] | + """ if name == 'central': filename = "central-broker.json" elif name.startswith('module'): @@ -751,6 +928,18 @@ def broker_config_clear_outputs_except(name, ex: list): def broker_config_add_item(name, key, value): + """ + Add an item to the broker configuration + + Args: + name (str): Which broker instance: central, rrd or module%d. + key (str): The key to add directly in the configuration first level. + value: The value. + + *Example:* + + | Broker Config Add Item | module0 | bbdo_version | 3.0.1 | + """ if name == 'central': filename = "central-broker.json" elif name == 'rrd': @@ -769,6 +958,17 @@ def broker_config_add_item(name, key, value): def broker_config_remove_item(name, key): + """ + Remove an item from the broker configuration + + Args: + name: The broker instance name among central, rrd and module%d + key: The key to remove. It must be defined at the first level of the configuration. + + *Example:* + + | Broker Config Remove Item | module0 | bbdo_version | + """ if name == 'central': filename = "central-broker.json" elif name == 'rrd': @@ -787,6 +987,18 @@ def broker_config_remove_item(name, key): def broker_config_add_lua_output(name, output, luafile): + """ + Add a lua output to the broker configuration. + + Args: + name (str): The broker instance name among central, rrd, module%d + output (str): The name of the Lua output. + luafile (str): The full name of the Lua script. + + *Example:* + + | Broker Config Add Lua Output | central | test-protobuf | /tmp/lua.lua | + """ if name == 'central': filename = "central-broker.json" elif name.startswith('module'): @@ -810,6 +1022,19 @@ def broker_config_add_lua_output(name, output, luafile): def broker_config_output_set(name, output, key, value): + """ + Set an attribute value in a broker output. + + Args: + name (str): The broker instance among central, rrd, module%d. + output (str): The output to work with. + key (str): The key whose value is to modify. + value (str): The new value to set. + + *Example:* + + | Broker Config Output Set | central | central-broker-master-sql | host | localhost | + """ if name == 'central': filename = "central-broker.json" elif name.startswith('module'): @@ -829,6 +1054,19 @@ def broker_config_output_set(name, output, key, value): def broker_config_output_set_json(name, output, key, value): + """ + Set an attribute value in a broker output. The value is given as a json string. + + Args: + name (str): The broker instance among central, rrd, module%d. + output (str): The output to work with. + key (str): The key whose value is to modify. + value (str): The new value to set. + + *Example:* + + | Broker Config Output Set Json | central | central-broker-master-sql | filters | {"category": ["neb", "foo", "bar"]} | + """ if name == 'central': filename = "central-broker.json" elif name.startswith('module'): @@ -849,6 +1087,18 @@ def broker_config_output_set_json(name, output, key, value): def broker_config_output_remove(name, output, key): + """ + Remove a key from an output of the broker configuration. + + Args: + name: The broker instance among central, rrd, module%d. + output: The output to work with. + key: The key to remove. + + *Example:* + + | Broker Config Output Remove | central | centreon-broker-master-rrd | host | + """ if name == 'central': filename = "central-broker.json" elif name.startswith('module'): @@ -869,6 +1119,19 @@ def broker_config_output_remove(name, output, key): def broker_config_input_set(name, inp, key, value): + """ + Set an attribute in an input of a broker configuration. + + Args: + name (str): The broker instance among central, rrd, module%d. + inp (str): The input to work with. + key (str): The key whose value is to modify. + value (str): The new value to set. + + *Example:* + + | Broker Config Input Set | rrd | rrd-broker-master-input | encryption | yes | + """ if name == 'central': filename = "central-broker.json" elif name.startswith('module'): @@ -888,6 +1151,14 @@ def broker_config_input_set(name, inp, key, value): def broker_config_input_remove(name, inp, key): + """ + Remove a key from an input of the broker configuration. + + Args: + name: The broker instance among central, rrd, module%d. + inp: The input to work with. + key: The key to remove. + """ if name == 'central': filename = "central-broker.json" elif name.startswith('module'): @@ -908,6 +1179,18 @@ def broker_config_input_remove(name, inp, key): def broker_config_log(name, key, value): + """ + Configure broker log level. + + Args: + name (str): The broker instance among central, rrd, module%d. + key (str): The logger name to modify. + value (str): The level among error, trace, info, debug, etc... + + *Example:* + + | Broker Config Log | central | bam | trace | + """ if name == 'central': filename = "central-broker.json" elif name.startswith('module'): @@ -926,6 +1209,17 @@ def broker_config_log(name, key, value): def broker_config_flush_log(name, value): + """ + Configure the flush interval of the broker loggers. This value is in seconds, with 0, every logs are flushed. + + Args: + name (str): the broker instance among central, rrd, module%d. + value (int): The value in seconds. + + *Example:* + + | Broker Config Flush Log | central | 1 | + """ if name == 'central': filename = "central-broker.json" elif name.startswith('module'): @@ -944,6 +1238,17 @@ def broker_config_flush_log(name, value): def broker_config_source_log(name, value): + """ + Configure if logs should contain the source file and its line number. + + Args: + name: The broker instance name among central, rrd and module%d. + value: A boolean that can be "true/false", "1/0" or "yes/no". + + *Example:* + + | Broker Config Source Log | central | 1 | + """ if name == 'central': filename = "central-broker.json" elif name.startswith('module'): @@ -962,6 +1267,23 @@ def broker_config_source_log(name, value): def check_broker_stats_exist(name, key1, key2, timeout=TIMEOUT): + """ + Return True if the Broker stats file contain keys pair (key1,key2). key2 must + be a daughter key of key1. + + Should be true if the poller is connected to the central broker. + + Args: + name: The broker instance name among central, rrd and module%d. + key1 (str): A key at first level. + key2 (str): A key under the key1 key. + timeout (int, optional): . Defaults to TIMEOUT. + + *Example:* + + | ${exist} | Check Broker Stats Exist | mysql manager | poller | waiting tasks in connection 0 | + | Should Be True | ${exist} | + """ limit = time.time() + timeout while time.time() < limit: if name == 'central': @@ -989,6 +1311,18 @@ def check_broker_stats_exist(name, key1, key2, timeout=TIMEOUT): def get_broker_stats_size(name, key, timeout=TIMEOUT): + """ + Return the number of items under the given key in the stats file. + + Args: + name: The broker instance name among central, rrd and module%d. + key: The key to work with. + timeout (int, optional): Defaults to TIMEOUT = 30s. + + *Example:* + + | ${size} | Get Broker Stats Size | central | poller | # 2 | + """ limit = time.time() + timeout retval = 0 while time.time() < limit: @@ -1022,13 +1356,16 @@ def get_broker_stats_size(name, key, timeout=TIMEOUT): def get_broker_stats(name: str, expected: str, timeout: int, *keys): - """! - read a value from broker stats - @param name central, module or rrd - @param expected: value expected (regexp) - @timeout delay to find key in stats - @param keys keys in json stats output - @return True if value found and matches expected + """ + Read a value from the broker stats file following the given keys. If the value is the expected one, return True. + + Args: + name The broker instance to work with among central, module%d or rrd + expected: value expected (regexp) + timeout: duration in seconds after what the check fails. + keys: keys in json stats output + + Returns: True if the expected value was found, otherwise it returns False. """ def json_get(json_dict, keys: tuple, index: int): @@ -1069,14 +1406,21 @@ def json_get(json_dict, keys: tuple, index: int): return False -## -# @brief Gets count indexes that does not exist in index_data. -# -# @param count:int The number of indexes to get. -# -# @return a list of index ids. -# def get_not_existing_indexes(count: int): + """ + Gets count indexes that does not exist in the centreon_storage.index_data table. + + Args: + count (int): The number of indexes to get. + + *Example:* + + | @{indexes} | Get Not Existing Indexes | 10 | + | Log To Console | @{indexes} | + + Returns: + a list of index IDs. + """ # Connect to the database connection = pymysql.connect(host=DB_HOST, user=DB_USER, @@ -1107,14 +1451,20 @@ def get_not_existing_indexes(count: int): return ids_db -## -# @brief Gets count indexes from available ones. -# -# @param count:int The number of indexes to get. -# -# @return a list of index ids. -# def get_indexes_to_delete(count: int): + """ + Gets count indexes from centreon_storage.index_data that really exist. + + Args: + count (int): The number of indexes to get. + + *Example:* + + | @{indexes} | Get Not Existing Indexes | 10 | + + Returns: + A list of index IDs. + """ files = [os.path.basename(x) for x in glob.glob( VAR_ROOT + "/lib/centreon/metrics/[0-9]*.rrd")] ids = [int(f.split(".")[0]) for f in files] @@ -1148,7 +1498,8 @@ def get_indexes_to_delete(count: int): def delete_all_rrd_metrics(): - """! remove all rrd metrics files + """ + Remove all rrd metrics files. """ with os.scandir(VAR_ROOT + "/lib/centreon/metrics/") as it: for entry in it: @@ -1157,12 +1508,19 @@ def delete_all_rrd_metrics(): def check_rrd_info(metric_id: int, key: str, value, timeout: int = 60): - """! execute rrdtool info and check one value of the returned informations - @param metric_id - @param key key to search in the rrdtool info result - @param value value to search in the rrdtool info result fot key - @param timeout timeout for metric file creation - @return True if key = value found + """ + Execute rrdtool info and check one value of the returned informations + + Args: + metric_id (int): A metric ID. + key (str): The key whose value is to check. + value: The expected value. + timeout (int, optional): Defaults to 60. + + *Example:* + + | ${result} | Check Rrd Info | 1 | step | 60 | + | Should Be True | ${result} | """ limit = time.time() + timeout @@ -1180,11 +1538,23 @@ def check_rrd_info(metric_id: int, key: str, value, timeout: int = 60): def get_metrics_for_service(service_id: int, metric_name: str = "%", timeout: int = 60): - """! scan data base every 5s to extract metric ids for a service + """ + Try to get the metric IDs of a service. + + Warning: + A service is identified by a host ID and a service ID. This function should be used with caution. + + Args: + service_id (int): The ID of the service. + metric_name (str, optional): Defaults to "%". + timeout (int, optional): Defaults to 60. + + Returns: + A list of metric IDs or None if no metric found. + + *Example:* - @param service_id id of the service - @param timeout timeout in second - @return array of metric ids + | ${metrics} | Get Metrics For Service | 1 | % | """ limit = time.time() + timeout @@ -1211,14 +1581,20 @@ def get_metrics_for_service(service_id: int, metric_name: str = "%", timeout: in return None -## -# @brief Gets count metrics that does not exist. -# -# @param count:int The number of metrics to get. -# -# @return a list of metric ids. -# def get_not_existing_metrics(count: int): + """ + Return a list of metrics that does not exist. + + Args: + count (int): How many metric IDs do we want. + + Returns: + A list of IDs. + + *Example:* + + | @{metrics} | Get Not Existing Metrics | 10 | + """ files = [os.path.basename(x) for x in glob.glob( VAR_ROOT + "/lib/centreon/metrics/[0-9]*.rrd")] ids = [int(f.split(".")[0]) for f in files] @@ -1249,14 +1625,16 @@ def get_not_existing_metrics(count: int): return retval -## -# @brief Gets count metrics from available ones. -# -# @param count:int The number of metrics to get. -# -# @return a list of metric ids. -# def get_metrics_to_delete(count: int): + """ + Get count metrics from availables ones. + + Args: + count (int): The number of metrics to get. + + Returns: + A list of metric IDs. + """ files = [os.path.basename(x) for x in glob.glob( VAR_ROOT + "/lib/centreon/metrics/[0-9]*.rrd")] ids = [int(f.split(".")[0]) for f in files] @@ -1281,12 +1659,13 @@ def get_metrics_to_delete(count: int): return inter[:count] -## -# @brief creat metrics from available ones. -# -# @param count:int The number of metrics to create. -# def create_metrics(count: int): + """ + Create count metrics from available ones. + + Args: + count (int): How many metrics to create. + """ files = [os.path.basename(x) for x in glob.glob( VAR_ROOT + "/lib/centreon/metrics/[0-9]*.rrd")] ids = [int(f.split(".")[0]) for f in files] @@ -1330,6 +1709,14 @@ def create_metrics(count: int): def run_reverse_bam(duration, interval): + """ + Launch the map_client.py script that simulates map. + + Args: + duration: The duration in seconds before to stop map_client.py + interval: Interval given to the map_client.py that tells the duration + between to recv calls. + """ subp.Popen("broker/map_client.py {:f}".format(interval), shell=True, stdout=subp.PIPE, stdin=subp.PIPE) time.sleep(duration) @@ -1337,17 +1724,36 @@ def run_reverse_bam(duration, interval): def start_map(): + """ + Launch the map_client_types.py script that simulates map. + """ global map_process map_process = subp.Popen("broker/map_client_types.py", shell=True, stdout=subp.DEVNULL, stdin=subp.DEVNULL) def clear_map_logs(): + """ + Reset the content of the /tmp/map-output.log file. + """ with open('/tmp/map-output.log', 'w') as f: f.write("") def check_map_output(categories_str, expected_events, timeout: int = TIMEOUT): + """ + Check the content of the /tmp/map-output.log file. This file contains informations on categories/elements of each + received event. A list of categories and event types are given to this function, so it can check if the file + contain these types of events. If it contains them, True is returned. + + Args: + categories_str: A list of categories, for example ["1", "2"]. + expected_events: A list of event types. + timeout (int, optional): A number of seconds, the default value is TIMEOUT. + + Returns: + True on success, otherwise False. + """ retval = False limit = time.time() + timeout while time.time() < limit: @@ -1394,11 +1800,20 @@ def check_map_output(categories_str, expected_events, timeout: int = TIMEOUT): def get_map_output(): + """ + The map_client_types.py script writes on STDOUT. This function allows to get this output. + + Returns: + A string containing the output. + """ global map_process return map_process.communicate()[0] def stop_map(): + """ + Stop the script simulating map. Works with map_client_type. + """ for proc in psutil.process_iter(): if 'map_client_type' in proc.name(): logger.console( @@ -1420,13 +1835,17 @@ def stop_map(): logger.console("map_client_type stopped") -## -# @brief Get count indexes that are available to rebuild them. -# -# @param count is the number of indexes to get. -# -# @return a list of indexes def get_indexes_to_rebuild(count: int, nb_day=180): + """ + Get count indexes that are available to rebuild. + + Args: + count (int): The number of indexes to get. + nb_day (int, optional): Defaults to 180. + + Returns: + A list of indexes. + """ files = [os.path.basename(x) for x in glob.glob( VAR_ROOT + "/lib/centreon/metrics/[0-9]*.rrd")] ids = [int(f.split(".")[0]) for f in files] @@ -1470,12 +1889,13 @@ def get_indexes_to_rebuild(count: int, nb_day=180): return retval -## -# @brief add a value at the mid of the first day of each metric -# -# -# @return a list of indexes of pair