main
py 201 lines 7.57 KB
Raw
1 from typing import Union
2
3 from fastapi import APIRouter
4 from fastapi import HTTPException
5 from fastapi import Security
6
7 from app.auth.utils import AuthHandler
8 from app.connectors.wazuh_indexer.schema.monitoring import ClusterHealthResponse
9 from app.connectors.wazuh_indexer.schema.monitoring import CustomerIndicesSizeResponse
10 from app.connectors.wazuh_indexer.schema.monitoring import IndicesStatsResponse
11 from app.connectors.wazuh_indexer.schema.monitoring import NodeAllocationResponse
12 from app.connectors.wazuh_indexer.schema.monitoring import ShardsResponse
13
14 # from app.connectors.wazuh_indexer.schema import WazuhIndexerResponse, WazuhIndexerListResponse
15 from app.connectors.wazuh_indexer.services.monitoring import cluster_healthcheck
16 from app.connectors.wazuh_indexer.services.monitoring import indices_size_per_customer
17 from app.connectors.wazuh_indexer.services.monitoring import indices_stats
18 from app.connectors.wazuh_indexer.services.monitoring import node_allocation
19 from app.connectors.wazuh_indexer.services.monitoring import (
20 output_shard_number_to_be_set_based_on_nodes,
21 )
22 from app.connectors.wazuh_indexer.services.monitoring import shards
23 from app.connectors.wazuh_indexer.utils.universal import resize_wazuh_index_fields
24
25 wazuh_indexer_router = APIRouter()
26
27
28 @wazuh_indexer_router.get(
29 "/health",
30 response_model=ClusterHealthResponse,
31 description="Fetch Wazuh Indexer cluster health",
32 dependencies=[Security(AuthHandler().require_any_scope("admin", "analyst"))],
33 )
34 async def get_cluster_health() -> Union[ClusterHealthResponse, HTTPException]:
35 """
36 Fetch Wazuh Indexer cluster health.
37
38 This endpoint retrieves the cluster health of the Wazuh Indexer service.
39
40 Returns:
41 ElasticsearchResponse: A Pydantic model representing the cluster health of the Wazuh Indexer service.
42
43 Raises:
44 HTTPException: An exception with a 500 status code is raised if the cluster health cannot be retrieved.
45 """
46 cluster_health = await cluster_healthcheck()
47 if cluster_health is not None:
48 return cluster_health
49 else:
50 raise Exception("Failed to retrieve cluster health.")
51
52
53 @wazuh_indexer_router.get(
54 "/allocation",
55 response_model=NodeAllocationResponse,
56 description="Fetch Wazuh Indexer node allocation",
57 dependencies=[Security(AuthHandler().require_any_scope("admin", "analyst"))],
58 )
59 async def get_node_allocation() -> Union[NodeAllocationResponse, HTTPException]:
60 """
61 Fetch Wazuh Indexer node allocation.
62
63 This endpoint retrieves the node allocation of the Wazuh Indexer service.
64
65 Returns:
66 ElasticsearchResponse: A Pydantic model representing the node allocation of the Wazuh Indexer service.
67
68 Raises:
69 HTTPException: An exception with a 500 status code is raised if the node allocation cannot be retrieved.
70 """
71 node_allocation_response = await node_allocation()
72 if node_allocation_response is not None:
73 return node_allocation_response
74 else:
75 raise HTTPException(
76 status_code=500,
77 detail="Failed to retrieve node allocation.",
78 )
79
80
81 @wazuh_indexer_router.get(
82 "/indices",
83 response_model=IndicesStatsResponse,
84 description="Fetch Wazuh Indexer indices stats",
85 dependencies=[Security(AuthHandler().require_any_scope("admin", "analyst"))],
86 )
87 async def get_indices_stats() -> Union[IndicesStatsResponse, HTTPException]:
88 """
89 Fetch Wazuh Indexer indices stats.
90
91 This endpoint retrieves the indices stats of the Wazuh Indexer service.
92
93 Returns:
94 ElasticsearchResponse: A Pydantic model representing the indices stats of the Wazuh Indexer service.
95
96 Raises:
97 HTTPException: An exception with a 500 status code is raised if the indices stats cannot be retrieved.
98 """
99 indices_stats_response = await indices_stats()
100 if indices_stats_response is not None:
101 return indices_stats_response
102 else:
103 raise HTTPException(status_code=500, detail="Failed to retrieve indices stats.")
104
105
106 @wazuh_indexer_router.get(
107 "/indices/size-per-customer",
108 response_model=CustomerIndicesSizeResponse,
109 description="Fetch Wazuh Indexer indices size aggregated per customer",
110 dependencies=[Security(AuthHandler().require_any_scope("admin", "analyst"))],
111 )
112 async def get_indices_size_per_customer() -> Union[CustomerIndicesSizeResponse, HTTPException]:
113 """
114 Fetch Wazuh Indexer indices size per customer.
115
116 This endpoint retrieves the total indices size aggregated per customer,
117 where customer is extracted from index names (e.g., wazuh-copilot_37 -> copilot).
118
119 Returns:
120 CustomerIndicesSizeResponse: A Pydantic model representing the indices size per customer.
121
122 Raises:
123 HTTPException: An exception with a 500 status code is raised if the data cannot be retrieved.
124 """
125 try:
126 response = await indices_size_per_customer()
127 return response
128 except Exception as e:
129 raise HTTPException(
130 status_code=500,
131 detail=f"Failed to retrieve indices size per customer: {str(e)}",
132 )
133
134
135 @wazuh_indexer_router.get(
136 "/shards",
137 response_model=ShardsResponse,
138 description="Fetch Wazuh Indexer shards",
139 dependencies=[Security(AuthHandler().require_any_scope("admin", "analyst"))],
140 )
141 async def get_shards() -> Union[ShardsResponse, HTTPException]:
142 """
143 Fetch Wazuh Indexer shards.
144
145 This endpoint retrieves the shards of the Wazuh Indexer service.
146
147 Returns:
148 ElasticsearchResponse: A Pydantic model representing the shards of the Wazuh Indexer service.
149
150 Raises:
151 HTTPException: An exception with a 500 status code is raised if the shards cannot be retrieved.
152 """
153 shards_response = await shards()
154 if shards_response is not None:
155 return shards_response
156 else:
157 raise HTTPException(status_code=500, detail="Failed to retrieve shards.")
158
159
160 @wazuh_indexer_router.get(
161 "/output_shard_number_to_be_set_based_on_nodes",
162 description="Fetch Wazuh Indexer output_shard_number_to_be_set_based_on_nodes",
163 dependencies=[Security(AuthHandler().require_any_scope("admin", "analyst"))],
164 )
165 async def get_output_shard_number_to_be_set_based_on_nodes_route() -> int:
166 """
167 Fetch Wazuh Indexer output_shard_number_to_be_set_based_on_nodes.
168
169 This endpoint retrieves the output_shard_number_to_be_set_based_on_nodes of the Wazuh Indexer service.
170
171 Returns:
172 ElasticsearchResponse: A Pydantic model representing the output_shard_number_to_be_set_based_on_nodes of the Wazuh Indexer service.
173
174 Raises:
175 HTTPException: An exception with a 500 status code is raised if the output_shard_number_to_be_set_based_on_nodes cannot be retrieved.
176 """
177 output_shard_number_to_be_set_based_on_nodes_response = await output_shard_number_to_be_set_based_on_nodes()
178 if output_shard_number_to_be_set_based_on_nodes_response is not None:
179 return output_shard_number_to_be_set_based_on_nodes_response
180 else:
181 raise HTTPException(status_code=500, detail="Failed to retrieve output_shard_number_to_be_set_based_on_nodes.")
182
183
184 @wazuh_indexer_router.get(
185 "/resize_wazuh_index_fields",
186 description="Resize Wazuh Index fields",
187 dependencies=[Security(AuthHandler().require_any_scope("admin", "analyst"))],
188 )
189 async def resize_wazuh_index_fields_route():
190 """
191 Resize Wazuh Index fields.
192
193 This endpoint resizes the Wazuh Index fields.
194
195 Returns:
196 str: A string representing the resize Wazuh Index fields response.
197
198 Raises:
199 HTTPException: An exception with a 500 status code is raised if the Wazuh Index fields cannot be resized.
200 """
201 return await resize_wazuh_index_fields()