Code

Removes the __name__ from callback identifiers. It was useless and made things way...
[collectd.git] / src / collectd-python.pod
1 =head1 NAME
3 collectd-python - Documentation of collectd's C<python plugin>
5 =head1 SYNOPSIS
7   <LoadPlugin python>
8     Globals true
9   </LoadPlugin>
10   # ...
11   <Plugin python>
12     ModulePath "/path/to/your/python/modules"
13     LogTraces true
14     Interactive true
15     Import "spam"
17     <Module spam>
18       spam "wonderful" "lovely"
19     </Module>
20   </Plugin>
22 =head1 DESCRIPTION
24 The C<python plugin> embeds a Python-interpreter into collectd and provides an
25 interface to collectd's plugin system. This makes it possible to write plugins
26 for collectd in Python. This is a lot more efficient than executing a
27 Python-script every time you want to read a value with the C<exec plugin> (see
28 L<collectd-exec(5)>) and provides a lot more functionality, too.
30 =head1 CONFIGURATION
32 =over 4
34 =item B<LoadPlugin> I<Plugin>
36 Loads the Python plugin I<Plugin>. Unlike most other LoadPlugin lines, this one
37 should be a block containing the line "Globals true". This will cause collectd
38 to export the name of all objects in the python interpreter for all plugins to
39 see. If you don't do this or your platform does not support it, the embeded
40 interpreter will start anywa but you won't be able to load certain python
41 modules, e.g. "time".
43 =item B<MudulePath> I<Name>
45 Appends I<Name> to B<sys.path>. You won't be able to import any scripts you
46 wrote unless they are located in one of the directuries in this list. Please
47 note that it only has effect on plugins loaded after this option.
49 =item B<LogTraces> I<bool>
51 If a python script throws an exception it will be logged by collectd with the
52 name of the exception and the message. If you set this option to true it will
53 also log the full stacktrace just like the default output of an interactive
54 python interpreter. This should probably be set to false most of the time but
55 is very useful for development and debugging of new modules.
57 =item B<Interactive> I<bool>
59 This option will causethe module to launch an interactive python interpreter
60 that reads from and writes to the terminal. Note that collectd will terminate
61 right after starting up if you try to run it as a daemon while this option is
62 enabled to make sure to start collectd with the B<-f> option.
64 The B<collectd> module is I<not> imported into the interpreter's globals. You
65 have to do it manually. Be sure to read the help text of the module, it can be
66 used as a reference guide during coding.
68 This interactive session will behave slightly differently from a daemonized
69 collectd script as well as from a normal python interpreter:
70 1. collectd will try to import the B<readline> module to give you a decent
71 way of entering your commmands. The daemonized collectd won't do that.
72 2. collectd will block SIGINT. Pressing Ctrl+C will usually cause collectd to
73 shut down. This would be problematic in an interactive session, therefore this
74 signal will be blocked. You can still use it to interrupt syscalls like sleep
75 and pause but it won't generate a KeyboardInterrupt exception either.
77 To quit collectd send EOF (press Ctrl+D at the beginning of a new line).
79 =item E<lt>B<Module> I<Name>E<gt> block
81 This block may be used to pass on configuration settings to a Python module.
82 The configuration is converted into an instance of the B<Config> class which
83 is passed to the registered configuration callback. See below for details about
84 the B<Config> class and how to register callbacks.
86 The I<name> identifies the callback.
88 =back
90 =head1 WRITING YOUR OWN PLUGINS
92 Writing your own plugins is quite simple. collectd manages plugins by means of
93 B<dispatch functions> which call the appropriate B<callback functions>
94 registered by the plugins. Any plugin basically consists of the implementation
95 of these callback functions and initializing code which registers the
96 functions with collectd. See the section "EXAMPLES" below for a really basic
97 example. The following types of B<callback functions> are known to collectd
98 (all of them are optional):
100 =over 4
102 =item configuration functions
104 This type of functions is called during configuration if an appropriate
105 B<Module> block has been encountered. It is called once for each B<Module>
106 block which matches the name of the callback as provided with the
107 B<register_config> method - see below.
109 Python thread support has not been initialized at this point so do not use any
110 threading functions here!
112 =item init functions
114 This type of functions is called once after loading the module and before any
115 calls to the read and write functions. It should be used to initialize the
116 internal state of the plugin (e.E<nbsp>g. open sockets, ...). This is the
117 earliest point where you may use threads.
119 =item read functions
121 This type of function is used to collect the actual data. It is called once
122 per interval (see the B<Interval> configuration option of collectd). Usually
123 it will call B<plugin_dispatch_values> to dispatch the values to collectd
124 which will pass them on to all registered B<write functions>. If this function
125 throws any kind of exception the plugin will be skipped for an increasing
126 amount of time until it returns normally again.
128 =item write functions
130 This type of function is used to write the dispatched values. It is called
131 once for every value that was dispatched by any plugin.
133 =item flush functions
135 This type of function is used to flush internal caches of plugins. It is
136 usually triggered by the user only. Any plugin which caches data before
137 writing it to disk should provide this kind of callback function.
139 =item log functions
141 This type of function is used to pass messages of plugins or the daemon itself
142 to the user.
144 =item notification function
146 This type of function is used to act upon notifications. In general, a
147 notification is a status message that may be associated with a data instance.
148 Usually, a notification is generated by the daemon if a configured threshold
149 has been exceeded (see the section "THRESHOLD CONFIGURATION" in
150 L<collectd.conf(5)> for more details), but any plugin may dispatch
151 notifications as well.
153 =item shutdown functions
155 This type of function is called once before the daemon shuts down. It should
156 be used to clean up the plugin (e.g. close sockets, ...).
158 =back
160 Any function (except log functions) may set throw an exception in case of any
161 errors. The exception will be passed on to the user using collectd's logging
162 mechanism. If a log callback throws an exception it will be printed to stderr
163 instead.
165 See the documentation of the various B<register_> methods in the section
166 "FUNCTIONS" below for the number and types of arguments passed to each
167 B<callback function>. This section also explains how to register B<callback
168 functions> with collectd.
170 To enable a module, copy it to a place where Python can find it (i.E<nbsp>e. a
171 directory listed in B<sys.path>) just as any other Python plugin and add
172 an appropriate B<Import> option to the configuration file. After restarting
173 collectd you're done.
175 =head1 CLASSES
177 The following complex types are used to pass values between the Python plugin
178 and collectd:
180 =over 4
182 =item Config
184 The Config class is an object which keeps the informations provided in the
185 configuration file. The sequence of children keeps one entry for each
186 configuration option. Each such entry is another Config instance, which
187 may nest further if nested blocks are used.
189 class Config(object)
190  |  This represents a piece of collectd's config file.
191  |  It is passed to scripts with config callbacks (see B<register_config>)
192  |  and is of little use if created somewhere else.
193  |
194  |  It has no methods beyond the bare minimum and only exists for its
195  |  data members
196  |
197  |  ----------------------------------------------------------------------
198  |  Data descriptors defined here:
199  |
200  |  parent
201  |      This represents the parent of this node. On the root node
202  |      of the config tree it will be None.
203  |
204  |  key
205  |      This is the keyword of this item, ie the first word of any
206  |      given line in the config file. It will always be a string.
207  |
208  |  values
209  |      This is a tuple (which might be empty) of all value, ie words
210  |      following the keyword in any given line in the config file.
211  |
212  |      Every item in this tuple will be either a string or a float or a bool,
213  |      depending on the contents of the configuration file.
214  |
215  |  children
216  |      This is a tuple of child nodes. For most nodes this will be
217  |      empty. If this node represents a block instead of a single line of the config
218  |      file it will contain all nodes in this block.
221 =item PluginData
223 This should not be used directly but it is the base class for both Values and
224 Notification. It is used to identify the source of a value or notification.
226 class PluginData(object)
227  |  This is an internal class that is the base for Values
228  |  and Notification. It is pretty useless by itself and was therefore not
229  |  exported to the collectd module.
230  |
231  |  ----------------------------------------------------------------------
232  |  Data descriptors defined here:
233  |
234  |  host
235  |      The hostname of the host this value was read from.
236  |      For dispatching this can be set to an empty string which means
237  |      the local hostname as defined in collectd.conf.
238  |
239  |  plugin
240  |      The name of the plugin that read the data. Setting this
241  |      member to an empty string will insert "python" upon dispatching.
242  |
243  |  plugin_instance
244  |
245  |  time
246  |      This is the Unix timestap of the time this value was read.
247  |      For dispatching values this can be set to 0 which means "now".
248  |      This means the time the value is actually dispatched, not the time
249  |      it was set to 0.
250  |
251  |  type
252  |      The type of this value. This type has to be defined
253  |      in your types.db. Attempting to set it to any other value will
254  |      raise a TypeError exception.
255  |      Assigning a type is mandetory, calling dispatch without doing
256  |      so will raise a RuntimeError exception.
257  |
258  |  type_instance
261 =item Values
263 A Value is an object which features a sequence of values. It is based on then
264 I<PluginData> type and uses its members to identify the values.
266 class Values(PluginData)
267  |  A Values object used for dispatching values to collectd and receiving
268  |  values from write callbacks.
269  |  
270  |  Method resolution order:
271  |      Values
272  |      PluginData
273  |      object
274  |  
275  |  Methods defined here:
276  |  
277  |  dispatch(...)
278  |      dispatch([type][, values][, plugin_instance][, type_instance][, plugin][, host][, time][, interval]) -> None.  Dispatch a value list.
279  |      
280  |      Dispatch this instance to the collectd process. The object has members
281  |      for each of the possible arguments for this method. For a detailed
282  |      explanation of these parameters see the member of the same same.
283  |      
284  |      If you do not submit a parameter the value saved in its member will be
285  |      submitted. If you do provide a parameter it will be used instead,
286  |      without altering the member.
287  |  
288  |  write(...)
289  |      write([destination][, type][, values][, plugin_instance][, type_instance][, plugin][, host][, time][, interval]) -> None.  Dispatch a value list.
290  |
291  |      Write this instance to a single plugin or all plugins if 'destination' is obmitted.
292  |      This will bypass the main collectd process and all filtering and caching.
293  |      Other than that it works similar to 'dispatch'. In most cases 'dispatch' should be
294  |      used instead of 'write'.
295  |
296  |  ----------------------------------------------------------------------
297  |  Data descriptors defined here:
298  |  
299  |  interval
300  |      The interval is the timespan in seconds between two submits for the
301  |      same data source. This value has to be a positive integer, so you can't
302  |      submit more than one value per second. If this member is set to a
303  |      non-positive value, the default value as specified in the config file
304  |      will be used (default: 10).
305  |      
306  |      If you submit values more often than the specified interval, the average
307  |      will be used. If you submit less values, your graphs will have gaps.
308  |  
309  |  values
310  |      These are the actual values that get dispatched to collectd.
311  |      It has to be a sequence (a tuple or list) of numbers.
312  |      The size of the sequence and the type of its content depend on the type
313  |      member your types.db file. For more information on this read the
314  |      types.db man page.
315  |      
316  |      If the sequence does not have the correct size upon dispatch a
317  |      RuntimeError exception will be raised. If the content of the sequence
318  |      is not a number, a TypeError exception will be raised.
321 =item Notification
323 A notification is an object defining the severity and message of the status
324 message as well as an identification of a data instance by means of the members
325 of PluginData on which it is based.
327 class Notification(PluginData)
328  |  The Notification class is a wrapper around the collectd notification.
329  |  It can be used to notify other plugins about bad stuff happening. It works
330  |  similar to Values but has a severity and a message instead of interval
331  |  and time.
332  |  Notifications can be dispatched at any time and can be received with
333  |  register_notification.
334  |  
335  |  Method resolution order:
336  |      Notification
337  |      PluginData
338  |      object
339  |  
340  |  Methods defined here:
341  |  
342  |  dispatch(...)
343  |      dispatch([type][, values][, plugin_instance][, type_instance][, plugin][, host][, time][, interval]) -> None.  Dispatch a value list.
344  |      
345  |      Dispatch this instance to the collectd process. The object has members
346  |      for each of the possible arguments for this method. For a detailed
347  |      explanation of these parameters see the member of the same same.
348  |      
349  |      If you do not submit a parameter the value saved in its member will be
350  |      submitted. If you do provide a parameter it will be used instead,
351  |      without altering the member.
352  |  
353  |  ----------------------------------------------------------------------
354  |  Data descriptors defined here:
355  |  
356  |  message
357  |      Some kind of description what's going on and why this Notification
358  |      was generated.
359  |  
360  |  severity
361  |      The severity of this notification. Assign or compare to
362  |      NOTIF_FAILURE, NOTIF_WARNING or NOTIF_OKAY.
365 =back
367 =head1 FUNCTIONS
369 The following functions provide the C-interface to Python-modules.
371 =over 4
373 =item B<register_*>(I<callback>[, I<data>][, I<name>]) -> identifier
375 There are eight different register functions to get callback for eight
376 different events. With one exception all of them are called as shown above.
378 I<callback> is a callable object that will be called every time the event is
379         triggered.
380 I<data> is an optional object that will be passed back to the callback function
381         every time it is called. If you obmit this parameter no object is
382         passed back to your callback, not even None.
383 I<name> is an optional identifier for this callback. The default name is
384         B<python>.I<module>. I<module> is taken from the B<__module__>
385         attribute of your callback function.
386         Every callback needs a unique identifier, so if you want to register
387         the same callback multiple time in the same module you need to specify
388         a name here. Otherwise it's save to ignore this parameter
389 I<identifier> is the full identifier assigned to this callback.
391 These functions are called in the various stages of the daemon (see the
392 section "WRITING YOUR OWN PLUGINS" above) and are passed the following
393 arguments:
395 =over 4
397 =item register_config
399 The only argument passed is a I<Config> object. See above for the layout of this
400 data type.
401 Note that you can not receive the whole config files this way, only B<Module>
402 blocks inside the Python configuration block. Additionally you will only
403 receive blocks where your callback identifier matches B<python.>I<blockname>.
405 =item register_init
407 The callback will be called without arguments.
409 =item register_read(callback[, interval][, data][, name]) -> identifier
411 This function takes an additional parameter: I<interval>. It specifies the
412 time between calls to the callback function.
414 The callback will be called without arguments.
416 =item register_shutdown
418 The callback will be called without arguments.
420 =item register_write
422 The callback function will be called with one arguments passed, which will be a
423 I<Values> object. For the layout of I<Values> see above.
424 If this callback function throws an exception the next call will be delayed by
425 an increasing interval.
427 =item register_flush
429 Like B<register_config> is important for this callbavk because it determines
430 what flush requests the plugin will receive.
432 The arguments passed are I<timeout> and I<identifier>. I<timeout> indicates
433 that only data older than I<timeout> seconds is to be flushed. I<identifier>
434 specifies which values are to be flushed.
436 =item register_log
438 The arguments are I<severity> and I<message>. The severity is an integer and
439 small for important messages and high for less important messages. The least
440 important level is B<LOG_DEBUG>, the most important level is B<LOG_ERR>. In
441 between there are (from least to most important): B<LOG_INFO>, B<LOG_NOTICE>,
442 and B<LOG_WARNING>. I<message> is simply a string B<without> a newline at the
443 end.
445 If this callback throws an exception it will B<not> be logged. It will just be
446 printed to sys.stderr which usually means silently ignored.
448 =item register_notification
450 The only argument passed is a I<Notification> object. See above for the layout of this
451 data type.
453 =back
455 =item B<unregister_*>(I<identifier>) -> None
457 Removes a callback or data-set from collectd's internal list of callback
458 functions. Every register_* function has an unregister_* function. I<identifier>
459 is either the string that was returned by the register function or a callback
460 function. The identifier will be constructed in the same way as for the
461 register functions.
463 =item B<flush>(I<plugin[, I<timeout>][, I<identifier>]) -> None
465 Flush one or all plugins. I<timeout> and the specified I<identifiers> are
466 passed on to the registered flush-callbacks. If omitted, the timeout defaults
467 to C<-1>. The identifier defaults to None. If the B<plugin> argument has been
468 specified, only named plugin will be flushed.
470 =item B<error>, B<warning>, B<notice>, B<info>, B<debug>(I<message>)
472 Log a message with the specified severity.
474 =back
476 =head1 EXAMPLES
478 Any Python module will start similar to:
480   import collectd
482 A very simple read function might look like:
484   def read(data=None):
485     vl = collectd.Values(type='gauge')
486     vl.plugin='python.spam'
487     vl.dispatch(values=[random.random() * 100])
489 A very simple write function might look like:
491   def write(vl, data=None):
492     for i in vl.values:
493       print "%s (%s): %f" % (vl.plugin, vl.type, i)
495 To register those functions with collectd:
497   collectd.register_read(read);
498   collectd.register_write(write);
500 See the section "CLASSES" above for a complete documentation of the data
501 types used by the read, write and match functions.
503 =head1 NOTES
505 =over 4
507 =item
509 Please feel free to send in new plugins to collectd's mailinglist at
510 E<lt>collectdE<nbsp>atE<nbsp>verplant.orgE<gt> for review and, possibly,
511 inclusion in the main distribution. In the latter case, we will take care of
512 keeping the plugin up to date and adapting it to new versions of collectd.
514 Before submitting your plugin, please take a look at
515 L<http://collectd.org/dev-info.shtml>.
517 =back
519 =head1 CAVEATS
521 =over 4
523 =item
525 collectd is heavily multi-threaded. Each collectd thread accessing the python
526 plugin will be mapped to a Python interpreter thread. Any such thread will be
527 created and destroyed transparently and on-the-fly.
529 Hence, any plugin has to be thread-safe if it provides several entry points
530 from collectd (i.E<nbsp>e. if it registers more than one callback or if a
531 registered callback may be called more than once in parallel).
533 =item
535 The Python thread module is initialized just before calling the init callbacks.
536 This means you must not use Python's threading module prior to this point. This
537 includes all config and possibly other callback as well.
539 =item
541 The python plugin exports the internal API of collectd which is considered
542 unstable and subject to change at any time. We try hard to not break backwards
543 compatibility in the Python API during the life cycle of one major release.
544 However, this cannot be guaranteed at all times. Watch out for warnings
545 dispatched by the python plugin after upgrades.
547 =back
549 =head1 KNOWN BUGS
551 =over 4
553 =item
555 This plugin is not compatible with python3. Trying to complie it with python3
556 will fail because of the ways string, unicode and bytearray bahavior was
557 changed.
559 Not all aspects of the collectd API are accessable from python. This includes
560 but is not limited to meta-data, filters and data sets.
562 =back
564 =head1 SEE ALSO
566 L<collectd(1)>,
567 L<collectd.conf(5)>,
568 L<collectd-perl(5)>,
569 L<collectd-exec(5)>,
570 L<types.db(5)>,
571 L<python(1)>,
573 =head1 AUTHOR
575 The C<python plugin> has been written by Sebastian Harl
576 E<lt>shE<nbsp>atE<nbsp>tokkee.orgE<gt>.
578 This manpage has been written by Florian Forster
579 E<lt>octoE<nbsp>atE<nbsp>verplant.orgE<gt> and Sebastian Harl
580 E<lt>shE<nbsp>atE<nbsp>tokkee.orgE<gt>.
582 =cut