added output_shard_number_to_be_set_based_on_nodes
Taylor committed
Apr 18, 2024 at 08:57 UTC
f5f89df894ecc439ac51622fa78e013d3452c070
4 files changed
+49
-3
backend/app/connectors/wazuh_indexer/routes/monitoring.py
+25
@@ -15,6 +15,7 @@ from app.connectors.wazuh_indexer.services.monitoring import cluster_healthcheck
15
from app.connectors.wazuh_indexer.services.monitoring import indices_stats
16
from app.connectors.wazuh_indexer.services.monitoring import node_allocation
17
from app.connectors.wazuh_indexer.services.monitoring import shards
18
+from app.connectors.wazuh_indexer.services.monitoring import output_shard_number_to_be_set_based_on_nodes
19
20
wazuh_indexer_router = APIRouter()
21
@@ -120,3 +121,27 @@ async def get_shards() -> Union[ShardsResponse, HTTPException]:
121
return shards_response
122
else:
123
raise HTTPException(status_code=500, detail="Failed to retrieve shards.")
124
+
125
+@wazuh_indexer_router.get(
126
+ "/output_shard_number_to_be_set_based_on_nodes",
127
+ description="Fetch Wazuh Indexer output_shard_number_to_be_set_based_on_nodes",
128
+ dependencies=[Security(AuthHandler().require_any_scope("admin", "analyst"))],
129
+)
130
+async def get_output_shard_number_to_be_set_based_on_nodes_route() -> int:
131
+ """
132
+ Fetch Wazuh Indexer output_shard_number_to_be_set_based_on_nodes.
133
+
134
+ This endpoint retrieves the output_shard_number_to_be_set_based_on_nodes of the Wazuh Indexer service.
135
+
136
+ Returns:
137
+ ElasticsearchResponse: A Pydantic model representing the output_shard_number_to_be_set_based_on_nodes of the Wazuh Indexer service.
138
+
139
+ Raises:
140
+ HTTPException: An exception with a 500 status code is raised if the output_shard_number_to_be_set_based_on_nodes cannot be retrieved.
141
+ """
142
+ output_shard_number_to_be_set_based_on_nodes_response = await output_shard_number_to_be_set_based_on_nodes()
143
+ if output_shard_number_to_be_set_based_on_nodes_response is not None:
144
+ return output_shard_number_to_be_set_based_on_nodes_response
145
+ else:
146
+ raise HTTPException(status_code=500, detail="Failed to retrieve output_shard_number_to_be_set_based_on_nodes.")
147
+
backend/app/connectors/wazuh_indexer/services/monitoring.py
+20
@@ -133,3 +133,23 @@ async def shards() -> Union[ShardsResponse, Dict[str, str]]:
133
logger.error(f"Shards check failed with error: {e}")
134
e = f"Shards check failed with error: {e}"
135
raise Exception(str(e))
136
+
137
+
138
+async def output_shard_number_to_be_set_based_on_nodes() -> int:
139
+ """
140
+ Retrieves the number of nodes in the Wazuh Indexer cluster.
141
+ Based on that number, it returns the number of shards to be set for the new index.
142
+ This is a 1:1 mapping between the number of nodes and the number of shards.
143
+
144
+ Returns:
145
+ int: The number of shards to be set for the new index.
146
+ """
147
+ es_client = await create_wazuh_indexer_client("Wazuh-Indexer")
148
+ try:
149
+ cluster_health_data = es_client.cluster.health()
150
+ cluster_health_model = ClusterHealth(**cluster_health_data)
151
+ return cluster_health_model.number_of_nodes
152
+ except Exception as e:
153
+ logger.error(f"Shards check failed with error: {e}")
154
+ e = f"Shards check failed with error: {e}"
155
+ raise Exception(str(e))
backend/app/integrations/carbonblack/services/provision.py
+2
-1
@@ -28,6 +28,7 @@ from app.integrations.models.customer_integration_settings import CustomerIntegr
28
from app.integrations.routes import create_integration_meta
29
from app.integrations.schema import CustomerIntegrationsMetaSchema
30
from app.utils import get_connector_attribute
31
+from app.connectors.wazuh_indexer.services.monitoring import output_shard_number_to_be_set_based_on_nodes
32
33
34
################## ! GRAYLOG ! ##################
@@ -62,7 +63,7 @@ async def build_index_set_config(
63
},
64
creation_date=datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
65
index_analyzer="standard",
65
- shards=1,
66
+ shards=await output_shard_number_to_be_set_based_on_nodes(),
67
replicas=0,
68
index_optimization_max_num_segments=1,
69
index_optimization_disabled=False,
backend/app/integrations/huntress/services/provision.py
+2
-2
@@ -28,7 +28,7 @@ from app.integrations.models.customer_integration_settings import CustomerIntegr
28
from app.integrations.routes import create_integration_meta
29
from app.integrations.schema import CustomerIntegrationsMetaSchema
30
from app.utils import get_connector_attribute
31
-
31
+from app.connectors.wazuh_indexer.services.monitoring import output_shard_number_to_be_set_based_on_nodes
32
33
################## ! GRAYLOG ! ##################
34
async def build_index_set_config(
@@ -62,7 +62,7 @@ async def build_index_set_config(
62
},
63
creation_date=datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%fZ"),
64
index_analyzer="standard",
65
- shards=1,
65
+ shards=await output_shard_number_to_be_set_based_on_nodes(),
66
replicas=0,
67
index_optimization_max_num_segments=1,
68
index_optimization_disabled=False,