COLLECTD-PERL(5)

๐ŸŒธ NAME ๐ŸŒธ

Okay so, bestie, this is collectd-perl(5) โ€” the documentation for collectd's perl plugin. That's the whole name vibe. ๐Ÿ’…


โœจ SYNOPSIS โœจ

Here's the config shape you're working with:

         LoadPlugin perl
         # ...
         <Plugin perl>
           IncludeDir "/path/to/perl/plugins"
           BaseName "Collectd::Plugins"
           EnableDebugger ""
           LoadPlugin "FooBar"

           <Plugin FooBar>
             Foo "Bar"
           </Plugin>
         </Plugin>

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

The perl plugin embeds a Perl-interpreter into collectd and gives you an interface to collectd's plugin system. This makes it possible to write plugins for collectd in Perl. It's a lot more efficient than executing a Perl-script every single time you want to read a value with the exec plugin (see collectd-exec(5)), and it provides a lot more functionality too. ๐Ÿซถ


๐ŸŽ€ CONFIGURATION ๐ŸŽ€

These are the config knobs for the <Plugin perl> block, and yes, they all matter. No cap.

  • LoadPlugin Plugin โ€” Loads the Perl plugin Plugin. This does basically the same as use would do in a Perl program. As a side effect, the first occurrence of this option causes the Perl-interpreter to be initialized. โญ
  • BaseName Name โ€” Prepends Name:: to all plugin names loaded after this option. This is provided for convenience to keep plugin names short. All Perl-based plugins provided with the collectd distributions reside in the Collectd::Plugins namespace.
  • <Plugin Name> block โ€” This block may be used to pass on configuration settings to a Perl plugin. The configuration is converted into a config-item data type which is passed to the registered configuration callback. See below for details about the config-item data type and how to register callbacks. The name identifies the callback. It is used literally and independent of the BaseName setting. ๐Ÿ’–
  • EnableDebugger Package[=option,...] โ€” Run collectd under the control of the Perl source debugger. If Package is not the empty string, control is passed to the debugging, profiling, or tracing module installed as Devel::Package. A comma-separated list of options may be specified after the = character. Please note that you may not leave out the Package option even if you specify "". This is the same as using the -d:Package command line option.

See perldebug for detailed documentation about debugging Perl.

โš ๏ธ This option does not prevent collectd from daemonizing, so you should start collectd with the -f command line option. Else you will not be able to use the command line driven interface of the debugger.
  • IncludeDir Dir โ€” Adds Dir to the @INC array. This is the same as using the -IDir command line option or use lib Dir in the source code. Please note that it only has effect on plugins loaded after this option. ๐ŸŒˆ
  • RegisterLegacyFlush true|false โ€” The perl plugin used to register one flush callback (called perl) and call all Perl-based flush handlers when this callback was called. Newer versions of the plugin wrap the Perl flush handlers and register them directly with the daemon in addition to the legacy perl callback. This allows to call specific Perl flush handlers, but has the downside that flushing all plugins now calls the Perl flush handlers twice (once directly and once via the legacy callback). Unfortunately, removing the perl callback would break backwards compatibility. This option allows you to disable the legacy perl flush callback if you care about the double call and don't call the perl callback in your setup. ๐Ÿ’€

๐Ÿงš WRITING YOUR OWN PLUGINS ๐Ÿงš

Writing your own plugins is quite simple. collectd manages plugins by means of dispatch functions which call the appropriate callback functions registered by the plugins. Any plugin basically consists of the implementation of these callback functions and initializing code which registers the functions with collectd. See the section EXAMPLES below for a really basic example. The following types of callback functions are known to collectd (all of them are optional):

๐Ÿงฉ configuration functions

This type of functions is called during configuration if an appropriate Plugin block has been encountered. It is called once for each Plugin block which matches the name of the callback as provided with the plugin_register method โ€” see below.

๐ŸŒฑ init functions

This type of functions is called once after loading the module and before any calls to the read and write functions. It should be used to initialize the internal state of the plugin (e.g. open sockets, ...). If the return value evaluates to false, the plugin will be disabled.

๐Ÿ“ฅ read functions

This type of function is used to collect the actual data. It is called once per interval (see the Interval configuration option of collectd). Usually it will call plugin_dispatch_values to dispatch the values to collectd which will pass them on to all registered write functions. If the return value evaluates to false the plugin will be skipped for an increasing amount of time until it returns true again.

๐Ÿ“ค write functions

This type of function is used to write the dispatched values. It is called once for each call to plugin_dispatch_values.

๐Ÿšฟ flush functions

