agent dictionary (#65)
taylor_socfortress committed
Jul 25, 2023 at 18:24 UTC
8a06e24fb937c017be6f699d9ac3f03fbd3c1b51
2 files changed
+49
-3
backend/app/routes/healthchecks.py
+4
-3
@@ -22,7 +22,8 @@ def get_agents_full() -> Any:
22
agent_service = AgentService()
23
agents = agent_service.get_all_agents()
24
healthcheck_service = HealthcheckAgentsService()
25
- agent_health = healthcheck_service.perform_healthcheck_full(agents, check_logs=True)
25
+ logger.info(f"All agents: {agents}")
26
+ agent_health = healthcheck_service.perform_healthcheck_full(agents["agents"], check_logs=True)
27
return jsonify(agent_health)
28
29
@@ -56,7 +57,7 @@ def get_agents_wazuh() -> Any:
57
agent_service = AgentService()
58
agents = agent_service.get_all_agents()
59
healthcheck_service = HealthcheckAgentsService()
59
- agent_health = healthcheck_service.perform_healthcheck_wazuh(agents)
60
+ agent_health = healthcheck_service.perform_healthcheck_wazuh(agents["agents"])
61
return jsonify(agent_health)
62
63
@@ -92,7 +93,7 @@ def get_agents_velociraptor() -> Any:
93
agent_service = AgentService()
94
agents = agent_service.get_all_agents()
95
healthcheck_service = HealthcheckAgentsService()
95
- agent_health = healthcheck_service.perform_healthcheck_velociraptor(agents)
96
+ agent_health = healthcheck_service.perform_healthcheck_velociraptor(agents["agents"])
97
return jsonify(agent_health)
98
99
backend/app/services/Healthchecks/agents.py
+45
@@ -20,15 +20,37 @@ class HealthcheckAgentsService:
20
}
21
22
def __init__(self):
23
+ """
24
+ Initialize HealthcheckAgentsService with a UniversalService instance.
25
+ """
26
self.universal_service = UniversalService()
27
28
def convert_string_to_datetime(self, date_string: str) -> datetime:
29
+ """
30
+ Convert a string to a datetime object.
31
+
32
+ Args:
33
+ date_string (str): The date string to be converted.
34
+
35
+ Returns:
36
+ datetime: The converted datetime object.
37
+ """
38
try:
39
return datetime.strptime(date_string, "%Y-%m-%dT%H:%M:%S")
40
except ValueError:
41
return datetime.strptime(date_string, "%Y-%m-%dT%H:%M:%S.%f")
42
43
def is_agent_unhealthy(self, agent: Dict, current_time: datetime) -> bool:
44
+ """
45
+ Check if an agent is unhealthy based on the last_seen and client_last_seen timestamps.
46
+
47
+ Args:
48
+ agent (Dict): The agent dictionary to be checked.
49
+ current_time (datetime): The current time to compare with the last_seen and client_last_seen timestamps.
50
+
51
+ Returns:
52
+ bool: True if the agent is unhealthy, False otherwise.
53
+ """
54
last_seen = self.convert_string_to_datetime(agent["last_seen"])
55
client_last_seen = self.convert_string_to_datetime(agent["client_last_seen"])
56
@@ -40,6 +62,9 @@ class HealthcheckAgentsService:
62
def get_indices(self):
63
"""
64
Returns a list of all indices in the Wazuh-Indexer.
65
+
66
+ Returns:
67
+ List[str]: A list of all indices.
68
"""
69
indices_response = self.universal_service.collect_indices()
70
if indices_response["success"]:
@@ -49,6 +74,16 @@ class HealthcheckAgentsService:
74
return []
75
76
def has_agent_recent_logs(self, agent: Dict, indices: List[str]) -> bool:
77
+ """
78
+ Check if an agent has recent logs within the Wazuh Indexer.
79
+
80
+ Args:
81
+ agent (Dict): The agent dictionary to be checked.
82
+ indices (List[str]): The list of indices to be checked.
83
+
84
+ Returns:
85
+ bool: True if the agent has recent logs, False otherwise.
86
+ """
87
for interval in [1, 5, 15]:
88
logger.info(f"Checking agent {agent['hostname']} for logs within the last {interval} minutes.")
89
query = self._generate_recent_logs_query(agent["hostname"], interval)
@@ -67,6 +102,16 @@ class HealthcheckAgentsService:
102
103
@staticmethod
104
def _generate_recent_logs_query(agent_hostname: str, minutes: int) -> Dict:
105
+ """
106
+ Generate a query to get recent logs of an agent.
107
+
108
+ Args:
109
+ agent_hostname (str): The hostname of the agent.
110
+ minutes (int): The number of past minutes to look for logs.
111
+
112
+ Returns:
113
+ Dict: The generated query.
114
+ """
115
return {
116
"query": {"bool": {"must": [{"match": {"agent_name": agent_hostname}}, {"range": {"timestamp": {"gte": f"now-{minutes}m"}}}]}},
117
}