Wednesday, March 26, 2014

Splunk - Basic Custom Search Command Example

Basic Custom Search Command Example(s)

Executing an arbitrary shell script w/o parameters

For this exercise, we will be executing a very basic script with no Splunk parameters. The purpose of this is to execute a python/shell script. You could just execute a shell script directly, but in the likely chance that you will eventually pass data/query, I'm using Python to execute a shell script.
  • Create your test script in /$SPLUNKHOME/etc/apps/<appname>/bin/test.py
  • Example test.py code:
import os
os.system("(cd /splunkscripts/; ./test.sh)")
  • This navigates to a directory I created at the root directory called "splunkscripts" where I house all of the various scripts I use related to Splunk. It then executes test.sh.
  • Example test.sh code:
echo "This is a successful test." > splunktest.txt

  • This will echo this "Hello World" test to splunktest.txt. 
  • Make sure both scripts (test.py and test.sh) are executable via chmod (i.e. chmod 755 )
  • Edit your /$SPLUNKHOME/etc/apps/<appname>/local/commands.conf with the following:

[shelltest]

type = python
filename = test.py
generating = false
streaming = false
retainsevents = false

  • Note: Generating/Streaming/Retainsevents all default to false, but for real world uses you will likely end up generating results. Be aware of these. Read the Splunk docs on custom searches as well: http://docs.splunk.com/Documentation/Splunk/latest/Search/Customsearchcommandexample 
  • Restart Splunk.
  • Go to your appropriate Splunk app where you stored this script and search: | shelltest
  • Navigate to /splunkscripts/ and see if your test.sh wrote out the data to the splunktest.txt.
  • If you get an "Error Code 1", then there is an issue with your Python/Shell code.


 


 

2 comments:

  1. I think this is interesting articles and Business ethics for new information's, and i like that kind of information.So the i like that post,because all of given information was very excellent.Devops jobs in Hyderabad

    ReplyDelete
  2. wonderful information, I had come to know about your blog from my friend nandu , hyderabad,i have read atleast 7 posts of yours by now, and let me tell you, your website gives the best and the most interesting information. This is just the kind of information that i had been looking for, i'm already your rss reader now and i would regularly watch out for the new posts, once again hats off to you! Thanks a ton once again, Regards, Sailpoint Online Training

    ReplyDelete