Splunk Search

Custom command with splunklib not working

VatsalJagani
SplunkTrust
SplunkTrust

I want to write custom search command with one argument(option). Below is the code that I've written, but I'm not getting any result with search command.

commands.conf

[test]
type = python
filename = test_code.py
supports_getinfo = true
supports_rawargs = true

authorization.conf

[capability::run_script_shape]

[role_admin]
run_script_shape= enabled

test_code.py

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__)

When I'm trying to execute below command

| test count=5

I get below errors and no result.

2 errors occurred while the search was executing. Therefore, search results might be incomplete. Hide errors.
Unable to access 'count=5': No such file or directory
[<server>.local] Unable to access 'count=5': No such file or directory

Please help me to understand error or give me better example to create non streaming custom command with argument with splunklib.

0 Karma
1 Solution

kamlesh_vaghela
SplunkTrust
SplunkTrust

HI @VatsalJagani,

As I found that you trying to replicate generating example from https://www.splunk.com/blog/2014/04/14/building-custom-search-commands-in-python-part-i-a-simple-gen....

I've checked your code.
1) Have you placed Python SDK library in lib folder?
2) Can you please verify the naming convention of the command name, python file name and the Class name defined in above link.

Thanks

View solution in original post

kamlesh_vaghela
SplunkTrust
SplunkTrust

HI @VatsalJagani,

As I found that you trying to replicate generating example from https://www.splunk.com/blog/2014/04/14/building-custom-search-commands-in-python-part-i-a-simple-gen....

I've checked your code.
1) Have you placed Python SDK library in lib folder?
2) Can you please verify the naming convention of the command name, python file name and the Class name defined in above link.

Thanks

Get Updates on the Splunk Community!

Index This | Forward, I’m heavy; backward, I’m not. What am I?

April 2024 Edition Hayyy Splunk Education Enthusiasts and the Eternally Curious!  We’re back with another ...

A Guide To Cloud Migration Success

As enterprises’ rapid expansion to the cloud continues, IT leaders are continuously looking for ways to focus ...

Join Us for Splunk University and Get Your Bootcamp Game On!

If you know, you know! Splunk University is the vibe this summer so register today for bootcamps galore ...