Advertisement
phoenixdigital

Untitled

Oct 9th, 2014
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.42 KB | None | 0 0
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2013 Splunk, Inc.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License"): you may
  6. # not use this file except in compliance with the License. You may obtain
  7. # a copy of the License at
  8. #
  9. #    http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. # License for the specific language governing permissions and limitations
  15. # under the License.
  16.  
  17. import random, sys, time
  18.  
  19. from splunklib.modularinput import *
  20.  
  21. class MyScript(Script):
  22.     """All modular inputs should inherit from the abstract base class Script
  23.     from splunklib.modularinput.script.
  24.     They must override the get_scheme and stream_events functions, and,
  25.     if the scheme returned by get_scheme has Scheme.use_external_validation
  26.     set to True, the validate_input function.
  27.     """
  28.     def get_scheme(self):
  29.         """When Splunk starts, it looks for all the modular inputs defined by
  30.         its configuration, and tries to run them with the argument --scheme.
  31.         Splunkd expects the modular inputs to print a description of the
  32.         input in XML on stdout. The modular input framework takes care of all
  33.         the details of formatting XML and printing it. The user need only
  34.         override get_scheme and return a new Scheme object.
  35.  
  36.         :return: scheme, a Scheme object
  37.         """
  38.         # "random_numbers" is the name Splunk will display to users for this input.
  39.         scheme = Scheme("Random Numbers")
  40.  
  41.         scheme.description = "Streams events containing a random number."
  42.         # If you set external validation to True, without overriding validate_input,
  43.         # the script will accept anything as valid. Generally you only need external
  44.         # validation if there are relationships you must maintain among the
  45.         # parameters, such as requiring min to be less than max in this example,
  46.         # or you need to check that some resource is reachable or valid.
  47.         # Otherwise, Splunk lets you specify a validation string for each argument
  48.         # and will run validation internally using that string.
  49.         scheme.use_external_validation = True
  50.         scheme.use_single_instance = True
  51.  
  52.         min_argument = Argument("min")
  53.         min_argument.data_type = Argument.data_type_number
  54.         min_argument.description = "Minimum random number to be produced by this input."
  55.         min_argument.required_on_create = True
  56.         # If you are not using external validation, you would add something like:
  57.         #
  58.         # scheme.validation = "min > 0"
  59.         scheme.add_argument(min_argument)
  60.  
  61.         max_argument = Argument("max")
  62.         max_argument.data_type = Argument.data_type_number
  63.         max_argument.description = "Maximum random number to be produced by this input."
  64.         max_argument.required_on_create = True
  65.         scheme.add_argument(max_argument)
  66.  
  67.         interval_argument = Argument("interval")
  68.         interval_argument.data_type = Argument.data_type_number
  69.         interval_argument.description = "Number of seconds between random number generations."
  70.         interval_argument.required_on_create = True
  71.         scheme.add_argument(interval_argument)     
  72.  
  73.         return scheme
  74.  
  75.     def validate_input(self, validation_definition):
  76.         """In this example we are using external validation to verify that min is
  77.         less than max. If validate_input does not raise an Exception, the input is
  78.         assumed to be valid. Otherwise it prints the exception as an error message
  79.         when telling splunkd that the configuration is invalid.
  80.  
  81.         When using external validation, after splunkd calls the modular input with
  82.         --scheme to get a scheme, it calls it again with --validate-arguments for
  83.         each instance of the modular input in its configuration files, feeding XML
  84.         on stdin to the modular input to do validation. It is called the same way
  85.         whenever a modular input's configuration is edited.
  86.  
  87.         :param validation_definition: a ValidationDefinition object
  88.         """
  89.         # Get the parameters from the ValidationDefinition object,
  90.         # then typecast the values as floats
  91.         minimum = float(validation_definition.parameters["min"])
  92.         maximum = float(validation_definition.parameters["max"])
  93.  
  94.         if minimum >= maximum:
  95.             raise ValueError("min must be less than max; found min=%f, max=%f" % minimum, maximum)
  96.  
  97.     def stream_events(self, inputs, ew):
  98.         """This function handles all the action: splunk calls this modular input
  99.         without arguments, streams XML describing the inputs to stdin, and waits
  100.         for XML on stdout describing events.
  101.  
  102.         If you set use_single_instance to True on the scheme in get_scheme, it
  103.         will pass all the instances of this input to a single instance of this
  104.         script.
  105.  
  106.         :param inputs: an InputDefinition object
  107.         :param ew: an EventWriter object
  108.         """
  109.  
  110.         while 1:
  111.             # Go through each input for this modular input
  112.             for input_name, input_item in inputs.inputs.iteritems():
  113.                 # Get the values, cast them as floats
  114.                 minimum = float(input_item["min"])
  115.                 maximum = float(input_item["max"])
  116.  
  117.                 interval = int(input_item["interval"])
  118.  
  119.                 if interval == 0:
  120.                     interval = 60
  121.  
  122.                 # Create an Event object, and set its data fields
  123.                 event = Event()
  124.                 event.stanza = input_name
  125.                 event.data = "number=\"%s\"" % str(random.uniform(minimum, maximum))
  126.  
  127.                 # Tell the EventWriter to write this event
  128.                 ew.write_event(event)
  129.                 ew.log('randomlog','just wrote message')
  130.                
  131.                 time.sleep(interval)
  132.  
  133. if __name__ == "__main__":
  134.     sys.exit(MyScript().run(sys.argv))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement