|
15 | 15 | # |
16 | 16 | import socket |
17 | 17 | from time import sleep |
18 | | -import os |
19 | | -import re |
20 | | -import concurrent.futures |
21 | | -from .base_event_ingestor import EventIngestor |
22 | 18 | import logging |
23 | 19 |
|
| 20 | +from typing import Dict |
| 21 | + |
| 22 | +from .base_event_ingestor import EventIngestor |
| 23 | + |
24 | 24 | LOGGER = logging.getLogger("pytest-splunk-addon") |
25 | 25 |
|
26 | 26 |
|
27 | 27 | class SC4SEventIngestor(EventIngestor): |
28 | 28 | """ |
29 | | - Class to Ingest Events via SC4S |
30 | | -
|
31 | | - The format for required_configs is:: |
32 | | -
|
33 | | - { |
34 | | - sc4s_host (str): Address of the Splunk Server. Do not provide http scheme in the host. |
35 | | - sc4s_port (int): Port number of the above host address |
36 | | - } |
| 29 | + Class to ingest events via SC4S (supports both IPv4 and IPv6) |
37 | 30 |
|
38 | 31 | Args: |
39 | 32 | required_configs (dict): Dictionary containing splunk host and sc4s port |
40 | 33 | """ |
41 | 34 |
|
42 | | - def __init__(self, required_configs): |
| 35 | + def __init__(self, required_configs: Dict[str, str]) -> None: |
43 | 36 | self.sc4s_host = required_configs["sc4s_host"] |
44 | 37 | self.sc4s_port = required_configs["sc4s_port"] |
45 | | - self.server_address = ( |
46 | | - required_configs["sc4s_host"], |
47 | | - required_configs["sc4s_port"], |
48 | | - ) |
| 38 | + |
| 39 | + def _create_socket(self): |
| 40 | + """Try all addresses (IPv4 and IPv6) and return a connected socket.""" |
| 41 | + last_exc = None |
| 42 | + for res in socket.getaddrinfo( |
| 43 | + self.sc4s_host, self.sc4s_port, socket.AF_UNSPEC, socket.SOCK_STREAM |
| 44 | + ): |
| 45 | + af, socktype, proto, _, sa = res |
| 46 | + try: |
| 47 | + sock = socket.socket(af, socktype, proto) |
| 48 | + if af == socket.AF_INET6: |
| 49 | + # Attempt dual-stack if supported |
| 50 | + try: |
| 51 | + sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 0) |
| 52 | + except (AttributeError, OSError): |
| 53 | + pass |
| 54 | + sock.connect(sa) |
| 55 | + return sock |
| 56 | + except Exception as e: |
| 57 | + last_exc = e |
| 58 | + LOGGER.debug(f"Failed to connect to {sa}: {e}") |
| 59 | + try: |
| 60 | + sock.close() |
| 61 | + except Exception: |
| 62 | + pass |
| 63 | + continue |
| 64 | + raise ConnectionError( |
| 65 | + f"Could not connect to SC4S at {self.sc4s_host}:{self.sc4s_port} via IPv4 or IPv6" |
| 66 | + ) from last_exc |
49 | 67 |
|
50 | 68 | def ingest(self, events, thread_count): |
51 | 69 | """ |
52 | | - Ingests events in the splunk via sc4s (Single/Batch of Events) |
| 70 | + Ingests events in Splunk via SC4S (single/batch of events) |
53 | 71 |
|
54 | 72 | Args: |
55 | 73 | events (list): Events with newline character or LineBreaker as separator |
56 | | -
|
57 | 74 | """ |
58 | 75 |
|
59 | | - # This loop just checks for a viable remote connection |
| 76 | + # Retry loop to establish connection |
60 | 77 | tried = 0 |
61 | | - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
62 | 78 | while True: |
63 | 79 | try: |
64 | | - sock.connect(self.server_address) |
| 80 | + sock = self._create_socket() |
65 | 81 | break |
66 | 82 | except Exception as e: |
67 | 83 | tried += 1 |
68 | | - LOGGER.debug("Attempt {} to ingest data with SC4S".format(str(tried))) |
| 84 | + LOGGER.debug(f"Attempt {tried} to ingest data with SC4S") |
69 | 85 | if tried > 90: |
70 | | - LOGGER.error( |
71 | | - "Failed to ingest event with SC4S {} times".format(str(tried)) |
72 | | - ) |
| 86 | + LOGGER.error(f"Failed to ingest event with SC4S {tried} times") |
73 | 87 | raise e |
74 | 88 | sleep(1) |
75 | | - finally: |
76 | | - sock.close() |
77 | 89 |
|
78 | | - raw_events = list() |
79 | | - sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
80 | | - sock.connect(self.server_address) |
81 | | - for event in events: |
82 | | - # raw_events.extend() |
83 | | - for se in event.event.splitlines(): |
84 | | - try: |
85 | | - sock.sendall(str.encode(se + "\n")) |
86 | | - except Exception as e: |
87 | | - LOGGER.debug("Attempt ingest data with SC4S=".format(se)) |
88 | | - LOGGER.exception(e) |
89 | | - sleep(1) |
90 | | - sock.close() |
| 90 | + # Send events |
| 91 | + try: |
| 92 | + for event in events: |
| 93 | + for se in event.event.splitlines(): |
| 94 | + try: |
| 95 | + sock.sendall(str.encode(se + "\n")) |
| 96 | + except Exception as e: |
| 97 | + LOGGER.debug(f"Attempt ingest data with SC4S: {se}") |
| 98 | + LOGGER.exception(e) |
| 99 | + sleep(1) |
| 100 | + finally: |
| 101 | + sock.close() |
0 commit comments