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