Sometimes, you just need that extra bit of logic or custom processing of data that might be very unique to your line of business. You might also simply be in a position where you have picky executives who like to see their data formatted in a very specific manner.
In this recipe, you will learn how to use Splunk's Python SDK to create a custom search command that you can use to apply consistent formatting to product names or any other string field, by capitalizing the first letter of each word in a string.
To step through this recipe, you will need a running Splunk Enterprise server, with the sample data loaded from Chapter 1, Play Time – Getting Data In. You should be familiar with navigating the Splunk user interface and using the Splunk search language. Some basic knowledge of Python is recommended. The Splunk Python SDK should also be downloaded and available on your Splunk Enterprise server.
Perform the steps in this recipe to create a custom search command to format product names:
splunklib
directory inside the $SPLUNK_HOME/etc/apps/ operational_intelligence/bin
directory.splunk-sdk-python/splunklib
directory to $SPLUNK_HOME/etc/apps/operational_intelligence/bin/splunklib
.commands.conf
, located in the $SPLUNK_HOME/etc/apps/operational_intelligence/local
directory:[fixname] filename = fixname.py supports_getinfo = true supports_rawargs = true outputheader = true requires_srinfo = true
$SPLUNK_HOME/etc/apps/operational_intelligence/bin
, create fixname.py
and add the following code:#!/usr/bin/env python import sys from splunklib.searchcommands import dispatch, StreamingCommand, Configuration, Option, validators @Configuration() class FixNameCommand(StreamingCommand): """ Takes the first letter of each word in the field and capitalizes it ##Syntax .. code-block:: fixname fieldname=<field> ##Description Takes the first letter of each word in the field and capitalizes it ##Example Uppercase the first letter of each word in the message field in the _internal index .. code-block:: index=_internal | head 20 | fixname fieldname=message """ fieldname = Option( doc=''' **Syntax:** **fieldname=***<fieldname>* **Description:** Name of the field that will be capitalized''', require=True, validate=validators.Fieldname()) def stream(self, records): self.logger.debug('FixNameCommand: %s' % self) # logs command line for record in records: record[self.fieldname] = record[self.fieldname].title() yield record dispatch(FixNameCommand, sys.argv, sys.stdin, sys.stdout, __name__)
fixname.py
script is marked as an executable by executing the following command:chmod a+x fixname.py
index=main sourcetype=log4j ProductName=* | eval ProductName=lower(ProductName) | fixname fieldname=ProductName
You should see that despite forcing the ProductName
field values to be all lowercase, the fixname
command has now capitalized each value.
The Splunk Python SDK can allow us to not only get information out of Splunk in an easy, programmatic way, but also manipulate the processing of events as they move through our search.
Originally, custom search commands could be created using Python and added to Splunk, but they were difficult to debug and had no logging mechanism. With the Python SDK, you can now create your own custom search commands in a quicker and easier way with better tools for troubleshooting.
Custom search commands come in three different flavors:
Command |
Description |
---|---|
Generating commands |
This type of command generates new events that are inserted into the results. Examples include commands that read from lookup files, such as |
Reporting commands |
This type of command takes incoming events and generates a new set of outgoing events, usually based on some sort of processing or analysis. Examples include commands that do statistics, such as |
Streaming commands |
This type of command takes incoming events and modifies or filters outgoing events. Examples include commands that add or replace fields or eliminate events based on some calculations, such as |
Let's explain how the fixname.py
script works:
The fixname
command is a straightforward command that leverages the title operation, available within the String
object in Python, to uppercase the string that is in the requested field. It is a streaming command, because it manipulates a field within an event as it moves through the command.
By leveraging the SDK, any number of commands can be developed that integrate with third-party systems or apply proprietary algorithms or logic to implement business rules that give organizations better visibility of their operations.
For more information on how to create custom search commands, check out the documentation at http://dev.splunk.com.
3.138.105.31