Code

Merge remote-tracking branch 'github/pr/387'
[collectd.git] / src / collectd-perl.pod
1 =encoding UTF-8
3 =head1 NAME
5 collectd-perl - Documentation of collectd's C<perl plugin>
7 =head1 SYNOPSIS
9   <LoadPlugin perl>
10     Globals true
11   </LoadPlugin>
12   # ...
13   <Plugin perl>
14     IncludeDir "/path/to/perl/plugins"
15     BaseName "Collectd::Plugins"
16     EnableDebugger ""
17     LoadPlugin "FooBar"
19     <Plugin FooBar>
20       Foo "Bar"
21     </Plugin>
22   </Plugin>
24 =head1 DESCRIPTION
26 The C<perl plugin> embeds a Perl-interpreter into collectd and provides an
27 interface to collectd's plugin system. This makes it possible to write plugins
28 for collectd in Perl. This is a lot more efficient than executing a
29 Perl-script every time you want to read a value with the C<exec plugin> (see
30 L<collectd-exec(5)>) and provides a lot more functionality, too.
32 When loading the C<perl plugin>, the B<Globals> option should be enabled.
33 Else, the perl plugin will fail to load any Perl modules implemented in C,
34 which includes, amongst many others, the B<threads> module used by the plugin
35 itself. See the documentation of the B<Globals> option in L<collectd.conf(5)>
36 for details.
38 =head1 CONFIGURATION
40 =over 4
42 =item B<LoadPlugin> I<Plugin>
44 Loads the Perl plugin I<Plugin>. This does basically the same as B<use> would
45 do in a Perl program. As a side effect, the first occurrence of this option
46 causes the Perl-interpreter to be initialized.
48 =item B<BaseName> I<Name>
50 Prepends I<Name>B<::> to all plugin names loaded after this option. This is
51 provided for convenience to keep plugin names short. All Perl-based plugins
52 provided with the I<collectd> distributions reside in the C<Collectd::Plugins>
53 namespace.
55 =item E<lt>B<Plugin> I<Name>E<gt> block
57 This block may be used to pass on configuration settings to a Perl plugin. The
58 configuration is converted into a config-item data type which is passed to the
59 registered configuration callback. See below for details about the config-item
60 data type and how to register callbacks.
62 The I<name> identifies the callback. It is used literally and independent of
63 the B<BaseName> setting.
65 =item B<EnableDebugger> I<Package>[=I<option>,...]
67 Run collectd under the control of the Perl source debugger. If I<Package> is
68 not the empty string, control is passed to the debugging, profiling, or
69 tracing module installed as Devel::I<Package>. A comma-separated list of
70 options may be specified after the "=" character. Please note that you may not
71 leave out the I<Package> option even if you specify B<"">. This is the same as
72 using the B<-d:Package> command line option.
74 See L<perldebug> for detailed documentation about debugging Perl.
76 This option does not prevent collectd from daemonizing, so you should start
77 collectd with the B<-f> command line option. Else you will not be able to use
78 the command line driven interface of the debugger.
80 =item B<IncludeDir> I<Dir>
82 Adds I<Dir> to the B<@INC> array. This is the same as using the B<-IDir>
83 command line option or B<use lib Dir> in the source code. Please note that it
84 only has effect on plugins loaded after this option.
86 =back
88 =head1 WRITING YOUR OWN PLUGINS
90 Writing your own plugins is quite simple. collectd manages plugins by means of
91 B<dispatch functions> which call the appropriate B<callback functions>
92 registered by the plugins. Any plugin basically consists of the implementation
93 of these callback functions and initializing code which registers the
94 functions with collectd. See the section "EXAMPLES" below for a really basic
95 example. The following types of B<callback functions> are known to collectd
96 (all of them are optional):
98 =over 4
100 =item configuration functions
102 This type of functions is called during configuration if an appropriate
103 B<Plugin> block has been encountered. It is called once for each B<Plugin>
104 block which matches the name of the callback as provided with the
105 B<plugin_register> method - see below.
107 =item init functions
109 This type of functions is called once after loading the module and before any
110 calls to the read and write functions. It should be used to initialize the
111 internal state of the plugin (e.E<nbsp>g. open sockets, ...). If the return
112 value evaluates to B<false>, the plugin will be disabled.
114 =item read functions
116 This type of function is used to collect the actual data. It is called once
117 per interval (see the B<Interval> configuration option of collectd). Usually
118 it will call B<plugin_dispatch_values> to dispatch the values to collectd
119 which will pass them on to all registered B<write functions>. If the return
120 value evaluates to B<false> the plugin will be skipped for an increasing
121 amount of time until it returns B<true> again.
123 =item write functions
125 This type of function is used to write the dispatched values. It is called
126 once for each call to B<plugin_dispatch_values>.
128 =item flush functions
130 This type of function is used to flush internal caches of plugins. It is
131 usually triggered by the user only. Any plugin which caches data before
132 writing it to disk should provide this kind of callback function.
134 =item log functions
136 This type of function is used to pass messages of plugins or the daemon itself
137 to the user.
139 =item notification function
141 This type of function is used to act upon notifications. In general, a
142 notification is a status message that may be associated with a data instance.
143 Usually, a notification is generated by the daemon if a configured threshold
144 has been exceeded (see the section "THRESHOLD CONFIGURATION" in
145 L<collectd.conf(5)> for more details), but any plugin may dispatch
146 notifications as well.
148 =item shutdown functions
150 This type of function is called once before the daemon shuts down. It should
151 be used to clean up the plugin (e.g. close sockets, ...).
153 =back
155 Any function (except log functions) may set the B<$@> variable to describe
156 errors in more detail. The message will be passed on to the user using
157 collectd's logging mechanism.
159 See the documentation of the B<plugin_register> method in the section
160 "METHODS" below for the number and types of arguments passed to each
161 B<callback function>. This section also explains how to register B<callback
162 functions> with collectd.
164 To enable a plugin, copy it to a place where Perl can find it (i.E<nbsp>e. a
165 directory listed in the B<@INC> array) just as any other Perl plugin and add
166 an appropriate B<LoadPlugin> option to the configuration file. After
167 restarting collectd you're done.
169 =head1 DATA TYPES
171 The following complex types are used to pass values between the Perl plugin
172 and collectd:
174 =over 4
176 =item Config-Item
178 A config-item is one structure which keeps the information provided in the
179 configuration file. The array of children keeps one entry for each
180 configuration option. Each such entry is another config-item structure, which
181 may nest further if nested blocks are used.
183   {
184     key      => key,
185     values   => [ val1, val2, ... ],
186     children => [ { ... }, { ... }, ... ]
187   }
189 =item Data-Set
191 A data-set is a list of one or more data-sources. Each data-source defines a
192 name, type, min- and max-value and the data-set wraps them up into one
193 structure. The general layout looks like this:
195   [{
196     name => 'data_source_name',
197     type => DS_TYPE_COUNTER || DS_TYPE_GAUGE || DS_TYPE_DERIVE || DS_TYPE_ABSOLUTE,
198     min  => value || undef,
199     max  => value || undef
200   }, ...]
202 =item Value-List
204 A value-list is one structure which features an array of values and fields to
205 identify the values, i.E<nbsp>e. time and host, plugin name and
206 plugin-instance as well as a type and type-instance. Since the "type" is not
207 included in the value-list but is passed as an extra argument, the general
208 layout looks like this:
210   {
211     values => [123, 0.5],
212     time   => time (),
213     interval => plugin_get_interval (),
214     host   => $hostname_g,
215     plugin => 'myplugin',
216     type   => 'myplugin',
217     plugin_instance => '',
218     type_instance   => ''
219   }
221 =item Notification
223 A notification is one structure defining the severity, time and message of the
224 status message as well as an identification of a data instance. Also, it
225 includes an optional list of user-defined meta information represented as
226 (name, value) pairs:
228   {
229     severity => NOTIF_FAILURE || NOTIF_WARNING || NOTIF_OKAY,
230     time     => time (),
231     message  => 'status message',
232     host     => $hostname_g,
233     plugin   => 'myplugin',
234     type     => 'mytype',
235     plugin_instance => '',
236     type_instance   => '',
237     meta     => [ { name => <name>, value => <value> }, ... ]
238   }
240 =item Match-Proc
242 A match-proc is one structure storing the callbacks of a "match" of the filter
243 chain infrastructure. The general layout looks like this:
245   {
246     create  => 'my_create',
247     destroy => 'my_destroy',
248     match   => 'my_match'
249   }
251 =item Target-Proc
253 A target-proc is one structure storing the callbacks of a "target" of the
254 filter chain infrastructure. The general layout looks like this:
256   {
257     create  => 'my_create',
258     destroy => 'my_destroy',
259     invoke  => 'my_invoke'
260   }
262 =back
264 =head1 METHODS
266 The following functions provide the C-interface to Perl-modules. They are
267 exported by the ":plugin" export tag (see the section "EXPORTS" below).
269 =over 4
271 =item B<plugin_register> (I<type>, I<name>, I<data>)
273 Registers a callback-function or data-set.
275 I<type> can be one of:
277 =over 4
279 =item TYPE_CONFIG
281 =item TYPE_INIT
283 =item TYPE_READ
285 =item TYPE_WRITE
287 =item TYPE_FLUSH
289 =item TYPE_LOG
291 =item TYPE_NOTIF
293 =item TYPE_SHUTDOWN
295 =item TYPE_DATASET
297 =back
299 I<name> is the name of the callback-function or the type of the data-set,
300 depending on the value of I<type>. (Please note that the type of the data-set
301 is the value passed as I<name> here and has nothing to do with the I<type>
302 argument which simply tells B<plugin_register> what is being registered.)
304 The last argument, I<data>, is either a function name or an array-reference.
305 If I<type> is B<TYPE_DATASET>, then the I<data> argument must be an
306 array-reference which points to an array of hashes. Each hash describes one
307 data-set. For the exact layout see B<Data-Set> above. Please note that
308 there is a large number of predefined data-sets available in the B<types.db>
309 file which are automatically registered with collectd - see L<types.db(5)> for
310 a description of the format of this file.
312 B<Note>: Using B<plugin_register> to register a data-set is deprecated. Add
313 the new type to a custom L<types.db(5)> file instead. This functionality might
314 be removed in a future version of collectd.
316 If the I<type> argument is any of the other types (B<TYPE_INIT>, B<TYPE_READ>,
317 ...) then I<data> is expected to be a function name. If the name is not
318 prefixed with the plugin's package name collectd will add it automatically.
319 The interface slightly differs from the C interface (which expects a function
320 pointer instead) because Perl does not support to share references to
321 subroutines between threads.
323 These functions are called in the various stages of the daemon (see the
324 section "WRITING YOUR OWN PLUGINS" above) and are passed the following
325 arguments:
327 =over 4
329 =item TYPE_CONFIG
331 The only argument passed is I<config-item>. See above for the layout of this
332 data type.
334 =item TYPE_INIT
336 =item TYPE_READ
338 =item TYPE_SHUTDOWN
340 No arguments are passed.
342 =item TYPE_WRITE
344 The arguments passed are I<type>, I<data-set>, and I<value-list>. I<type> is a
345 string. For the layout of I<data-set> and I<value-list> see above.
347 =item TYPE_FLUSH
349 The arguments passed are I<timeout> and I<identifier>. I<timeout> indicates
350 that only data older than I<timeout> seconds is to be flushed. I<identifier>
351 specifies which values are to be flushed.
353 =item TYPE_LOG
355 The arguments are I<log-level> and I<message>. The log level is small for
356 important messages and high for less important messages. The least important
357 level is B<LOG_DEBUG>, the most important level is B<LOG_ERR>. In between there
358 are (from least to most important): B<LOG_INFO>, B<LOG_NOTICE>, and
359 B<LOG_WARNING>. I<message> is simply a string B<without> a newline at the end.
361 =item TYPE_NOTIF
363 The only argument passed is I<notification>. See above for the layout of this
364 data type.
366 =back
368 =item B<plugin_unregister> (I<type>, I<plugin>)
370 Removes a callback or data-set from collectd's internal list of
371 functionsE<nbsp>/ datasets.
373 =item B<plugin_dispatch_values> (I<value-list>)
375 Submits a I<value-list> to the daemon. If the data-set identified by
376 I<value-list>->{I<type>}
377 is found (and the number of values matches the number of data-sources) then the
378 type, data-set and value-list is passed to all write-callbacks that are
379 registered with the daemon.
381 =item B<plugin_write> ([B<plugins> => I<...>][, B<datasets> => I<...>],
382 B<valuelists> => I<...>)
384 Calls the write function of the given I<plugins> with the provided I<data
385 sets> and I<value lists>. In contrast to B<plugin_dispatch_values>, it does
386 not update collectd's internal cache and bypasses the filter mechanism (see
387 L<collectd.conf(5)> for details). If the B<plugins> argument has been omitted,
388 the values will be dispatched to all registered write plugins. If the
389 B<datasets> argument has been omitted, the required data sets are looked up
390 according to the C<type> member in the appropriate value list. The value of
391 all three arguments may either be a single scalar or a reference to an array.
392 If the B<datasets> argument has been specified, the number of data sets has to
393 equal the number of specified value lists.
395 =item B<plugin_flush> ([B<timeout> => I<timeout>][, B<plugins> => I<...>][,
396 B<identifiers> => I<...>])
398 Flush one or more plugins. I<timeout> and the specified I<identifiers> are
399 passed on to the registered flush-callbacks. If omitted, the timeout defaults
400 to C<-1>. The identifier defaults to the undefined value. If the B<plugins>
401 argument has been specified, only named plugins will be flushed. The value of
402 the B<plugins> and B<identifiers> arguments may either be a string or a
403 reference to an array of strings.
405 =item B<plugin_dispatch_notification> (I<notification>)
407 Submits a I<notification> to the daemon which will then pass it to all
408 notification-callbacks that are registered.
410 =item B<plugin_log> (I<log-level>, I<message>)
412 Submits a I<message> of level I<log-level> to collectd's logging mechanism.
413 The message is passed to all log-callbacks that are registered with collectd.
415 =item B<ERROR>, B<WARNING>, B<NOTICE>, B<INFO>, B<DEBUG> (I<message>)
417 Wrappers around B<plugin_log>, using B<LOG_ERR>, B<LOG_WARNING>,
418 B<LOG_NOTICE>, B<LOG_INFO> and B<LOG_DEBUG> respectively as I<log-level>.
420 =item B<plugin_get_interval> ()
422 Returns the interval of the current plugin as a floating point number in
423 seconds. This value depends on the interval configured within the
424 C<LoadPlugin perl> block or the global interval (see L<collectd.conf(5)> for
425 details).
427 =back
429 The following function provides the filter chain C-interface to Perl-modules.
430 It is exported by the ":filter_chain" export tag (see the section "EXPORTS"
431 below).
433 =over 4
435 =item B<fc_register> (I<type>, I<name>, I<proc>)
437 Registers filter chain callbacks with collectd.
439 I<type> may be any of:
441 =over 4
443 =item FC_MATCH
445 =item FC_TARGET
447 =back
449 I<name> is the name of the match or target. By this name, the callbacks are
450 identified in the configuration file when specifying a B<Match> or B<Target>
451 block (see L<collectd.conf(5)> for details).
453 I<proc> is a hash reference. The hash includes up to three callbacks: an
454 optional constructor (B<create>) and destructor (B<destroy>) and a mandatory
455 B<match> or B<invoke> callback. B<match> is called whenever processing an
456 appropriate match, while B<invoke> is called whenever processing an
457 appropriate target (see the section "FILTER CONFIGURATION" in
458 L<collectd.conf(5)> for details). Just like any other callbacks, filter chain
459 callbacks are identified by the function name rather than a function pointer
460 because Perl does not support to share references to subroutines between
461 threads. The following arguments are passed to the callbacks:
463 =over 4
465 =item create
467 The arguments passed are I<config-item> and I<user-data>. See above for the
468 layout of the config-item data-type. I<user-data> is a reference to a scalar
469 value that may be used to store any information specific to this particular
470 instance. The daemon does not care about this information at all. It's for the
471 plugin's use only.
473 =item destroy
475 The only argument passed is I<user-data> which is a reference to the user data
476 initialized in the B<create> callback. This callback may be used to cleanup
477 instance-specific information and settings.
479 =item match, invoke
481 The arguments passed are I<data-set>, I<value-list>, I<meta> and I<user-data>.
482 See above for the layout of the data-set and value-list data-types. I<meta> is
483 a pointer to an array of meta information, just like the B<meta> member of the
484 notification data-type (see above). I<user-data> is a reference to the user
485 data initialized in the B<create> callback.
487 =back
489 =back
491 =head1 GLOBAL VARIABLES
493 =over 4
495 =item B<$hostname_g>
497 As the name suggests this variable keeps the hostname of the system collectd
498 is running on. The value might be influenced by the B<Hostname> or
499 B<FQDNLookup> configuration options (see L<collectd.conf(5)> for details).
501 =item B<$interval_g>
503 This variable keeps the interval in seconds in which the read functions are
504 queried (see the B<Interval> configuration option).
506 B<Note:> This variable should no longer be used in favor of
507 C<plugin_get_interval()> (see above). This function takes any plugin-specific
508 interval settings into account (see the C<Interval> option of C<LoadPlugin> in
509 L<collectd.conf(5)> for details).
511 =back
513 Any changes to these variables will be globally visible in collectd.
515 =head1 EXPORTS
517 By default no symbols are exported. However, the following export tags are
518 available (B<:all> will export all of them):
520 =over 4
522 =item B<:plugin>
524 =over 4
526 =item B<plugin_register> ()
528 =item B<plugin_unregister> ()
530 =item B<plugin_dispatch_values> ()
532 =item B<plugin_flush> ()
534 =item B<plugin_flush_one> ()
536 =item B<plugin_flush_all> ()
538 =item B<plugin_dispatch_notification> ()
540 =item B<plugin_log> ()
542 =back
544 =item B<:types>
546 =over 4
548 =item B<TYPE_CONFIG>
550 =item B<TYPE_INIT>
552 =item B<TYPE_READ>
554 =item B<TYPE_WRITE>
556 =item B<TYPE_FLUSH>
558 =item B<TYPE_SHUTDOWN>
560 =item B<TYPE_LOG>
562 =item B<TYPE_DATASET>
564 =back
566 =item B<:ds_types>
568 =over 4
570 =item B<DS_TYPE_COUNTER>
572 =item B<DS_TYPE_GAUGE>
574 =item B<DS_TYPE_DERIVE>
576 =item B<DS_TYPE_ABSOLUTE>
578 =back
580 =item B<:log>
582 =over 4
584 =item B<ERROR> ()
586 =item B<WARNING> ()
588 =item B<NOTICE> ()
590 =item B<INFO> ()
592 =item B<DEBUG> ()
594 =item B<LOG_ERR>
596 =item B<LOG_WARNING>
598 =item B<LOG_NOTICE>
600 =item B<LOG_INFO>
602 =item B<LOG_DEBUG>
604 =back
606 =item B<:filter_chain>
608 =over 4
610 =item B<fc_register>
612 =item B<FC_MATCH_NO_MATCH>
614 =item B<FC_MATCH_MATCHES>
616 =item B<FC_TARGET_CONTINUE>
618 =item B<FC_TARGET_STOP>
620 =item B<FC_TARGET_RETURN>
622 =back
624 =item B<:fc_types>
626 =over 4
628 =item B<FC_MATCH>
630 =item B<FC_TARGET>
632 =back
634 =item B<:notif>
636 =over 4
638 =item B<NOTIF_FAILURE>
640 =item B<NOTIF_WARNING>
642 =item B<NOTIF_OKAY>
644 =back
646 =item B<:globals>
648 =over 4
650 =item B<$hostname_g>
652 =item B<$interval_g>
654 =back
656 =back
658 =head1 EXAMPLES
660 Any Perl plugin will start similar to:
662   package Collectd::Plugins::FooBar;
664   use strict;
665   use warnings;
667   use Collectd qw( :all );
669 A very simple read function might look like:
671   sub foobar_read
672   {
673     my $vl = { plugin => 'foobar', type => 'gauge' };
674     $vl->{'values'} = [ rand(42) ];
675     plugin_dispatch_values ($vl);
676     return 1;
677   }
679 A very simple write function might look like:
681   sub foobar_write
682   {
683     my ($type, $ds, $vl) = @_;
684     for (my $i = 0; $i < scalar (@$ds); ++$i) {
685       print "$vl->{'plugin'} ($vl->{'type'}): $vl->{'values'}->[$i]\n";
686     }
687     return 1;
688   }
690 A very simple match callback might look like:
692   sub foobar_match
693   {
694     my ($ds, $vl, $meta, $user_data) = @_;
695     if (matches($ds, $vl)) {
696       return FC_MATCH_MATCHES;
697     } else {
698       return FC_MATCH_NO_MATCH;
699     }
700   }
702 To register those functions with collectd:
704   plugin_register (TYPE_READ, "foobar", "foobar_read");
705   plugin_register (TYPE_WRITE, "foobar", "foobar_write");
707   fc_register (FC_MATCH, "foobar", "foobar_match");
709 See the section "DATA TYPES" above for a complete documentation of the data
710 types used by the read, write and match functions.
712 =head1 NOTES
714 =over 4
716 =item
718 Please feel free to send in new plugins to collectd's mailing list at
719 E<lt>collectdE<nbsp>atE<nbsp>verplant.orgE<gt> for review and, possibly,
720 inclusion in the main distribution. In the latter case, we will take care of
721 keeping the plugin up to date and adapting it to new versions of collectd.
723 Before submitting your plugin, please take a look at
724 L<http://collectd.org/dev-info.shtml>.
726 =back
728 =head1 CAVEATS
730 =over 4
732 =item
734 collectd is heavily multi-threaded. Each collectd thread accessing the perl
735 plugin will be mapped to a Perl interpreter thread (see L<threads(3perl)>).
736 Any such thread will be created and destroyed transparently and on-the-fly.
738 Hence, any plugin has to be thread-safe if it provides several entry points
739 from collectd (i.E<nbsp>e. if it registers more than one callback or if a
740 registered callback may be called more than once in parallel). Please note
741 that no data is shared between threads by default. You have to use the
742 B<threads::shared> module to do so.
744 =item
746 Each function name registered with collectd has to be available before the
747 first thread has been created (i.E<nbsp>e. basically at compile time). This
748 basically means that hacks (yes, I really consider this to be a hack) like
749 C<*foo = \&bar; plugin_register (TYPE_READ, "plugin", "foo");> most likely
750 will not work. This is due to the fact that the symbol table is not shared
751 across different threads.
753 =item
755 Each plugin is usually only loaded once and kept in memory for performance
756 reasons. Therefore, END blocks are only executed once when collectd shuts
757 down. You should not rely on END blocks anyway - use B<shutdown functions>
758 instead.
760 =item
762 The perl plugin exports the internal API of collectd which is considered
763 unstable and subject to change at any time. We try hard to not break backwards
764 compatibility in the Perl API during the life cycle of one major release.
765 However, this cannot be guaranteed at all times. Watch out for warnings
766 dispatched by the perl plugin after upgrades.
768 =back
770 =head1 KNOWN BUGS
772 =over 4
774 =item
776 Currently, it is not possible to flush a single Perl plugin only. You can
777 either flush all Perl plugins or none at all and you have to use C<perl> as
778 plugin name when doing so.
780 =back
782 =head1 SEE ALSO
784 L<collectd(1)>,
785 L<collectd.conf(5)>,
786 L<collectd-exec(5)>,
787 L<types.db(5)>,
788 L<perl(1)>,
789 L<threads(3perl)>,
790 L<threads::shared(3perl)>,
791 L<perldebug(1)>
793 =head1 AUTHOR
795 The C<perl plugin> has been written by Sebastian Harl
796 E<lt>shE<nbsp>atE<nbsp>tokkee.orgE<gt>.
798 This manpage has been written by Florian Forster
799 E<lt>octoE<nbsp>atE<nbsp>verplant.orgE<gt> and Sebastian Harl
800 E<lt>shE<nbsp>atE<nbsp>tokkee.orgE<gt>.
802 =cut