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
49 plugin_dispatch_notification
79 'filter_chain' => [ qw(
104 push @{$EXPORT_TAGS{'all'}}, grep {! $seen{$_}++ } @{$EXPORT_TAGS{$_}}
105 foreach keys %EXPORT_TAGS;
112 Exporter::export_ok_tags ('all');
114 my @plugins : shared = ();
115 my @fc_plugins : shared = ();
116 my %cf_callbacks : shared = ();
119 TYPE_CONFIG, "config",
123 TYPE_SHUTDOWN, "shutdown",
125 TYPE_NOTIF, "notify",
134 my %fc_exec_names = (
141 FC_CB_CREATE, "create",
142 FC_CB_DESTROY, "destroy"
145 foreach my $type (keys %types) {
146 $plugins[$type] = &share ({});
149 foreach my $type (keys %fc_types) {
150 $fc_plugins[$type] = &share ({});
158 if ("Collectd" eq $caller) {
161 return plugin_log ($lvl, $msg);
164 sub ERROR { _log (scalar caller, LOG_ERR, shift); }
165 sub WARNING { _log (scalar caller, LOG_WARNING, shift); }
166 sub NOTICE { _log (scalar caller, LOG_NOTICE, shift); }
167 sub INFO { _log (scalar caller, LOG_INFO, shift); }
168 sub DEBUG { _log (scalar caller, LOG_DEBUG, shift); }
170 sub plugin_call_all {
175 our $cb_name = undef;
177 if (! defined $type) {
181 if (TYPE_LOG != $type) {
182 DEBUG ("Collectd::plugin_call: type = \"$type\" ("
183 . $types{$type} . "), args=\""
184 . join(', ', map { defined($_) ? $_ : '<undef>' } @_) . "\"");
187 if (! defined $plugins[$type]) {
188 ERROR ("Collectd::plugin_call: unknown type \"$type\"");
193 lock %{$plugins[$type]};
194 %plugins = %{$plugins[$type]};
197 foreach my $plugin (keys %plugins) {
198 my $p = $plugins{$plugin};
202 if ($p->{'wait_left'} > 0) {
203 $p->{'wait_left'} -= $interval_g;
206 next if ($p->{'wait_left'} > 0);
208 $cb_name = $p->{'cb_name'};
209 $status = call_by_name (@_);
218 $err = "callback returned false";
221 if (TYPE_LOG != $type) {
222 ERROR ("Execution of callback \"$cb_name\" failed: $err");
229 $p->{'wait_left'} = 0;
230 $p->{'wait_time'} = $interval_g;
232 elsif (TYPE_READ == $type) {
233 if ($p->{'wait_time'} < $interval_g) {
234 $p->{'wait_time'} = $interval_g;
237 $p->{'wait_left'} = $p->{'wait_time'};
238 $p->{'wait_time'} *= 2;
240 if ($p->{'wait_time'} > 86400) {
241 $p->{'wait_time'} = 86400;
244 WARNING ("${plugin}->read() failed with status $status. "
245 . "Will suspend it for $p->{'wait_left'} seconds.");
247 elsif (TYPE_INIT == $type) {
248 ERROR ("${plugin}->init() failed with status $status. "
249 . "Plugin will be disabled.");
251 foreach my $type (keys %types) {
252 plugin_unregister ($type, $plugin);
255 elsif (TYPE_LOG != $type) {
256 WARNING ("${plugin}->$types{$type}() failed with status $status.");
262 # Collectd::plugin_register (type, name, data).
265 # init, read, write, shutdown, data set
271 # reference to the plugin's subroutine that does the work or the data set
273 sub plugin_register {
278 DEBUG ("Collectd::plugin_register: "
279 . "type = \"$type\" (" . $types{$type}
280 . "), name = \"$name\", data = \"$data\"");
282 if (! ((defined $type) && (defined $name) && (defined $data))) {
283 ERROR ("Usage: Collectd::plugin_register (type, name, data)");
287 if ((! defined $plugins[$type]) && (TYPE_DATASET != $type)
288 && (TYPE_CONFIG != $type)) {
289 ERROR ("Collectd::plugin_register: Invalid type \"$type\"");
293 if ((TYPE_DATASET == $type) && ("ARRAY" eq ref $data)) {
294 return plugin_register_data_set ($name, $data);
296 elsif ((TYPE_CONFIG == $type) && (! ref $data)) {
297 my $pkg = scalar caller;
299 if ($data !~ m/^$pkg\:\:/) {
300 $data = $pkg . "::" . $data;
304 $cf_callbacks{$name} = $data;
306 elsif ((TYPE_DATASET != $type) && (! ref $data)) {
307 my $pkg = scalar caller;
311 if ($data !~ m/^$pkg\:\:/) {
312 $data = $pkg . "::" . $data;
316 wait_time => $interval_g,
321 lock %{$plugins[$type]};
322 $plugins[$type]->{$name} = \%p;
325 ERROR ("Collectd::plugin_register: Invalid data.");
331 sub plugin_unregister {
335 DEBUG ("Collectd::plugin_unregister: type = \"$type\" ("
336 . $types{$type} . "), name = \"$name\"");
338 if (! ((defined $type) && (defined $name))) {
339 ERROR ("Usage: Collectd::plugin_unregister (type, name)");
343 if (TYPE_DATASET == $type) {
344 return plugin_unregister_data_set ($name);
346 elsif (TYPE_CONFIG == $type) {
348 delete $cf_callbacks{$name};
350 elsif (defined $plugins[$type]) {
351 lock %{$plugins[$type]};
352 delete $plugins[$type]->{$name};
355 ERROR ("Collectd::plugin_unregister: Invalid type.");
367 if (! defined $args{'valuelists'}) {
368 ERROR ("Collectd::plugin_write: Missing 'valuelists' argument.");
372 DEBUG ("Collectd::plugin_write:"
373 . (defined ($args{'plugins'}) ? " plugins = $args{'plugins'}" : "")
374 . (defined ($args{'datasets'}) ? " datasets = $args{'datasets'}" : "")
375 . " valueslists = $args{'valuelists'}");
377 if (defined ($args{'plugins'})) {
378 if ("ARRAY" eq ref ($args{'plugins'})) {
379 @plugins = @{$args{'plugins'}};
382 @plugins = ($args{'plugins'});
389 if ("ARRAY" eq ref ($args{'valuelists'})) {
390 @valuelists = @{$args{'valuelists'}};
393 @valuelists = ($args{'valuelists'});
396 if (defined ($args{'datasets'})) {
397 if ("ARRAY" eq ref ($args{'datasets'})) {
398 @datasets = @{$args{'datasets'}};
401 @datasets = ($args{'datasets'});
405 @datasets = (undef) x scalar (@valuelists);
408 if ($#datasets != $#valuelists) {
409 ERROR ("Collectd::plugin_write: Invalid number of datasets.");
413 foreach my $plugin (@plugins) {
414 for (my $i = 0; $i < scalar (@valuelists); ++$i) {
415 _plugin_write ($plugin, $datasets[$i], $valuelists[$i]);
427 DEBUG ("Collectd::plugin_flush:"
428 . (defined ($args{'timeout'}) ? " timeout = $args{'timeout'}" : "")
429 . (defined ($args{'plugins'}) ? " plugins = $args{'plugins'}" : "")
430 . (defined ($args{'identifiers'})
431 ? " identifiers = $args{'identifiers'}" : ""));
433 if (defined ($args{'timeout'}) && ($args{'timeout'} > 0)) {
434 $timeout = $args{'timeout'};
437 if (defined ($args{'plugins'})) {
438 if ("ARRAY" eq ref ($args{'plugins'})) {
439 @plugins = @{$args{'plugins'}};
442 @plugins = ($args{'plugins'});
449 if (defined ($args{'identifiers'})) {
450 if ("ARRAY" eq ref ($args{'identifiers'})) {
451 @ids = @{$args{'identifiers'}};
454 @ids = ($args{'identifiers'});
461 foreach my $plugin (@plugins) {
462 foreach my $id (@ids) {
463 _plugin_flush($plugin, $timeout, $id);
475 our $cb_name = undef;
478 if (! ((defined $type) && (defined $name) && (defined $cb_type))) {
479 ERROR ("Usage: Collectd::fc_call(type, name, cb_type, ...)");
483 if (! defined $fc_plugins[$type]) {
484 ERROR ("Collectd::fc_call: Invalid type \"$type\"");
488 if (! defined $fc_plugins[$type]->{$name}) {
489 ERROR ("Collectd::fc_call: Unknown "
490 . ($type == FC_MATCH ? "match" : "target")
495 DEBUG ("Collectd::fc_call: "
496 . "type = \"$type\" (" . $fc_types{$type}
497 . "), name = \"$name\", cb_type = \"$cb_type\" ("
498 . $fc_cb_types{$cb_type} . ")");
501 lock %{$fc_plugins[$type]};
502 %proc = %{$fc_plugins[$type]->{$name}};
505 if (FC_CB_EXEC == $cb_type) {
506 $cb_name = $proc{$fc_exec_names{$type}};
508 elsif (FC_CB_CREATE == $cb_type) {
509 if (defined $proc{'create'}) {
510 $cb_name = $proc{'create'};
516 elsif (FC_CB_DESTROY == $cb_type) {
517 if (defined $proc{'destroy'}) {
518 $cb_name = $proc{'destroy'};
525 $status = call_by_name (@_);
534 $err = "callback returned false";
537 ERROR ("Execution of fc callback \"$cb_name\" failed: $err");
550 DEBUG ("Collectd::fc_register: "
551 . "type = \"$type\" (" . $fc_types{$type}
552 . "), name = \"$name\", proc = \"$proc\"");
554 if (! ((defined $type) && (defined $name) && (defined $proc))) {
555 ERROR ("Usage: Collectd::fc_register(type, name, proc)");
559 if (! defined $fc_plugins[$type]) {
560 ERROR ("Collectd::fc_register: Invalid type \"$type\"");
564 if (("HASH" ne ref ($proc)) || (! defined $proc->{$fc_exec_names{$type}})
565 || ("" ne ref ($proc->{$fc_exec_names{$type}}))) {
566 ERROR ("Collectd::fc_register: Invalid proc.");
570 for my $p (qw( create destroy )) {
571 if ((defined $proc->{$p}) && ("" ne ref ($proc->{$p}))) {
572 ERROR ("Collectd::fc_register: Invalid proc.");
579 foreach my $p (keys %fc) {
580 my $pkg = scalar caller;
582 if ($p !~ m/^(create|destroy|$fc_exec_names{$type})$/) {
586 if ($fc{$p} !~ m/^$pkg\:\:/) {
587 $fc{$p} = $pkg . "::" . $fc{$p};
591 lock %{$fc_plugins[$type]};
592 if (defined $fc_plugins[$type]->{$name}) {
593 WARNING ("Collectd::fc_register: Overwriting previous "
594 . "definition of match \"$name\".");
597 if (! _fc_register ($type, $name)) {
598 ERROR ("Collectd::fc_register: Failed to register \"$name\".");
602 $fc_plugins[$type]->{$name} = \%fc;
606 sub _plugin_dispatch_config {
610 our $cb_name = undef;
612 if (! (defined ($plugin) && defined ($config))) {
616 if (! defined $cf_callbacks{$plugin}) {
617 WARNING ("Found a configuration for the \"$plugin\" plugin, but "
618 . "the plugin isn't loaded or didn't register "
619 . "a configuration callback.");
625 $cb_name = $cf_callbacks{$plugin};
627 call_by_name ($config);
632 # vim: set sw=4 ts=4 tw=78 noexpandtab :