Code

collectd-perl(5): Replaced "BUGS" with "CAVEATS" section.
[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"
14   </Plugin>
16 =head1 DESCRIPTION
18 The C<perl plugin> embeds a Perl-interpreter into collectd and provides an
19 interface to collectd's plugin system. This makes it possible to write plugins
20 for collectd in Perl. This is a lot more efficient than executing a
21 Perl-script every time you want to read a value with the C<exec plugin> (see
22 L<collectd-exec(5)>) and provides a lot more functionality, too.
24 Please note that this is still considered to be experimental and subject to
25 change between minor releases.
27 =head1 CONFIGURATION
29 =over 4
31 =item B<LoadPlugin> I<Plugin>
33 Loads the Perl plugin I<Plugin>. This does basically the same as B<use> would
34 do in a Perl program. As a side effect, the first occurrence of this option
35 causes the Perl-interpreter to be initialized.
37 =item B<BaseName> I<Name>
39 Prepends I<Name>B<::> to all plugin names loaded after this option. This is
40 provided for convenience to keep plugin names short.
42 =item B<EnableDebugger> I<Package>[=I<option>,...]
44 Run collectd under the control of the Perl source debugger. If I<Package> is
45 not the empty string, control is passed to the debugging, profiling, or
46 tracing module installed as Devel::I<Package>. A comma-separated list of
47 options may be specified after the "=" character. Please note that you may not
48 leave out the I<Package> option even if you specify B<"">. This is the same as
49 using the B<-d:Package> command line option.
51 See L<perldebug> for detailed documentation about debugging Perl.
53 This option does not prevent collectd from daemonizing, so you should start
54 collectd with the B<-f> command line option. Else you will not be able to use
55 the command line driven interface of the debugger.
57 =item B<IncludeDir> I<Dir>
59 Adds I<Dir> to the B<@INC> array. This is the same as using the B<-IDir>
60 command line option or B<use lib Dir> in the source code. Please note that it
61 only has effect on plugins loaded after this option.
63 =back
65 =head1 WRITING YOUR OWN PLUGINS
67 Writing your own plugins is quite simply. collectd manages plugins by means of
68 B<dispatch functions> which call the appropriate B<callback functions>
69 registered by the plugins. Any plugin basically consists of the implementation
70 of these callback functions and initializing code which registers the
71 functions with collectd. See the section "EXAMPLES" below for a really basic
72 example. The following types of B<callback functions> are known to collectd
73 (all of these are optional):
75 =over 4
77 =item init functions
79 This type of functions is called once after loading the module and before any
80 calls to the read and write functions. It should be used to initialize the
81 internal state of the plugin (e.E<nbsp>g. open sockets, ...). If the return
82 value evaluates to B<false>, the plugin will be disabled.
84 =item read functions
86 This type of function is used to collect the actual data. It is called once
87 per interval (see the B<Interval> configuration option of collectd). Usually
88 it will call B<plugin_dispatch_values> to dispatch the values to collectd
89 which will pass them on to all registered B<write functions>. If the return
90 value evaluates to B<false> the plugin will be skipped for an increasing
91 amount of time until it returns B<true> again.
93 =item write functions
95 This type of function is used to write the dispatched values. It is called
96 once for each call to B<plugin_dispatch_values>.
98 =item log functions
100 This type of function is used to pass messages of plugins or the daemon itself
101 to the user.
103 =item shutdown functions
105 This type of function is called once before the daemon shuts down. It should
106 be used to clean up the plugin (e.g. close sockets, ...).
108 =back
110 Any function may set the B<$@> variable to describe errors in more detail. The
111 message will be passed on to the user using collectd's logging mechanism.
113 See the documentation of the B<plugin_register> method in the section
114 "METHODS" below for the number and types of arguments passed to each
115 B<callback function>. This section also explains how to register B<callback
116 functions> with collectd.
118 To enable a plugin, copy it to a place where Perl can find it (i.E<nbsp>e. a
119 directory listed in the B<@INC> array) just as any other Perl plugin and add
120 an appropriate B<LoadPlugin> option to the configuration file. After
121 restarting collectd you're done.
123 =head1 DATA TYPES
125 The following complex types are used to pass values between the Perl plugin
126 and collectd:
128 =over 4
130 =item Data-Set
132 A data-set is a list of one or more data-sources. Each data-source defines a
133 name, type, min- and max-value and the data-set wraps them up into one
134 structure. The general layout looks like this:
136   [{
137     name => 'data_source_name',
138     type => DS_TYPE_COUNTER || DS_TYPE_GAUGE
139     min  => value || undef,
140     max  => value || undef
141   }, ...]
143 =item Value-List
145 A value-list is one structure which features an array of values and fields to
146 identify the values, i.E<nbsp>e. time and host, plugin name and
147 plugin-instance as well as a type and type-instance. Since the "type" is not
148 included in the value-list but is passed as an extra argument, the general
149 layout looks like this:
151   {
152     values => [123, 0.5],
153     time   => time (),
154     host   => 'localhost',
155     plugin => 'myplugin',
156     plugin_instance => '',
157     type_instance   => ''
158   }
160 =back
162 =head1 METHODS
164 The following functions provide the C-interface to Perl-modules. They are
165 exported by the ":plugin" export tag (see the section "EXPORTS" below).
167 =over 4
169 =item B<plugin_register> (I<type>, I<name>, I<data>)
171 Registers a callback-function or data-set.
173 I<type> can be one of:
175 =over 4
177 =item TYPE_INIT
179 =item TYPE_READ
181 =item TYPE_WRITE
183 =item TYPE_LOG
185 =item TYPE_SHUTDOWN
187 =item TYPE_DATASET
189 =back
191 I<name> is the name of the callback-function or the type of the data-set,
192 depending on the value of I<type>. (Please note that the type of the data-set
193 is the value passed as I<name> here and has nothing to do with the I<type>
194 argument which simply tells B<plugin_register> what is being registered.)
196 The last argument, I<data>, is either a function name or an array-reference.
197 If I<type> is B<TYPE_DATASET>, then the I<data> argument must be an
198 array-reference which points to an array of hashes. Each hash describes one
199 data-source. For the exact layout see B<Data-Set> above. Please note that
200 there is a large number of predefined data-sets available in the B<types.db>
201 file which are automatically registered with collectd.
203 If the I<type> argument is any of the other types (B<TYPE_INIT>, B<TYPE_READ>,
204 ...) then I<data> is expected to be a function name. If the name is not
205 prefixed with the plugin's package name collectd will add it automatically.
206 The interface slightly differs from the C interface (which expects a function
207 pointer instead) because Perl does not support to share references to
208 subroutines between threads.
210 These functions are called in the various stages of the daemon (see the
211 section "WRITING YOUR OWN PLUGINS" above) and are passed the following
212 arguments:
214 =over 4
216 =item TYPE_INIT
218 =item TYPE_READ
220 =item TYPE_SHUTDOWN
222 No arguments are passed
224 =item TYPE_WRITE
226 The arguments passed are I<type>, I<data-set>, and I<value-list>. I<type> is a
227 string. For the layout of I<data-set> and I<value-list> see above.
229 =item TYPE_LOG
231 The arguments are I<log-level> and I<message>. The log level is small for
232 important messages and high for less important messages. The least important
233 level is B<LOG_DEBUG>, the most important level is B<LOG_ERR>. In between there
234 are (from least to most important): B<LOG_INFO>, B<LOG_NOTICE>, and
235 B<LOG_WARNING>. I<message> is simply a string B<without> a newline at the end.
237 =back
239 =item B<plugin_unregister> (I<type>, I<plugin>)
241 Removes a callback or data-set from collectd's internal list of
242 functionsE<nbsp>/ datasets.
244 =item B<plugin_dispatch_values> (I<type>, I<value-list>)
246 Submits a I<value-list> of type I<type> to the daemon. If the data-set I<type>
247 is found (and the number of values matches the number of data-sources) then the
248 type, data-set and value-list is passed to all write-callbacks that are
249 registered with the daemon.
251 =item B<plugin_log> (I<log-level>, I<message>)
253 Submits a I<message> of level I<log-level> to collectd's logging mechanism.
254 The message is passed to all log-callbacks that are registered with collectd.
256 =item B<ERROR>, B<WARNING>, B<NOTICE>, B<INFO>, B<DEBUG> (I<message>)
258 Wrappers around B<plugin_log>, using B<LOG_ERR>, B<LOG_WARNING>,
259 B<LOG_NOTICE>, B<LOG_INFO> and B<LOG_DEBUG> respectively as I<log-level>.
261 =back
263 =head1 GLOBAL VARIABLES
265 =over 4
267 =item B<$hostname_g>
269 As the name suggests this variable keeps the hostname of the system collectd
270 is running on. The value might be influenced by the B<Hostname> or
271 B<FQDNLookup> configuration options (see L<collectd.conf(5)> for details).
273 =item B<$interval_g>
275 This variable keeps the interval in seconds in which the read functions are
276 queried (see the B<Interval> configuration option).
278 =back
280 Any changes to these variables will be globally visible in collectd.
282 =head1 EXPORTS
284 By default no symbols are exported. However, the following export tags are
285 available (B<:all> will export all of them):
287 =over 4
289 =item B<:plugin>
291 =over 4
293 =item B<plugin_register> ()
295 =item B<plugin_unregister> ()
297 =item B<plugin_dispatch_values> ()
299 =item B<plugin_log> ()
301 =back
303 =item B<:types>
305 =over 4
307 =item B<TYPE_INIT>
309 =item B<TYPE_READ>
311 =item B<TYPE_WRITE>
313 =item B<TYPE_SHUTDOWN>
315 =item B<TYPE_LOG>
317 =back
319 =item B<:ds_types>
321 =over 4
323 =item B<DS_TYPE_COUNTER>
325 =item B<DS_TYPE_GAUGE>
327 =back
329 =item B<:log>
331 =over 4
333 =item B<ERROR> ()
335 =item B<WARNING> ()
337 =item B<NOTICE> ()
339 =item B<INFO> ()
341 =item B<DEBUG> ()
343 =item B<LOG_ERR>
345 =item B<LOG_WARNING>
347 =item B<LOG_NOTICE>
349 =item B<LOG_INFO>
351 =item B<LOG_DEBUG>
353 =back
355 =item B<:globals>
357 =over 4
359 =item B<$hostname_g>
361 =item B<$interval_g>
363 =back
365 =back
367 =head1 EXAMPLES
369 Any Perl plugin will start similar to:
371   package Collectd::Plugins::FooBar;
373   use strict;
374   use warnings;
376   use Collectd qw( :all );
378 A very simple read function will look like:
380   sub foobar_read
381   {
382     my $vl = { plugin => 'foobar' };
383     $vl->{'values'} = [ rand(42) ];
384     plugin_dispatch_values ('gauge', $vl);
385     return 1;
386   }
388 A very simple write function will look like:
390   sub foobar_write
391   {
392     my ($type, $ds, $vl) = @_;
393     for (my $i = 0; $i < scalar (@$ds); ++$i) {
394       print "$vl->{'plugin'} ($vl->{'type'}): $vl->{'values'}->[$i]\n";
395     }
396     return 1;
397   }
399 To register those functions with collectd:
401   plugin_register (TYPE_READ, "foobar", "foobar_read");
402   plugin_register (TYPE_WRITE, "foobar", "foobar_write");
404 See the section "DATA TYPES" above for a complete documentation of the data
405 types used by the read and write functions.
407 =head1 CAVEATS
409 =over 4
411 =item
413 collectd is heavily multi-threaded. Each collectd thread accessing the perl
414 plugin will be mapped to a Perl interpreter thread (see L<threads(3perl)>).
415 Any such thread will be created and destroyed transparently and on-the-fly.
417 Hence, any plugin has to be thread-safe if it provides several entry points
418 from collectd (i.E<nbsp>e. if it registers more than one callback). Please
419 note that no data is shared between threads by default. You have to use the
420 B<threads::shared> module to do so.
422 =item
424 Each function name registered with collectd has to be available before the
425 first thread has been created (i.E<nbsp>e. basically at compile time). This
426 basically means that hacks (yes, I really consider this to be a hack) like
427 C<*foo = \&bar; plugin_register (TYPE_READ, "plugin", "foo");> most likely
428 will not work. This is due to the fact that the symbol table is not shared
429 across different threads.
431 =item
433 Each plugin is usually only loaded once and kept in memory for performance
434 reasons. Therefore, END blocks are only executed once when collectd shuts
435 down. You should not rely on END blocks anyway - use B<shutdown functions>
436 instead.
438 =back
440 =head1 SEE ALSO
442 L<collectd(1)>,
443 L<collectd.conf(5)>,
444 L<collectd-exec(5)>,
445 L<perl(1)>,
446 L<threads(3perl)>,
447 L<threads::shared(3perl)>,
448 L<perldebug(1)>
450 =head1 AUTHOR
452 The C<perl plugin> has been written by Sebastian Harl
453 E<lt>shE<nbsp>atE<nbsp>tokkee.orgE<gt>.
455 This manpage has been written by Florian Forster
456 E<lt>octoE<nbsp>atE<nbsp>verplant.orgE<gt> and Sebastian Harl
457 E<lt>shE<nbsp>atE<nbsp>tokkee.orgE<gt>.
459 =cut