main
py 165 lines 5.92 KB
Raw
1 from loguru import logger
2
3 from app.connectors.graylog.schema.management import DeletedIndexBody
4 from app.connectors.graylog.schema.management import DeletedIndexResponse
5 from app.connectors.graylog.schema.management import GraylogServerInfo
6 from app.connectors.graylog.schema.management import StartInputBody
7 from app.connectors.graylog.schema.management import StartInputResponse
8 from app.connectors.graylog.schema.management import StartStreamBody
9 from app.connectors.graylog.schema.management import StartStreamResponse
10 from app.connectors.graylog.schema.management import StopInputBody
11 from app.connectors.graylog.schema.management import StopInputResponse
12 from app.connectors.graylog.schema.management import StopStreamBody
13 from app.connectors.graylog.schema.management import StopStreamResponse
14 from app.connectors.graylog.services.collector import get_index_names
15 from app.connectors.graylog.utils.universal import send_delete_request
16 from app.connectors.graylog.utils.universal import send_get_request
17 from app.connectors.graylog.utils.universal import send_post_request
18 from app.connectors.graylog.utils.universal import send_put_request
19
20
21 async def get_system_info() -> GraylogServerInfo:
22 """Get system information from Graylog.
23
24 Returns:
25 GraylogServerInfo: The system information from Graylog.
26 """
27 logger.info("Getting system information from Graylog")
28 response = await send_get_request(endpoint="/api/system")
29 return GraylogServerInfo(**response["data"])
30
31
32 async def delete_index(index_name: DeletedIndexBody) -> DeletedIndexResponse:
33 """Delete an index from Graylog.
34
35 Args:
36 index_name (DeletedIndexBody): The name of the index to be deleted.
37
38 Returns:
39 DeletedIndexResponse: The response indicating the success or failure of the index deletion.
40 """
41 logger.info(f"Deleting index {index_name} from Graylog")
42 await send_delete_request(endpoint=f"/api/system/indexer/indices/{index_name}")
43 # Check if the index still exists
44 index_names = await get_index_names()
45 logger.info(f"Index names: {index_names}")
46 if index_name in index_names:
47 return DeletedIndexResponse(
48 success=False,
49 message=f"Failed to delete index {index_name}. If the index is still in use, it cannot be deleted.",
50 )
51 else:
52 return DeletedIndexResponse(
53 success=True,
54 message=f"Successfully deleted index {index_name}",
55 )
56
57
58 async def delete_index_by_id(index_id: str) -> DeletedIndexResponse:
59 """Delete an index from Graylog.
60
61 Args:
62 index_id (str): The ID of the index to be deleted.
63
64 Returns:
65 DeletedIndexResponse: The response indicating the success or failure of the index deletion.
66 """
67 logger.info(f"Deleting index {index_id} from Graylog")
68 await send_delete_request(endpoint=f"/api/system/indices/index_sets/{index_id}")
69 return DeletedIndexResponse(
70 success=True,
71 message=f"Successfully deleted index {index_id}",
72 )
73
74
75 async def stop_input(input_id: StopInputBody) -> StopInputResponse:
76 """Stop an input in Graylog.
77
78 Args:
79 input_id (StopInputBody): The ID of the input to stop.
80
81 Returns:
82 StopInputResponse: The response indicating the success or failure of stopping the input.
83 """
84 logger.info(f"Stopping input {input_id} in Graylog")
85 response = await send_delete_request(endpoint=f"/api/system/inputstates/{input_id}")
86 if response["success"]:
87 return StopInputResponse(
88 success=True,
89 message=f"Successfully stopped input {input_id}",
90 )
91 else:
92 return StopInputResponse(
93 success=False,
94 message=f"Failed to stop input {input_id}",
95 )
96
97
98 async def start_input(input_id: StartInputBody) -> StartInputResponse:
99 """Start an input in Graylog.
100
101 Args:
102 input_id (StartInputBody): The ID of the input to start.
103
104 Returns:
105 StartInputResponse: The response indicating the success or failure of starting the input.
106 """
107 logger.info(f"Starting input {input_id} in Graylog")
108 response = await send_put_request(endpoint=f"/api/system/inputstates/{input_id}")
109 if response["success"]:
110 return StartInputResponse(
111 success=True,
112 message=f"Successfully started input {input_id}",
113 )
114 else:
115 return StartInputResponse(
116 success=False,
117 message=f"Failed to start input {input_id}",
118 )
119
120
121 async def stop_stream(stream_id: StopStreamBody) -> StopStreamResponse:
122 """Stop a stream in Graylog.
123
124 Args:
125 stream_id (StopStreamBody): The ID of the stream to stop.
126
127 Returns:
128 StopStreamResponse: The response indicating the success or failure of stopping the stream.
129 """
130 logger.info(f"Stopping stream {stream_id} in Graylog")
131 response = await send_post_request(endpoint=f"/api/streams/{stream_id}/pause")
132 logger.info(f"Response: {response}")
133 if response["success"]:
134 return StopStreamResponse(
135 success=True,
136 message=f"Successfully stopped stream {stream_id}",
137 )
138 else:
139 return StopStreamResponse(
140 success=False,
141 message=f"Failed to stop stream {stream_id}",
142 )
143
144
145 async def start_stream(stream_id: StartStreamBody) -> StartStreamResponse:
146 """Start a stream in Graylog.
147
148 Args:
149 stream_id (StartStreamBody): The ID of the stream to start.
150
151 Returns:
152 StartStreamResponse: The response indicating the success or failure of starting the stream.
153 """
154 logger.info(f"Starting stream {stream_id} in Graylog")
155 response = await send_post_request(endpoint=f"/api/streams/{stream_id}/resume")
156 if response["success"]:
157 return StartStreamResponse(
158 success=True,
159 message=f"Successfully started stream {stream_id}",
160 )
161 else:
162 return StartStreamResponse(
163 success=False,
164 message=f"Failed to start stream {stream_id}",
165 )