<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Why does my python custom reporting command work only if I use a stats command in the pipeline before? in Splunk Search</title>
    <link>https://community.splunk.com/t5/Splunk-Search/Why-does-my-python-custom-reporting-command-work-only-if-I-use-a/m-p/349675#M103500</link>
    <description>&lt;P&gt;No, I haven't removed the condition in the prepare fuction...&lt;BR /&gt;
The (reporting command) template show nothing about it:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;#!/usr/bin/env python

import sys
from splunklib.searchcommands import \
    dispatch, ReportingCommand, Configuration, Option, validators


@Configuration()
class %(command.title())Command(ReportingCommand):
    """ %(synopsis)

    ##Syntax

    %(syntax)

    ##Description

    %(description)

    """
    @Configuration()
    def map(self, events):
        # Put your streaming preop implementation here, or remove the map method,
        # if you have no need for a streaming preop
        pass

    def reduce(self, events):
        # Put your reporting implementation
        pass

dispatch(%(command.title())Command, sys.argv, sys.stdin, sys.stdout, __name__)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;and the prepare function is only found in the ReportingCommand class (splunklib/searchcommands/reporting_command.py) without info about overriding the prepare function...&lt;/P&gt;

&lt;P&gt;I tried to remove the map section completely, or keep the return only, but it doesn't help. It triggers other errors:&lt;/P&gt;

&lt;UL&gt;
&lt;LI&gt;&lt;P&gt;with only return: &lt;/P&gt;

&lt;P&gt;/splunklib/searchcommands/internals.py", line 519 : 'NotImplementedType' object is not iterable &lt;/P&gt;&lt;/LI&gt;
&lt;/UL&gt;

&lt;P&gt;(which shows that it tries to use the map() function of the base class, which returns "NotImplemented")&lt;/P&gt;

&lt;UL&gt;
&lt;LI&gt;without the if phase=='map' section:&lt;/LI&gt;
&lt;/UL&gt;

&lt;P&gt;It triggers the exception, as the phase is not recognised.&lt;/P&gt;

&lt;P&gt;So basically, I think I need to implement a dummy map function that does nothing. Any idea how this would look like?&lt;/P&gt;</description>
    <pubDate>Mon, 13 Mar 2017 07:22:07 GMT</pubDate>
    <dc:creator>grundsch</dc:creator>
    <dc:date>2017-03-13T07:22:07Z</dc:date>
    <item>
      <title>Why does my python custom reporting command work only if I use a stats command in the pipeline before?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-does-my-python-custom-reporting-command-work-only-if-I-use-a/m-p/349672#M103497</link>
      <description>&lt;P&gt;I'm trying to write a new custom search command, more specifically a reporting command. I'm using the Python SDK 1.6.2.&lt;BR /&gt;
I took the searchcommand template report.py. I removed the map function, as I want to do only a reduce operation.&lt;/P&gt;

&lt;P&gt;If I run my custom reporting command in a search after a &lt;CODE&gt;stats&lt;/CODE&gt; command, it works pretty well.&lt;/P&gt;

