Tuesday, February 19, 2013

Splunk SDK for Python - Connecting to Splunk

I am going to try and cover my experience with the Splunk documentation here.

Connecting to Splunk via .py

If you have just installed the Splunk SDK for Python and you want to know how to connect to Splunk in the Python interpreter or through a python script, this is for you.

I used the saved_searches.py example as a starting place to see how python connects to Splunk.

Step 1) Use your preferred editor to modify ~/.splunkrc to contain your corresponding username, password, port and other configuration settings.

Note: "Storing login credentials in the .splunkrc file is only for convenience during development—this file isn't part of the Splunk platform and shouldn't be used for storing user credentials for production. And, if you're at all concerned about the security of your credentials, just enter them at the command line and don't bother using the .splunkrc file."

Step 2) Once you have that, test to make sure your authentication is correct by using the Splunk-Python interpreter located in the /examples/ folder:

 $ python spcmd.py
Welcome to Splunk SDK's Python interactive shell
admin connected to localhost:8089
>

If you can successfully connect there without passing any credentials at the command line than your .splunkrc file is set up correctly and you can continue. 

Step 3) Use your preferred editor to create a .py file.

Step 4) I used the saved_searches.py example as a template:

import sys, os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))

from splunklib.client import connect

try:
    from utils import parse
except ImportError:
    raise Exception("Add the SDK repository to your PYTHONPATH to run the examples "
                    "(e.g., export PYTHONPATH=~/splunk-sdk-python.")

def main():
    opts = parse(sys.argv[1:], {}, ".splunkrc")
    service = connect(**opts.kwargs)

    for app in service.apps:
        print app.name

if __name__ == "__main__":
    main()



Step 5) chmod 755 that .py file and execute it with: python filename.py

It should display all the installed apps on your Splunk instance. 

If it gives an error but you've already added PYTHONPATH to your splunk-sdk-python, try moving the .py file to a different folder. I store mine in ~/splunk-splunk-sdk-python/scripts/.


This is a brief example on how I connected to Splunk with Python. If you have a better way to do it, please share in the comments!


29 comments:

  1. brillant piece of information, I had come to know about your web-page from my friend hardkik, chennai,i have read atleast 9 posts of yours by now, and let me tell you, your webpage 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! Thanx a million once again, Regards,splunk training in hyderabad

    ReplyDelete

  2. Great article! Very informative describes your steps. All clear, thank you for sharing the experience, it was useful to know.
    Richard Brown data rooms

    ReplyDelete
  3. I have been reading out a lot of your articles.
    I will certainly bookmark your Blog.
    Free Devops Training
    Hybris Training
    Emc San Training
    Servicenow Training

    ReplyDelete
  4. My partner and I stumbled over here different website and thought I might as well check things out. I like what I see so now I’m following you. Look forward to checking out your web page repeatedly.

    python training in bangalore|

    ReplyDelete
  5. Clearly, it is the actual advisory article, all advice is familiar real. Thank you for this administration with us. Thank you so much for this information. Splunk Dashboard Input Dropdown

    ReplyDelete
  6. Nice! you are sharing such helpful and easy to understandable blog. i have no words for say i just say thanks because it is helpful for me


    Dot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery




    ReplyDelete
  7. Accessing a target account remotely. Downloading a user archive from a backup copy containing the following: username and password for login, Facebook Hacking APK

    ReplyDelete
  8. Tally ERP 9 Crack patch free download 2022 is enabling medium-sized companies to carry out daily management tasks within a friendly Crack Of Tally ERP 9

    ReplyDelete
  9. Amazing birthday wishes for best friend with his/her name edit online and make beautiful bday image and send to his or her to make birthday sepcial. Good Morning Wishes For Friends

    ReplyDelete
  10. I really appreciate your writing skill.Thanks for post.
    Python Course in Nagpur

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. informative blog, i would like to share my knowledge on python course in satara

    ReplyDelete
  13. great article.Thanks for sharing such an wonderful post with us.
    Python Training in Nagpur

    ReplyDelete
  14. This comment has been removed by the author.

    ReplyDelete