Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix monitoring module for e2e tests #4959

Merged
merged 9 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
41 changes: 25 additions & 16 deletions deps/wazuh_testing/wazuh_testing/end_to_end/monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -61,16 +65,17 @@ 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.

Args:
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.
Expand All @@ -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?')
Comment on lines +103 to +109
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you already have the greater_than_timestamp field, is it necessary to add the timestamp regex to the regex field? Isn't it a bit repetitive?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To effectively compare with the time specified in the greater_than_timestamp parameters, we need to gather the timestamp from the event. Therefore, the specified regex should be designed to match at least one group, specifically tailored for capturing the timestamp.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will revisit this in the future


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
Expand All @@ -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:
Expand All @@ -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 = {}
Expand All @@ -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}")

Expand Down
Loading