&lt;P&gt;If I run it as the first reporting command, I get the following error:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;External search command 'toxlsx' returned error code 1. Script output = "error_message=AttributeError at "/opt/splunk/etc/apps/ta-toxlsx/bin/splunklib/searchcommands/reporting_command.py", line 88 : 'function' object has no attribute 'ConfigurationSettings' "
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I have the following decorator for the reporting command class:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;@Configuration(run_in_preview=False)
class toxlsxCommand(ReportingCommand):
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;and nothing for the reduce:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt; @Configuration()
 def reduce(self, events):
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Any idea what's going on here?&lt;/P&gt;</description>
      <pubDate>Thu, 09 Mar 2017 13:26:48 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-does-my-python-custom-reporting-command-work-only-if-I-use-a/m-p/349672#M103497</guid>
      <dc:creator>grundsch</dc:creator>
      <dc:date>2017-03-09T13:26:48Z</dc:date>
    </item>
    <item>
      <title>Re: Why does my python custom reporting command work only if I use a stats command in the pipeline before?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-does-my-python-custom-reporting-command-work-only-if-I-use-a/m-p/349673#M103498</link>
      <description>&lt;P&gt;any chance to see a bit more code for reporting_command.py?&lt;/P&gt;</description>
      <pubDate>Thu, 09 Mar 2017 20:46:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-does-my-python-custom-reporting-command-work-only-if-I-use-a/m-p/349673#M103498</guid>
      <dc:creator>koshyk</dc:creator>
      <dc:date>2017-03-09T20:46:34Z</dc:date>
    </item>
    <item>
      <title>Re: Why does my python custom reporting command work only if I use a stats command in the pipeline before?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-does-my-python-custom-reporting-command-work-only-if-I-use-a/m-p/349674#M103499</link>
      <description>&lt;P&gt;You removed the map function, but did you remove the &lt;CODE&gt;if phase=='map':&lt;/CODE&gt; conditional statement in the prepare function?&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;    def map(self, records):
        """ Override this method to compute partial results.
        :param records:
        :type records:
        You must override this method, if :code:`requires_preop=True`.
        """
        return NotImplemented

    def prepare(self):

        phase = self.phase

        if phase == 'map':
            # noinspection PyUnresolvedReferences
            self._configuration = self.map.ConfigurationSettings(self)
            return

        if phase == 'reduce':
            streaming_preop = chain((self.name, 'phase="map"', str(self._options)), self.fieldnames)
            self._configuration.streaming_preop = ' '.join(streaming_preop)
            return

        raise RuntimeError('Unrecognized reporting command phase: {}'.format(json_encode_string(unicode(phase))))
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;This would explain why it works after a stats (phase=reduce at that point) and not before a stats because it would still possibly be in a mapping phase at that time.&lt;/P&gt;</description>
      <pubDate>Sun, 12 Mar 2017 17:20:14 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-does-my-python-custom-reporting-command-work-only-if-I-use-a/m-p/349674#M103499</guid>
      <dc:creator>jkat54</dc:creator>
      <dc:date>2017-03-12T17:20:14Z</dc:date>
    </item>
    <item>
      <title>Re: Why does my python custom reporting command work only if I use a stats command in the pipeline before?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-does-my-python-custom-reporting-command-work-only-if-I-use-a/m-p/349675#M103500</link>
      <description>&lt;P&gt;No, I haven't removed the condition in the prepare fuction...&lt;BR /&gt;
The (reporting command) template show nothing about it:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;#!/usr/bin/env python

import sys
from splunklib.searchcommands import \
    dispatch, ReportingCommand, Configuration, Option, validators


@Configuration()
class %(command.title())Command(ReportingCommand):
    """ %(synopsis)

    ##Syntax

    %(syntax)

    ##Description

    %(description)

    """
    @Configuration()
    def map(self, events):
        # Put your streaming preop implementation here, or remove the map method,
        # if you have no need for a streaming preop
        pass

    def reduce(self, events):
        # Put your reporting implementation
        pass

dispatch(%(command.title())Command, sys.argv, sys.stdin, sys.stdout, __name__)
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;and the prepare function is only found in the ReportingCommand class (splunklib/searchcommands/reporting_command.py) without info about overriding the prepare function...&lt;/P&gt;

&lt;P&gt;I tried to remove the map section completely, or keep the return only, but it doesn't help. It triggers other errors:&lt;/P&gt;

&lt;UL&gt;
&lt;LI&gt;&lt;P&gt;with only return: &lt;/P&gt;

&lt;P&gt;/splunklib/searchcommands/internals.py", line 519 : 'NotImplementedType' object is not iterable &lt;/P&gt;&lt;/LI&gt;
&lt;/UL&gt;

&lt;P&gt;(which shows that it tries to use the map() function of the base class, which returns "NotImplemented")&lt;/P&gt;

&lt;UL&gt;
&lt;LI&gt;without the if phase=='map' section:&lt;/LI&gt;
&lt;/UL&gt;

&lt;P&gt;It triggers the exception, as the phase is not recognised.&lt;/P&gt;

