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!

Join Us at the Builder Bar at .conf24 – Empowering Innovation and Collaboration

What is the Builder Bar? The Builder Bar is more than just a place; it's a hub of creativity, collaboration, ...

Combine Multiline Logs into a Single Event with SOCK - a Guide for Advanced Users

This article is the continuation of the “Combine multiline logs into a single event with SOCK - a step-by-step ...

Everything Community at .conf24!

You may have seen mention of the .conf Community Zone 'round these parts and found yourself wondering what ...