Path: blob/master/tutorials-and-examples/feature-tutorials/Splunk-DataConnector.ipynb
3253 views
Splunk - Data Connector
Description
The data provider module of msticpy provides functions to allow for the defining of data sources, connectors to them and queries for them as well as the ability to return query result from the defined data sources.
For more information on Data Propviders, check documentation
In this notebooks we will demonstrate Splunk data connector feature of msticpy. This feature is built on-top of the [Splunk Enterprise SDK for Python] (https://dev.splunk.com/enterprise/docs/devtools/python/sdk-python/) with some customizations and enhancements.
Installation
Authentication
Authentication for the Splunk data provider is handled by specifying credentials directly in the connect call or specifying the credentials in msticpy config file.
For more information on how to create new user with approapriate roles and permissions, follow Splunk Docs Addandeditusers and Aboutusersandroles. The user should have permission to at least run its own searches or more depending upon the actions to be performed by user.
Once you created user account with the appropriate roles, you will require the following details to specify while connecting
host = "localhost"(Splunk server FQDN hostname to connect, for locally installed splunk, you can specify localhost)
port = 8089 (Splunk REST API )
username = "admin" (username to connect to Splunk instance)
password = "yourpassword" (password of the userspecified in username)
Once you have details, you can specify it in msticpyconfig.yaml as shown in below example
Instantiating a query provider
You can instantiate a data provider for Splunk by specifying the credentials in connect or in msticpy config file.
If the details are correct and authentication is successful, it will show connected.
Listing available queries
Upon connecting to the Splunk data environment, we can take a look what query options available to us by running QUERY_PROVIDER.list_queries()
For more information, refer documentation : Listing available queries.
This will display all the saved searches from the connected splunk instance and also pre-built custom queries to do common operations such as list datatypes, list saved searches, alerts, audittrail informaion.
In order to get help for specific query , you can execute QUERY_PROVIDER.<QueryName>('?') .
For more information , refer documentation - Getting Help for a query
If you want to print the query prior to executing, pass 'print' as an argument
If you have set the arguments and then would like to validate the query, use below example
Running pre-defined query
In order to run pre-defined query , execute with the name either by setting values for arguments if available or run with default arguments.
For more information , refer documentation - Running an pre-definedfined query
By-default, splunk query results are limited to 100. you can specify count=0 argument to return all the results. Deafult value for add_query_items argument is set to | head 100 which you can reset as shown in below example while retrieving all results.
Running a Ad-hoc Splunk query
You can also define a your own splunk query and run it via splunk provider via QUERY_PROVIDER.exec_query(<queryname>)
For more information, check documentation Running and Ad-hoc Query
References
Splunk Enterprise SDK for Python: https://dev.splunk.com/enterprise/docs/devtools/python/sdk-python/
Splunk Community : https://community.splunk.com/t5/Community/ct-p/en-us
Splunk Documentation: https://docs.splunk.com/Documentation