Skip to content

Commit 60d974e

Browse files
author
David Noble
committed
splunklib.searchcommands | Writing messages header causes Getinfo probe failure CAUSE: + outputheader=true must be set in commands.conf + messages header may only be sent during __EXECUTE__ phase, not __GETINFO__ phase. TODO: + Issue feature ask for the search team: outputheader can be specified dynamically. Signed-off-by: David Noble <[email protected]>
1 parent 9ed0d27 commit 60d974e

File tree

5 files changed

+30
-24
lines changed

5 files changed

+30
-24
lines changed

examples/searchcommands_app/default/commands.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ supports_rawargs = true
1010
filename = simulate.py
1111
supports_getinfo = true
1212
supports_rawargs = true
13+
outputheader = true
1314

1415
[sum]
1516
filename = sum.py

splunklib/searchcommands/search_command.py

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,17 @@
2828
# Relative imports
2929

3030
from . import csv, logging
31-
from . decorators import Option
32-
from . validators import Boolean, Fieldname
33-
from . search_command_internals import InputHeader, MessagesHeader, \
31+
from .decorators import Option
32+
from .validators import Boolean, Fieldname
33+
from .search_command_internals import InputHeader, MessagesHeader, \
3434
SearchCommandParser
3535

3636

3737
class SearchCommand(object):
3838
""" Represents a custom search command
3939
4040
"""
41+
4142
def __init__(self):
4243

4344
# Variables that may be used, but not altered by derived classes
@@ -102,14 +103,14 @@ def __str__(self):
102103
# value = self._default_logging_level
103104
# self.logger.setLevel(value)
104105
# return
105-
#
106-
# show_configuration = Option(doc='''
107-
# **Syntax:** show_configuration=<bool>
108-
#
109-
# **Description:** When `true`, reports command configuration in the
110-
# messages header for this command invocation. Defaults to `false`.
111-
#
112-
# ''', default=False, validate=Boolean())
106+
107+
show_configuration = Option(doc='''
108+
**Syntax:** show_configuration=<bool>
109+
110+
**Description:** When `true`, reports command configuration in the
111+
messages header for this command invocation. Defaults to `false`.
112+
113+
''', default=False, validate=Boolean())
113114
#
114115
# #endregion
115116

@@ -159,10 +160,9 @@ def process(self, args=argv, input_file=stdin, output_file=stdout):
159160
writer.writerow({'ERROR': e})
160161
self.logger.error(e)
161162
return
163+
162164
self._configuration = ConfigurationSettings(self)
163-
# Disabled in splunk-sdk-python-1.2.0 due to known issues
164-
# if self.show_configuration:
165-
# self.messages.append('info_message', str(self._configuration))
165+
166166
writer = csv.DictWriter(
167167
output_file, self, self.configuration.keys(), mv_delimiter=',')
168168
writer.writerow(self.configuration.items())
@@ -182,6 +182,12 @@ def process(self, args=argv, input_file=stdin, output_file=stdout):
182182
return
183183

184184
self._configuration = ConfigurationSettings(self)
185+
186+
if self.show_configuration:
187+
self.messages.append('info_message',
188+
'%s command configuration settings: %s' %
189+
(self.name, self._configuration))
190+
185191
writer = csv.DictWriter(output_file, self)
186192
self._execute(operation, reader, writer)
187193

@@ -218,6 +224,7 @@ class ConfigurationSettings(object):
218224
:class:`SearchCommand` classes.
219225
220226
"""
227+
221228
def __init__(self, command):
222229
self.command = command
223230

@@ -230,8 +237,8 @@ def __str__(self):
230237
:return: String representation of this instance
231238
232239
"""
233-
text = '\n'.join(
234-
['%s = %s' % (k, getattr(self, k)) for k in self.keys()])
240+
text = ', '.join(
241+
['%s=%s' % (k, getattr(self, k)) for k in self.keys()])
235242
return text
236243

237244
#region Properties
@@ -511,4 +518,4 @@ def keys(self):
511518

512519
#endregion
513520

514-
#endregion
521+
#endregion

splunklib/searchcommands/search_command_internals.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,8 @@ def write(self, output_file):
169169
by a pair of '\r\n' sequences.
170170
171171
"""
172-
# Disabled in splunk-sdk-python-1.2.0 due to known issues
173-
# for level, message in self:
174-
# output_file.write('%s=%s\r\n' % (level, message))
175-
self
172+
for level, message in self:
173+
output_file.write('%s=%s\r\n' % (level, message))
176174
output_file.write('\r\n')
177175

178176

File renamed without changes.

tests/test_searchcommands_app.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ def disable_test_option_logging_level(self):
298298
os.path.join('log', 'test_option_logging_level.log')))
299299
return
300300

301-
def disable_test_option_show_configuration(self):
301+
def test_option_show_configuration(self):
302302
self._run(
303303
'simulate', [
304304
'csv=population.csv',
@@ -307,7 +307,7 @@ def disable_test_option_show_configuration(self):
307307
'rate=200',
308308
'seed=%s' % TestSearchCommandsApp._seed,
309309
'show_configuration=true'],
310-
__GETINFO__=(
310+
__EXECUTE__=(
311311
os.path.join('input', '_empty.csv'),
312312
os.path.join('output', 'test_option_show_configuration.csv'),
313313
os.path.join('log', 'test_option_show_configuration.log')))
@@ -327,7 +327,7 @@ def test_generating_command_in_isolation(self):
327327
'csv=population.csv',
328328
'duration=00:00:02',
329329
'interval=00:00:01',
330-
'rate=200',
330+
'rate=50',
331331
'seed=%s' % TestSearchCommandsApp._seed],
332332
__GETINFO__=(
333333
os.path.join('input', '_empty.csv'),

0 commit comments

Comments
 (0)