Splunk Dev

Key Error in Python Splunk SDK while accessing KV store

manideveloper4u
Engager

Hi,

I'm getting Key Error while accessing splunk kv store using splunk python sdk.
Python Version: 2.7
Splunk sdk version : 1.6.6
OS: Windows 7 & 10

Code:

import sys, json
from splunklib.client import connect 
import getpass


def main():
    #Getting Splunk access details from User
    hostname = raw_input("Enter Splunk Hostname/IP: ")
    splunkUser = raw_input("Enter Splunk Admin Username: ")
    splunkPassword = getpass.getpass("Enter Splunk Admin Password: ")
    HOST = hostname
    PORT = 8089
    USERNAME = splunkUser
    PASSWORD = splunkPassword
    #Connecting splunk server
    splunk_service = connect(host=HOST,port=PORT,username=USERNAME,password=PASSWORD)
    print("Splunk connection succeed !!!!")
    splunk_service.namespace['owner'] = 'Nobody'
    collection_name="test"
    collection=splunk_service.kvstore[collection_name]

    if collection_name in splunk_service.kvstore:
        print("{} Already exist !!!".format(collection_name))
    else:
        splunk_service.kvstore.create(collection_name)

    #Adding data to KV store
    collection.data.insert(json.dumps({"ID":"123","sId":1234,"Tech":"text"}))

    #Retriving data from kv store
    kv_data=json.dumps(collection.data.query(),indent=1)
    print(kv_data)

if __name__ == '__main__':
    main()

Error:
Traceback (most recent call last):
File "kv_store_opps.py", line 36, in
main()
File "kv_store_opps.py", line 21, in main
collection=splunk_service.kvstore[collection_name]
File "C:\Python27\lib\site-packages\splunklib\client.py", line 1243, in getitem
raise KeyError(key)
KeyError: UrlEncoded('test')

Any help on this much appreciated.

Thanks,
Mani

Labels (3)
0 Karma

manikandankasi
Explorer

Hi,
you should first check kv store exist or not. if exist then you can access the kv store otherwise you can create. but in your code you try to accessing kvstore before checking kv store exist or not. so the problem here is test kvstore does not exist in splunk collection thats why getting key error.
i have modified the code below. hope this should work!!!.

     collection_name="test"
     if collection_name in splunk_service.kvstore:
         print("{} Already exist !!!".format(collection_name))
     else:
         splunk_service.kvstore.create(collection_name)

     #Adding data to KV store
     collection.data.insert(json.dumps({"ID":"123","sId":1234,"Tech":"text"}))

     collection=splunk_service.kvstore[collection_name]
0 Karma
Get Updates on the Splunk Community!

Security Professional: Sharpen Your Defenses with These .conf25 Sessions

Sooooooooooo, guess what. .conf25 is almost here, and if you're on the Security Learning Path, this is your ...

First Steps with Splunk SOAR

Our first step was to gather a list of the playbooks we wanted and to sort them by priority.  Once this list ...

How To Build a Self-Service Observability Practice with Splunk Observability Cloud

If you’ve read our previous post on self-service observability, you already know what it is and why it ...