-
Notifications
You must be signed in to change notification settings - Fork 21
feat: Adds support to ingest data via file monitor #253
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c6a1983
feat: Adds support to ingest data via file monitor
rajkarkar-crest 1a4e8ac
test: Updated docs and ingestor_helper
rajkarkar-crest 5148bcb
test: Updated doc and wait for uf method
rajkarkar-crest ea4046a
test: Updated doc for release history
rajkarkar-crest File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,7 @@ | ||
| ARG SPLUNK_VERSION=latest | ||
| FROM splunk/universalforwarder:$SPLUNK_VERSION | ||
| ARG SPLUNK_VERSION=latest | ||
| ARG SPLUNK_APP_ID=TA_UNKNOWN | ||
| ARG SPLUNK_APP_PACKAGE=package | ||
| RUN echo ${SPLUNK_VERSION} $SPLUNK_APP_PACKAGE | ||
| COPY ${SPLUNK_APP_PACKAGE} /opt/splunkforwarder/etc/apps/${SPLUNK_APP_ID} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
122 changes: 122 additions & 0 deletions
122
pytest_splunk_addon/standard_lib/event_ingestors/file_monitor_ingestor.py
This file contains hidden or 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,122 @@ | ||
| from .base_event_ingestor import EventIngestor | ||
| import requests | ||
| import logging | ||
| import os | ||
| from time import sleep | ||
| from requests.exceptions import ConnectionError | ||
|
|
||
| LOGGER = logging.getLogger("pytest-splunk-addon") | ||
| MONITOR_DIR = "uf_files" | ||
|
|
||
| class FileMonitorEventIngestor(EventIngestor): | ||
| """ | ||
| Class to ingest event via File monitor | ||
| This ingestor will only work if splunk_type is docker and container of universal forwarder is linked with container | ||
| of splunk instance as 'splunk' service. | ||
|
|
||
| The format for required_configs is:: | ||
|
|
||
| { | ||
| uf_host: Host of universal forwarder | ||
| uf_port: Management port of universal forwarder | ||
| uf_username: Name of user for universal forwarder | ||
| uf_password: Password of universal forwarder | ||
| } | ||
|
|
||
| Args: | ||
| required_configs (dict): Dictionary containing information about universal forwarder | ||
|
|
||
| """ | ||
| def __init__(self, required_configs): | ||
| self.uf_host = required_configs.get("uf_host") | ||
| self.uf_port = required_configs.get("uf_port") | ||
| self.uf_username = required_configs.get("uf_username") | ||
| self.uf_password = required_configs.get("uf_password") | ||
| # Container of universal forwarder is linked with splunk instance. | ||
| # So using splunk_host as splunk and port 9997 directly. | ||
| self.splunk_host = "splunk" | ||
| self.splunk_s2s_port = "9997" | ||
| self.uf_rest_uri = "https://{}:{}".format(self.uf_host, self.uf_port) | ||
| self.outputs_endpoint = "{}/services/data/outputs/tcp/group".format(self.uf_rest_uri) | ||
| self.inputs_endpoint = "{}/servicesNS/nobody/search/data/inputs/monitor".format(self.uf_rest_uri) | ||
|
|
||
| def ingest(self, events, thread_count): | ||
| """ | ||
| Ingests data into splunk via file monitor. | ||
| Args: | ||
| events (list): List of events (SampleEvent) to be ingested | ||
| """ | ||
| self.create_output_conf() | ||
| for each_event in events: | ||
| self.create_event_file(each_event) | ||
| sleep(10) | ||
| self.create_inputs_stanza(each_event) | ||
|
|
||
| def create_output_conf(self): | ||
| """ | ||
| Create stanza in outputs.conf file of universal forwarder to send on splunk(indexer). | ||
| """ | ||
| tcp_out_dict = {"name":"uf_monitor", "servers":"{}:{}".format(self.splunk_host, self.splunk_s2s_port)} | ||
| LOGGER.info("Making rest call to create stanza in outputs.conf file with following endpoint : {}".format(self.outputs_endpoint)) | ||
| LOGGER.debug("Creating following stanza in output.conf : {}".format(tcp_out_dict)) | ||
| try: | ||
| response = requests.post(self.outputs_endpoint, tcp_out_dict, auth=(self.uf_username, self.uf_password), verify=False) | ||
| if response.status_code not in (200, 201): | ||
| LOGGER.warning("Unable to create stanza in outputs.conf\nStatus code: {} \nReason: {} \ntext:{}".format(response.status_code, response.reason, response.text)) | ||
| except ConnectionError as e: | ||
| LOGGER.error("Unable to connect to Universal forwarder, {}".format(e)) | ||
|
|
||
| def create_event_file(self, event): | ||
| """ | ||
| Write each tokenized event in files with host name as name of file. The host of all events will be unique. | ||
|
|
||
| Args: | ||
| event (SampleEvent): Instance containing event info | ||
| """ | ||
| try: | ||
| with open(self.get_file_path(event), "w+") as fp: | ||
| LOGGER.info("Writing events file for host={}".format(event.metadata.get("host"))) | ||
| fp.write(event.event) | ||
| LOGGER.debug("Wrote tokenized events file on path : {}".format(self.get_file_path(event))) | ||
| except Exception as e: | ||
| LOGGER.warning("Unable to create event file for host : {}, Reason : {}".format(event.metadata.get("host"), e)) | ||
|
|
||
| def create_inputs_stanza(self, event): | ||
| """ | ||
| Create stanza in inputs.conf on universal forwarder for each tokenized event. | ||
|
|
||
| Args: | ||
| event (SampleEvent): Instance containing event info | ||
| """ | ||
| file_path = self.get_file_path(event) | ||
| sourcetype = event.metadata.get("sourcetype") | ||
| if not sourcetype: | ||
| sourcetype = event.metadata.get("sourcetype_to_search", "pytest_splunk_addon") | ||
| stanza = { | ||
| "name": file_path, | ||
| "sourcetype": sourcetype, | ||
| "index": event.metadata.get("index", "main"), | ||
| "disabled": False, | ||
| "crc-salt": "<SOURCE>" | ||
| } | ||
| if event.metadata.get("host_type") in ("plugin"): | ||
| stanza["host"] = event.metadata.get("host") | ||
| if event.metadata.get("source"): | ||
| stanza["rename-source"] = event.metadata.get("source") | ||
| LOGGER.info("Making rest call to create stanza in inputs.conf file with following endpoint : {}".format(self.inputs_endpoint)) | ||
| LOGGER.debug("Creating following stanza in inputs.conf : {}".format(stanza)) | ||
| try: | ||
| response = requests.post(self.inputs_endpoint, stanza, auth=(self.uf_username, self.uf_password), verify=False) | ||
| if response.status_code not in (200, 201): | ||
| LOGGER.warning("Unable to add stanza in inputs.conf\nStatus code: {} \nReason: {} \ntext:{}".format(response.status_code, response.reason, response.text)) | ||
| except ConnectionError as e: | ||
| LOGGER.error("Unable to connect to Universal forwarder, {}".format(e)) | ||
|
|
||
| def get_file_path(self, event): | ||
| """ | ||
| Returns absolute path for tokenized events. | ||
|
|
||
| Args: | ||
| event (SampleEvent): Instance containing event info | ||
| """ | ||
| return "{}/{}/{}".format(os.getcwd(), MONITOR_DIR, event.metadata.get("host")) |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.