Extending SPL With Custom Search Commands and The Splunk SDK For Python
Extending SPL With Custom Search Commands and The Splunk SDK For Python
2017/08/11 | Washington, DC
Forward-Looking Statements
During the course of this presentation, we may make forward-looking statements regarding future events or
the expected performance of the company. We caution you that such statements reflect our current
expectations and estimates based on factors currently known to us and that actual events or results could
differ materially. For important factors that may cause actual results to differ from those contained in our
forward-looking statements, please review our filings with the SEC.
The forward-looking statements made in this presentation are being made as of the time and date of its live
presentation. If reviewed after its live presentation, this presentation may not contain current or accurate
information. We do not assume any obligation to update any forward looking statements we may make. In
addition, any information about our roadmap outlines our general product direction and is subject to change
at any time without notice. It is for informational purposes only and shall not be incorporated into any contract
or other commitment. Splunk undertakes no obligation either to develop the features or functionality
described or to include any such feature or functionality in a future release.
Splunk, Splunk>, Listen to Your Data, The Engine for Machine Data, Splunk Cloud, Splunk Light and SPL are trademarks and registered trademarks of Splunk Inc. in
the United States and other countries. All other brand names, product names, or trademarks belong to their respective owners. © 2017 Splunk Inc. All rights reserved.
Who am I?
3
Agenda
4
Introduction to
Custom Search
Commands
What is a Custom Search Command?
7
8
What is a Custom Search Command?
Partners
– Concanon, etc.
Customers
– Use-case specific analytics
Splunk!
– predict command
– IT Service Intelligence
– Enterprise Security
– DB Connect
– Machine Learning Toolkit
12
Parsing #1: Split search into commands
| inputlookup geo_attr_us_states.csv | GOCRAZY | head 5
inputlookup geo_attr_us_states.csv
GOCRAZY head 5
13
Parsing #2: Look for custom search commands
| inputlookup geo_attr_us_states.csv | GOCRAZY | head 5
GOCRAZY head 5
14
Parsing #3: Spawn external process
| inputlookup geo_attr_us_states.csv | GOCRAZY | head 5
inputlookup geo_attr_us_states.csv
GOCRAZY head 5
$SPLUNK_HOME/bin/python gocrazy.py
15
Parsing #4: Let external process parse arguments
| inputlookup geo_attr_us_states.csv | GOCRAZY | head 5
inputlookup geo_attr_us_states.csv
GOCRAZY head 5
$SPLUNK_HOME/bin/python gocrazy.py
16
Search: Pipe results through external process
| inputlookup geo_attr_us_states.csv | GOCRAZY | head 5
inputlookup geo_attr_us_states.csv
GOCRAZY head 5
$SPLUNK_HOME/bin/python gocrazy.py
17
Recap: high-level concepts
18
Custom Commands: low-level details
19
splunkd ⬌ custom command
Version 2 protocol
– Spawns external process once, streams results through chunk by chunk
– Simple commands.conf configuration
ê “chunked=true”
– Support for platform-specific programs
Version 1 protocol
– Spawns external process for each chunk of search results (!)
– “Transforming” commands limited to 50,000 events
20
Search Command protocol comparison
Protocol APIs Performance Scalability Simple Platform- Programming
configuration specific languages
programs
21
Search Command Protocol Version 2
• Transaction-oriented
• splunkd sends a command, external process responds with reply
22
Transport “chunk”
Metadata length Data length
24
Protocol Version 2: Transaction timeline
… ✘
25
“getinfo” command
26
Sample “getinfo” metadata
{
"action": "getinfo",
"streaming_command_will_restart": false,
"searchinfo": {
"earliest_time": "0",
"raw_args": [
"LinearRegression", "petal_length", "from", "petal_width”
],
"session_key": "...",
"maxresultrows": 50000,
"args": [
"LinearRegression", "petal_length", "from", "petal_width”
],
"dispatch_dir": "/Users/jleverich/builds/conf_mlapp_demo/var/run/splunk/dispatch/1475007525.265",
"command": "fit",
"latest_time": "0",
"sid": "1475007525.265",
"splunk_version": "6.5.0",
"username": "admin",
"search": "%7C%20inputlookup%20iris.csv%20%7C%20fit%20LinearRegression%20petal_length%20from%20petal_width",
"splunkd_uri": "https://127.0.0.1:8090",
"owner": "admin",
"app": "Splunk_ML_Toolkit”
},
"preview": false
}
“execute” command
28
Types of Search
Commands
Types of Search Commands
“Streaming” commands
“Transforming” commands
– “Events” commands
– “Reporting” commands
“Streaming” commands
Examples:
– eval
– where
– rex
“Streaming” command example
Remote results
... | eval foo=“bar” | ...
32
“Stateful Streaming” commands
Examples:
– accum
– streamstats
– dedup
“Stateful Streaming” command example
... | accum foo | ...
34
“Events” commands
Examples:
– sort
– eventstats
“Events” command example
... | sort field_A | ...
36
“Reporting” commands
Examples:
– stats
– timechart
– transpose
“Reporting” command example
... | stats count | ...
38
Beware of large result sets!
39
Streaming “pre-op”
... | stats count | ... ... | prestats count | stats count | ...
40
Implementing Custom
Search Commands
with the Splunk SDK
for Python
41
Basic steps to create a search command
1. Create an “App”
2. Deploy the Python SDK for Splunk in the bin directory
3. Write a script for your Custom Search Command
4. Register your command in commands.conf
5. Restart Splunk Enterprise
6. (optional) Export the command to other apps
Create an “App”
43
Deploy the Python SDK in the bin directory
cd $SPLUNK_HOME/etc/apps/MyNewApp/bin
44
Write a script for your Custom Search Command
$SPLUNK_HOME/etc/apps/MyNewApp/bin/foobar.py
import sys
from splunklib.searchcommands import dispatch, StreamingCommand, Configuration
@Configuration()
class FoobarCommand(StreamingCommand):
def stream(self, records):
for record in records:
record['foo'] = 'bar'
yield record
if __name__ == "__main__":
dispatch(FoobarCommand, sys.argv, sys.stdin, sys.stdout, __name__)
45
Register your command in commands.conf
$SPLUNK_HOME/etc/apps/MyNewApp/default/commands.conf
[foobar]
chunked=true
# filename=foobar.py ## <--- optional
46
Restart Splunk Enterprise
$SPLUNK_HOME/bin/splunk restart
47
Export to other apps (optional)
48
Export to other apps (optional)
49
Export to other apps (optional)
50
Example Streaming Command
$SPLUNK_HOME/etc/apps/MyNewApp/bin/exstream.py
import sys
from splunklib.searchcommands import dispatch, StreamingCommand, Configuration
@Configuration()
class ExStreamCommand(StreamingCommand):
def stream(self, records):
for record in records:
record['foo'] = 'bar'
yield record
if __name__ == "__main__":
dispatch(ExStreamCommand, sys.argv, sys.stdin, sys.stdout, __name__)
51
Example Stateful Streaming Command
$SPLUNK_HOME/etc/apps/MyNewApp/bin/exstateful.py
import sys
from splunklib.searchcommands import dispatch, StreamingCommand, Configuration
@Configuration(local=True)
class ExStatefulCommand(StreamingCommand):
def stream(self, records):
for record in records:
record['foo'] = 'bar'
yield record
if __name__ == "__main__":
dispatch(ExStatefulCommand, sys.argv, sys.stdin, sys.stdout, __name__)
52
Example Events Command
$SPLUNK_HOME/etc/apps/MyNewApp/bin/exevents.py
import sys
from splunklib.searchcommands import dispatch, EventingCommand, Configuration
@Configuration()
class ExEventsCommand(EventingCommand):
def transform(self, records):
l = list(records)
l.sort(key=lambda r: r['_raw'])
return l
if __name__ == "__main__":
dispatch(ExEventsCommand, sys.argv, sys.stdin, sys.stdout, __name__)
53
Example Reporting Command
$SPLUNK_HOME/etc/apps/MyNewApp/bin/exreport.py
import sys
from splunklib.searchcommands import dispatch, ReportingCommand, Configuration
@Configuration()
class ExReportCommand(ReportingCommand):
@Configuration()
def map(self, records):
return records
if __name__ == "__main__":
dispatch(ExReportCommand, sys.argv, sys.stdin, sys.stdout, __name__)
54
A little advice
55
What Now?
https://github.com/splunk/splunk-sdk-python
– https://github.com/splunk/splunk-sdk-python/tree/master/examples/searchcommands_app
56
© 2017 SPLUNK INC.
Thank You
Don't forget to rate this session in the
.conf2017 mobile app
Q&A
Backup Slides
Streaming Commands only serialize required fields
{“required_fields”: [“fieldX”], …}
Result set
• Supports
In splunkd To external process – Removing events
– Adding events
New field
Slice
+ – Editing fields
idx
idx
Result set
– Adding fields
• Can’t re-order events
New field
Result set
Performance comparison
2.5 million events
180
160
140
Runtime (seconds)
120
100
Splunk
80 Protocol v1
60 Protocol v2
40
20
0
Echo Echo (CSV) Echo | where
(selected)
“Streaming” command example
... | eval foo=“bar” | ...
63