How to get data into Splunk Enterprise using the Splunk Enterprise SDK for Python
Getting data into Splunk® involves taking data from inputs, and then indexing that data by transforming it into individual events that contain searchable fields. Here's a brief overview of how it all works.
Data inputs
A data input is a source of incoming event data. Splunk can index data from the following types of inputs:
-
Files and directories—the contents of files and directories of files. You can upload a file for one-time indexing (a oneshot input), monitor for new data, or monitor for file system changes (events are generated when the directory undergoes a change). You can include and exclude files and directories using filter lists.
-
Network events—data that is received over network Transmission Control Protocol (TCP) and User Datagram Protocol (UDP) ports, such as data that is sent from a Splunk forwarder from a remote computer. TCP inputs are separated into raw (unprocessed) and cooked (processed) inputs, with SSL as an option for either type.
-
Windows data—data from Windows computers, which includes:
- Windows event log data
- Windows Registry data
- Windows Management Instrumentation (WMI) data
- Active Directory data
- Performance monitoring (perfmon) data
-
Other data sources—data from custom apps, FIFO queues, scripts that get data from APIs, and other remote data interfaces and message queues.
Your data inputs and their configurations are saved in the inputs.conf(Opens new window) configuration file.
Indexes
The index stores compressed, raw event data. When receiving data from your inputs, Splunk parses the data into events and then indexes them, as follows:
- During parsing, Splunk extracts default fields, configures character-set encoding, identifies line termination, identifies timestamps (creating them if they aren't there), masks sensitive or private data, and can apply custom metadata. Parsing can be done by heavy forwarders. Universal forwarders do minimal parsing.
- During indexing, Splunk breaks events into segments, builds the index data structures, and writes the raw data and index files to disk.
Splunk can usually determine the data type and handle the data accordingly. But when setting up new inputs, you might consider sending data to a test index first to make sure everything is configured the way you want. You can delete the indexed data (clean the index) and start over as needed. Event processing rules are set in the props.conf(Opens new window) configuration file, which you'll need to modify directly if you want to reconfigure how events are processed.
Each index is stored as a collection of database directories (also known as buckets) in the file system, located in $SPLUNK_HOME/var/lib/splunk. Buckets are organized by age:
- Hot buckets are searchable, actively being written to, one per index. Hot buckets roll to warm at a certain size or when splunkd is restarted, then a new hot bucket is created.
- Warm buckets are searchable. Oldest warm buckets roll to cold when the number of warm buckets reaches a number limit.
- Cold buckets are searchable. After a set period of time, cold buckets roll to frozen.
- Frozen buckets are not searchable. These buckets are archived or deleted.
You can configure aspects such as the path configuration for your buckets. For example, keep the hot and warm buckets on a local computer for quick access, and put the cold and frozen buckets on a separate disks for long-term storage. You can also set the storage size.
By default, data is stored in the main index, but you can add more indexes for different data inputs. You might want multiple indexes to:
- Control user access. Users can search only in indexes they are allowed to by their assigned role.
- Accommodate varying retention policies. Set a different archive or retention policy by index.
- Speed searches in certain situations. Create dedicated indexes for each data source, search just in the index you want.
The input and index APIs
The classes for working with getting data into Splunk are:
- The splunklib.client.Inputs(Opens new window) class for the collection of inputs.
- The splunklib.client.Input(Opens new window) class for an individual input.
- The splunklib.client.Indexes(Opens new window) class for the collection of indexes.
- The splunklib.client.Index(Opens new window) class for an individual index.
Access these classes through an instance of the splunklib.client.Service(Opens new window) class. Retrieve a collection, and from there you can access individual items in the collection and create new ones. For example, here's a simplified program for getting a collection of inputs and creating a new one:
# Connect to Splunkservice = client.connect(...)# Get the collection of inputsmyinputs = service.inputs# Create an inputmynewinput = myinputs.create(name, inputkind, optional_arguments)
Code examples
This section provides examples of how to use the index and input APIs, assuming you first connect to a Splunk instance.
To list data inputs
This example shows how to use the splunklib.client.Inputs(Opens new window) class to retrieve the collection of data inputs that have been configured for Splunk and list them. For a list of available parameters to use when retrieving a collection, see "Collection parameters".
To be able to list inputs, the user's role must include those capabilities.
# Get the collection of data inputsinputs = service.inputs# List the inputs and kindfor item in inputs:print "%s (%s)" % (item.name, item.kind)
To create a new data input
This example shows how to create a data input. You'll need to provide an input kind and a name. The values for these depend on the kind of input you are creating:
Input | kind parameter | name parameter |
|---|---|---|
Active Directory | "ad" | The name of the configuration for a specific domain controller. |
Monitor | "monitor" | The file or directory path to monitor. |
Script | "script" | The name of the script. |
TCP cooked | "splunktcp" | The port number of the input. |
TCP raw | "tcp" | The port number of the input. |
UDP | "udp" | The port number of the input. |
Windows event log | "win-event-log-collections" | The name of the collection. |
Windows Perfmon | "win-perfmon" | The name of the collection. |
Windows Registry | "registry" | The name of the configuration stanza. |
WMI | "win-wmi-collections" | The name of the collection. |
Oneshot inputs are created differently.
This example shows how to create a tcp data input.
To be able to create and modify inputs, the user's role must include those capabilites. For a list of available capabilities, see Capabilities.
# Get the collection of data inputsinputs = service.inputs# Create a new TCP data inputtcpinput = inputs.create("9999", "tcp", host="sdk-test")print "New TCP input:", tcpinput.name
To view and modify the properties of a data input
This example continues from the previous example―it displays the properties of the new tcp input using the splunklib.client.Input(Opens new window) class then modifies a few properties. To make these changes to the server, call the update(Opens new window) method, then call the refresh(Opens new window) method to update your local object (allow time for the update between server and client to show up). For more about the properties you can set for different types of data inputs, see "Input parameters".
To be able to create and modify inputs, the user's role must include those capabilites. For a list of available capabilities, see Capabilities.
# Continue the previous example with the new tcpinput# Retrieve the new TCP data inputtcpinput = service.inputs["9999"]# Print the input's current propertiesprint tcpinput.nameprint "connection_host:", tcpinput["connection_host"]
To list indexes
This example shows how to use the splunklib.client.Indexes(Opens new window) class to retrieve and list the indexes that have been configured for Splunk, along with the number of events contained in each. For a list of available parameters to use when retrieving a collection, see "Collection parameters".
# Get the collection of indexesindexes = service.indexes# List the indexes and their event countsfor index in indexes:count = index["totalEventCount"]print "%s (events: %s)" % (index.name, count)
To create a new index
When you create an index, all you need to provide is a name. You can also specify additional properties at the same time by providing a dictionary of key-value pairs for the properties. Or, modify properties after you have created the index.
To be able to create and modify an index, the user's role must include those capabilites. For a list of available capabilities, see Capabilities.
This example shows how to create a new index.
If you are using a version of Splunk earlier than 5.0, you can't delete indexes using the SDK or the REST API—something to be aware of before creating lots of test indexes.
# Create a new indexmynewindex = service.indexes.create("test_index")
To add data directly to an index
There are different ways to add data directly to an index, without configuring a data input. First, retrieve an index using the splunklib.client.Index(Opens new window) class, and then use one of the following methods:
- Use the upload(Opens new window) method to upload a single file as an event stream for one-time indexing, which corresponds to a oneshot data input. You'll need to specify the file and path to upload, too.
- Use the submit(Opens new window) method to send an event over HTTP. You'll need to provide the event as a string, and specify values to apply to the event (
host,source, andsourcetype). - Use the attach(Opens new window) method to send events over a writeable socket. You can also specify the values to apply to these events (
host,source, andsourcetype).
Here is an example of uploading a single file:
# Create a oneshot input# Retrieve the index for the datamyindex = service.indexes["test_index"]# Create a variable with the path and filenameuploadme = "/Applications/Splunk/README-splunk.txt"
Here is an example of submitting an event over HTTP:
# Send an event over HTTP# Retrieve the index for the datamyindex = service.indexes["test_index"]# Submit an event over HTTPmyindex.submit("This is my HTTP event", sourcetype="access_combined.log", host="local")
Here is an example of sending event to an open socket:
# Send an event over an open socket# Retrieve the index for the datamyindex = service.indexes["test_index"]# Open a socketmysocket = myindex.attach()
To view and modify the properties of an index
This example shows how to view the properties of the index created in the previous example and modify its properties. For more about the available properties for indexes, see "Index parameters".
To be able to create and modify an index, the user's role must include those capabilites.
# Retrieve the new index and display some propertiesmynewindex = service.indexes["test_index"]print "coldPath:", mynewindex["coldPath"]print "currentDBSizeMB:", mynewindex["currentDBSizeMB"]print "frozenTimePeriodInSecs:", mynewindex["frozenTimePeriodInSecs"]print "homePath:", mynewindex["homePath"]print "maxDataSize:", mynewindex["maxDataSize"]
To clean events from an index
This example shows how to clean an index using the Index.clean(Opens new window) method, which removes the events from it. Note that this method can block up to the timeout period.
# Retrieve the indexmyindex = service.indexes["test_index"]# Display the current sizeprint "Current DB size:", myindex["currentDBSizeMB"], "MB"# Clean all events from the index and display its size againtimeout = 60
Input kinds
The Splunk Enterprise SDK for Python takes a kind parameter to several methods to specify the type of input, for example when you create a data input. Here are the values for kind:
Input kind | Description |
|---|---|
ad | Active Directory |
monitor | Files and directories |
registry | Windows Registry |
script | Scripts |
splunktcp | TCP, processed |
tcp | TCP, unprocessed |
udp | UDP |
win-event-log-collections | Windows event log |
win-perfmon | Performance monitoring |
win-wmi-collections | WMI |
Parameters
Here are the available parameters for inputs and indexes.
Collection parameters
By default, all entities are returned when you retrieve a collection. But by using the parameters below, you can also specify the number of entities to return and how to sort them. These parameters are available whenever you retrieve a collection:
Parameter | Description |
|---|---|
count | A number that indicates the maximum number of entities to return. |
offset | A number that specifies the index of the first entity to return. |
search | A string that specifies a search expression to filter the response with, matching field values against the search expression. For example, "search=foo" matches any object that has "foo" as a substring in a field, and "search=field_name%3Dfield_value" restricts the match to a single field. |
sort_dir | An enum value that specifies how to sort entities. Valid values are "asc" (ascending order) and "desc" (descending order). |
sort_key | A string that specifies the field to sort by. |
sort_mode | An enum value that specifies how to sort entities. Valid values are "auto", "alpha" (alphabetically), "alpha_case" (alphabetically, case sensitive), or "num" (numerically). |
Input parameters
The properties that are available for each type of data input corresponds to the parameters for the following data/inputs/* REST API endpoints:
- Active Directory: POST data/inputs/ad(Opens new window) endpoint
- files and directories (monitor): POST data/inputs/monitor(Opens new window) endpoint
- files and directories (oneshot): POST data/inputs/oneshot(Opens new window) endpoint
- scripts: POST data/inputs/script(Opens new window) endpoint
- TCP, cooked: POST data/inputs/tcp/cooked(Opens new window) endpoint
- TCP, raw: POST data/inputs/tcp/raw(Opens new window) endpoint
- TCP, SSL: POST data/inputs/tcp/ssl(Opens new window) endpoint
- UDP: POST data/inputs/udp(Opens new window) endpoint
- Windows event log: POST data/inputs/win-event-log-collections(Opens new window) endpoint
- Windows performance monitor: POST data/inputs/win-perfmon(Opens new window) endpoint
- Windows Registry: POST data/inputs/registry(Opens new window) endpoint
- WMI: POST data/inputs/win-wmi-collections(Opens new window) endpoint
Index parameters
The parameters you can use for indexes correspond to the parameters for the data/indexes endpoint(Opens new window) in the Splunk Enterprise REST API Reference Manual.