Added collectd-python man page.
[collectd.git] / src / collectd-python.pod
1 =head1 NAME
2
3 collectd-python - Documentation of collectd's C<python plugin>
4
5 =head1 SYNOPSIS
6
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"
16
17     <Module spam>
18       spam "wonderful" "lovely"
19     </Module>
20   </Plugin>
21
22 =head1 DESCRIPTION
23
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.
29
30 =head1 CONFIGURATION
31
32 =over 4
33
34 =item B<LoadPlugin> I<Plugin>
35
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".
42
43 =item B<MudulePath> I<Name>
44
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.
48
49 =item B<LogTraces> I<bool>
50
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.
56
57 =item B<Interactive> I<bool>
58
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.
63
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.
67
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.
76
77 To quit collectd send EOF (press Ctrl+D at the beginning of a new line).
78
79 =item E<lt>B<Module> I<Name>E<gt> block
80
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.
85
86 The I<name> identifies the callback.
87
88 =back
89
90 =head1 WRITING YOUR OWN PLUGINS
91
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):
99
100 =over 4
101
102 =item configuration functions
103
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.
108
109 Python thread support has not been initialized at this point so do not use any
110 threading functions at this point!
111
112 =item init functions
113
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.
118
119 =item read functions
120
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.
127
128 =item write functions
129
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.
132
133 =item flush functions
134
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.
138
139 =item log functions
140
141 This type of function is used to pass messages of plugins or the daemon itself
142 to the user.
143
144 =item notification function
145
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.
152
153 =item shutdown functions
154
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, ...).
157
158 =back
159
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.
164
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.
169
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.
174
175 =head1 CLASSES
176
177 The following complex types are used to pass values between the Python plugin
178 and collectd:
179
180 =over 4
181
182 =item Config
183
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.
188
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.
219
220
221 =item PluginData
222
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.
225
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
259
260
261 =item Values
262
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.
265
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.
319
320
321 =item Notification
322
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.
326
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.
363
364
365 =back
366
367 =head1 FUNCTIONS
368
369 The following functions provide the C-interface to Python-modules.
370
371 =over 4
372
373 =item B<register_*>(I<callback>[, I<data>][, I<name>]) -> identifier
374
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.
377
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<name>'. I<module> and I<name> are taken from the
385         B<__module__> and B<__name__> attributes of your callback function. If
386         the parameter I<name> contains a B<.>' it replaces both I<module> and
387         I<name>, otherwise it replaces only I<name>.
388         Every callback needs a unique identifier, so if you want to register
389         one function multiple time you need to specify a name here. Otherwise
390         it's save to ignore this parameter
391 I<identifier> is the full identifier assigned to this callback.
392
393 These functions are called in the various stages of the daemon (see the
394 section "WRITING YOUR OWN PLUGINS" above) and are passed the following
395 arguments:
396
397 =over 4
398
399 =item register_config
400
401 The only argument passed is a I<Config> object. See above for the layout of this
402 data type.
403 Note that you can not receive the whole config files this way, only B<Module>
404 blocks inside the Python configuration block. Additionally you will only
405 receive blocks where your callback identifier matches B<python.>I<blockname>. In
406 order for this to work the way the identifier is constructed is shortened to
407 not have a I<name> part.
408
409 =item register_init
410
411 The callback will be called without arguments.
412
413 =item register_read(callback[, interval][, data][, name]) -> identifier
414
415 This function takes an additional parameter: I<interval>. It specifies the
416 time between calls to the callback function.
417
418 The callback will be called without arguments.
419
420 =item register_shutdown
421
422 The callback will be called without arguments.
423
424 =item register_write
425
426 The callback function will be called with one arguments passed, which will be a
427 I<Values> object. For the layout of I<Values> see above.
428 If this callback function throws an exception the next call will be delayed by
429 an increasing interval.
430
431 =item register_flush
432
433 Like B<register_config> the identifier is shortened because it determines what
434 flush requests the plugin will receive.
435
436 The arguments passed are I<timeout> and I<identifier>. I<timeout> indicates
437 that only data older than I<timeout> seconds is to be flushed. I<identifier>
438 specifies which values are to be flushed.
439
440 =item register_log
441
442 The arguments are I<severity> and I<message>. The severity is an integer and
443 small for important messages and high for less important messages. The least
444 important level is B<LOG_DEBUG>, the most important level is B<LOG_ERR>. In
445 between there are (from least to most important): B<LOG_INFO>, B<LOG_NOTICE>,
446 and B<LOG_WARNING>. I<message> is simply a string B<without> a newline at the
447 end.
448
449 If this callback throws an exception it will B<not> be logged. It will just be
450 printed to sys.stderr which usually means silently ignored.
451
452 =item register_notification
453
454 The only argument passed is a I<Notification> object. See above for the layout of this
455 data type.
456
457 =back
458
459 =item B<unregister_*>(I<identifier>) -> None
460
461 Removes a callback or data-set from collectd's internal list of callback
462 functions. Every register_* function has an unregister_* function. I<identifier>
463 is either the string that was returned by the register function or a callback
464 function. The identifier will be constructed in the same way as for the
465 register functions.
466
467 =item B<flush>(I<plugin[, I<timeout>][, I<identifier>]) -> None
468
469 Flush one or all plugins. I<timeout> and the specified I<identifiers> are
470 passed on to the registered flush-callbacks. If omitted, the timeout defaults
471 to C<-1>. The identifier defaults to None. If the B<plugin> argument has been
472 specified, only named plugin will be flushed.
473
474 =item B<error>, B<warning>, B<notice>, B<info>, B<debug>(I<message>)
475
476 Log a message with the specified severity.
477
478 =back
479
480 =head1 EXAMPLES
481
482 Any Python module will start similar to:
483
484   import collectd
485
486 A very simple read function might look like:
487
488   def read(data=None):
489     vl = collectd.Values(type='gauge')
490     vl.plugin='python.spam'
491     vl.dispatch(values=[random.random() * 100])
492
493 A very simple write function might look like:
494
495   def write(vl, data=None):
496     for i in vl.values:
497       print "%s (%s): %f" % (vl.plugin, vl.type, i)
498
499 To register those functions with collectd:
500
501   collectd.register_read(read);
502   collectd.register_write(write);
503
504 See the section "CLASSES" above for a complete documentation of the data
505 types used by the read, write and match functions.
506
507 =head1 NOTES
508
509 =over 4
510
511 =item
512
513 Please feel free to send in new plugins to collectd's mailinglist at
514 E<lt>collectdE<nbsp>atE<nbsp>verplant.orgE<gt> for review and, possibly,
515 inclusion in the main distribution. In the latter case, we will take care of
516 keeping the plugin up to date and adapting it to new versions of collectd.
517
518 Before submitting your plugin, please take a look at
519 L<http://collectd.org/dev-info.shtml>.
520
521 =back
522
523 =head1 CAVEATS
524
525 =over 4
526
527 =item
528
529 collectd is heavily multi-threaded. Each collectd thread accessing the python
530 plugin will be mapped to a Python interpreter thread. Any such thread will be
531 created and destroyed transparently and on-the-fly.
532
533 Hence, any plugin has to be thread-safe if it provides several entry points
534 from collectd (i.E<nbsp>e. if it registers more than one callback or if a
535 registered callback may be called more than once in parallel).
536
537 =item
538
539 The Python thread module is initialized just before calling the init callbacks.
540 This means you must not use Python's threading module prior to this point. This
541 includes all config and possibly other callback as well.
542
543 =item
544
545 The python plugin exports the internal API of collectd which is considered
546 unstable and subject to change at any time. We try hard to not break backwards
547 compatibility in the Python API during the life cycle of one major release.
548 However, this cannot be guaranteed at all times. Watch out for warnings
549 dispatched by the python plugin after upgrades.
550
551 =back
552
553 =head1 KNOWN BUGS
554
555 =over 4
556
557 =item
558
559 This plugin is not compatible with python3. Trying to complie it with python3
560 will fail because of the ways string, unicode and bytearray bahavior was
561 changed.
562
563 Not all aspects of the collectd API are accessable from python. This includes
564 but is not limited to meta-data, filters and data sets.
565
566 =back
567
568 =head1 SEE ALSO
569
570 L<collectd(1)>,
571 L<collectd.conf(5)>,
572 L<collectd-perl(5)>,
573 L<collectd-exec(5)>,
574 L<types.db(5)>,
575 L<python(1)>,
576
577 =head1 AUTHOR
578
579 The C<python plugin> has been written by Sebastian Harl
580 E<lt>shE<nbsp>atE<nbsp>tokkee.orgE<gt>.
581
582 This manpage has been written by Florian Forster
583 E<lt>octoE<nbsp>atE<nbsp>verplant.orgE<gt> and Sebastian Harl
584 E<lt>shE<nbsp>atE<nbsp>tokkee.orgE<gt>.
585
586 =cut
587