1 # collectd - Collectd.pm
2 # Copyright (C) 2007-2009 Sebastian Harl
4 # This program is free software; you can redistribute it and/or modify it
5 # under the terms of the GNU General Public License as published by the
6 # Free Software Foundation; only version 2 of the License is applicable.
8 # This program is distributed in the hope that it will be useful, but
9 # WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # General Public License for more details.
13 # You should have received a copy of the GNU General Public License along
14 # with this program; if not, write to the Free Software Foundation, Inc.,
15 # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 # Sebastian Harl <sh at tokkee.org>
31 if (! $Config{'useithreads'}) {
32 die "Perl does not support ithreads!";
38 our @ISA = qw( Exporter );
44 plugin_dispatch_values
50 plugin_dispatch_notification
80 'filter_chain' => [ qw(
105 push @{$EXPORT_TAGS{'all'}}, grep {! $seen{$_}++ } @{$EXPORT_TAGS{$_}}
106 foreach keys %EXPORT_TAGS;
113 Exporter::export_ok_tags ('all');
115 my @plugins : shared = ();
116 my @fc_plugins : shared = ();
117 my %cf_callbacks : shared = ();
120 TYPE_CONFIG, "config",
124 TYPE_SHUTDOWN, "shutdown",
126 TYPE_NOTIF, "notify",
135 my %fc_exec_names = (
142 FC_CB_CREATE, "create",
143 FC_CB_DESTROY, "destroy"
146 foreach my $type (keys %types) {
147 $plugins[$type] = &share ({});
150 foreach my $type (keys %fc_types) {
151 $fc_plugins[$type] = &share ({});
159 if ("Collectd" eq $caller) {
162 return plugin_log ($lvl, $msg);
165 sub ERROR { _log (scalar caller, LOG_ERR, shift); }
166 sub WARNING { _log (scalar caller, LOG_WARNING, shift); }
167 sub NOTICE { _log (scalar caller, LOG_NOTICE, shift); }
168 sub INFO { _log (scalar caller, LOG_INFO, shift); }
169 sub DEBUG { _log (scalar caller, LOG_DEBUG, shift); }
171 sub plugin_call_all {
176 our $cb_name = undef;
178 if (! defined $type) {
182 if (TYPE_LOG != $type) {
183 DEBUG ("Collectd::plugin_call_all: type = \"$type\" ("
184 . $types{$type} . "), args=\""
185 . join(', ', map { defined($_) ? $_ : '<undef>' } @_) . "\"");
188 if (! defined $plugins[$type]) {
189 ERROR ("Collectd::plugin_call_all: unknown type \"$type\"");
194 lock %{$plugins[$type]};
195 %plugins = %{$plugins[$type]};
198 foreach my $plugin (keys %plugins) {
199 $cb_name = $plugins{$plugin};
200 my $status = call_by_name (@_);
209 $err = "callback returned false";
212 if (TYPE_LOG != $type) {
213 ERROR ("Execution of callback \"$cb_name\" failed: $err");
222 elsif (TYPE_INIT == $type) {
223 ERROR ("${plugin}->init() failed with status $status. "
224 . "Plugin will be disabled.");
226 foreach my $type (keys %types) {
227 plugin_unregister ($type, $plugin);
230 elsif (TYPE_LOG != $type) {
231 WARNING ("${plugin}->$types{$type}() failed with status $status.");
237 # Collectd::plugin_register (type, name, data).
240 # init, read, write, shutdown, data set
246 # reference to the plugin's subroutine that does the work or the data set
248 sub plugin_register {
253 DEBUG ("Collectd::plugin_register: "
254 . "type = \"$type\" (" . $types{$type}
255 . "), name = \"$name\", data = \"$data\"");
257 if (! ((defined $type) && (defined $name) && (defined $data))) {
258 ERROR ("Usage: Collectd::plugin_register (type, name, data)");
262 if ((! defined $plugins[$type]) && (TYPE_DATASET != $type)
263 && (TYPE_CONFIG != $type)) {
264 ERROR ("Collectd::plugin_register: Invalid type \"$type\"");
268 if ((TYPE_DATASET == $type) && ("ARRAY" eq ref $data)) {
269 return plugin_register_data_set ($name, $data);
271 elsif ((TYPE_CONFIG == $type) && (! ref $data)) {
272 my $pkg = scalar caller;
274 if ($data !~ m/^$pkg\:\:/) {
275 $data = $pkg . "::" . $data;
279 $cf_callbacks{$name} = $data;
281 elsif ((TYPE_DATASET != $type) && (! ref $data)) {
282 my $pkg = scalar caller;
283 if ($data !~ m/^$pkg\:\:/) {
284 $data = $pkg . "::" . $data;
286 if (TYPE_READ == $type) {
287 return plugin_register_read($name, $data);
289 if (TYPE_WRITE == $type) {
290 return plugin_register_write($name, $data);
292 if (TYPE_LOG == $type) {
293 return plugin_register_log($name, $data);
295 if (TYPE_NOTIF == $type) {
296 return plugin_register_notification($name, $data);
298 if (TYPE_FLUSH == $type) {
299 #For collectd-5.6 only
300 lock %{$plugins[$type]};
301 $plugins[$type]->{$name} = $data;
302 return plugin_register_flush($name, $data);
304 lock %{$plugins[$type]};
305 $plugins[$type]->{$name} = $data;
308 ERROR ("Collectd::plugin_register: Invalid data.");
314 sub plugin_unregister {
318 DEBUG ("Collectd::plugin_unregister: type = \"$type\" ("
319 . $types{$type} . "), name = \"$name\"");
321 if (! ((defined $type) && (defined $name))) {
322 ERROR ("Usage: Collectd::plugin_unregister (type, name)");
326 if (TYPE_DATASET == $type) {
327 return plugin_unregister_data_set ($name);
329 elsif (TYPE_CONFIG == $type) {
331 delete $cf_callbacks{$name};
333 elsif (TYPE_READ == $type) {
334 return plugin_unregister_read ($name);
336 elsif (TYPE_WRITE == $type) {
337 return plugin_unregister_write($name);
339 elsif (TYPE_LOG == $type) {
340 return plugin_unregister_log ($name);
342 elsif (TYPE_NOTIF == $type) {
343 return plugin_unregister_notification($name);
345 elsif (TYPE_FLUSH == $type) {
346 return plugin_unregister_flush($name);
348 elsif (defined $plugins[$type]) {
349 lock %{$plugins[$type]};
350 delete $plugins[$type]->{$name};
353 ERROR ("Collectd::plugin_unregister: Invalid type.");
365 if (! defined $args{'valuelists'}) {
366 ERROR ("Collectd::plugin_write: Missing 'valuelists' argument.");
370 DEBUG ("Collectd::plugin_write:"
371 . (defined ($args{'plugins'}) ? " plugins = $args{'plugins'}" : "")
372 . (defined ($args{'datasets'}) ? " datasets = $args{'datasets'}" : "")
373 . " valueslists = $args{'valuelists'}");
375 if (defined ($args{'plugins'})) {
376 if ("ARRAY" eq ref ($args{'plugins'})) {
377 @plugins = @{$args{'plugins'}};
380 @plugins = ($args{'plugins'});
387 if ("ARRAY" eq ref ($args{'valuelists'})) {
388 @valuelists = @{$args{'valuelists'}};
391 @valuelists = ($args{'valuelists'});
394 if (defined ($args{'datasets'})) {
395 if ("ARRAY" eq ref ($args{'datasets'})) {
396 @datasets = @{$args{'datasets'}};
399 @datasets = ($args{'datasets'});
403 @datasets = (undef) x scalar (@valuelists);
406 if ($#datasets != $#valuelists) {
407 ERROR ("Collectd::plugin_write: Invalid number of datasets.");
411 foreach my $plugin (@plugins) {
412 for (my $i = 0; $i < scalar (@valuelists); ++$i) {
413 _plugin_write ($plugin, $datasets[$i], $valuelists[$i]);
425 DEBUG ("Collectd::plugin_flush:"
426 . (defined ($args{'timeout'}) ? " timeout = $args{'timeout'}" : "")
427 . (defined ($args{'plugins'}) ? " plugins = $args{'plugins'}" : "")
428 . (defined ($args{'identifiers'})
429 ? " identifiers = $args{'identifiers'}" : ""));
431 if (defined ($args{'timeout'}) && ($args{'timeout'} > 0)) {
432 $timeout = $args{'timeout'};
435 if (defined ($args{'plugins'})) {
436 if ("ARRAY" eq ref ($args{'plugins'})) {
437 @plugins = @{$args{'plugins'}};
440 @plugins = ($args{'plugins'});
447 if (defined ($args{'identifiers'})) {
448 if ("ARRAY" eq ref ($args{'identifiers'})) {
449 @ids = @{$args{'identifiers'}};
452 @ids = ($args{'identifiers'});
459 foreach my $plugin (@plugins) {
460 foreach my $id (@ids) {
461 _plugin_flush($plugin, $timeout, $id);
473 our $cb_name = undef;
476 if (! ((defined $type) && (defined $name) && (defined $cb_type))) {
477 ERROR ("Usage: Collectd::fc_call(type, name, cb_type, ...)");
481 if (! defined $fc_plugins[$type]) {
482 ERROR ("Collectd::fc_call: Invalid type \"$type\"");
486 if (! defined $fc_plugins[$type]->{$name}) {
487 ERROR ("Collectd::fc_call: Unknown "
488 . ($type == FC_MATCH ? "match" : "target")
493 DEBUG ("Collectd::fc_call: "
494 . "type = \"$type\" (" . $fc_types{$type}
495 . "), name = \"$name\", cb_type = \"$cb_type\" ("
496 . $fc_cb_types{$cb_type} . ")");
499 lock %{$fc_plugins[$type]};
500 %proc = %{$fc_plugins[$type]->{$name}};
503 if (FC_CB_EXEC == $cb_type) {
504 $cb_name = $proc{$fc_exec_names{$type}};
506 elsif (FC_CB_CREATE == $cb_type) {
507 if (defined $proc{'create'}) {
508 $cb_name = $proc{'create'};
514 elsif (FC_CB_DESTROY == $cb_type) {
515 if (defined $proc{'destroy'}) {
516 $cb_name = $proc{'destroy'};
523 $status = call_by_name (@_);
532 $err = "callback returned false";
535 ERROR ("Execution of fc callback \"$cb_name\" failed: $err");
548 DEBUG ("Collectd::fc_register: "
549 . "type = \"$type\" (" . $fc_types{$type}
550 . "), name = \"$name\", proc = \"$proc\"");
552 if (! ((defined $type) && (defined $name) && (defined $proc))) {
553 ERROR ("Usage: Collectd::fc_register(type, name, proc)");
557 if (! defined $fc_plugins[$type]) {
558 ERROR ("Collectd::fc_register: Invalid type \"$type\"");
562 if (("HASH" ne ref ($proc)) || (! defined $proc->{$fc_exec_names{$type}})
563 || ("" ne ref ($proc->{$fc_exec_names{$type}}))) {
564 ERROR ("Collectd::fc_register: Invalid proc.");
568 for my $p (qw( create destroy )) {
569 if ((defined $proc->{$p}) && ("" ne ref ($proc->{$p}))) {
570 ERROR ("Collectd::fc_register: Invalid proc.");
577 foreach my $p (keys %fc) {
578 my $pkg = scalar caller;
580 if ($p !~ m/^(create|destroy|$fc_exec_names{$type})$/) {
584 if ($fc{$p} !~ m/^$pkg\:\:/) {
585 $fc{$p} = $pkg . "::" . $fc{$p};
589 lock %{$fc_plugins[$type]};
590 if (defined $fc_plugins[$type]->{$name}) {
591 WARNING ("Collectd::fc_register: Overwriting previous "
592 . "definition of match \"$name\".");
595 if (! _fc_register ($type, $name)) {
596 ERROR ("Collectd::fc_register: Failed to register \"$name\".");
600 $fc_plugins[$type]->{$name} = \%fc;
604 sub _plugin_dispatch_config {
608 our $cb_name = undef;
610 if (! (defined ($plugin) && defined ($config))) {
614 if (! defined $cf_callbacks{$plugin}) {
615 WARNING ("Found a configuration for the \"$plugin\" plugin, but "
616 . "the plugin isn't loaded or didn't register "
617 . "a configuration callback.");
623 $cb_name = $cf_callbacks{$plugin};
625 call_by_name ($config);
630 # vim: set sw=4 ts=4 tw=78 noexpandtab :