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!

Webinar Recap | Revolutionizing IT Operations: The Transformative Power of AI and ML ...

The Transformative Power of AI and ML in Enhancing Observability   In the realm of IT operations, the ...

.conf24 | Registration Open!

Hello, hello! I come bearing good news: Registration for .conf24 is now open!   conf is Splunk’s rad annual ...

ICYMI - Check out the latest releases of Splunk Edge Processor

Splunk is pleased to announce the latest enhancements to Splunk Edge Processor.  HEC Receiver authorization ...