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!

.conf24 | Day 0

Hello Splunk Community! My name is Chris, and I'm based in Canberra, Australia's capital, and I travelled for ...

Enhance Security Visibility with Splunk Enterprise Security 7.1 through Threat ...

(view in My Videos)Struggling with alert fatigue, lack of context, and prioritization around security ...

Troubleshooting the OpenTelemetry Collector

  In this tech talk, you’ll learn how to troubleshoot the OpenTelemetry collector - from checking the ...