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!

Now Available: Cisco Talos Threat Intelligence Integrations for Splunk Security Cloud ...

At .conf24, we shared that we were in the process of integrating Cisco Talos threat intelligence into Splunk ...

Preparing your Splunk Environment for OpenSSL3

The Splunk platform will transition to OpenSSL version 3 in a future release. Actions are required to prepare ...

Easily Improve Agent Saturation with the Splunk Add-on for OpenTelemetry Collector

Agent Saturation What and Whys In application performance monitoring, saturation is defined as the total load ...