Merge branch 'collectd-4.3' into collectd-4.4
[collectd.git] / src / collectd-perl.pod
1 =head1 NAME
2
3 collectd-perl - Documentation of collectd's C<perl plugin>
4
5 =head1 SYNOPSIS
6
7   LoadPlugin perl
8   # ...
9   <Plugin perl>
10     IncludeDir "/path/to/perl/plugins"
11     BaseName "Collectd::Plugin"
12     EnableDebugger ""
13     LoadPlugin "FooBar"
14   </Plugin>
15
16 =head1 DESCRIPTION
17
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.
23
24 =head1 CONFIGURATION
25
26 =over 4
27
28 =item B<LoadPlugin> I<Plugin>
29
30 Loads the Perl plugin I<Plugin>. This does basically the same as B<use> would
31 do in a Perl program. As a side effect, the first occurrence of this option
32 causes the Perl-interpreter to be initialized.
33
34 =item B<BaseName> I<Name>
35
36 Prepends I<Name>B<::> to all plugin names loaded after this option. This is
37 provided for convenience to keep plugin names short.
38
39 =item B<EnableDebugger> I<Package>[=I<option>,...]
40
41 Run collectd under the control of the Perl source debugger. If I<Package> is
42 not the empty string, control is passed to the debugging, profiling, or
43 tracing module installed as Devel::I<Package>. A comma-separated list of
44 options may be specified after the "=" character. Please note that you may not
45 leave out the I<Package> option even if you specify B<"">. This is the same as
46 using the B<-d:Package> command line option.
47
48 See L<perldebug> for detailed documentation about debugging Perl.
49
50 This option does not prevent collectd from daemonizing, so you should start
51 collectd with the B<-f> command line option. Else you will not be able to use
52 the command line driven interface of the debugger.
53
54 =item B<IncludeDir> I<Dir>
55
56 Adds I<Dir> to the B<@INC> array. This is the same as using the B<-IDir>
57 command line option or B<use lib Dir> in the source code. Please note that it
58 only has effect on plugins loaded after this option.
59
60 =back
61
62 =head1 WRITING YOUR OWN PLUGINS
63
64 Writing your own plugins is quite simple. collectd manages plugins by means of
65 B<dispatch functions> which call the appropriate B<callback functions>
66 registered by the plugins. Any plugin basically consists of the implementation
67 of these callback functions and initializing code which registers the
68 functions with collectd. See the section "EXAMPLES" below for a really basic
69 example. The following types of B<callback functions> are known to collectd
70 (all of them are optional):
71
72 =over 4
73
74 =item init functions
75
76 This type of functions is called once after loading the module and before any
77 calls to the read and write functions. It should be used to initialize the
78 internal state of the plugin (e.E<nbsp>g. open sockets, ...). If the return
79 value evaluates to B<false>, the plugin will be disabled.
80
81 =item read functions
82
83 This type of function is used to collect the actual data. It is called once
84 per interval (see the B<Interval> configuration option of collectd). Usually
85 it will call B<plugin_dispatch_values> to dispatch the values to collectd
86 which will pass them on to all registered B<write functions>. If the return
87 value evaluates to B<false> the plugin will be skipped for an increasing
88 amount of time until it returns B<true> again.
89
90 =item write functions
91
92 This type of function is used to write the dispatched values. It is called
93 once for each call to B<plugin_dispatch_values>.
94
95 =item flush functions
96
97 This type of function is used to flush internal caches of plugins. It is
98 usually triggered by the user only. Any plugin which caches data before
99 writing it to disk should provide this kind of callback function.
100
101 =item log functions
102
103 This type of function is used to pass messages of plugins or the daemon itself
104 to the user.
105
106 =item notification function
107
108 This type of function is used to act upon notifications. In general, a
109 notification is a status message that may be associated with a data instance.
110 Usually, a notification is generated by the daemon if a configured threshold
111 has been exceeded (see the section "THRESHOLD CONFIGURATION" in
112 L<collectd.conf(5)> for more details), but any plugin may dispatch
113 notifications as well.
114
115 =item shutdown functions
116
117 This type of function is called once before the daemon shuts down. It should
118 be used to clean up the plugin (e.g. close sockets, ...).
119
120 =back
121
122 Any function (except log functions) may set the B<$@> variable to describe
123 errors in more detail. The message will be passed on to the user using
124 collectd's logging mechanism.
125
126 See the documentation of the B<plugin_register> method in the section
127 "METHODS" below for the number and types of arguments passed to each
128 B<callback function>. This section also explains how to register B<callback
129 functions> with collectd.
130
131 To enable a plugin, copy it to a place where Perl can find it (i.E<nbsp>e. a
132 directory listed in the B<@INC> array) just as any other Perl plugin and add
133 an appropriate B<LoadPlugin> option to the configuration file. After
134 restarting collectd you're done.
135
136 =head1 DATA TYPES
137
138 The following complex types are used to pass values between the Perl plugin
139 and collectd:
140
141 =over 4
142
143 =item Data-Set
144
145 A data-set is a list of one or more data-sources. Each data-source defines a
146 name, type, min- and max-value and the data-set wraps them up into one
147 structure. The general layout looks like this:
148
149   [{
150     name => 'data_source_name',
151     type => DS_TYPE_COUNTER || DS_TYPE_GAUGE,
152     min  => value || undef,
153     max  => value || undef
154   }, ...]
155
156 =item Value-List
157
158 A value-list is one structure which features an array of values and fields to
159 identify the values, i.E<nbsp>e. time and host, plugin name and
160 plugin-instance as well as a type and type-instance. Since the "type" is not
161 included in the value-list but is passed as an extra argument, the general
162 layout looks like this:
163
164   {
165     values => [123, 0.5],
166     time   => time (),
167     host   => $hostname_g,
168     plugin => 'myplugin',
169     plugin_instance => '',
170     type_instance   => ''
171   }
172
173 =item Notification
174
175 A notification is one structure defining the severity, time and message of the
176 status message as well as an identification of a data instance:
177
178   {
179     severity => NOTIF_FAILURE || NOTIF_WARNING || NOTIF_OKAY,
180     time     => time (),
181     message  => 'status message',
182     host     => $hostname_g,
183     plugin   => 'myplugin',
184     type     => 'mytype',
185     plugin_instance => '',
186     type_instance   => ''
187   }
188
189 =back
190
191 =head1 METHODS
192
193 The following functions provide the C-interface to Perl-modules. They are
194 exported by the ":plugin" export tag (see the section "EXPORTS" below).
195
196 =over 4
197
198 =item B<plugin_register> (I<type>, I<name>, I<data>)
199
200 Registers a callback-function or data-set.
201
202 I<type> can be one of:
203
204 =over 4
205
206 =item TYPE_INIT
207
208 =item TYPE_READ
209
210 =item TYPE_WRITE
211
212 =item TYPE_FLUSH
213
214 =item TYPE_LOG
215
216 =item TYPE_NOTIF
217
218 =item TYPE_SHUTDOWN
219
220 =item TYPE_DATASET
221
222 =back
223
224 I<name> is the name of the callback-function or the type of the data-set,
225 depending on the value of I<type>. (Please note that the type of the data-set
226 is the value passed as I<name> here and has nothing to do with the I<type>
227 argument which simply tells B<plugin_register> what is being registered.)
228
229 The last argument, I<data>, is either a function name or an array-reference.
230 If I<type> is B<TYPE_DATASET>, then the I<data> argument must be an
231 array-reference which points to an array of hashes. Each hash describes one
232 data-set. For the exact layout see B<Data-Set> above. Please note that
233 there is a large number of predefined data-sets available in the B<types.db>
234 file which are automatically registered with collectd - see L<types.db(5)> for
235 a description of the format of this file.
236
237 If the I<type> argument is any of the other types (B<TYPE_INIT>, B<TYPE_READ>,
238 ...) then I<data> is expected to be a function name. If the name is not
239 prefixed with the plugin's package name collectd will add it automatically.
240 The interface slightly differs from the C interface (which expects a function
241 pointer instead) because Perl does not support to share references to
242 subroutines between threads.
243
244 These functions are called in the various stages of the daemon (see the
245 section "WRITING YOUR OWN PLUGINS" above) and are passed the following
246 arguments:
247
248 =over 4
249
250 =item TYPE_INIT
251
252 =item TYPE_READ
253
254 =item TYPE_SHUTDOWN
255
256 No arguments are passed.
257
258 =item TYPE_WRITE
259
260 The arguments passed are I<type>, I<data-set>, and I<value-list>. I<type> is a
261 string. For the layout of I<data-set> and I<value-list> see above.
262
263 =item TYPE_FLUSH
264
265 The only argument passed is I<timeout> which indicates that only data older
266 than I<timeout> seconds is to be flushed.
267
268 =item TYPE_LOG
269
270 The arguments are I<log-level> and I<message>. The log level is small for
271 important messages and high for less important messages. The least important
272 level is B<LOG_DEBUG>, the most important level is B<LOG_ERR>. In between there
273 are (from least to most important): B<LOG_INFO>, B<LOG_NOTICE>, and
274 B<LOG_WARNING>. I<message> is simply a string B<without> a newline at the end.
275
276 =item TYPE_NOTIF
277
278 The only argument passed is I<notification>. See above for the layout of this
279 data type.
280
281 =back
282
283 =item B<plugin_unregister> (I<type>, I<plugin>)
284
285 Removes a callback or data-set from collectd's internal list of
286 functionsE<nbsp>/ datasets.
287
288 =item B<plugin_dispatch_values> (I<type>, I<value-list>)
289
290 Submits a I<value-list> of type I<type> to the daemon. If the data-set I<type>
291 is found (and the number of values matches the number of data-sources) then the
292 type, data-set and value-list is passed to all write-callbacks that are
293 registered with the daemon.
294
295 =item B<plugin_flush> ([B<timeout> => I<timeout>,] [B<plugins> => I<...>])
296
297 Flush one or more plugins. I<timeout> is passed on to the registered
298 flush-callbacks. If omitted, C<-1> is used. If the I<plugins> argument has
299 been specified, only named plugins will be flushed. The argument's value may
300 either be a string or a reference to an array of strings.
301
302 =item B<plugin_flush_one> (I<timeout>, I<plugin>)
303
304 This is identical to using "plugin_flush (timeout =E<gt> I<timeout>, plugins
305 =E<gt> I<plugin>".
306
307 =item B<plugin_flush_all> (I<timeout>)
308
309 This is identical to using "plugin_flush (timeout =E<gt> I<timeout>)".
310
311 =item B<plugin_dispatch_notification> (I<notification>)
312
313 Submits a I<notification> to the daemon which will then pass it to all
314 notification-callbacks that are registered.
315
316 =item B<plugin_log> (I<log-level>, I<message>)
317
318 Submits a I<message> of level I<log-level> to collectd's logging mechanism.
319 The message is passed to all log-callbacks that are registered with collectd.
320
321 =item B<ERROR>, B<WARNING>, B<NOTICE>, B<INFO>, B<DEBUG> (I<message>)
322
323 Wrappers around B<plugin_log>, using B<LOG_ERR>, B<LOG_WARNING>,
324 B<LOG_NOTICE>, B<LOG_INFO> and B<LOG_DEBUG> respectively as I<log-level>.
325
326 =back
327
328 =head1 GLOBAL VARIABLES
329
330 =over 4
331
332 =item B<$hostname_g>
333
334 As the name suggests this variable keeps the hostname of the system collectd
335 is running on. The value might be influenced by the B<Hostname> or
336 B<FQDNLookup> configuration options (see L<collectd.conf(5)> for details).
337
338 =item B<$interval_g>
339
340 This variable keeps the interval in seconds in which the read functions are
341 queried (see the B<Interval> configuration option).
342
343 =back
344
345 Any changes to these variables will be globally visible in collectd.
346
347 =head1 EXPORTS
348
349 By default no symbols are exported. However, the following export tags are
350 available (B<:all> will export all of them):
351
352 =over 4
353
354 =item B<:plugin>
355
356 =over 4
357
358 =item B<plugin_register> ()
359
360 =item B<plugin_unregister> ()
361
362 =item B<plugin_dispatch_values> ()
363
364 =item B<plugin_flush> ()
365
366 =item B<plugin_flush_one> ()
367
368 =item B<plugin_flush_all> ()
369
370 =item B<plugin_dispatch_notification> ()
371
372 =item B<plugin_log> ()
373
374 =back
375
376 =item B<:types>
377
378 =over 4
379
380 =item B<TYPE_INIT>
381
382 =item B<TYPE_READ>
383
384 =item B<TYPE_WRITE>
385
386 =item B<TYPE_FLUSH>
387
388 =item B<TYPE_SHUTDOWN>
389
390 =item B<TYPE_LOG>
391
392 =back
393
394 =item B<:ds_types>
395
396 =over 4
397
398 =item B<DS_TYPE_COUNTER>
399
400 =item B<DS_TYPE_GAUGE>
401
402 =back
403
404 =item B<:log>
405
406 =over 4
407
408 =item B<ERROR> ()
409
410 =item B<WARNING> ()
411
412 =item B<NOTICE> ()
413
414 =item B<INFO> ()
415
416 =item B<DEBUG> ()
417
418 =item B<LOG_ERR>
419
420 =item B<LOG_WARNING>
421
422 =item B<LOG_NOTICE>
423
424 =item B<LOG_INFO>
425
426 =item B<LOG_DEBUG>
427
428 =back
429
430 =item B<:notif>
431
432 =over 4
433
434 =item B<NOTIF_FAILURE>
435
436 =item B<NOTIF_WARNING>
437
438 =item B<NOTIF_OKAY>
439
440 =back
441
442 =item B<:globals>
443
444 =over 4
445
446 =item B<$hostname_g>
447
448 =item B<$interval_g>
449
450 =back
451
452 =back
453
454 =head1 EXAMPLES
455
456 Any Perl plugin will start similar to:
457
458   package Collectd::Plugins::FooBar;
459
460   use strict;
461   use warnings;
462
463   use Collectd qw( :all );
464
465 A very simple read function will look like:
466
467   sub foobar_read
468   {
469     my $vl = { plugin => 'foobar' };
470     $vl->{'values'} = [ rand(42) ];
471     plugin_dispatch_values ('gauge', $vl);
472     return 1;
473   }
474
475 A very simple write function will look like:
476
477   sub foobar_write
478   {
479     my ($type, $ds, $vl) = @_;
480     for (my $i = 0; $i < scalar (@$ds); ++$i) {
481       print "$vl->{'plugin'} ($vl->{'type'}): $vl->{'values'}->[$i]\n";
482     }
483     return 1;
484   }
485
486 To register those functions with collectd:
487
488   plugin_register (TYPE_READ, "foobar", "foobar_read");
489   plugin_register (TYPE_WRITE, "foobar", "foobar_write");
490
491 See the section "DATA TYPES" above for a complete documentation of the data
492 types used by the read and write functions.
493
494 =head1 NOTES
495
496 =over 4
497
498 =item
499
500 Please feel free to send in new plugins to collectd's mailinglist at
501 E<lt>collectdE<nbsp>atE<nbsp>verplant.orgE<gt> for review and, possibly,
502 inclusion in the main distribution. In the latter case, we will take care of
503 keeping the plugin up to date and adapting it to new versions of collectd.
504
505 Before submitting your plugin, please take a look at
506 L<http://collectd.org/dev-info.shtml>.
507
508 =back
509
510 =head1 CAVEATS
511
512 =over 4
513
514 =item
515
516 collectd is heavily multi-threaded. Each collectd thread accessing the perl
517 plugin will be mapped to a Perl interpreter thread (see L<threads(3perl)>).
518 Any such thread will be created and destroyed transparently and on-the-fly.
519
520 Hence, any plugin has to be thread-safe if it provides several entry points
521 from collectd (i.E<nbsp>e. if it registers more than one callback or if a
522 registered callback may be called more than once in parallel). Please note
523 that no data is shared between threads by default. You have to use the
524 B<threads::shared> module to do so.
525
526 =item
527
528 Each function name registered with collectd has to be available before the
529 first thread has been created (i.E<nbsp>e. basically at compile time). This
530 basically means that hacks (yes, I really consider this to be a hack) like
531 C<*foo = \&bar; plugin_register (TYPE_READ, "plugin", "foo");> most likely
532 will not work. This is due to the fact that the symbol table is not shared
533 across different threads.
534
535 =item
536
537 Each plugin is usually only loaded once and kept in memory for performance
538 reasons. Therefore, END blocks are only executed once when collectd shuts
539 down. You should not rely on END blocks anyway - use B<shutdown functions>
540 instead.
541
542 =back
543
544 =head1 KNOWN BUGS
545
546 =over 4
547
548 =item
549
550 Currently, it is not possible to flush a single Perl plugin only. You can
551 either flush all Perl plugins or none at all and you have to use C<perl> as
552 plugin name when doing so.
553
554 =back
555
556 =head1 SEE ALSO
557
558 L<collectd(1)>,
559 L<collectd.conf(5)>,
560 L<collectd-exec(5)>,
561 L<types.db(5)>,
562 L<perl(1)>,
563 L<threads(3perl)>,
564 L<threads::shared(3perl)>,
565 L<perldebug(1)>
566
567 =head1 AUTHOR
568
569 The C<perl plugin> has been written by Sebastian Harl
570 E<lt>shE<nbsp>atE<nbsp>tokkee.orgE<gt>.
571
572 This manpage has been written by Florian Forster
573 E<lt>octoE<nbsp>atE<nbsp>verplant.orgE<gt> and Sebastian Harl
574 E<lt>shE<nbsp>atE<nbsp>tokkee.orgE<gt>.
575
576 =cut
577