Code

Merge branch 'collectd-4.5' into collectd-4.6
[collectd.git] / src / collectd-perl.pod
1 =head1 NAME
3 collectd-perl - Documentation of collectd's C<perl plugin>
5 =head1 SYNOPSIS
7   LoadPlugin perl
8   # ...
9   <Plugin perl>
10     IncludeDir "/path/to/perl/plugins"
11     BaseName "Collectd::Plugin"
12     EnableDebugger ""
13     LoadPlugin "FooBar"
15     <Plugin FooBar>
16       Foo "Bar"
17     </Plugin>
18   </Plugin>
20 =head1 DESCRIPTION
22 The C<perl plugin> embeds a Perl-interpreter into collectd and provides an
23 interface to collectd's plugin system. This makes it possible to write plugins
24 for collectd in Perl. This is a lot more efficient than executing a
25 Perl-script every time you want to read a value with the C<exec plugin> (see
26 L<collectd-exec(5)>) and provides a lot more functionality, too.
28 =head1 CONFIGURATION
30 =over 4
32 =item B<LoadPlugin> I<Plugin>
34 Loads the Perl plugin I<Plugin>. This does basically the same as B<use> would
35 do in a Perl program. As a side effect, the first occurrence of this option
36 causes the Perl-interpreter to be initialized.
38 =item B<BaseName> I<Name>
40 Prepends I<Name>B<::> to all plugin names loaded after this option. This is
41 provided for convenience to keep plugin names short.
43 =item E<lt>B<Plugin> I<Name>E<gt> block
45 This block may be used to pass on configuration settings to a Perl plugin. The
46 configuration is converted into a config-item data type which is passed to the
47 registered configuration callback. See below for details about the config-item
48 data type and how to register callbacks.
50 The I<name> identifies the callback. It is used literally and independent of
51 the B<BaseName> setting.
53 =item B<EnableDebugger> I<Package>[=I<option>,...]
55 Run collectd under the control of the Perl source debugger. If I<Package> is
56 not the empty string, control is passed to the debugging, profiling, or
57 tracing module installed as Devel::I<Package>. A comma-separated list of
58 options may be specified after the "=" character. Please note that you may not
59 leave out the I<Package> option even if you specify B<"">. This is the same as
60 using the B<-d:Package> command line option.
62 See L<perldebug> for detailed documentation about debugging Perl.
64 This option does not prevent collectd from daemonizing, so you should start
65 collectd with the B<-f> command line option. Else you will not be able to use
66 the command line driven interface of the debugger.
68 =item B<IncludeDir> I<Dir>
70 Adds I<Dir> to the B<@INC> array. This is the same as using the B<-IDir>
71 command line option or B<use lib Dir> in the source code. Please note that it
72 only has effect on plugins loaded after this option.
74 =back
76 =head1 WRITING YOUR OWN PLUGINS
78 Writing your own plugins is quite simple. collectd manages plugins by means of
79 B<dispatch functions> which call the appropriate B<callback functions>
80 registered by the plugins. Any plugin basically consists of the implementation
81 of these callback functions and initializing code which registers the
82 functions with collectd. See the section "EXAMPLES" below for a really basic
83 example. The following types of B<callback functions> are known to collectd
84 (all of them are optional):
86 =over 4
88 =item configuration functions
90 This type of functions is called during configuration if an appropriate
91 B<Plugin> block has been encountered. It is called once for each B<Plugin>
92 block which matches the name of the callback as provided with the
93 B<plugin_register> method - see below.
95 =item init functions
97 This type of functions is called once after loading the module and before any
98 calls to the read and write functions. It should be used to initialize the
99 internal state of the plugin (e.E<nbsp>g. open sockets, ...). If the return
100 value evaluates to B<false>, the plugin will be disabled.
102 =item read functions
104 This type of function is used to collect the actual data. It is called once
105 per interval (see the B<Interval> configuration option of collectd). Usually
106 it will call B<plugin_dispatch_values> to dispatch the values to collectd
107 which will pass them on to all registered B<write functions>. If the return
108 value evaluates to B<false> the plugin will be skipped for an increasing
109 amount of time until it returns B<true> again.
111 =item write functions
113 This type of function is used to write the dispatched values. It is called
114 once for each call to B<plugin_dispatch_values>.
116 =item flush functions
118 This type of function is used to flush internal caches of plugins. It is
119 usually triggered by the user only. Any plugin which caches data before
120 writing it to disk should provide this kind of callback function.
122 =item log functions
124 This type of function is used to pass messages of plugins or the daemon itself
125 to the user.
127 =item notification function
129 This type of function is used to act upon notifications. In general, a
130 notification is a status message that may be associated with a data instance.
131 Usually, a notification is generated by the daemon if a configured threshold
132 has been exceeded (see the section "THRESHOLD CONFIGURATION" in
133 L<collectd.conf(5)> for more details), but any plugin may dispatch
134 notifications as well.
136 =item shutdown functions
138 This type of function is called once before the daemon shuts down. It should
139 be used to clean up the plugin (e.g. close sockets, ...).
141 =back
143 Any function (except log functions) may set the B<$@> variable to describe
144 errors in more detail. The message will be passed on to the user using
145 collectd's logging mechanism.
147 See the documentation of the B<plugin_register> method in the section
148 "METHODS" below for the number and types of arguments passed to each
149 B<callback function>. This section also explains how to register B<callback
150 functions> with collectd.
152 To enable a plugin, copy it to a place where Perl can find it (i.E<nbsp>e. a
153 directory listed in the B<@INC> array) just as any other Perl plugin and add
154 an appropriate B<LoadPlugin> option to the configuration file. After
155 restarting collectd you're done.
157 =head1 DATA TYPES
159 The following complex types are used to pass values between the Perl plugin
160 and collectd:
162 =over 4
164 =item Config-Item
166 A config-item is one structure which keeps the informations provided in the
167 configuration file. The array of children keeps one entry for each
168 configuration option. Each such entry is another config-item structure, which
169 may nest further if nested blocks are used.
171   {
172     key      => key,
173     values   => [ val1, val2, ... ],
174     children => [ { ... }, { ... }, ... ]
175   }
177 =item Data-Set
179 A data-set is a list of one or more data-sources. Each data-source defines a
180 name, type, min- and max-value and the data-set wraps them up into one
181 structure. The general layout looks like this:
183   [{
184     name => 'data_source_name',
185     type => DS_TYPE_COUNTER || DS_TYPE_GAUGE,
186     min  => value || undef,
187     max  => value || undef
188   }, ...]
190 =item Value-List
192 A value-list is one structure which features an array of values and fields to
193 identify the values, i.E<nbsp>e. time and host, plugin name and
194 plugin-instance as well as a type and type-instance. Since the "type" is not
195 included in the value-list but is passed as an extra argument, the general
196 layout looks like this:
198   {
199     values => [123, 0.5],
200     time   => time (),
201     interval => $interval_g,
202     host   => $hostname_g,
203     plugin => 'myplugin',
204     type   => 'myplugin',
205     plugin_instance => '',
206     type_instance   => ''
207   }
209 =item Notification
211 A notification is one structure defining the severity, time and message of the
212 status message as well as an identification of a data instance. Also, it
213 includes an optional list of user-defined meta information represented as
214 (name, value) pairs:
216   {
217     severity => NOTIF_FAILURE || NOTIF_WARNING || NOTIF_OKAY,
218     time     => time (),
219     message  => 'status message',
220     host     => $hostname_g,
221     plugin   => 'myplugin',
222     type     => 'mytype',
223     plugin_instance => '',
224     type_instance   => '',
225     meta     => [ { name => <name>, value => <value> }, ... ]
226   }
228 =item Match-Proc
230 A match-proc is one structure storing the callbacks of a "match" of the filter
231 chain infrastructure. The general layout looks like this:
233   {
234     create  => 'my_create',
235     destroy => 'my_destroy',
236     match   => 'my_match'
237   }
239 =item Target-Proc
241 A target-proc is one structure storing the callbacks of a "target" of the
242 filter chain infrastructure. The general layout looks like this:
244   {
245     create  => 'my_create',
246     destroy => 'my_destroy',
247     invoke  => 'my_invoke'
248   }
250 =back
252 =head1 METHODS
254 The following functions provide the C-interface to Perl-modules. They are
255 exported by the ":plugin" export tag (see the section "EXPORTS" below).
257 =over 4
259 =item B<plugin_register> (I<type>, I<name>, I<data>)
261 Registers a callback-function or data-set.
263 I<type> can be one of:
265 =over 4
267 =item TYPE_CONFIG
269 =item TYPE_INIT
271 =item TYPE_READ
273 =item TYPE_WRITE
275 =item TYPE_FLUSH
277 =item TYPE_LOG
279 =item TYPE_NOTIF
281 =item TYPE_SHUTDOWN
283 =item TYPE_DATASET
285 =back
287 I<name> is the name of the callback-function or the type of the data-set,
288 depending on the value of I<type>. (Please note that the type of the data-set
289 is the value passed as I<name> here and has nothing to do with the I<type>
290 argument which simply tells B<plugin_register> what is being registered.)
292 The last argument, I<data>, is either a function name or an array-reference.
293 If I<type> is B<TYPE_DATASET>, then the I<data> argument must be an
294 array-reference which points to an array of hashes. Each hash describes one
295 data-set. For the exact layout see B<Data-Set> above. Please note that
296 there is a large number of predefined data-sets available in the B<types.db>
297 file which are automatically registered with collectd - see L<types.db(5)> for
298 a description of the format of this file.
300 If the I<type> argument is any of the other types (B<TYPE_INIT>, B<TYPE_READ>,
301 ...) then I<data> is expected to be a function name. If the name is not
302 prefixed with the plugin's package name collectd will add it automatically.
303 The interface slightly differs from the C interface (which expects a function
304 pointer instead) because Perl does not support to share references to
305 subroutines between threads.
307 These functions are called in the various stages of the daemon (see the
308 section "WRITING YOUR OWN PLUGINS" above) and are passed the following
309 arguments:
311 =over 4
313 =item TYPE_CONFIG
315 The only argument passed is I<config-item>. See above for the layout of this
316 data type.
318 =item TYPE_INIT
320 =item TYPE_READ
322 =item TYPE_SHUTDOWN
324 No arguments are passed.
326 =item TYPE_WRITE
328 The arguments passed are I<type>, I<data-set>, and I<value-list>. I<type> is a
329 string. For the layout of I<data-set> and I<value-list> see above.
331 =item TYPE_FLUSH
333 The arguments passed are I<timeout> and I<identifier>. I<timeout> indicates
334 that only data older than I<timeout> seconds is to be flushed. I<identifier>
335 specifies which values are to be flushed.
337 =item TYPE_LOG
339 The arguments are I<log-level> and I<message>. The log level is small for
340 important messages and high for less important messages. The least important
341 level is B<LOG_DEBUG>, the most important level is B<LOG_ERR>. In between there
342 are (from least to most important): B<LOG_INFO>, B<LOG_NOTICE>, and
343 B<LOG_WARNING>. I<message> is simply a string B<without> a newline at the end.
345 =item TYPE_NOTIF
347 The only argument passed is I<notification>. See above for the layout of this
348 data type.
350 =back
352 =item B<plugin_unregister> (I<type>, I<plugin>)
354 Removes a callback or data-set from collectd's internal list of
355 functionsE<nbsp>/ datasets.
357 =item B<plugin_dispatch_values> (I<value-list>)
359 Submits a I<value-list> to the daemon. If the data-set identified by
360 I<value-list>->{I<type>}
361 is found (and the number of values matches the number of data-sources) then the
362 type, data-set and value-list is passed to all write-callbacks that are
363 registered with the daemon.
365 B<Note>: Prior to version 4.4 of collectd, the data-set type used to be passed
366 as the first argument to B<plugin_register>. This syntax is still supported
367 for backwards compatibility but has been deprecated and will be removed in
368 some future version of collectd.
370 =item B<plugin_write> ([B<plugins> => I<...>][, B<datasets> => I<...>],
371 B<valuelists> => I<...>)
373 Calls the write function of the given I<plugins> with the provided I<data
374 sets> and I<value lists>. In contrast to B<plugin_dispatch_values>, it does
375 not update collectd's internal cache and bypasses the filter mechanism (see
376 L<collectd.conf(5)> for details). If the B<plugins> argument has been omitted,
377 the values will be dispatched to all registered write plugins. If the
378 B<datasets> argument has been omitted, the required data sets are looked up
379 according to the C<type> member in the appropriate value list. The value of
380 all three arguments may either be a single scalar or a reference to an array.
381 If the B<datasets> argument has been specified, the number of data sets has to
382 equal the number of specified value lists.
384 =item B<plugin_flush> ([B<timeout> => I<timeout>][, B<plugins> => I<...>][,
385 B<identifiers> => I<...>])
387 Flush one or more plugins. I<timeout> and the specified I<identifiers> are
388 passed on to the registered flush-callbacks. If omitted, the timeout defaults
389 to C<-1>. The identifier defaults to the undefined value. If the B<plugins>
390 argument has been specified, only named plugins will be flushed. The value of
391 the B<plugins> and B<identifiers> arguments may either be a string or a
392 reference to an array of strings.
394 =item B<plugin_flush_one> (I<timeout>, I<plugin>)
396 This is identical to using "plugin_flush (timeout =E<gt> I<timeout>, plugins
397 =E<gt> I<plugin>".
399 B<Note>: Starting with version 4.5 of collectd, B<plugin_flush_one> has been
400 deprecated and will be removed in some future version of collectd. Use
401 B<plugin_flush> instead.
403 =item B<plugin_flush_all> (I<timeout>)
405 This is identical to using "plugin_flush (timeout =E<gt> I<timeout>)".
407 B<Note>: Starting with version 4.5 of collectd, B<plugin_flush_all> has been
408 deprecated and will be removed in some future version of collectd. Use
409 B<plugin_flush> instead.
411 =item B<plugin_dispatch_notification> (I<notification>)
413 Submits a I<notification> to the daemon which will then pass it to all
414 notification-callbacks that are registered.
416 =item B<plugin_log> (I<log-level>, I<message>)
418 Submits a I<message> of level I<log-level> to collectd's logging mechanism.
419 The message is passed to all log-callbacks that are registered with collectd.
421 =item B<ERROR>, B<WARNING>, B<NOTICE>, B<INFO>, B<DEBUG> (I<message>)
423 Wrappers around B<plugin_log>, using B<LOG_ERR>, B<LOG_WARNING>,
424 B<LOG_NOTICE>, B<LOG_INFO> and B<LOG_DEBUG> respectively as I<log-level>.
426 =back
428 The following function provides the filter chain C-interface to Perl-modules.
429 It is exported by the ":filter_chain" export tag (see the section "EXPORTS"
430 below).
432 =over 4
434 =item B<fc_register> (I<type>, I<name>, I<proc>)
436 Registers filter chain callbacks with collectd.
438 I<type> may be any of:
440 =over 4
442 =item FC_MATCH
444 =item FC_TARGET
446 =back
448 I<name> is the name of the match or target. By this name, the callbacks are
449 identified in the configuration file when specifying a B<Match> or B<Target>
450 block (see L<collectd.conf(5)> for details).
452 I<proc> is a hash reference. The hash includes up to three callbacks: an
453 optional constructor (B<create>) and destructor (B<destroy>) and a mandatory
454 B<match> or B<invoke> callback. B<match> is called whenever processing an
455 appropriate match, while B<invoke> is called whenever processing an
456 appropriate target (see the section "FILTER CONFIGURATION" in
457 L<collectd.conf(5)> for details). Just like any other callbacks, filter chain
458 callbacks are identified by the function name rather than a function pointer
459 because Perl does not support to share references to subroutines between
460 threads. The following arguments are passed to the callbacks:
462 =over 4
464 =item create
466 The arguments passed are I<config-item> and I<user-data>. See above for the
467 layout of the config-item data-type. I<user-data> is a reference to a scalar
468 value that may be used to store any information specific to this particular
469 instance. The daemon does not care about this information at all. It's for the
470 plugin's use only.
472 =item destroy
474 The only argument passed is I<user-data> which is a reference to the user data
475 initialized in the B<create> callback. This callback may be used to cleanup
476 instance-specific information and settings.
478 =item match, invoke
480 The arguments passed are I<data-set>, I<value-list>, I<meta> and I<user-data>.
481 See above for the layout of the data-set and value-list data-types. I<meta> is
482 a pointer to an array of meta information, just like the B<meta> member of the
483 notification data-type (see above). I<user-data> is a reference to the user
484 data initialized in the B<create> callback.
486 =back
488 =back
490 =head1 GLOBAL VARIABLES
492 =over 4
494 =item B<$hostname_g>
496 As the name suggests this variable keeps the hostname of the system collectd
497 is running on. The value might be influenced by the B<Hostname> or
498 B<FQDNLookup> configuration options (see L<collectd.conf(5)> for details).
500 =item B<$interval_g>
502 This variable keeps the interval in seconds in which the read functions are
503 queried (see the B<Interval> configuration option).
505 =back
507 Any changes to these variables will be globally visible in collectd.
509 =head1 EXPORTS
511 By default no symbols are exported. However, the following export tags are
512 available (B<:all> will export all of them):
514 =over 4
516 =item B<:plugin>
518 =over 4
520 =item B<plugin_register> ()
522 =item B<plugin_unregister> ()
524 =item B<plugin_dispatch_values> ()
526 =item B<plugin_flush> ()
528 =item B<plugin_flush_one> ()
530 =item B<plugin_flush_all> ()
532 =item B<plugin_dispatch_notification> ()
534 =item B<plugin_log> ()
536 =back
538 =item B<:types>
540 =over 4
542 =item B<TYPE_CONFIG>
544 =item B<TYPE_INIT>
546 =item B<TYPE_READ>
548 =item B<TYPE_WRITE>
550 =item B<TYPE_FLUSH>
552 =item B<TYPE_SHUTDOWN>
554 =item B<TYPE_LOG>
556 =item B<TYPE_DATASET>
558 =back
560 =item B<:ds_types>
562 =over 4
564 =item B<DS_TYPE_COUNTER>
566 =item B<DS_TYPE_GAUGE>
568 =back
570 =item B<:log>
572 =over 4
574 =item B<ERROR> ()
576 =item B<WARNING> ()
578 =item B<NOTICE> ()
580 =item B<INFO> ()
582 =item B<DEBUG> ()
584 =item B<LOG_ERR>
586 =item B<LOG_WARNING>
588 =item B<LOG_NOTICE>
590 =item B<LOG_INFO>
592 =item B<LOG_DEBUG>
594 =back
596 =item B<:filter_chain>
598 =over 4
600 =item B<fc_register>
602 =item B<FC_MATCH_NO_MATCH>
604 =item B<FC_MATCH_MATCHES>
606 =item B<FC_TARGET_CONTINUE>
608 =item B<FC_TARGET_STOP>
610 =item B<FC_TARGET_RETURN>
612 =back
614 =item B<:fc_types>
616 =over 4
618 =item B<FC_MATCH>
620 =item B<FC_TARGET>
622 =back
624 =item B<:notif>
626 =over 4
628 =item B<NOTIF_FAILURE>
630 =item B<NOTIF_WARNING>
632 =item B<NOTIF_OKAY>
634 =back
636 =item B<:globals>
638 =over 4
640 =item B<$hostname_g>
642 =item B<$interval_g>
644 =back
646 =back
648 =head1 EXAMPLES
650 Any Perl plugin will start similar to:
652   package Collectd::Plugins::FooBar;
654   use strict;
655   use warnings;
657   use Collectd qw( :all );
659 A very simple read function might look like:
661   sub foobar_read
662   {
663     my $vl = { plugin => 'foobar' };
664     $vl->{'values'} = [ rand(42) ];
665     plugin_dispatch_values ('gauge', $vl);
666     return 1;
667   }
669 A very simple write function might look like:
671   sub foobar_write
672   {
673     my ($type, $ds, $vl) = @_;
674     for (my $i = 0; $i < scalar (@$ds); ++$i) {
675       print "$vl->{'plugin'} ($vl->{'type'}): $vl->{'values'}->[$i]\n";
676     }
677     return 1;
678   }
680 A very simple match callback might look like:
682   sub foobar_match
683   {
684     my ($ds, $vl, $meta, $user_data) = @_;
685     if (matches($ds, $vl)) {
686       return FC_MATCH_MATCHES;
687     } else {
688       return FC_MATCH_NO_MATCH;
689     }
690   }
692 To register those functions with collectd:
694   plugin_register (TYPE_READ, "foobar", "foobar_read");
695   plugin_register (TYPE_WRITE, "foobar", "foobar_write");
697   fc_register (FC_MATCH, "foobar", "foobar_match");
699 See the section "DATA TYPES" above for a complete documentation of the data
700 types used by the read, write and match functions.
702 =head1 NOTES
704 =over 4
706 =item
708 Please feel free to send in new plugins to collectd's mailinglist at
709 E<lt>collectdE<nbsp>atE<nbsp>verplant.orgE<gt> for review and, possibly,
710 inclusion in the main distribution. In the latter case, we will take care of
711 keeping the plugin up to date and adapting it to new versions of collectd.
713 Before submitting your plugin, please take a look at
714 L<http://collectd.org/dev-info.shtml>.
716 =back
718 =head1 CAVEATS
720 =over 4
722 =item
724 collectd is heavily multi-threaded. Each collectd thread accessing the perl
725 plugin will be mapped to a Perl interpreter thread (see L<threads(3perl)>).
726 Any such thread will be created and destroyed transparently and on-the-fly.
728 Hence, any plugin has to be thread-safe if it provides several entry points
729 from collectd (i.E<nbsp>e. if it registers more than one callback or if a
730 registered callback may be called more than once in parallel). Please note
731 that no data is shared between threads by default. You have to use the
732 B<threads::shared> module to do so.
734 =item
736 Each function name registered with collectd has to be available before the
737 first thread has been created (i.E<nbsp>e. basically at compile time). This
738 basically means that hacks (yes, I really consider this to be a hack) like
739 C<*foo = \&bar; plugin_register (TYPE_READ, "plugin", "foo");> most likely
740 will not work. This is due to the fact that the symbol table is not shared
741 across different threads.
743 =item
745 Each plugin is usually only loaded once and kept in memory for performance
746 reasons. Therefore, END blocks are only executed once when collectd shuts
747 down. You should not rely on END blocks anyway - use B<shutdown functions>
748 instead.
750 =item
752 The perl plugin exports the internal API of collectd which is considered
753 unstable and subject to change at any time. We try hard to not break backwards
754 compatibility in the Perl API during the life cycle of one major release.
755 However, this cannot be guaranteed at all times. Watch out for warnings
756 dispatched by the perl plugin after upgrades.
758 =back
760 =head1 KNOWN BUGS
762 =over 4
764 =item
766 Currently, it is not possible to flush a single Perl plugin only. You can
767 either flush all Perl plugins or none at all and you have to use C<perl> as
768 plugin name when doing so.
770 =back
772 =head1 SEE ALSO
774 L<collectd(1)>,
775 L<collectd.conf(5)>,
776 L<collectd-exec(5)>,
777 L<types.db(5)>,
778 L<perl(1)>,
779 L<threads(3perl)>,
780 L<threads::shared(3perl)>,
781 L<perldebug(1)>
783 =head1 AUTHOR
785 The C<perl plugin> has been written by Sebastian Harl
786 E<lt>shE<nbsp>atE<nbsp>tokkee.orgE<gt>.
788 This manpage has been written by Florian Forster
789 E<lt>octoE<nbsp>atE<nbsp>verplant.orgE<gt> and Sebastian Harl
790 E<lt>shE<nbsp>atE<nbsp>tokkee.orgE<gt>.
792 =cut