types.db(5): Added a manpage documenting the format of the TypesDB file.
[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 these 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 log functions
96
97 This type of function is used to pass messages of plugins or the daemon itself
98 to the user.
99
100 =item shutdown functions
101
102 This type of function is called once before the daemon shuts down. It should
103 be used to clean up the plugin (e.g. close sockets, ...).
104
105 =back
106
107 Any function may set the B<$@> variable to describe errors in more detail. The
108 message will be passed on to the user using collectd's logging mechanism.
109
110 See the documentation of the B<plugin_register> method in the section
111 "METHODS" below for the number and types of arguments passed to each
112 B<callback function>. This section also explains how to register B<callback
113 functions> with collectd.
114
115 To enable a plugin, copy it to a place where Perl can find it (i.E<nbsp>e. a
116 directory listed in the B<@INC> array) just as any other Perl plugin and add
117 an appropriate B<LoadPlugin> option to the configuration file. After
118 restarting collectd you're done.
119
120 =head1 DATA TYPES
121
122 The following complex types are used to pass values between the Perl plugin
123 and collectd:
124
125 =over 4
126
127 =item Data-Set
128
129 A data-set is a list of one or more data-sources. Each data-source defines a
130 name, type, min- and max-value and the data-set wraps them up into one
131 structure. The general layout looks like this:
132
133   [{
134     name => 'data_source_name',
135     type => DS_TYPE_COUNTER || DS_TYPE_GAUGE
136     min  => value || undef,
137     max  => value || undef
138   }, ...]
139
140 =item Value-List
141
142 A value-list is one structure which features an array of values and fields to
143 identify the values, i.E<nbsp>e. time and host, plugin name and
144 plugin-instance as well as a type and type-instance. Since the "type" is not
145 included in the value-list but is passed as an extra argument, the general
146 layout looks like this:
147
148   {
149     values => [123, 0.5],
150     time   => time (),
151     host   => 'localhost',
152     plugin => 'myplugin',
153     plugin_instance => '',
154     type_instance   => ''
155   }
156
157 =back
158
159 =head1 METHODS
160
161 The following functions provide the C-interface to Perl-modules. They are
162 exported by the ":plugin" export tag (see the section "EXPORTS" below).
163
164 =over 4
165
166 =item B<plugin_register> (I<type>, I<name>, I<data>)
167
168 Registers a callback-function or data-set.
169
170 I<type> can be one of:
171
172 =over 4
173
174 =item TYPE_INIT
175
176 =item TYPE_READ
177
178 =item TYPE_WRITE
179
180 =item TYPE_LOG
181
182 =item TYPE_SHUTDOWN
183
184 =item TYPE_DATASET
185
186 =back
187
188 I<name> is the name of the callback-function or the type of the data-set,
189 depending on the value of I<type>. (Please note that the type of the data-set
190 is the value passed as I<name> here and has nothing to do with the I<type>
191 argument which simply tells B<plugin_register> what is being registered.)
192
193 The last argument, I<data>, is either a function name or an array-reference.
194 If I<type> is B<TYPE_DATASET>, then the I<data> argument must be an
195 array-reference which points to an array of hashes. Each hash describes one
196 data-set. For the exact layout see B<Data-Set> above. Please note that
197 there is a large number of predefined data-sets available in the B<types.db>
198 file which are automatically registered with collectd - see L<types.db(5)> for
199 a description of the format of this file.
200
201 If the I<type> argument is any of the other types (B<TYPE_INIT>, B<TYPE_READ>,
202 ...) then I<data> is expected to be a function name. If the name is not
203 prefixed with the plugin's package name collectd will add it automatically.
204 The interface slightly differs from the C interface (which expects a function
205 pointer instead) because Perl does not support to share references to
206 subroutines between threads.
207
208 These functions are called in the various stages of the daemon (see the
209 section "WRITING YOUR OWN PLUGINS" above) and are passed the following
210 arguments:
211
212 =over 4
213
214 =item TYPE_INIT
215
216 =item TYPE_READ
217
218 =item TYPE_SHUTDOWN
219
220 No arguments are passed
221
222 =item TYPE_WRITE
223
224 The arguments passed are I<type>, I<data-set>, and I<value-list>. I<type> is a
225 string. For the layout of I<data-set> and I<value-list> see above.
226
227 =item TYPE_LOG
228
229 The arguments are I<log-level> and I<message>. The log level is small for
230 important messages and high for less important messages. The least important
231 level is B<LOG_DEBUG>, the most important level is B<LOG_ERR>. In between there
232 are (from least to most important): B<LOG_INFO>, B<LOG_NOTICE>, and
233 B<LOG_WARNING>. I<message> is simply a string B<without> a newline at the end.
234
235 =back
236
237 =item B<plugin_unregister> (I<type>, I<plugin>)
238
239 Removes a callback or data-set from collectd's internal list of
240 functionsE<nbsp>/ datasets.
241
242 =item B<plugin_dispatch_values> (I<type>, I<value-list>)
243
244 Submits a I<value-list> of type I<type> to the daemon. If the data-set I<type>
245 is found (and the number of values matches the number of data-sources) then the
246 type, data-set and value-list is passed to all write-callbacks that are
247 registered with the daemon.
248
249 =item B<plugin_log> (I<log-level>, I<message>)
250
251 Submits a I<message> of level I<log-level> to collectd's logging mechanism.
252 The message is passed to all log-callbacks that are registered with collectd.
253
254 =item B<ERROR>, B<WARNING>, B<NOTICE>, B<INFO>, B<DEBUG> (I<message>)
255
256 Wrappers around B<plugin_log>, using B<LOG_ERR>, B<LOG_WARNING>,
257 B<LOG_NOTICE>, B<LOG_INFO> and B<LOG_DEBUG> respectively as I<log-level>.
258
259 =back
260
261 =head1 GLOBAL VARIABLES
262
263 =over 4
264
265 =item B<$hostname_g>
266
267 As the name suggests this variable keeps the hostname of the system collectd
268 is running on. The value might be influenced by the B<Hostname> or
269 B<FQDNLookup> configuration options (see L<collectd.conf(5)> for details).
270
271 =item B<$interval_g>
272
273 This variable keeps the interval in seconds in which the read functions are
274 queried (see the B<Interval> configuration option).
275
276 =back
277
278 Any changes to these variables will be globally visible in collectd.
279
280 =head1 EXPORTS
281
282 By default no symbols are exported. However, the following export tags are
283 available (B<:all> will export all of them):
284
285 =over 4
286
287 =item B<:plugin>
288
289 =over 4
290
291 =item B<plugin_register> ()
292
293 =item B<plugin_unregister> ()
294
295 =item B<plugin_dispatch_values> ()
296
297 =item B<plugin_log> ()
298
299 =back
300
301 =item B<:types>
302
303 =over 4
304
305 =item B<TYPE_INIT>
306
307 =item B<TYPE_READ>
308
309 =item B<TYPE_WRITE>
310
311 =item B<TYPE_SHUTDOWN>
312
313 =item B<TYPE_LOG>
314
315 =back
316
317 =item B<:ds_types>
318
319 =over 4
320
321 =item B<DS_TYPE_COUNTER>
322
323 =item B<DS_TYPE_GAUGE>
324
325 =back
326
327 =item B<:log>
328
329 =over 4
330
331 =item B<ERROR> ()
332
333 =item B<WARNING> ()
334
335 =item B<NOTICE> ()
336
337 =item B<INFO> ()
338
339 =item B<DEBUG> ()
340
341 =item B<LOG_ERR>
342
343 =item B<LOG_WARNING>
344
345 =item B<LOG_NOTICE>
346
347 =item B<LOG_INFO>
348
349 =item B<LOG_DEBUG>
350
351 =back
352
353 =item B<:globals>
354
355 =over 4
356
357 =item B<$hostname_g>
358
359 =item B<$interval_g>
360
361 =back
362
363 =back
364
365 =head1 EXAMPLES
366
367 Any Perl plugin will start similar to:
368
369   package Collectd::Plugins::FooBar;
370
371   use strict;
372   use warnings;
373
374   use Collectd qw( :all );
375
376 A very simple read function will look like:
377
378   sub foobar_read
379   {
380     my $vl = { plugin => 'foobar' };
381     $vl->{'values'} = [ rand(42) ];
382     plugin_dispatch_values ('gauge', $vl);
383     return 1;
384   }
385
386 A very simple write function will look like:
387
388   sub foobar_write
389   {
390     my ($type, $ds, $vl) = @_;
391     for (my $i = 0; $i < scalar (@$ds); ++$i) {
392       print "$vl->{'plugin'} ($vl->{'type'}): $vl->{'values'}->[$i]\n";
393     }
394     return 1;
395   }
396
397 To register those functions with collectd:
398
399   plugin_register (TYPE_READ, "foobar", "foobar_read");
400   plugin_register (TYPE_WRITE, "foobar", "foobar_write");
401
402 See the section "DATA TYPES" above for a complete documentation of the data
403 types used by the read and write functions.
404
405 =head1 NOTES
406
407 =over 4
408
409 =item
410
411 Please feel free to send in new plugins to collectd's mailinglist at
412 E<lt>collectdE<nbsp>atE<nbsp>verplant.orgE<gt> for review and, possibly,
413 inclusion in the main distribution. In the latter case, we will take care of
414 keeping the plugin up to date and adapting it to new versions of collectd.
415
416 Before submitting your plugin, please take a look at
417 L<http://collectd.org/dev-info.shtml>.
418
419 =back
420
421 =head1 CAVEATS
422
423 =over 4
424
425 =item
426
427 collectd is heavily multi-threaded. Each collectd thread accessing the perl
428 plugin will be mapped to a Perl interpreter thread (see L<threads(3perl)>).
429 Any such thread will be created and destroyed transparently and on-the-fly.
430
431 Hence, any plugin has to be thread-safe if it provides several entry points
432 from collectd (i.E<nbsp>e. if it registers more than one callback). Please
433 note that no data is shared between threads by default. You have to use the
434 B<threads::shared> module to do so.
435
436 =item
437
438 Each function name registered with collectd has to be available before the
439 first thread has been created (i.E<nbsp>e. basically at compile time). This
440 basically means that hacks (yes, I really consider this to be a hack) like
441 C<*foo = \&bar; plugin_register (TYPE_READ, "plugin", "foo");> most likely
442 will not work. This is due to the fact that the symbol table is not shared
443 across different threads.
444
445 =item
446
447 Each plugin is usually only loaded once and kept in memory for performance
448 reasons. Therefore, END blocks are only executed once when collectd shuts
449 down. You should not rely on END blocks anyway - use B<shutdown functions>
450 instead.
451
452 =back
453
454 =head1 SEE ALSO
455
456 L<collectd(1)>,
457 L<collectd.conf(5)>,
458 L<collectd-exec(5)>,
459 L<types.db(5)>,
460 L<perl(1)>,
461 L<threads(3perl)>,
462 L<threads::shared(3perl)>,
463 L<perldebug(1)>
464
465 =head1 AUTHOR
466
467 The C<perl plugin> has been written by Sebastian Harl
468 E<lt>shE<nbsp>atE<nbsp>tokkee.orgE<gt>.
469
470 This manpage has been written by Florian Forster
471 E<lt>octoE<nbsp>atE<nbsp>verplant.orgE<gt> and Sebastian Harl
472 E<lt>shE<nbsp>atE<nbsp>tokkee.orgE<gt>.
473
474 =cut
475