&lt;P&gt;So basically, I think I need to implement a dummy map function that does nothing. Any idea how this would look like?&lt;/P&gt;</description>
      <pubDate>Mon, 13 Mar 2017 07:22:07 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-does-my-python-custom-reporting-command-work-only-if-I-use-a/m-p/349675#M103500</guid>
      <dc:creator>grundsch</dc:creator>
      <dc:date>2017-03-13T07:22:07Z</dc:date>
    </item>
    <item>
      <title>Re: Why does my python custom reporting command work only if I use a stats command in the pipeline before?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-does-my-python-custom-reporting-command-work-only-if-I-use-a/m-p/349676#M103501</link>
      <description>&lt;P&gt;It looks like the map function is missing a decorator, causing the AttributeError provided in the question:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;@Configuration()
def map(self, records):
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;You're doing the right thing with the class decorator, in specifying &lt;CODE&gt;requires_preop=False&lt;/CODE&gt;, but be sure to &lt;CODE&gt;return NotImplemented&lt;/CODE&gt; in the map function.&lt;/P&gt;

&lt;P&gt;Although it uses a map function, looking at the example 'sum' reporting command may also help: &lt;A href="https://github.com/splunk/splunk-sdk-python/blob/master/examples/searchcommands_app/package/bin/sum.py"&gt;https://github.com/splunk/splunk-sdk-python/blob/master/examples/searchcommands_app/package/bin/sum.py&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 03 Apr 2017 12:05:42 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-does-my-python-custom-reporting-command-work-only-if-I-use-a/m-p/349676#M103501</guid>
      <dc:creator>doksu</dc:creator>
      <dc:date>2017-04-03T12:05:42Z</dc:date>
    </item>
    <item>
      <title>Re: Why does my python custom reporting command work only if I use a stats command in the pipeline before?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-does-my-python-custom-reporting-command-work-only-if-I-use-a/m-p/349677#M103502</link>
      <description>&lt;P&gt;I added a map function, but it leads to the same error:&lt;/P&gt;

&lt;P&gt;External search command 'toxlsx' returned error code 1. Script output = "error_message=TypeError at "/opt/splunk-6.5.2/etc/apps/ta-toxls/bin/splunklib/searchcommands/internals.py", line 519 : 'NotImplementedType' object is not iterable "&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;@Configuration()
def map(self, records):
    return NotImplemented
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 04 Apr 2017 06:22:30 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-does-my-python-custom-reporting-command-work-only-if-I-use-a/m-p/349677#M103502</guid>
      <dc:creator>grundsch</dc:creator>
      <dc:date>2017-04-04T06:22:30Z</dc:date>
    </item>
    <item>
      <title>Re: Why does my python custom reporting command work only if I use a stats command in the pipeline before?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-does-my-python-custom-reporting-command-work-only-if-I-use-a/m-p/349678#M103503</link>
      <description>&lt;P&gt;So, I added a map function that returns all records:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;@Configuration()
def map(self, records):
    return records
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;It seems to work (no errors, my script receives all data and works as expected), &lt;STRONG&gt;but I have absolutely no idea what I'm doing&lt;/STRONG&gt;.&lt;BR /&gt;
Is that the correct way to not do anything in the map phase?  (especially, what is the difference with yield() vs what I'm doing here?)&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2017 06:54:07 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-does-my-python-custom-reporting-command-work-only-if-I-use-a/m-p/349678#M103503</guid>
      <dc:creator>grundsch</dc:creator>
      <dc:date>2017-04-04T06:54:07Z</dc:date>
    </item>
    <item>
      <title>Re: Why does my python custom reporting command work only if I use a stats command in the pipeline before?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-does-my-python-custom-reporting-command-work-only-if-I-use-a/m-p/349679#M103504</link>
      <description>&lt;P&gt;Hi grundsch,&lt;/P&gt;

&lt;P&gt;Would it be possible if you shared you code please?&lt;BR /&gt;
I've been stuck on creating a reporting command for a few weeks now. I'm getting the following error even though I've tried doing what you said above:&lt;BR /&gt;
Error: “ERROR dispatchRunner - RunDispatch::runDispatchThread threw error: Search Factory: Unknown search command 'my'.”&lt;/P&gt;

&lt;P&gt;Any help would be much appreciated.&lt;/P&gt;

&lt;P&gt;Thanks,&lt;BR /&gt;
Richie.&lt;/P&gt;</description>
      <pubDate>Mon, 03 Feb 2020 16:42:05 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-does-my-python-custom-reporting-command-work-only-if-I-use-a/m-p/349679#M103504</guid>
      <dc:creator>richielynch89</dc:creator>
      <dc:date>2020-02-03T16:42:05Z</dc:date>
    </item>
    <item>
      <title>Re: Why does my python custom reporting command work only if I use a stats command in the pipeline before?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-does-my-python-custom-reporting-command-work-only-if-I-use-a/m-p/349680#M103505</link>
      <description>&lt;P&gt;Hi Richie,&lt;BR /&gt;
looks like you have another problem. Did you configure the command in the /local/commands.conf correctly?&lt;BR /&gt;
i.e. something like:&lt;BR /&gt;
    [toxlsx]&lt;BR /&gt;
    filename = toxlsx.py&lt;BR /&gt;
    requires_srinfo = true&lt;BR /&gt;
    stderr_dest = message&lt;BR /&gt;
    supports_getinfo = true&lt;BR /&gt;
    supports_rawargs = true&lt;BR /&gt;
    supports_multivalues = true&lt;BR /&gt;
    run_in_preview = false&lt;/P&gt;

&lt;P&gt;(here my command is named toxlsx, and the file name and class name muss match).&lt;BR /&gt;
You need to restart Splunk to let it know about the new command.&lt;BR /&gt;
If it still doesn't work, try to make the example commands in the SDK, without changes.&lt;/P&gt;

&lt;P&gt;Good luck,&lt;BR /&gt;
  Stéphane&lt;/P&gt;</description>
      <pubDate>Wed, 30 Sep 2020 03:59:48 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-does-my-python-custom-reporting-command-work-only-if-I-use-a/m-p/349680#M103505</guid>
      <dc:creator>grundsch</dc:creator>
      <dc:date>2020-09-30T03:59:48Z</dc:date>
    </item>
    <item>
      <title>Re: Why does my python custom reporting command work only if I use a stats command in the pipeline before?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-does-my-python-custom-reporting-command-work-only-if-I-use-a/m-p/349681#M103506</link>
      <description>&lt;P&gt;My commands.conf file consists of the following:&lt;BR /&gt;
[mycommand]&lt;BR /&gt;
chunked=true&lt;BR /&gt;
filename=mycommand.py&lt;/P&gt;

&lt;P&gt;With these settings, the streaming command works just fine so I am assuming that a reporting command should work fine once I call the ReportingCommand class and appropriate methods in my own script.&lt;/P&gt;

&lt;P&gt;Anyway, thanks for your reply. I'll keep playing around with it.&lt;/P&gt;

&lt;P&gt;All the best,&lt;BR /&gt;
Richie.&lt;/P&gt;</description>
      <pubDate>Tue, 04 Feb 2020 12:30:05 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-does-my-python-custom-reporting-command-work-only-if-I-use-a/m-p/349681#M103506</guid>
      <dc:creator>richielynch89</dc:creator>
      <dc:date>2020-02-04T12:30:05Z</dc:date>
    </item>
    <item>
      <title>Re: Why does my python custom reporting command work only if I use a stats command in the pipeline before?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-does-my-python-custom-reporting-command-work-only-if-I-use-a/m-p/349682#M103507</link>
      <description>&lt;P&gt;mmmh... if your command is named "mycommand", then the class must be &lt;/P&gt;

&lt;BLOCKQUOTE&gt;
&lt;P&gt;@Configuration()  class&lt;BR /&gt;
mycommandCommand(ReportingCommand):&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;</description>
      <pubDate>Tue, 04 Feb 2020 12:41:25 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-does-my-python-custom-reporting-command-work-only-if-I-use-a/m-p/349682#M103507</guid>
      <dc:creator>grundsch</dc:creator>
      <dc:date>2020-02-04T12:41:25Z</dc:date>
    </item>
    <item>
      <title>Re: Why does my python custom reporting command work only if I use a stats command in the pipeline before?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-does-my-python-custom-reporting-command-work-only-if-I-use-a/m-p/349683#M103508</link>
      <description>&lt;P&gt;Weird, when I use the StreamingCommand class, I do not need to append "Command" to my class name. &lt;BR /&gt;
It works now since I have changed my class name from "MyCommand" to "MyCommandCommand".&lt;/P&gt;

&lt;P&gt;Thank you for pointing that out Stéphane.&lt;/P&gt;

&lt;P&gt;Regards,&lt;BR /&gt;
Richie.&lt;/P&gt;</description>
      <pubDate>Tue, 04 Feb 2020 13:12:38 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-does-my-python-custom-reporting-command-work-only-if-I-use-a/m-p/349683#M103508</guid>
      <dc:creator>richielynch89</dc:creator>
      <dc:date>2020-02-04T13:12:38Z</dc:date>
    </item>
    <item>
      <title>Re: Why does my python custom reporting command work only if I use a stats command in the pipeline before?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-does-my-python-custom-reporting-command-work-only-if-I-use-a/m-p/349684#M103509</link>
      <description>&lt;P&gt;yep, this whole SDK could get more documentation. For the moment, I'm staying with my previous statement:&lt;/P&gt;

&lt;BLOCKQUOTE&gt;
&lt;P&gt;It seems to work, but I have&lt;BR /&gt;
absolutely no idea what I'm doing.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;

&lt;P&gt;&lt;span class="lia-unicode-emoji" title=":grinning_face_with_smiling_eyes:"&gt;😄&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 04 Feb 2020 14:27:12 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-does-my-python-custom-reporting-command-work-only-if-I-use-a/m-p/349684#M103509</guid>
      <dc:creator>grundsch</dc:creator>
      <dc:date>2020-02-04T14:27:12Z</dc:date>
    </item>
    <item>
      <title>Re: Why does my python custom reporting command work only if I use a stats command in the pipeline before?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-does-my-python-custom-reporting-command-work-only-if-I-use-a/m-p/349685#M103510</link>
      <description>&lt;P&gt;Yeah, I agree. its quite difficult to find clear instructions and examples for each search command type.&lt;BR /&gt;
I found this presentation after some time of Googling. Maybe it will help shed some light in areas where you are uncertain.&lt;BR /&gt;
&lt;A href="https://conf.splunk.com/files/2017/slides/extending-spl-with-custom-search-commands-and-the-splunk-sdk-for-python.pdf"&gt;https://conf.splunk.com/files/2017/slides/extending-spl-with-custom-search-commands-and-the-splunk-sdk-for-python.pdf&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;Also, if, like me, you run into an issue where the base search is sending a lot of events to the script (over 150k) then try processing the events in the "map" function rather than the "reduce" function. &lt;/P&gt;</description>
      <pubDate>Tue, 04 Feb 2020 16:05:49 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-does-my-python-custom-reporting-command-work-only-if-I-use-a/m-p/349685#M103510</guid>
      <dc:creator>richielynch89</dc:creator>
      <dc:date>2020-02-04T16:05:49Z</dc:date>
    </item>
    <item>
      <title>Re: Why does my python custom reporting command work only if I use a stats command in the pipeline before?</title>
      <link>https://community.splunk.com/t5/Splunk-Search/Why-does-my-python-custom-reporting-command-work-only-if-I-use-a/m-p/349686#M103511</link>
      <description>&lt;P&gt;cool, thanks!&lt;/P&gt;</description>
      <pubDate>Tue, 04 Feb 2020 16:17:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Search/Why-does-my-python-custom-reporting-command-work-only-if-I-use-a/m-p/349686#M103511</guid>
      <dc:creator>grundsch</dc:creator>
      <dc:date>2020-02-04T16:17:13Z</dc:date>
    </item>
  </channel>
</rss>

