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