5 collectd-perl - Documentation of collectd's C<perl plugin>
12 IncludeDir "/path/to/perl/plugins"
13 BaseName "Collectd::Plugins"
24 The C<perl plugin> embeds a Perl-interpreter into collectd and provides an
25 interface to collectd's plugin system. This makes it possible to write plugins
26 for collectd in Perl. This is a lot more efficient than executing a
27 Perl-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.
34 =item B<LoadPlugin> I<Plugin>
36 Loads the Perl plugin I<Plugin>. This does basically the same as B<use> would
37 do in a Perl program. As a side effect, the first occurrence of this option
38 causes the Perl-interpreter to be initialized.
40 =item B<BaseName> I<Name>
42 Prepends I<Name>B<::> to all plugin names loaded after this option. This is
43 provided for convenience to keep plugin names short. All Perl-based plugins
44 provided with the I<collectd> distributions reside in the C<Collectd::Plugins>
47 =item E<lt>B<Plugin> I<Name>E<gt> block
49 This block may be used to pass on configuration settings to a Perl plugin. The
50 configuration is converted into a config-item data type which is passed to the
51 registered configuration callback. See below for details about the config-item
52 data type and how to register callbacks.
54 The I<name> identifies the callback. It is used literally and independent of
55 the B<BaseName> setting.
57 =item B<EnableDebugger> I<Package>[=I<option>,...]
59 Run collectd under the control of the Perl source debugger. If I<Package> is
60 not the empty string, control is passed to the debugging, profiling, or
61 tracing module installed as Devel::I<Package>. A comma-separated list of
62 options may be specified after the "=" character. Please note that you may not
63 leave out the I<Package> option even if you specify B<"">. This is the same as
64 using the B<-d:Package> command line option.
66 See L<perldebug> for detailed documentation about debugging Perl.
68 This option does not prevent collectd from daemonizing, so you should start
69 collectd with the B<-f> command line option. Else you will not be able to use
70 the command line driven interface of the debugger.
72 =item B<IncludeDir> I<Dir>
74 Adds I<Dir> to the B<@INC> array. This is the same as using the B<-IDir>
75 command line option or B<use lib Dir> in the source code. Please note that it
76 only has effect on plugins loaded after this option.
80 =head1 WRITING YOUR OWN PLUGINS
82 Writing your own plugins is quite simple. collectd manages plugins by means of
83 B<dispatch functions> which call the appropriate B<callback functions>
84 registered by the plugins. Any plugin basically consists of the implementation
85 of these callback functions and initializing code which registers the
86 functions with collectd. See the section "EXAMPLES" below for a really basic
87 example. The following types of B<callback functions> are known to collectd
88 (all of them are optional):
92 =item configuration functions
94 This type of functions is called during configuration if an appropriate
95 B<Plugin> block has been encountered. It is called once for each B<Plugin>
96 block which matches the name of the callback as provided with the
97 B<plugin_register> method - see below.
101 This type of functions is called once after loading the module and before any
102 calls to the read and write functions. It should be used to initialize the
103 internal state of the plugin (e.E<nbsp>g. open sockets, ...). If the return
104 value evaluates to B<false>, the plugin will be disabled.
108 This type of function is used to collect the actual data. It is called once
109 per interval (see the B<Interval> configuration option of collectd). Usually
110 it will call B<plugin_dispatch_values> to dispatch the values to collectd
111 which will pass them on to all registered B<write functions>. If the return
112 value evaluates to B<false> the plugin will be skipped for an increasing
113 amount of time until it returns B<true> again.
115 =item write functions
117 This type of function is used to write the dispatched values. It is called
118 once for each call to B<plugin_dispatch_values>.
120 =item flush functions
122 This type of function is used to flush internal caches of plugins. It is
123 usually triggered by the user only. Any plugin which caches data before
124 writing it to disk should provide this kind of callback function.
128 This type of function is used to pass messages of plugins or the daemon itself
131 =item notification function
133 This type of function is used to act upon notifications. In general, a
134 notification is a status message that may be associated with a data instance.
135 Usually, a notification is generated by the daemon if a configured threshold
136 has been exceeded (see the section "THRESHOLD CONFIGURATION" in
137 L<collectd.conf(5)> for more details), but any plugin may dispatch
138 notifications as well.
140 =item shutdown functions
142 This type of function is called once before the daemon shuts down. It should
143 be used to clean up the plugin (e.g. close sockets, ...).
147 Any function (except log functions) may set the B<$@> variable to describe
148 errors in more detail. The message will be passed on to the user using
149 collectd's logging mechanism.
151 See the documentation of the B<plugin_register> method in the section
152 "METHODS" below for the number and types of arguments passed to each
153 B<callback function>. This section also explains how to register B<callback
154 functions> with collectd.
156 To enable a plugin, copy it to a place where Perl can find it (i.E<nbsp>e. a
157 directory listed in the B<@INC> array) just as any other Perl plugin and add
158 an appropriate B<LoadPlugin> option to the configuration file. After
159 restarting collectd you're done.
163 The following complex types are used to pass values between the Perl plugin
170 A config-item is one structure which keeps the information provided in the
171 configuration file. The array of children keeps one entry for each
172 configuration option. Each such entry is another config-item structure, which
173 may nest further if nested blocks are used.
177 values => [ val1, val2, ... ],
178 children => [ { ... }, { ... }, ... ]
183 A data-set is a list of one or more data-sources. Each data-source defines a
184 name, type, min- and max-value and the data-set wraps them up into one
185 structure. The general layout looks like this:
188 name => 'data_source_name',
189 type => DS_TYPE_COUNTER || DS_TYPE_GAUGE || DS_TYPE_DERIVE || DS_TYPE_ABSOLUTE,
190 min => value || undef,
191 max => value || undef
196 A value-list is one structure which features an array of values and fields to
197 identify the values, i.E<nbsp>e. time and host, plugin name and
198 plugin-instance as well as a type and type-instance. Since the "type" is not
199 included in the value-list but is passed as an extra argument, the general
200 layout looks like this:
203 values => [123, 0.5],
205 interval => plugin_get_interval (),
207 plugin => 'myplugin',
209 plugin_instance => '',
215 A notification is one structure defining the severity, time and message of the
216 status message as well as an identification of a data instance. Also, it
217 includes an optional list of user-defined meta information represented as
221 severity => NOTIF_FAILURE || NOTIF_WARNING || NOTIF_OKAY,
223 message => 'status message',
225 plugin => 'myplugin',
227 plugin_instance => '',
229 meta => [ { name => <name>, value => <value> }, ... ]
234 A match-proc is one structure storing the callbacks of a "match" of the filter
235 chain infrastructure. The general layout looks like this:
238 create => 'my_create',
239 destroy => 'my_destroy',
245 A target-proc is one structure storing the callbacks of a "target" of the
246 filter chain infrastructure. The general layout looks like this:
249 create => 'my_create',
250 destroy => 'my_destroy',
251 invoke => 'my_invoke'
258 The following functions provide the C-interface to Perl-modules. They are
259 exported by the ":plugin" export tag (see the section "EXPORTS" below).
263 =item B<plugin_register> (I<type>, I<name>, I<data>)
265 Registers a callback-function or data-set.
267 I<type> can be one of:
291 I<name> is the name of the callback-function or the type of the data-set,
292 depending on the value of I<type>. (Please note that the type of the data-set
293 is the value passed as I<name> here and has nothing to do with the I<type>
294 argument which simply tells B<plugin_register> what is being registered.)
296 The last argument, I<data>, is either a function name or an array-reference.
297 If I<type> is B<TYPE_DATASET>, then the I<data> argument must be an
298 array-reference which points to an array of hashes. Each hash describes one
299 data-set. For the exact layout see B<Data-Set> above. Please note that
300 there is a large number of predefined data-sets available in the B<types.db>
301 file which are automatically registered with collectd - see L<types.db(5)> for
302 a description of the format of this file.
304 B<Note>: Using B<plugin_register> to register a data-set is deprecated. Add
305 the new type to a custom L<types.db(5)> file instead. This functionality might
306 be removed in a future version of collectd.
308 If the I<type> argument is any of the other types (B<TYPE_INIT>, B<TYPE_READ>,
309 ...) then I<data> is expected to be a function name. If the name is not
310 prefixed with the plugin's package name collectd will add it automatically.
311 The interface slightly differs from the C interface (which expects a function
312 pointer instead) because Perl does not support to share references to
313 subroutines between threads.
315 These functions are called in the various stages of the daemon (see the
316 section "WRITING YOUR OWN PLUGINS" above) and are passed the following
323 The only argument passed is I<config-item>. See above for the layout of this
332 No arguments are passed.
336 The arguments passed are I<type>, I<data-set>, and I<value-list>. I<type> is a
337 string. For the layout of I<data-set> and I<value-list> see above.
341 The arguments passed are I<timeout> and I<identifier>. I<timeout> indicates
342 that only data older than I<timeout> seconds is to be flushed. I<identifier>
343 specifies which values are to be flushed.
347 The arguments are I<log-level> and I<message>. The log level is small for
348 important messages and high for less important messages. The least important
349 level is B<LOG_DEBUG>, the most important level is B<LOG_ERR>. In between there
350 are (from least to most important): B<LOG_INFO>, B<LOG_NOTICE>, and
351 B<LOG_WARNING>. I<message> is simply a string B<without> a newline at the end.
355 The only argument passed is I<notification>. See above for the layout of this
360 =item B<plugin_unregister> (I<type>, I<plugin>)
362 Removes a callback or data-set from collectd's internal list of
363 functionsE<nbsp>/ datasets.
365 =item B<plugin_dispatch_values> (I<value-list>)
367 Submits a I<value-list> to the daemon. If the data-set identified by
368 I<value-list>->{I<type>}
369 is found (and the number of values matches the number of data-sources) then the
370 type, data-set and value-list is passed to all write-callbacks that are
371 registered with the daemon.
373 =item B<plugin_write> ([B<plugins> => I<...>][, B<datasets> => I<...>],
374 B<valuelists> => I<...>)
376 Calls the write function of the given I<plugins> with the provided I<data
377 sets> and I<value lists>. In contrast to B<plugin_dispatch_values>, it does
378 not update collectd's internal cache and bypasses the filter mechanism (see
379 L<collectd.conf(5)> for details). If the B<plugins> argument has been omitted,
380 the values will be dispatched to all registered write plugins. If the
381 B<datasets> argument has been omitted, the required data sets are looked up
382 according to the C<type> member in the appropriate value list. The value of
383 all three arguments may either be a single scalar or a reference to an array.
384 If the B<datasets> argument has been specified, the number of data sets has to
385 equal the number of specified value lists.
387 =item B<plugin_flush> ([B<timeout> => I<timeout>][, B<plugins> => I<...>][,
388 B<identifiers> => I<...>])
390 Flush one or more plugins. I<timeout> and the specified I<identifiers> are
391 passed on to the registered flush-callbacks. If omitted, the timeout defaults
392 to C<-1>. The identifier defaults to the undefined value. If the B<plugins>
393 argument has been specified, only named plugins will be flushed. The value of
394 the B<plugins> and B<identifiers> arguments may either be a string or a
395 reference to an array of strings.
397 =item B<plugin_dispatch_notification> (I<notification>)
399 Submits a I<notification> to the daemon which will then pass it to all
400 notification-callbacks that are registered.
402 =item B<plugin_log> (I<log-level>, I<message>)
404 Submits a I<message> of level I<log-level> to collectd's logging mechanism.
405 The message is passed to all log-callbacks that are registered with collectd.
407 =item B<ERROR>, B<WARNING>, B<NOTICE>, B<INFO>, B<DEBUG> (I<message>)
409 Wrappers around B<plugin_log>, using B<LOG_ERR>, B<LOG_WARNING>,
410 B<LOG_NOTICE>, B<LOG_INFO> and B<LOG_DEBUG> respectively as I<log-level>.
412 =item B<plugin_get_interval> ()
414 Returns the interval of the current plugin as a floating point number in
415 seconds. This value depends on the interval configured within the
416 C<LoadPlugin perl> block or the global interval (see L<collectd.conf(5)> for
421 The following function provides the filter chain C-interface to Perl-modules.
422 It is exported by the ":filter_chain" export tag (see the section "EXPORTS"
427 =item B<fc_register> (I<type>, I<name>, I<proc>)
429 Registers filter chain callbacks with collectd.
431 I<type> may be any of:
441 I<name> is the name of the match or target. By this name, the callbacks are
442 identified in the configuration file when specifying a B<Match> or B<Target>
443 block (see L<collectd.conf(5)> for details).
445 I<proc> is a hash reference. The hash includes up to three callbacks: an
446 optional constructor (B<create>) and destructor (B<destroy>) and a mandatory
447 B<match> or B<invoke> callback. B<match> is called whenever processing an
448 appropriate match, while B<invoke> is called whenever processing an
449 appropriate target (see the section "FILTER CONFIGURATION" in
450 L<collectd.conf(5)> for details). Just like any other callbacks, filter chain
451 callbacks are identified by the function name rather than a function pointer
452 because Perl does not support to share references to subroutines between
453 threads. The following arguments are passed to the callbacks:
459 The arguments passed are I<config-item> and I<user-data>. See above for the
460 layout of the config-item data-type. I<user-data> is a reference to a scalar
461 value that may be used to store any information specific to this particular
462 instance. The daemon does not care about this information at all. It's for the
467 The only argument passed is I<user-data> which is a reference to the user data
468 initialized in the B<create> callback. This callback may be used to cleanup
469 instance-specific information and settings.
473 The arguments passed are I<data-set>, I<value-list>, I<meta> and I<user-data>.
474 See above for the layout of the data-set and value-list data-types. I<meta> is
475 a pointer to an array of meta information, just like the B<meta> member of the
476 notification data-type (see above). I<user-data> is a reference to the user
477 data initialized in the B<create> callback.
483 =head1 GLOBAL VARIABLES
489 As the name suggests this variable keeps the hostname of the system collectd
490 is running on. The value might be influenced by the B<Hostname> or
491 B<FQDNLookup> configuration options (see L<collectd.conf(5)> for details).
495 This variable keeps the interval in seconds in which the read functions are
496 queried (see the B<Interval> configuration option).
498 B<Note:> This variable should no longer be used in favor of
499 C<plugin_get_interval()> (see above). This function takes any plugin-specific
500 interval settings into account (see the C<Interval> option of C<LoadPlugin> in
501 L<collectd.conf(5)> for details).
505 Any changes to these variables will be globally visible in collectd.
509 By default no symbols are exported. However, the following export tags are
510 available (B<:all> will export all of them):
518 =item B<plugin_register> ()
520 =item B<plugin_unregister> ()
522 =item B<plugin_dispatch_values> ()
524 =item B<plugin_flush> ()
526 =item B<plugin_flush_one> ()
528 =item B<plugin_flush_all> ()
530 =item B<plugin_dispatch_notification> ()
532 =item B<plugin_log> ()
550 =item B<TYPE_SHUTDOWN>
554 =item B<TYPE_DATASET>
562 =item B<DS_TYPE_COUNTER>
564 =item B<DS_TYPE_GAUGE>
566 =item B<DS_TYPE_DERIVE>
568 =item B<DS_TYPE_ABSOLUTE>
598 =item B<:filter_chain>
604 =item B<FC_MATCH_NO_MATCH>
606 =item B<FC_MATCH_MATCHES>
608 =item B<FC_TARGET_CONTINUE>
610 =item B<FC_TARGET_STOP>
612 =item B<FC_TARGET_RETURN>
630 =item B<NOTIF_FAILURE>
632 =item B<NOTIF_WARNING>
652 Any Perl plugin will start similar to:
654 package Collectd::Plugins::FooBar;
659 use Collectd qw( :all );
661 A very simple read function might look like:
665 my $vl = { plugin => 'foobar', type => 'gauge' };
666 $vl->{'values'} = [ rand(42) ];
667 plugin_dispatch_values ($vl);
671 A very simple write function might look like:
675 my ($type, $ds, $vl) = @_;
676 for (my $i = 0; $i < scalar (@$ds); ++$i) {
677 print "$vl->{'plugin'} ($vl->{'type'}): $vl->{'values'}->[$i]\n";
682 A very simple match callback might look like:
686 my ($ds, $vl, $meta, $user_data) = @_;
687 if (matches($ds, $vl)) {
688 return FC_MATCH_MATCHES;
690 return FC_MATCH_NO_MATCH;
694 To register those functions with collectd:
696 plugin_register (TYPE_READ, "foobar", "foobar_read");
697 plugin_register (TYPE_WRITE, "foobar", "foobar_write");
699 fc_register (FC_MATCH, "foobar", "foobar_match");
701 See the section "DATA TYPES" above for a complete documentation of the data
702 types used by the read, write and match functions.
710 Please feel free to send in new plugins to collectd's mailing list at
711 E<lt>collectdE<nbsp>atE<nbsp>collectd.orgE<gt> for review and, possibly,
712 inclusion in the main distribution. In the latter case, we will take care of
713 keeping the plugin up to date and adapting it to new versions of collectd.
715 Before submitting your plugin, please take a look at
716 L<http://collectd.org/dev-info.shtml>.
726 collectd is heavily multi-threaded. Each collectd thread accessing the perl
727 plugin will be mapped to a Perl interpreter thread (see L<threads(3perl)>).
728 Any such thread will be created and destroyed transparently and on-the-fly.
730 Hence, any plugin has to be thread-safe if it provides several entry points
731 from collectd (i.E<nbsp>e. if it registers more than one callback or if a
732 registered callback may be called more than once in parallel). Please note
733 that no data is shared between threads by default. You have to use the
734 B<threads::shared> module to do so.
738 Each function name registered with collectd has to be available before the
739 first thread has been created (i.E<nbsp>e. basically at compile time). This
740 basically means that hacks (yes, I really consider this to be a hack) like
741 C<*foo = \&bar; plugin_register (TYPE_READ, "plugin", "foo");> most likely
742 will not work. This is due to the fact that the symbol table is not shared
743 across different threads.
747 Each plugin is usually only loaded once and kept in memory for performance
748 reasons. Therefore, END blocks are only executed once when collectd shuts
749 down. You should not rely on END blocks anyway - use B<shutdown functions>
754 The perl plugin exports the internal API of collectd which is considered
755 unstable and subject to change at any time. We try hard to not break backwards
756 compatibility in the Perl API during the life cycle of one major release.
757 However, this cannot be guaranteed at all times. Watch out for warnings
758 dispatched by the perl plugin after upgrades.
768 Currently, it is not possible to flush a single Perl plugin only. You can
769 either flush all Perl plugins or none at all and you have to use C<perl> as
770 plugin name when doing so.
782 L<threads::shared(3perl)>,
787 The C<perl plugin> has been written by Sebastian Harl
788 E<lt>shE<nbsp>atE<nbsp>tokkee.orgE<gt>.
790 This manpage has been written by Florian Forster
791 E<lt>octoE<nbsp>atE<nbsp>collectd.orgE<gt> and Sebastian Harl
792 E<lt>shE<nbsp>atE<nbsp>tokkee.orgE<gt>.