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.
LoadPluginPlugin โ Loads the Perl plugin Plugin. This does basically the same asusewould do in a Perl program. As a side effect, the first occurrence of this option causes the Perl-interpreter to be initialized. โญBaseNameName โ PrependsName::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 theCollectd::Pluginsnamespace.<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 theBaseNamesetting. ๐EnableDebuggerPackage[=option,...]โ Run collectd under the control of the Perl source debugger. IfPackageis not the empty string, control is passed to the debugging, profiling, or tracing module installed asDevel::Package. A comma-separated list of options may be specified after the=character. Please note that you may not leave out thePackageoption even if you specify"". This is the same as using the-d:Packagecommand 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.
IncludeDirDir โ Adds Dir to the@INCarray. This is the same as using the-IDircommand line option oruse lib Dirin the source code. Please note that it only has effect on plugins loaded after this option. ๐RegisterLegacyFlushtrue|falseโ Theperl pluginused to register one flush callback (calledperl) 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 legacyperlcallback. 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 theperlcallback would break backwards compatibility. This option allows you to disable the legacyperlflush callback if you care about the double call and don't call theperlcallback 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_CONFIGTYPE_INITTYPE_READTYPE_WRITETYPE_FLUSHTYPE_LOGTYPE_NOTIFTYPE_SHUTDOWNTYPE_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: usingplugin_registerto register a data-set is deprecated. Add the new type to a customtypes.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 isconfig-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 aretype,data-set, andvalue-list.typeis a string. For the layout ofdata-setandvalue-list, see above.TYPE_FLUSHโ the arguments passed aretimeoutandidentifier.timeoutmeans only data older thantimeoutseconds is to be flushed.identifierspecifies which values are to be flushed. ๐ฟTYPE_LOGโ the arguments arelog-levelandmessage. The log level is small for important messages and high for less important messages. The least important level isLOG_DEBUG, the most important level isLOG_ERR. In between there are (from least to most important):LOG_INFO,LOG_NOTICE, andLOG_WARNING.messageis 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 isnotification. 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
pluginsargument is omitted, the values get dispatched to all registered write plugins. - If the
datasetsargument 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
datasetsargument 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,
timeoutdefaults to-1. - The identifier defaults to the undefined value.
- If the
pluginsargument has been specified, only the named plugins will be flushed. - The value of the
pluginsandidentifiersarguments 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_MATCHFC_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 areconfig-itemanduser-data. See above for the layout of theconfig-itemdata-type.user-datais 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 isuser-data, which is a reference to the user data initialized in thecreatecallback. This callback may be used to clean up instance-specific information and settings. ๐งนmatch,invokeโ the arguments passed aredata-set,value-list,metaanduser-data. See above for the layout of thedata-setandvalue-listdata-types.metais a pointer to an array of meta information, just like themetamember of the notification data-type (see above).user-datais a reference to the user data initialized in thecreatecallback.
๐ธ 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 ofplugin_get_interval()(see above). That function takes any plugin-specific interval settings into account (see the"Interval"option of"LoadPlugin"incollectd.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,
ENDblocks are only executed once when collectd shuts down. You should not rely onENDblocks 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
- opensuse-leap 16.0: collectd 5.12.0.348.g93f9bdcb-bp160.1.1 ยท GPL-2.0-only AND MIT
- opensuse-leap 16.0: collectd 5.12.0.348.g93f9bdcb-bp160.1.1 ยท GPL-2.0-only AND MIT
- opensuse-leap 16.0: collectd 5.12.0.348.g93f9bdcb-bp160.1.1 ยท GPL-2.0-only AND MIT
- opensuse-leap 16.0: collectd 5.12.0.348.g93f9bdcb-bp160.1.1 ยท GPL-2.0-only AND MIT
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms