Splunk Search

Why are the columns reordered when implementing reporting command?

schose
Builder

Hi forum,

I'm trying to implement a custom reporting command. Here is the smallest implementation which does nothing but giving the results back.

from __future__ import absolute_import, division, print_function, unicode_literals
import os

from splunklib.searchcommands import dispatch, ReportingCommand, Configuration, Option, validators
import sys
from splunklib import six

@Configuration()
class SavetableCommand(ReportingCommand):
    @Configuration()
    def map(self, records):
        return records

    def reduce(self, records):

        return records

dispatch(SavetableCommand, sys.argv, sys.stdin, sys.stdout, __name__)

Unfortunately this doesn't give me the correct order of the incoming resultset.
Example:

| tstats count values(source) as a where index=_internal by sourcetype  | table sourcetype count a | mycommand 

Gives back a table with a count sourcetype - so it looks like columns of the stats are reordered alphabetically.

How would it be possible to return the columns is in a correct order?!

Thanks and best regards,

Andreas

0 Karma

nickhills
Ultra Champion

Splunk's default behavior is to sort rows by _time, and columns by lexicographical order. (try |table * to see this)
If you want columns in a specific order you will have to |table z_field a_field m_field
When your command returns your results, it is ignorant of your previous table command, so you will need to format this yourself.

One option is to use a macro which invokes your command, and then formats the results with table.

If my comment helps, please give it a thumbs up!
0 Karma

schose
Builder

Hi nick,

thanks for your help. It looks like when it's implemented as StreamingCommand the columns are not reordered.

0 Karma
Get Updates on the Splunk Community!

.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 ...

Introducing the 2024 SplunkTrust!

Hello, Splunk Community! We are beyond thrilled to announce our newest group of SplunkTrust members!  The ...