This repository has been archived by the owner on Dec 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(config): Change default port to 2162 (#22)
fix: Accept connections on all interfaces Accept ipv4 and ipv6 by default Listen on all interfaces fix(output): Add try catch for post and don't log sensitive data fix: Update sourcetype for traps Reuse the same HTTP Session object Reuse the same HTTP Session object fix: Names clashing It looks like having the same name for python file inside a package with the same name doesn't get resolved by Pycharm. It'll complain that "no such module", I have changed the the name something different and it worked just fine. Not sure if this is a Pycharm issue or not, but now with this change we can run the server both in Pycharm and poetry. refactor: removed useless logger.debug() statements Reuse the same HTTP Session object Reuse the same HTTP Session object fix: Names clashing It looks like having the same name for python file inside a package with the same name doesn't get resolved by Pycharm. It'll complain that "no such module", I have changed the the name something different and it worked just fine. Not sure if this is a Pycharm issue or not, but now with this change we can run the server both in Pycharm and poetry. refactor: removed useless logger.debug() statements Co-authored-by: rfaircloth-splunk <[email protected]>
- Loading branch information
Showing
5 changed files
with
73 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import concurrent.futures | ||
import logging | ||
import threading | ||
|
||
import requests | ||
|
||
from splunk_connect_for_snmp_traps.manager.hec_config import HecConfiguration | ||
from splunk_connect_for_snmp_traps.manager.os_config_utils import ( | ||
max_allowed_working_threads, | ||
) | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class HecSender: | ||
def __init__(self, server_config): | ||
self._server_config = server_config | ||
self._hec_config = HecConfiguration() | ||
self._thread_local = threading.local() | ||
self._thread_pool_executor = self.configure_thread_pool() | ||
|
||
def configure_thread_pool(self): | ||
user_suggested_working_threads = self._server_config["thread-pool"][ | ||
"max-suggested-working-threads" | ||
] | ||
max_workers = max_allowed_working_threads(user_suggested_working_threads) | ||
logger.debug(f"Configured a thread-pool with {max_workers} concurrent threads") | ||
return concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) | ||
|
||
def get_session(self): | ||
if not hasattr(self._thread_local, "session"): | ||
self._thread_local.session = requests.Session() | ||
return self._thread_local.session | ||
|
||
def post_data_to_thread_pool(self, variables_binds): | ||
headers = { | ||
"Authorization": f"Splunk {self._hec_config.get_authentication_token()}" | ||
} | ||
splunk_trap_data = ",".join( | ||
[str(key) + str(value) for key, value in variables_binds] | ||
) | ||
data = {"sourcetype": "trap-server", "event": splunk_trap_data} | ||
try: | ||
session = self.get_session() | ||
for endpoint in self._hec_config.get_endpoints(): | ||
response = session.post( | ||
url=endpoint, json=data, headers=headers, verify=False | ||
) | ||
logger.debug(f"Response code is {response.status_code}") | ||
except requests.ConnectionError as e: | ||
logger.error(f"Connection error when sending data to HEC: {e}") | ||
|
||
def post_data(self, variables_binds): | ||
self._thread_pool_executor.submit( | ||
self.post_data_to_thread_pool, variables_binds | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.