X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fcollectd-python.pod;h=3e44d54652e248d85d128527078dd54db86ad127;hb=48622c3d7c8c0d3ab07cda8a1947c33bfe31df73;hp=267296cfd6009a82904198f3b999b3dfe5aa3ab7;hpb=a898c17330d9a2039bcdb8f7e6dbedba516a6cd8;p=collectd.git diff --git a/src/collectd-python.pod b/src/collectd-python.pod index 267296cf..3e44d546 100644 --- a/src/collectd-python.pod +++ b/src/collectd-python.pod @@ -8,20 +8,20 @@ # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. +=encoding UTF-8 + =head1 NAME collectd-python - Documentation of collectd's C =head1 SYNOPSIS - - Globals true - + LoadPlugin python # ... ModulePath "/path/to/your/python/modules" LogTraces true - Interactive true + Interactive false Import "spam" @@ -37,7 +37,7 @@ for collectd in Python. This is a lot more efficient than executing a Python-script every time you want to read a value with the C (see L) and provides a lot more functionality, too. -At least python I is required. +The minimum required Python version is I<2.6>. =head1 CONFIGURATION @@ -45,87 +45,94 @@ At least python I is required. =item B I -Loads the Python plugin I. Unlike most other LoadPlugin lines, this one -should be a block containing the line "Globals true". This will cause collectd -to export the name of all objects in the python interpreter for all plugins to -see. If you don't do this or your platform does not support it, the embedded -interpreter will start anyway but you won't be able to load certain python -modules, e.g. "time". +Loads the Python plugin I. =item B I The default encoding for Unicode objects you pass to collectd. If you omit this -option it will default to B on I and B on I. -This is hardcoded in Python and will ignore everything else, including your -locale. +option it will default to B on I. On I it will +always be B, as this function was removed, so this will be silently +ignored. +These defaults are hardcoded in Python and will ignore everything else, +including your locale. =item B I -Appends I to B. You won't be able to import any scripts you +Prepends I to B. You won't be able to import any scripts you wrote unless they are located in one of the directories in this list. Please note that it only has effect on plugins loaded after this option. You can use multiple B lines to add more than one directory. =item B I -If a python script throws an exception it will be logged by collectd with the +If a Python script throws an exception it will be logged by collectd with the name of the exception and the message. If you set this option to true it will also log the full stacktrace just like the default output of an interactive -python interpreter. This should probably be set to false most of the time but -is very useful for development and debugging of new modules. +Python interpreter. This does not apply to the CollectError exception, which +will never log a stacktrace. +This should probably be set to false most of the time but is very useful for +development and debugging of new modules. =item B I -This option will cause the module to launch an interactive python interpreter +This option will cause the module to launch an interactive Python interpreter that reads from and writes to the terminal. Note that collectd will terminate right after starting up if you try to run it as a daemon while this option is -enabled to make sure to start collectd with the B<-f> option. +enabled so make sure to start collectd with the B<-f> option. The B module is I imported into the interpreter's globals. You have to do it manually. Be sure to read the help text of the module, it can be used as a reference guide during coding. This interactive session will behave slightly differently from a daemonized -collectd script as well as from a normal python interpreter: +collectd script as well as from a normal Python interpreter: =over 4 -=item +=item * B<1.> collectd will try to import the B module to give you a decent way of entering your commands. The daemonized collectd won't do that. -=item +=item * -B<2.> collectd will block I. Pressing I will usually cause +B<2.> Python will be handling I. Pressing I will usually cause collectd to shut down. This would be problematic in an interactive session, -therefore this signal will be blocked. You can still use it to interrupt -syscalls like sleep and pause but it won't generate a I -exception either. +therefore Python will be handling it in interactive sessions. This allows you +to use I to interrupt Python code without killing collectd. This also +means you can catch I exceptions which does not work during +normal operation. To quit collectd send I (press I at the beginning of a new line). -=item +=item * -B<3.> collectd handles I. This means that python won't be able to +B<3.> collectd handles I. This means that Python won't be able to determine the return code of spawned processes with system(), popen() and -subprocess. This will result in python not using external programs like less +subprocess. This will result in Python not using external programs like less to display help texts. You can override this behavior with the B environment variable, e.g. I before starting collectd. -Depending on your version of python this might or might not result in an +Depending on your version of Python this might or might not result in an B exception which can be ignored. -If you really need to spawn new processes from python you can register an init +If you really need to spawn new processes from Python you can register an init callback and reset the action for SIGCHLD to the default behavior. Please note that this I break the exec plugin. Do not even load the exec plugin if you intend to do this! There is an example script located in B to do this. If you import this from I SIGCHLD will be handled -normally and spawning processes from python will work as intended. +normally and spawning processes from Python will work as intended. =back +=item B I + +Imports the python script I and loads it into the collectd +python process. If your python script is not found, be sure its +directory exists in python's B. You can prepend to the +B using the B configuration option. + =item EB IE block This block may be used to pass on configuration settings to a Python module. @@ -139,22 +146,22 @@ The I identifies the callback. =head1 STRINGS -There are a lot of places where strings are send from collectd to python and -from python to collectd. How exactly this works depends on wheather byte or -unicode strings or python2 or python3 are used. +There are a lot of places where strings are sent from collectd to Python and +from Python to collectd. How exactly this works depends on whether byte or +unicode strings or Python2 or Python3 are used. Python2 has I, which is just bytes, and I. Python3 has I, which is a unicode object, and I. -When passing strings from python to collectd all of these object are supported +When passing strings from Python to collectd all of these object are supported in all places, however I should be used if possible. These strings must not contain a NUL byte. Ignoring this will result in a I exception. If a byte string was used it will be used as is by collectd. If a unicode object was used it will be encoded using the default encoding (see above). If -this is not possible python will raise a I exception. +this is not possible Python will raise a I exception. -Wenn passing strings from collectd to python the behavior depends on the -python version used. Python2 will always receive a I object. Python3 will +When passing strings from collectd to Python the behavior depends on the +Python version used. Python2 will always receive a I object. Python3 will usually receive a I object as well, however the original string will be decoded to unicode using the default encoding. If this fails because the string is not a valid sequence for this encoding a I object will be @@ -174,7 +181,7 @@ example. The following types of B are known to collectd =item configuration functions -This type of functions is called during configuration if an appropriate +These are called during configuration if an appropriate B block has been encountered. It is called once for each B block which matches the name of the callback as provided with the B method - see below. @@ -184,14 +191,14 @@ threading functions here! =item init functions -This type of functions is called once after loading the module and before any +These are called once after loading the module and before any calls to the read and write functions. It should be used to initialize the internal state of the plugin (e.Eg. open sockets, ...). This is the earliest point where you may use threads. =item read functions -This type of function is used to collect the actual data. It is called once +These are used to collect the actual data. It is called once per interval (see the B configuration option of collectd). Usually it will call B to dispatch the values to collectd which will pass them on to all registered B. If this function @@ -200,23 +207,23 @@ amount of time until it returns normally again. =item write functions -This type of function is used to write the dispatched values. It is called +These are used to write the dispatched values. It is called once for every value that was dispatched by any plugin. =item flush functions -This type of function is used to flush internal caches of plugins. It is +These are used to flush internal caches of plugins. It is usually triggered by the user only. Any plugin which caches data before writing it to disk should provide this kind of callback function. =item log functions -This type of function is used to pass messages of plugins or the daemon itself +These are used to pass messages of plugins or the daemon itself to the user. =item notification function -This type of function is used to act upon notifications. In general, a +These are used to act upon notifications. In general, a notification is a status message that may be associated with a data instance. Usually, a notification is generated by the daemon if a configured threshold has been exceeded (see the section "THRESHOLD CONFIGURATION" in @@ -225,12 +232,12 @@ notifications as well. =item shutdown functions -This type of function is called once before the daemon shuts down. It should +These are called once before the daemon shuts down. It should be used to clean up the plugin (e.g. close sockets, ...). =back -Any function (except log functions) may set throw an exception in case of any +Any function (except log functions) may throw an exception in case of errors. The exception will be passed on to the user using collectd's logging mechanism. If a log callback throws an exception it will be printed to standard error instead. @@ -250,6 +257,18 @@ collectd you're done. The following complex types are used to pass values between the Python plugin and collectd: +=head2 CollectdError + +This is an exception. If any Python script raises this exception it will +still be treated like an error by collectd but it will be logged as a +warning instead of an error and it will never generate a stacktrace. + + class CollectdError(Exception) + +Basic exception for collectd Python scripts. +Throwing this exception will not cause a stacktrace to be logged, even if +LogTraces is enabled in the config. + =head2 Signed The Signed class is just a long. It has all its methods and behaves exactly @@ -306,7 +325,7 @@ config file. It will always be a string. This is a tuple (which might be empty) of all value, i.e. words following the keyword in any given line in the config file. -Every item in this tuple will be either a string or a float or a boolean, +Every item in this tuple will be either a string, a float or a boolean, depending on the contents of the configuration file. =item children @@ -367,7 +386,7 @@ Type instance string. May be empty. =head2 Values -A Value is an object which features a sequence of values. It is based on then +A Value is an object which features a sequence of values. It is based on the I type and uses its members to identify the values. class Values(PluginData) @@ -477,7 +496,7 @@ Methods defined here: =over 4 -=item B([type][, values][, plugin_instance][, type_instance][, plugin][, host][, time][, interval]) -> None. Dispatch a value list. +=item B([type][, message][, plugin_instance][, type_instance][, plugin][, host][, time][, severity][, meta]) -> None. Dispatch a notification. Dispatch this instance to the collectd process. The object has members for each of the possible arguments for this method. For a detailed explanation of these @@ -495,7 +514,7 @@ Data descriptors defined here: =item message -Some kind of description what's going on and why this Notification was +Some kind of description of what's going on and why this Notification was generated. =item severity @@ -503,6 +522,16 @@ generated. The severity of this notification. Assign or compare to I, I or I. +=item meta + +These are the meta data for the Notification object. +It has to be a dictionary of numbers, strings or bools. All keys must be +strings. I and I objects will be dispatched as signed integers unless +they are between 2**63 and 2**64-1, which will result in a unsigned integer. +One of these storage classes can be forced by using the classes +B and B. A meta object received by a +notification callback will always contain B or B objects. + =back =head1 FUNCTIONS @@ -518,25 +547,28 @@ different events. With one exception all of them are called as shown above. =over 4 -=item +=item * I is a callable object that will be called every time the event is triggered. -=item +=item * I is an optional object that will be passed back to the callback function every time it is called. If you omit this parameter no object is passed back to your callback, not even None. -=item +=item * I is an optional identifier for this callback. The default name is B.I. I is taken from the B<__module__> attribute of your callback function. Every callback needs a unique identifier, so if you -want to register the same callback multiple time in the same module you need to -specify a name here. Otherwise it's save to ignore this parameter I -is the full identifier assigned to this callback. +want to register the same callback multiple times in the same module you need to +specify a name here. Otherwise it's safe to ignore this parameter. + +=item * + +I is the full identifier assigned to this callback. =back @@ -549,7 +581,7 @@ L<"WRITING YOUR OWN PLUGINS"> above) and are passed the following arguments: The only argument passed is a I object. See above for the layout of this data type. -Note that you can not receive the whole config files this way, only B +Note that you cannot receive the whole config files this way, only B blocks inside the Python configuration block. Additionally you will only receive blocks where your callback identifier matches BI. @@ -557,7 +589,7 @@ receive blocks where your callback identifier matches BI. The callback will be called without arguments. -=item register_read(callback[, interval][, data][, name]) -> identifier +=item register_read(callback[, interval][, data][, name]) -> I This function takes an additional parameter: I. It specifies the time between calls to the callback function. @@ -570,7 +602,7 @@ The callback will be called without arguments. =item register_write -The callback function will be called with one arguments passed, which will be a +The callback function will be called with one argument passed, which will be a I object. For the layout of I see above. If this callback function throws an exception the next call will be delayed by an increasing interval. @@ -611,7 +643,33 @@ I is either the string that was returned by the register function or a callback function. The identifier will be constructed in the same way as for the register functions. -=item B(I][, I]) -> None +=item B(I) -> I + +Returns the definition of a dataset specified by I. I is a list +of tuples, each representing one data source. Each tuple has 4 values: + +=over 4 + +=item name + +A string, the name of the data source. + +=item type + +A string that is equal to either of the variables B, +B, B or B. + +=item min + +A float or None, the minimum value. + +=item max + +A float or None, the maximum value. + +=back + +=item B(I None Flush one or all plugins. I and the specified I are passed on to the registered flush-callbacks. If omitted, the timeout defaults @@ -632,6 +690,8 @@ Any Python module will start similar to: A very simple read function might look like: + import random + def read(data=None): vl = collectd.Values(type='gauge') vl.plugin='python.spam' @@ -645,35 +705,19 @@ A very simple write function might look like: To register those functions with collectd: - collectd.register_read(read); - collectd.register_write(write); + collectd.register_read(read) + collectd.register_write(write) See the section L<"CLASSES"> above for a complete documentation of the data types used by the read, write and match functions. -=head1 NOTES - -=over 4 - -=item - -Please feel free to send in new plugins to collectd's mailinglist at -EcollectdEatEverplant.orgE for review and, possibly, -inclusion in the main distribution. In the latter case, we will take care of -keeping the plugin up to date and adapting it to new versions of collectd. - -Before submitting your plugin, please take a look at -L. - -=back - =head1 CAVEATS =over 4 -=item +=item * -collectd is heavily multi-threaded. Each collectd thread accessing the python +collectd is heavily multi-threaded. Each collectd thread accessing the Python plugin will be mapped to a Python interpreter thread. Any such thread will be created and destroyed transparently and on-the-fly. @@ -681,13 +725,13 @@ Hence, any plugin has to be thread-safe if it provides several entry points from collectd (i.Ee. if it registers more than one callback or if a registered callback may be called more than once in parallel). -=item +=item * The Python thread module is initialized just before calling the init callbacks. This means you must not use Python's threading module prior to this point. This includes all config and possibly other callback as well. -=item +=item * The python plugin exports the internal API of collectd which is considered unstable and subject to change at any time. We try hard to not break backwards @@ -701,10 +745,10 @@ dispatched by the python plugin after upgrades. =over 4 -=item +=item * -Not all aspects of the collectd API are accessible from python. This includes -but is not limited to filters and data sets. +Not all aspects of the collectd API are accessible from Python. This includes +but is not limited to filters. =back @@ -725,7 +769,7 @@ Sven Trenkel EcollectdEatEsemidefinite.deE. This manpage has been written by Sven Trenkel EcollectdEatEsemidefinite.deE. It is based on the L manual page by -Florian Forster EoctoEatEverplant.orgE and +Florian Forster EoctoEatEcollectd.orgE and Sebastian Harl EshEatEtokkee.orgE. =cut