plugin – Plugin Class

This module provides the Plugin class, which is the basic class which encapsulates a single plugin. This is the class which should be subclassed when creating new plugins.

class pycinga.plugin.Plugin([argv=sys.argv])[source]

Instantiates a plugin, setting up the options and arguments state. Initialization by itself shouldn’t do much, since the plugin should run when check() is called.

This init method will parse the arguments given in args and will set the results on the options attribute. If no args are given, the command line arguments given to the whole Python application will be used.

All plugins parse standard command line arguments that are required by the Icinga developer guidelines:

  • hostname - Set via -H or --hostname, this should be the host that this check targets, if applicable.
  • warning - Set via -w or --warning, this should be a valid range in which the value of the plugin is considered to be a warning.
  • critical - Set via -c or --critical, this should be a valid range in which the value is considered to be critical.
  • timeout - Set via -t or --timeout, this is an int value for the timeout of this check.
  • verbosity - Set via -v, where additional v means more verbosity. Example: -vvv will set options.verbosity to 3.

Subclasses can define additional options by creating Action instances and assigning them to class attributes. The easiest way to make an Action is to use Python’s built-in argparse methods. The following is an example plugin which adds a simple string argument::

class MyPlugin(Plugin):
    parser = ArgumentParser()
    parser.add_argument("--your-name", dest="your_name", type="string")

Instantiating the above plugin will result in the value of the new argument being available in options.your_name.

options

Dictionary of parsed command line options and their values. As an example, to get the hostname passed in via the command line::

options.hostname
args

Array of additional positional arguments passed in via the command line. For example, if you call the plugin with ./plugin 1 2 3, then options.args will return [1,2,3].

check()[source]

This method is what should be called to run this plugin and return a proper Response object. Subclasses are expected to implement this.

response_for_value(value, message=None)[source]

This method is meant to be used by plugin implementers to return a valid Response object for the given value. The status of this response is determined based on the warning and critical ranges given via the command line, which the plugin automatically parses.

An optional message argument may be provided to set the message for the Response object. Note that this can easily be added later as well by simply setting the message attribute on the response object returned.

Creating a response using this method from check() makes it trivial to calculate the value, grab a response, set some performance metrics, and return it.