This type of function is used to flush internal caches of plugins. It is usually triggered by the user only. Any plugin which caches data before writing it to disk should provide this kind of callback function.

๐Ÿ“ log functions

This type of function is used to pass messages of plugins or the daemon itself to the user.

๐Ÿ”” notification function

This type of function is used to act upon notifications. In general, a notification is a status message that may be associated with a data instance. Usually, a notification is generated by the daemon if a configured threshold has been exceeded (see the section THRESHOLD CONFIGURATION in collectd.conf(5) for more details), but any plugin may dispatch notifications as well.

๐Ÿ›‘ shutdown functions

This type of function is called once before the daemon shuts down. It should be used to clean up the plugin (e.g. close sockets, ...).

Any function (except log functions) may set the $@ variable to describe errors in more detail. The message will be passed on to the user using collectd's logging mechanism.

See the documentation of the plugin_register method in the section METHODS below for the number and types of arguments passed to each callback function. This section also explains how to register callback functions with collectd.

To enable a plugin, copy it to a place where Perl can find it (i.e. a directory listed in the @INC array) just as any other Perl plugin and add an appropriate LoadPlugin option to the configuration file. After restarting collectd you're done. Periodt. ๐Ÿ‘‘


๐Ÿ“ DATA TYPES ๐Ÿ“

The following complex types are used to pass values between the Perl plugin and collectd:

๐Ÿงธ Config-Item

A config-item is one structure which keeps the information provided in the configuration file. The array of children keeps one entry for each configuration option. Each such entry is another config-item structure, which may nest further if nested blocks are used.

{
  key      => key,
  values   => [ val1, val2, ... ],
  children => [ { ... }, { ... }, ... ]
}

๐Ÿ“Š Data-Set

A data-set is a list of one or more data-sources. Each data-source defines a name, type, min- and max-value and the data-set wraps them up into one structure. The general layout looks like this:

[{
  name => 'data_source_name',
  type => DS_TYPE_COUNTER || DS_TYPE_GAUGE || DS_TYPE_DERIVE || DS_TYPE_ABSOLUTE,
  min  => value || undef,
  max  => value || undef
}, ...]

๐Ÿ’ซ Value-List

A value-list is one structure which features an array of values and fields to identify the values, i.e. time and host, plugin name and plugin-instance as well as a type and type-instance. Since the type is not included in the value-list but is passed as an extra argument, the general layout looks like this:

{
  values => [123, 0.5],
  time   => time (),
  interval => plugin_get_interval (),
  host   => $hostname_g,
  plugin => 'myplugin',
  type   => 'myplugin',
  plugin_instance => '',
  type_instance   => ''
}

๐Ÿ”” Notification

A notification is one structure defining the severity, time and message of the status message as well as an identification of a data instance. Also, it includes an optional list of user-defined meta information represented as (name, value) pairs:

{
  severity => NOTIF_FAILURE || NOTIF_WARNING || NOTIF_OKAY,
  time     => time (),
  message  => 'status message',
  host     => $hostname_g,
  plugin   => 'myplugin',
  type     => 'mytype',
  plugin_instance => '',
  type_instance   => '',
  meta     => [ { name => <name>, value => <value> }, ... ]
}

๐Ÿช„ Match-Proc

A match-proc is one structure storing the callbacks of a match of the filter chain infrastructure. The general layout looks like this:

{
  create  => 'my_create',
  destroy => 'my_destroy',
  match   => 'my_match'
}

๐ŸŽฏ Target-Proc

A target-proc is one structure storing the callbacks of a target of the filter chain infrastructure. The general layout looks like this:

{
  create  => 'my_create',
  destroy => 'my_destroy',
  invoke  => 'my_invoke'
}

๐Ÿ’… METHODS ๐Ÿ’…

Okay bestie, this is the good part โ€” these are the actual functions that let your Perl module talk to collectd's C guts. They're all exported by the :plugin export tag (peep the "EXPORTS" section below if you need a refresher, iykyk).


๐ŸŒธ plugin_register (type, name, data)

This one registers a callback-function or a data-set. Two-for-one deal, we love to see it.

The type can be any of these bad boys:

  • TYPE_CONFIG
  • TYPE_INIT
  • TYPE_READ
  • TYPE_WRITE
  • TYPE_FLUSH
  • TYPE_LOG
  • TYPE_NOTIF
  • TYPE_SHUTDOWN
  • TYPE_DATASET

name is the name of the callback-function or the type of the data-set, depending on what type is set to. โš ๏ธ Lowkey important note: the type of the data-set is whatever you pass as name here โ€” it has literally nothing to do with the type argument, which is just there to tell plugin_register what kind of thing you're registering. Don't get it twisted.

The last argument, data, is either a function name or an array-reference. If type is TYPE_DATASET, then data must be an array-reference pointing to an array of hashes, and each hash describes one data-set. For the exact layout, scroll back up to Data-Set. Also โ€” slay โ€” there's a huge stack of predefined data-sets chilling in the types.db file that get auto-registered with collectd; see types.db(5) for the format of that file.

Note: using plugin_register to register a data-set is deprecated. Add the new type to a custom types.db(5) file instead. This functionality might get removed in a future version of collectd. Consider this your friendly warning, bestie. ๐Ÿ’€

If type is any of the other types (TYPE_INIT, TYPE_READ, โ€ฆ) then data should be a function name. If the name isn't prefixed with the plugin's package name, collectd will add it for you automatically โ€” so convenient fr. The interface is a lil different from the C interface (which expects a function pointer instead) because Perl doesn't support sharing references to subroutines between threads. That's the whole reason, no cap.


๐Ÿฆ‹ what args get passed to your callbacks

These functions get called at the various stages of the daemon (see "WRITING YOUR OWN PLUGINS" above) and receive these arguments:

  • TYPE_CONFIG โ€” the only argument passed is config-item. See above for the layout of this data type.
  • TYPE_INIT, TYPE_READ, TYPE_SHUTDOWN โ€” no arguments are passed. Nothing. Nada. Enjoy the peace. โœจ
  • TYPE_WRITE โ€” the arguments passed are type, data-set, and value-list. type is a string. For the layout of data-set and value-list, see above.
  • TYPE_FLUSH โ€” the arguments passed are timeout and identifier. timeout means only data older than timeout seconds is to be flushed. identifier specifies which values are to be flushed. ๐Ÿšฟ
  • TYPE_LOG โ€” the arguments are log-level and message. The log level is small for important messages and high for less important messages. The least important level is LOG_DEBUG, the most important level is LOG_ERR. In between there are (from least to most important): LOG_INFO, LOG_NOTICE, and LOG_WARNING. message is simply a string without a newline at the end โ€” so don't go adding your own \n, she's already handled.
  • TYPE_NOTIF โ€” the only argument passed is notification. See above for the layout of this data type.

๐ŸŽ€ plugin_unregister (type, plugin)

Removes a callback or data-set from collectd's internal list of functions / datasets. It's giving Marie Kondo โ€” does this spark joy? No? Unregistered. ๐Ÿ’…


๐Ÿ”ฅ plugin_dispatch_values (value-list)

Submits a value-list to the daemon. If the data-set identified by value-list->{type} is found (and the number of values matches the number of data-sources), then the type, data-set and value-list get passed to all the write-callbacks registered with the daemon. This is the main character function, honestly.


๐ŸŒˆ plugin_write ([plugins => ...][, datasets => ...], valuelists => ...)

Calls the write function of the given plugins with the provided data sets and value lists. But here's the tea โ€” unlike plugin_dispatch_values, it does not update collectd's internal cache and it bypasses the filter mechanism (see collectd.conf(5) for details). Sneaky. ๐Ÿ™ˆ

  • If the plugins argument is omitted, the values get dispatched to all registered write plugins.
  • If the datasets argument is omitted, the required data sets are looked up according to the "type" member in the appropriate value list.
  • The value of all three arguments may either be a single scalar or a reference to an array.
  • If the datasets argument has been specified, the number of data sets has to equal the number of specified value lists. Math is math, bestie. ๐Ÿงฎ

โญ plugin_flush ([timeout => timeout][, plugins => ...][, identifiers => ...])

Flush one or more plugins. timeout and the specified identifiers get passed on to the registered flush-callbacks.

  • If omitted, timeout defaults to -1.
  • The identifier defaults to the undefined value.
  • If the plugins argument has been specified, only the named plugins will be flushed.
  • The value of the plugins and identifiers arguments may either be a string or a reference to an array of strings.

๐Ÿ’œ plugin_dispatch_notification (notification)

Submits a notification to the daemon, which will then pass it to all the notification-callbacks that are registered. Sharing is caring. ๐Ÿซถ


๐Ÿ“ plugin_log (log-level, message)

Submits a message of level log-level to collectd's logging mechanism. The message gets passed to all log-callbacks registered with collectd. Go off, log queen. ๐Ÿ‘‘


๐Ÿงš ERROR, WARNING, NOTICE, INFO, DEBUG (message)

