Custom search commands in our Python SDK allow you to extend Splunk’s search language and teach it new capabilities. In this and other upcoming posts we’re going to look at how to develop several different search commands to illustrate what you can do with this.
In this post, we’re going to focus on building a very basic Generating command. A generating command generates events which can be from any source, for example an internal system, or an external API. We’re going to create a GenerateHello command that will generate Hello World events based on a supplied count. The command is not very useful in itself, but it is a quick way to see how you can author custom commands.
Below is a screenshot of using the command we’re going to build. As you can see it outputs a series of “Hello World” events.
Custom search commands are deployed via a Splunk application. As with any Splunk app there is a specific file layout and some configuration files that are required. Fortunately the Splunk SDK for Python includes a template which you can use as a start point. Here are the steps to create a new app using the template.
Now we need to do some search and replacing in the template. Edit bin/generatehello.py, and app.conf, commands, conf and logging.conf in the default folder.
Authoring a search command involves 2 main steps, first specify parameters for the search command, second implement the generate() function with logic which creates events and returns them to Splunk.
Edit generatehello.py in the bin folder and paste the following code:
import sys, time from splunklib.searchcommands import \ dispatch, GeneratingCommand, Configuration, Option, validators @Configuration() class GenerateHelloCommand(GeneratingCommand): count = Option(require=True, validate=validators.Integer()) def generate(self): for i in range(1, self.count + 1): text = 'Hello World %d' % i yield {'_time': time.time(), 'event_no': i, '_raw': text } dispatch(GenerateHelloCommand, sys.argv, sys.stdin, sys.stdout, __name__)
If you try to run without passing count, you’ll get an error as count is required:
And if a letter is provided for count, it will also fail as it is not an Integer:
Finally, any error output that is generated is also available within the log file which is in the root of the application:
Now that we can see that the command is working we can test it out in Splunk. If your Splunk instance is already running, you’ll need to restart it to have it load the new command either using the Splunk CLI or from Splunk UI.
Once you have logged in to the instance you should the new app has been loaded. Notice below the “Generate Hello” app:
Clicking on that app will then take you to the search screen. Entering “| helloworld count=5” at the search bar should return the expected “Hello World” events as in the screenshot below. You can see Splunk has picked up our events!
You can see in the screenshot that the event_no field has also been picked up, allowing it to be selected if the view is switched to “Table”
It’s that easy!
Now that the search command is complete, the app can be packaged and up as any other application and then published on Splunk Apps or put on a share where it can be imported into a Splunk instance. You can also configure permissions for the app, or change it’s visibility to allow the commands to be accessible from anywhere within Splunk.
In this post we built a very simple example of a generating command, but even in its simplicity, it shows its power!
You can find a working version of the command we created in this post, along with instructions on installation in our Python SDK source on Github in the examples folder.
Enjoy!
----------------------------------------------------
Thanks!
Glenn Block
The Splunk platform removes the barriers between data and action, empowering observability, IT and security teams to ensure their organizations are secure, resilient and innovative.
Founded in 2003, Splunk is a global company — with over 7,500 employees, Splunkers have received over 1,020 patents to date and availability in 21 regions around the world — and offers an open, extensible data platform that supports shared data across any environment so that all teams in an organization can get end-to-end visibility, with context, for every interaction and business process. Build a strong data foundation with Splunk.