|
| 1 | +# Copyright 2011-2013 Splunk, Inc. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"): you may |
| 4 | +# not use this file except in compliance with the License. You may obtain |
| 5 | +# a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | +# License for the specific language governing permissions and limitations |
| 13 | +# under the License. |
| 14 | + |
| 15 | +from abc import ABCMeta, abstractmethod |
| 16 | +import sys |
| 17 | +from splunklib.modularinput.input_definition import InputDefinition, parse_input_definition |
| 18 | +from splunklib.modularinput.validation_definition import ValidationDefinition, parse_validation_definition |
| 19 | +from splunklib.modularinput.event_writer import EventWriter |
| 20 | +from splunklib.modularinput.scheme import Scheme |
| 21 | + |
| 22 | +try: |
| 23 | + import xml.etree.cElementTree as ET |
| 24 | +except ImportError: |
| 25 | + import xml.etree.ElementTree as ET |
| 26 | + |
| 27 | + |
| 28 | +class Script(object): |
| 29 | + """An abstract base class for implementing modular inputs. |
| 30 | +
|
| 31 | + Subclasses should override getScheme, StreamEvents, |
| 32 | + and optional configureValidator if the modular Input uses |
| 33 | + external validation. |
| 34 | +
|
| 35 | + The important function is run, which is used to run modular inputs |
| 36 | +
|
| 37 | + """ |
| 38 | + __metaclass__ = ABCMeta |
| 39 | + |
| 40 | + def run(self, args): |
| 41 | + """This function is stable, call run to run a modular input |
| 42 | +
|
| 43 | + :param args: String[] args from Java |
| 44 | + :return: |
| 45 | + """ |
| 46 | + return self.run_script(args, EventWriter(), sys.stdin) |
| 47 | + |
| 48 | + def run_script(self, args, event_writer, input_stream): |
| 49 | + """Handles all the specifics of running a modular input |
| 50 | +
|
| 51 | + :param args: |
| 52 | + :param event_writer: |
| 53 | + :param input_stream: |
| 54 | + :return: |
| 55 | + """ |
| 56 | + |
| 57 | + try: |
| 58 | + if len(args) == 0: |
| 59 | + # This script is running as an input. Input definitions will be passed on stdin |
| 60 | + # as XML, and the script will write events on stdout and log entries on stderr. |
| 61 | + input_definition = parse_input_definition(input_stream) |
| 62 | + self.stream_events(input_definition, event_writer) |
| 63 | + event_writer.close() |
| 64 | + return 0 |
| 65 | + |
| 66 | + elif str(args[0]).lower() == "--scheme": |
| 67 | + # Splunk has requested XML specifying the scheme for this modular input. |
| 68 | + # Return it and exit. |
| 69 | + scheme = self.get_scheme() |
| 70 | + if scheme is None: |
| 71 | + event_writer.log(EventWriter.FATAL, "Modular input script returned a null scheme.") |
| 72 | + return 1 |
| 73 | + else: |
| 74 | + event_writer.write_xml_document(scheme.to_XML()) |
| 75 | + return 0 |
| 76 | + |
| 77 | + elif args[0].lower() == "--validate-arguments": |
| 78 | + validation_definition = parse_validation_definition(input_stream) |
| 79 | + try: |
| 80 | + self.validate_input(validation_definition) |
| 81 | + return 0 |
| 82 | + except Exception as e: |
| 83 | + root = ET.Element("error") |
| 84 | + ET.SubElement(root, "message").text = e.message |
| 85 | + event_writer.write_xml_document(root) |
| 86 | + |
| 87 | + return 1 |
| 88 | + |
| 89 | + err_string = "ERROR Invalid arguments to modular input script:" + ' '.join(args) |
| 90 | + event_writer._err.write(err_string) |
| 91 | + |
| 92 | + except Exception as e: |
| 93 | + err_string = EventWriter.ERROR + e.message |
| 94 | + event_writer._err.write(err_string) |
| 95 | + return 1 |
| 96 | + |
| 97 | + @abstractmethod |
| 98 | + def get_scheme(self): |
| 99 | + """The scheme defines the parameters understood by this modular input. |
| 100 | +
|
| 101 | + :return: a Scheme object representing the parameters for this modular input |
| 102 | + """ |
| 103 | + |
| 104 | + def validate_input(self, definition): |
| 105 | + """Handles external validation for modular input kinds. When Splunk |
| 106 | + called a modular input script in validation mode, it will pass in an XML document |
| 107 | + giving information about the Splunk instance (so you can call back into it if needed) |
| 108 | + and the name and parameters of the proposed input. |
| 109 | +
|
| 110 | + If this function does not throw an exception, the validation is assumed to succeed. |
| 111 | + Otherwise any error throws will be turned into a string and logged back to Splunk. |
| 112 | +
|
| 113 | + The default implementation always passes. |
| 114 | +
|
| 115 | + :param definition: The parameters for the proposed input passed by splunkd |
| 116 | + """ |
| 117 | + @abstractmethod |
| 118 | + def stream_events(self, inputs, ew): |
| 119 | + """The method called to stream events into Splunk. It should do all of its output via |
| 120 | + EventWriter rather than assuming that there is a console attached. |
| 121 | +
|
| 122 | + :param inputs: an InputDefinition object |
| 123 | + :param ew: an object with methods to write events and log messages to Splunk |
| 124 | + """ |
| 125 | + return |
0 commit comments