Wrappers around plugin_log, using LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO and LOG_DEBUG respectively as the log-level. Same order, no confusion, you're welcome. โœจ


๐Ÿ‘‘ plugin_get_interval ()

Returns the interval of the current plugin as a floating point number in seconds. This value depends on the interval configured inside the "LoadPlugin perl" block, or the global interval (see collectd.conf(5) for details). Per-plugin intervals ate, honestly. ๐Ÿ”ฅ


The following function provides the filter chain C-interface to Perl modules. It's exported by the :filter_chain export tag (see the "EXPORTS" section below โ€” yes, again, go look).

๐Ÿ’– fc_register (type, name, proc)

Registers filter chain callbacks with collectd. Your filter era begins now.

type may be any of:

  • FC_MATCH
  • FC_TARGET

name is the name of the match or target. By this name, the callbacks are identified in the configuration file when specifying a Match or Target block (see collectd.conf(5) for details).

proc is a hash reference. The hash includes up to three callbacks: an optional constructor (create) and destructor (destroy), plus a mandatory match or invoke callback. match gets called whenever processing an appropriate match, while invoke gets called whenever processing an appropriate target (see the section "FILTER CONFIGURATION" in collectd.conf(5) for details). Just like any other callbacks, filter chain callbacks are identified by the function name rather than a function pointer, because Perl does not support sharing references to subroutines between threads โ€” same reason as before, we stay consistent. ๐Ÿ’…

The following arguments are passed to the callbacks:

  • create โ€” the arguments passed are config-item and user-data. See above for the layout of the config-item data-type. user-data is a reference to a scalar value that may be used to store any information specific to this particular instance. The daemon does not care about this information at all โ€” it's for the plugin's use only. Your little secret. ๐Ÿคซ
  • destroy โ€” the only argument passed is user-data, which is a reference to the user data initialized in the create callback. This callback may be used to clean up instance-specific information and settings. ๐Ÿงน
  • match, invoke โ€” the arguments passed are data-set, value-list, meta and user-data. See above for the layout of the data-set and value-list data-types. meta is a pointer to an array of meta information, just like the meta member of the notification data-type (see above). user-data is a reference to the user data initialized in the create callback.

๐ŸŒธ GLOBAL VARIABLES ๐ŸŒธ

๐ŸŒ $hostname_g

As the name suggests, this variable keeps the hostname of the system collectd is running on. The value might be influenced by the Hostname or FQDNLookup configuration options (see collectd.conf(5) for details). It knows where it lives. ๐Ÿ 

โฐ $interval_g

This variable keeps the interval in seconds in which the read functions are queried (see the Interval configuration option).

Note: this variable should no longer be used in favor of plugin_get_interval() (see above). That function takes any plugin-specific interval settings into account (see the "Interval" option of "LoadPlugin" in collectd.conf(5) for details). So likeโ€ฆ use the new one, bestie. It's the upgraded version. ๐Ÿ’…

Any changes to these variables will be globally visible in collectd. Everyone sees it. No secrets here. ๐Ÿ‘€


โœจ EXPORTS โœจ

