def log(self, severity, message): """Logs messages about the state of this modular input to Splunk. These messages will show up in Splunk's internal logs. :param severity: ``string``, severity of message, see severities defined as class constants. :param message: ``string``, message to log. """ self._err.write(ensure_text("%s %s\n" % (severity, message))) self._err.flush()
def write_xml_document(self, document): """Writes a string representation of an ``ElementTree`` object to the output stream. :param document: An ``ElementTree`` object. """ data = ET.tostring(document) self._out.write(ensure_text(data)) self._out.flush()
def write_event(self, event): """Writes an ``Event`` object to Splunk. :param event: An ``Event`` object. """ if not self.header_written: self._out.write(ensure_text("<stream>")) self.header_written = True event.write_to(self._out)
def process_line(input_dict, encoded, decoded): try: if input_dict[encoded] and not input_dict[decoded]: input_dict[decoded] = backslash_escape( from_b64(input_dict[encoded])) elif input_dict[decoded] and not input_dict[encoded]: data = input_dict[decoded].encode('utf8').decode( 'unicode_escape').encode('latin1') input_dict[encoded] = ensure_text(to_b64(data)) except: pass
def write_to(self, stream): """Write an XML representation of self, an ``Event`` object, to the given stream. The ``Event`` object will only be written if its data field is defined, otherwise a ``ValueError`` is raised. :param stream: stream to write XML to. """ if self.data is None: raise ValueError("Events must have at least the data field set to be written to XML.") event = ET.Element("event") if self.stanza is not None: event.set("stanza", self.stanza) event.set("unbroken", str(int(self.unbroken))) # if a time isn't set, let Splunk guess by not creating a <time> element if self.time is not None: ET.SubElement(event, "time").text = str(self.time) # add all other subelements to this Event, represented by (tag, text) subelements = [ ("source", self.source), ("sourcetype", self.sourceType), ("index", self.index), ("host", self.host), ("data", self.data) ] for node, value in subelements: if value is not None: ET.SubElement(event, node).text = value if self.done: ET.SubElement(event, "done") if isinstance(stream, TextIOBase): stream.write(ensure_text(ET.tostring(event))) else: stream.write(ET.tostring(event)) stream.flush()
def close(self): """Write the closing </stream> tag to make this XML well formed.""" self._out.write(ensure_text("</stream>")) self._out.flush()