| 1 | import asyncio |
| 2 | from typing import Any |
| 3 | from typing import Dict |
| 4 | |
| 5 | from fastapi import HTTPException |
| 6 | from loguru import logger |
| 7 | |
| 8 | from app.connectors.event_shipper.utils.universal import create_gelf_logger |
| 9 | from app.connectors.utils import get_connector_info_from_db |
| 10 | from app.db.db_session import get_db_session |
| 11 | from app.integrations.utils.schema import EventShipperPayload |
| 12 | from app.integrations.utils.schema import EventShipperPayloadResponse |
| 13 | |
| 14 | |
| 15 | async def get_gelf_logger(): |
| 16 | try: |
| 17 | gelf_logger = await create_gelf_logger() |
| 18 | return gelf_logger |
| 19 | except Exception as e: |
| 20 | logger.error(f"Failed to initialize GelfLogger: {e}") |
| 21 | raise HTTPException( |
| 22 | status_code=500, |
| 23 | detail=f"Failed to initialize GelfLogger: {e}", |
| 24 | ) |
| 25 | |
| 26 | |
| 27 | async def event_shipper(message: EventShipperPayload) -> EventShipperPayloadResponse: |
| 28 | """ |
| 29 | Test the log shipper. |
| 30 | """ |
| 31 | gelf_logger = await get_gelf_logger() |
| 32 | |
| 33 | try: |
| 34 | await gelf_logger.tcp_handler(message=message) |
| 35 | except Exception as e: |
| 36 | logger.error(f"Failed to send test message to log shipper: {e}") |
| 37 | raise HTTPException( |
| 38 | status_code=500, |
| 39 | detail=f"Failed to send test message to log shipper: {e}", |
| 40 | ) |
| 41 | |
| 42 | return EventShipperPayloadResponse( |
| 43 | success=True, |
| 44 | message="Successfully sent test message to log shipper.", |
| 45 | ) |
| 46 | |
| 47 | |
| 48 | async def send_json_test_message_to_event_shipper(message: EventShipperPayload) -> EventShipperPayloadResponse: |
| 49 | """ |
| 50 | Sends a test message to the Graylog Input. |
| 51 | """ |
| 52 | gelf_logger = await get_gelf_logger() |
| 53 | |
| 54 | try: |
| 55 | await gelf_logger.tcp_handler(message=message) |
| 56 | except Exception as e: |
| 57 | logger.error(f"Failed to send test message to log shipper: {e}") |
| 58 | raise HTTPException( |
| 59 | status_code=500, |
| 60 | detail=f"Failed to send test message to log shipper: {e}", |
| 61 | ) |
| 62 | |
| 63 | return EventShipperPayloadResponse( |
| 64 | success=True, |
| 65 | message="Successfully sent test message to log shipper.", |
| 66 | ) |
| 67 | |
| 68 | |
| 69 | async def verify_event_shipper_healtcheck(attributes: Dict[str, Any]) -> Dict[str, Any]: |
| 70 | """ |
| 71 | Verifies the connection to Graylog Input via a telnet connection. |
| 72 | |
| 73 | Returns: |
| 74 | dict: A dictionary containing 'connectionSuccessful' status. |
| 75 | """ |
| 76 | logger.info( |
| 77 | f"Verifying the event shipper connection to {attributes['connector_url']}", |
| 78 | ) |
| 79 | |
| 80 | # Make a TCP connection to the Graylog Input |
| 81 | try: |
| 82 | reader, writer = await asyncio.open_connection( |
| 83 | attributes["connector_url"], |
| 84 | attributes["connector_extra_data"], |
| 85 | ) |
| 86 | writer.close() |
| 87 | await writer.wait_closed() |
| 88 | await send_json_test_message_to_event_shipper( |
| 89 | EventShipperPayload( |
| 90 | message="Healthcheck successful", |
| 91 | integration="event_shipper", |
| 92 | customer_code="n/a", |
| 93 | ), |
| 94 | ) |
| 95 | return { |
| 96 | "connectionSuccessful": True, |
| 97 | "message": "Event shipper healthcheck successful", |
| 98 | } |
| 99 | except Exception as e: |
| 100 | logger.error( |
| 101 | f"Connection to {attributes['connector_url']} failed with error: {e}", |
| 102 | ) |
| 103 | return { |
| 104 | "connectionSuccessful": False, |
| 105 | "message": f"Connection to {attributes['connector_url']} failed", |
| 106 | } |
| 107 | |
| 108 | |
| 109 | async def verify_event_shipper_connection(connector_name: str) -> str: |
| 110 | """ |
| 111 | Returns the status of the connection to Graylog Input. |
| 112 | """ |
| 113 | async with get_db_session() as session: # This will correctly enter the context manager |
| 114 | attributes = await get_connector_info_from_db(connector_name, session) |
| 115 | if attributes is None: |
| 116 | logger.error("No attributes found for event shipper connector") |
| 117 | return None |
| 118 | return await verify_event_shipper_healtcheck(attributes) |