By default, no symbols are exported. Like, zero. But the following export tags are available (:all will export all of them, if you're feeling chaotic):

๐Ÿฆ‹ :plugin

plugin_register ()
plugin_unregister ()
plugin_dispatch_values ()
plugin_flush ()
plugin_flush_one ()
plugin_flush_all ()
plugin_dispatch_notification ()
plugin_log ()

๐ŸŽ€ :types

TYPE_CONFIG
TYPE_INIT
TYPE_READ
TYPE_WRITE
TYPE_FLUSH
TYPE_SHUTDOWN
TYPE_LOG
TYPE_DATASET

๐Ÿ’œ :ds_types

DS_TYPE_COUNTER
DS_TYPE_GAUGE
DS_TYPE_DERIVE
DS_TYPE_ABSOLUTE

๐ŸŒˆ :log

ERROR ()
WARNING ()
NOTICE ()
INFO ()
DEBUG ()
LOG_ERR
LOG_WARNING
LOG_NOTICE
LOG_INFO
LOG_DEBUG

โญ :filter_chain

fc_register
FC_MATCH_NO_MATCH
FC_MATCH_MATCHES
FC_TARGET_CONTINUE
FC_TARGET_STOP
FC_TARGET_RETURN

๐Ÿงš :fc_types

FC_MATCH
FC_TARGET

๐Ÿ“ :notif

NOTIF_FAILURE
NOTIF_WARNING
NOTIF_OKAY

๐Ÿ‘‘ :globals

$hostname_g
$interval_g

โœจ EXAMPLES โœจ

Okay bestie, real talk โ€” every Perl plugin you ever write is gonna open basically the same way, like the group chat intro of code:

package Collectd::Plugins::FooBar;

use strict;
use warnings;

use Collectd qw( :all );

Now let's actually do stuff. A super simple read function? It's giving minimal effort, maximum slay:

sub foobar_read
{
  my $vl = { plugin => 'foobar', type => 'gauge' };
  $vl->{'values'} = [ rand(42) ];
  plugin_dispatch_values ($vl);
  return 1;
}

And a super simple write function, for when you gotta log what's coming at you:

sub foobar_write
{
  my ($type, $ds, $vl) = @_;
  for (my $i = 0; $i < scalar (@$ds); ++$i) {
    print "$vl->{'plugin'} ($vl->{'type'}): $vl->{'values'}->[$i]\n";
  }
  return 1;
}

A super simple match callback, matching iconic energy:

sub foobar_match
{
  my ($ds, $vl, $meta, $user_data) = @_;
  if (matches($ds, $vl)) {
    return FC_MATCH_MATCHES;
  } else {
    return FC_MATCH_NO_MATCH;
  }
}

Then to register those functions with collectd โ€” the moment it all comes together ๐Ÿซถ:

plugin_register (TYPE_READ, "foobar", "foobar_read");
plugin_register (TYPE_WRITE, "foobar", "foobar_write");

fc_register (FC_MATCH, "foobar", "foobar_match");

Peep the section "DATA TYPES" above for a complete documentation of the data types used by the read, write and match functions โ€” that's where the full tea is spilled.


๐ŸŒธ NOTES ๐ŸŒธ

  • Ok so, please feel free to send in new plugins to collectd's mailing list at <collectd at collectd.org> for review and, possibly, inclusion in the main distribution. In the latter case, we will take care of keeping the plugin up to date and adapting it to new versions of collectd โ€” literally they'll babysit your code for you, bestie.

Before submitting your plugin, please take a look at <http://collectd.org/dev-info.shtml>. Required reading, no skips ๐Ÿ’…


๐Ÿ’€ CAVEATS ๐Ÿ’€

  • collectd is heavily multi-threaded. Each collectd thread accessing the perl plugin will be mapped to a Perl interpreter thread (see threads(3perl)). Any such thread will be created and destroyed transparently and on-the-fly.

Which means: any plugin has to be thread-safe if it provides several entry points from collectd (i. e. if it registers more than one callback or if a registered callback may be called more than once in parallel). Please note that no data is shared between threads by default. You have to use the threads::shared module to do so. No lurking on each other's variables, bestie ๐Ÿ™ˆ

  • Each function name registered with collectd has to be available before the first thread has been created (i. e. basically at compile time). This basically means that hacks โ€” yes, I really consider this to be a hack โ€” like "*foo = \&bar; plugin_register (TYPE_READ, "plugin", "foo");" most likely will not work. This is due to the fact that the symbol table is not shared across different threads. So like... don't try to be clever, it will not eat ๐Ÿ’€
  • Each plugin is usually only loaded once and kept in memory for performance reasons. Therefore, END blocks are only executed once when collectd shuts down. You should not rely on END blocks anyway โ€” use shutdown functions instead. Fr fr, use the shutdown functions.
  • The perl plugin exports the internal API of collectd which is considered unstable and subject to change at any time. We try hard to not break backwards compatibility in the Perl API during the life cycle of one major release. However, this cannot be guaranteed at all times. Watch out for warnings dispatched by the perl plugin after upgrades. Lowkey keep an eye on those warnings, they're telling on you for a reason ๐Ÿ‘€

๐Ÿฆ‹ SEE ALSO ๐Ÿฆ‹

collectd(1), collectd.conf(5), collectd-exec(5), types.db(5), perl(1), threads(3perl), threads::shared(3perl), perldebug(1) โ€” the whole reading list, go off ๐Ÿ“š


๐Ÿ‘‘ AUTHOR ๐Ÿ‘‘

The "perl plugin" has been written by Sebastian Harl <sh at tokkee.org>.

This manpage has been written by Florian Forster <octo at collectd.org> and Sebastian Harl <sh at tokkee.org>.


And that's the whole page, bestie โ€” you ate that ๐Ÿ’…โœจ This lil edition is version 5.12.0.348.g93f9bdc, dated 2024-12-18, and it's COLLECTD-PERL(5) for short. Go forth and write iconic plugins ๐ŸŽ€๐Ÿฆ‹

๐Ÿ“ฆ where this page comes from