diff --git a/CHANGELOG.md b/CHANGELOG.md index af08aaf6e2..f9f25706f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,7 @@ All notable changes to this project will be documented in this file. ### Fixed +- Fix E2E Vulnerability Detection monitoring function ([#4959](https://github.com/wazuh/wazuh-qa/pull/4959)) \- (Framework) - Fixed Filebeat provisioning role with pre-release and staging URLs ([#4950](https://github.com/wazuh/wazuh-qa/pull/4950)) \- (Framework) - Fix macOS Vulnerability Detection handler provision in E2E tests ([#4948](https://github.com/wazuh/wazuh-qa/pull/4948)) \- (Framework) - Migrate Vulnerability Detection timeouts variables to the waiters module ([#4949](https://github.com/wazuh/wazuh-qa/pull/4949)) \- (Framework) diff --git a/deps/wazuh_testing/wazuh_testing/end_to_end/monitoring.py b/deps/wazuh_testing/wazuh_testing/end_to_end/monitoring.py index 915ac8e112..72ebb895ce 100644 --- a/deps/wazuh_testing/wazuh_testing/end_to_end/monitoring.py +++ b/deps/wazuh_testing/wazuh_testing/end_to_end/monitoring.py @@ -26,14 +26,18 @@ from wazuh_testing.tools.system import HostManager -def monitoring_events_multihost(host_manager: HostManager, monitoring_data: Dict, ignore_error: bool = False) -> Dict: +DEFAULT_SCAN_INTERVAL = 5 + + +def monitoring_events_multihost(host_manager: HostManager, monitoring_data: Dict, ignore_timeout_error: bool = True, + scan_interval: int = DEFAULT_SCAN_INTERVAL) -> Dict: """ Monitor events on multiple hosts concurrently. Args: host_manager: An instance of the HostManager class containing information about hosts. monitoring_data: A dictionary containing monitoring data for each host. - ignore_error: If True, ignore errors and continue monitoring. + ignore_timeout_error: If True, ignore TimeoutError and return the result. Returns: dict: A dictionary containing the monitoring results. @@ -61,8 +65,9 @@ def monitoring_events_multihost(host_manager: HostManager, monitoring_data: Dict } } """ - def monitoring_event(host_manager: HostManager, host: str, monitoring_elements: List[Dict], scan_interval: int = 20, - ignore_error: bool = False) -> Dict: + def monitoring_event(host_manager: HostManager, host: str, monitoring_elements: List[Dict], + ignore_timeout_error: bool = True, + scan_interval: int = DEFAULT_SCAN_INTERVAL) -> Dict: """ Monitor the specified elements on a host. @@ -70,7 +75,7 @@ def monitoring_event(host_manager: HostManager, host: str, monitoring_elements: host_manager (HostManager): Host Manager to handle the environment host (str): The target host. monitoring_elements(List): A list of dictionaries containing regex, timeout, and file. - ignore_error: If True, ignore errors and continue monitoring. + ignore_timeout_error: If True, ignore TimeoutError and return the result. Raises: TimeoutError: If no match is found within the specified timeout. @@ -95,10 +100,15 @@ def filter_events_by_timestamp(match_events: List) -> List: timestamp_format = "%Y/%m/%d %H:%M:%S" timestamp_format_parameter = "%Y-%m-%dT%H:%M:%S.%f" - timestamp_datetime = datetime.strptime(timestamp_str, timestamp_format) - greater_than_timestamp_formatted = datetime.strptime(greater_than_timestamp, timestamp_format_parameter) + try: + timestamp_datetime = datetime.strptime(timestamp_str, timestamp_format) + greater_than_timestamp_formatted = datetime.strptime(greater_than_timestamp, + timestamp_format_parameter) + except ValueError: + raise ValueError(f"Timestamp format not supported: {timestamp_str}." + 'Do the regex includes the timestamp?') - if timestamp_datetime >= greater_than_timestamp_formatted: + if timestamp_datetime >= greater_than_timestamp_formatted: match_that_fit_timestamp.append(match) return match_that_fit_timestamp @@ -112,11 +122,12 @@ def filter_events_by_timestamp(match_events: List) -> List: element['n_iterations'], \ element.get('greater_than_timestamp', None) current_timeout = 0 - regex_match = None + regex_match = False while current_timeout < timeout: file_content = host_manager.get_file_content(host, monitoring_file) match_regex = re.findall(regex, file_content) + if greater_than_timestamp: match_that_fit_timestamp = filter_events_by_timestamp(match_regex) else: @@ -133,7 +144,7 @@ def filter_events_by_timestamp(match_events: List) -> List: if not regex_match: elements_not_found.append(element) - if not ignore_error: + if not ignore_timeout_error: raise TimeoutError(f"Element not found: {element}") monitoring_result = {} @@ -150,15 +161,13 @@ def filter_events_by_timestamp(match_events: List) -> List: with ThreadPoolExecutor() as executor: futures = [] for host, data in monitoring_data.items(): - futures.append(executor.submit(monitoring_event, host_manager, host, data, ignore_error)) + futures.append(executor.submit(monitoring_event, host_manager, host, data, ignore_timeout_error, + scan_interval)) results = {} for future in as_completed(futures): - try: - result = future.result() - results.update(result) - except Exception as e: - logging.error(f"An error occurred: {e}") + result = future.result() + results.update(result) logging.info(f"Monitoring results: {results}")