def _process_protocol_v2(self, argv, ifile, ofile): """ Processes records on the `input stream optionally writing records to the output stream. :param ifile: Input file object. :type ifile: file or InputType :param ofile: Output file object. :type ofile: file or OutputType :return: :const:`None` """ debug = environment.splunklib_logger.debug class_name = self.__class__.__name__ debug('%s.process started under protocol_version=2', class_name) self._protocol_version = 2 # Read search command metadata from splunkd # noinspection PyBroadException try: debug('Reading metadata') metadata, body = self._read_chunk(self._as_binary_stream(ifile)) action = getattr(metadata, 'action', None) if action != 'getinfo': raise RuntimeError( 'Expected getinfo action, not {}'.format(action)) if len(body) > 0: raise RuntimeError('Did not expect data for getinfo action') self._metadata = deepcopy(metadata) searchinfo = self._metadata.searchinfo searchinfo.earliest_time = float(searchinfo.earliest_time) searchinfo.latest_time = float(searchinfo.latest_time) searchinfo.search = unquote(searchinfo.search) self._map_input_header() debug(' metadata=%r, input_header=%r', self._metadata, self._input_header) try: tempfile.tempdir = self._metadata.searchinfo.dispatch_dir except AttributeError: raise RuntimeError( '%s.metadata.searchinfo.dispatch_dir is undefined'.format( class_name)) debug(' tempfile.tempdir=%r', tempfile.tempdir) except: self._record_writer = RecordWriterV2(ofile) self._report_unexpected_error() self.finish() exit(1) # Write search command configuration for consumption by splunkd # noinspection PyBroadException try: self._record_writer = RecordWriterV2( ofile, getattr(self._metadata.searchinfo, 'maxresultrows', None)) self.fieldnames = [] self.options.reset() args = self.metadata.searchinfo.args error_count = 0 debug('Parsing arguments') if args and type(args) == list: for arg in args: result = arg.split('=', 1) if len(result) == 1: self.fieldnames.append(str(result[0])) else: name, value = result name = str(name) try: option = self.options[name] except KeyError: self.write_error( 'Unrecognized option: {}={}'.format( name, value)) error_count += 1 continue try: option.value = value except ValueError: self.write_error('Illegal value: {}={}'.format( name, value)) error_count += 1 continue missing = self.options.get_missing() if missing is not None: if len(missing) == 1: self.write_error('A value for "{}" is required'.format( missing[0])) else: self.write_error( 'Values for these required options are missing: {}'. format(', '.join(missing))) error_count += 1 if error_count > 0: exit(1) debug(' command: %s', six.text_type(self)) debug('Preparing for execution') self.prepare() if self.record: ifile, ofile = self._prepare_recording(argv, ifile, ofile) self._record_writer.ofile = ofile # Record the metadata that initiated this command after removing the record option from args/raw_args info = self._metadata.searchinfo for attr in 'args', 'raw_args': setattr(info, attr, [ arg for arg in getattr(info, attr) if not arg.startswith('record=') ]) metadata = MetadataEncoder().encode(self._metadata) ifile.record('chunked 1.0,', six.text_type(len(metadata)), ',0\n', metadata) if self.show_configuration: self.write_info(self.name + ' command configuration: ' + str(self._configuration)) debug(' command configuration: %s', self._configuration) except SystemExit: self._record_writer.write_metadata(self._configuration) self.finish() raise except: self._record_writer.write_metadata(self._configuration) self._report_unexpected_error() self.finish() exit(1) self._record_writer.write_metadata(self._configuration) # Execute search command on data passing through the pipeline # noinspection PyBroadException try: debug('Executing under protocol_version=2') self._metadata.action = 'execute' self._execute(ifile, None) except SystemExit: self.finish() raise except: self._report_unexpected_error() self.finish() exit(1) debug('%s.process completed', class_name)
def _process_protocol_v2(self, argv, ifile, ofile): """ Processes records on the `input stream optionally writing records to the output stream. :param ifile: Input file object. :type ifile: file or InputType :param ofile: Output file object. :type ofile: file or OutputType :return: :const:`None` """ debug = environment.splunklib_logger.debug class_name = self.__class__.__name__ debug('%s.process started under protocol_version=2', class_name) self._protocol_version = 2 # Read search command metadata from splunkd # noinspection PyBroadException try: debug('Reading metadata') metadata, body = self._read_chunk(ifile) action = getattr(metadata, 'action', None) if action != 'getinfo': raise RuntimeError('Expected getinfo action, not {}'.format(action)) if len(body) > 0: raise RuntimeError('Did not expect data for getinfo action') self._metadata = deepcopy(metadata) searchinfo = self._metadata.searchinfo searchinfo.earliest_time = float(searchinfo.earliest_time) searchinfo.latest_time = float(searchinfo.latest_time) searchinfo.search = unquote(searchinfo.search) self._map_input_header() debug(' metadata=%r, input_header=%r', self._metadata, self._input_header) try: tempfile.tempdir = self._metadata.searchinfo.dispatch_dir except AttributeError: raise RuntimeError('%s.metadata.searchinfo.dispatch_dir is undefined'.format(class_name)) debug(' tempfile.tempdir=%r', tempfile.tempdir) except: self._record_writer = RecordWriterV2(ofile) self._report_unexpected_error() self.finish() exit(1) # Write search command configuration for consumption by splunkd # noinspection PyBroadException try: self._record_writer = RecordWriterV2(ofile, getattr(self._metadata.searchinfo, 'maxresultrows', None)) self.fieldnames = [] self.options.reset() args = self.metadata.searchinfo.args error_count = 0 debug('Parsing arguments') if args and type(args) == list: for arg in args: result = arg.split('=', 1) if len(result) == 1: self.fieldnames.append(str(result[0])) else: name, value = result name = str(name) try: option = self.options[name] except KeyError: self.write_error('Unrecognized option: {}={}'.format(name, value)) error_count += 1 continue try: option.value = value except ValueError: self.write_error('Illegal value: {}={}'.format(name, value)) error_count += 1 continue missing = self.options.get_missing() if missing is not None: if len(missing) == 1: self.write_error('A value for "{}" is required'.format(missing[0])) else: self.write_error('Values for these required options are missing: {}'.format(', '.join(missing))) error_count += 1 if error_count > 0: exit(1) debug(' command: %s', six.text_type(self)) debug('Preparing for execution') self.prepare() if self.record: ifile, ofile = self._prepare_recording(argv, ifile, ofile) self._record_writer.ofile = ofile # Record the metadata that initiated this command after removing the record option from args/raw_args info = self._metadata.searchinfo for attr in 'args', 'raw_args': setattr(info, attr, [arg for arg in getattr(info, attr) if not arg.startswith('record=')]) metadata = MetadataEncoder().encode(self._metadata) ifile.record('chunked 1.0,', six.text_type(len(metadata)), ',0\n', metadata) if self.show_configuration: self.write_info(self.name + ' command configuration: ' + str(self._configuration)) debug(' command configuration: %s', self._configuration) except SystemExit: self._record_writer.write_metadata(self._configuration) self.finish() raise except: self._record_writer.write_metadata(self._configuration) self._report_unexpected_error() self.finish() exit(1) self._record_writer.write_metadata(self._configuration) # Execute search command on data passing through the pipeline # noinspection PyBroadException try: debug('Executing under protocol_version=2') self._records = self._records_protocol_v2 self._metadata.action = 'execute' self._execute(ifile, None) except SystemExit: self.finish() raise except: self._report_unexpected_error() self.finish() exit(1) debug('%s.process completed', class_name)