1 # collectd - Collectd.pm
2 # Copyright (C) 2007, 2008 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 = ();
122 TYPE_SHUTDOWN, "shutdown",
124 TYPE_NOTIF, "notify",
133 my %fc_exec_names = (
138 foreach my $type (keys %types) {
139 $plugins[$type] = &share ({});
142 foreach my $type (keys %fc_types) {
143 $fc_plugins[$type] = &share ({});
151 if ("Collectd" eq $caller) {
154 return plugin_log ($lvl, $msg);
157 sub ERROR { _log (scalar caller, LOG_ERR, shift); }
158 sub WARNING { _log (scalar caller, LOG_WARNING, shift); }
159 sub NOTICE { _log (scalar caller, LOG_NOTICE, shift); }
160 sub INFO { _log (scalar caller, LOG_INFO, shift); }
161 sub DEBUG { _log (scalar caller, LOG_DEBUG, shift); }
163 sub plugin_call_all {
168 our $cb_name = undef;
170 if (! defined $type) {
174 if (TYPE_LOG != $type) {
175 DEBUG ("Collectd::plugin_call: type = \"$type\", args=\"@_\"");
178 if (! defined $plugins[$type]) {
179 ERROR ("Collectd::plugin_call: unknown type \"$type\"");
184 lock %{$plugins[$type]};
185 %plugins = %{$plugins[$type]};
188 foreach my $plugin (keys %plugins) {
189 my $p = $plugins{$plugin};
193 if ($p->{'wait_left'} > 0) {
194 $p->{'wait_left'} -= $interval_g;
197 next if ($p->{'wait_left'} > 0);
199 $cb_name = $p->{'cb_name'};
200 $status = call_by_name (@_);
209 $err = "callback returned false";
212 if (TYPE_LOG != $type) {
213 ERROR ("Execution of callback \"$cb_name\" failed: $err");
220 $p->{'wait_left'} = 0;
221 $p->{'wait_time'} = $interval_g;
223 elsif (TYPE_READ == $type) {
224 if ($p->{'wait_time'} < $interval_g) {
225 $p->{'wait_time'} = $interval_g;
228 $p->{'wait_left'} = $p->{'wait_time'};
229 $p->{'wait_time'} *= 2;
231 if ($p->{'wait_time'} > 86400) {
232 $p->{'wait_time'} = 86400;
235 WARNING ("${plugin}->read() failed with status $status. "
236 . "Will suspend it for $p->{'wait_left'} seconds.");
238 elsif (TYPE_INIT == $type) {
239 ERROR ("${plugin}->init() failed with status $status. "
240 . "Plugin will be disabled.");
242 foreach my $type (keys %types) {
243 plugin_unregister ($type, $plugin);
246 elsif (TYPE_LOG != $type) {
247 WARNING ("${plugin}->$types{$type}() failed with status $status.");
253 # Collectd::plugin_register (type, name, data).
256 # init, read, write, shutdown, data set
262 # reference to the plugin's subroutine that does the work or the data set
264 sub plugin_register {
269 DEBUG ("Collectd::plugin_register: "
270 . "type = \"$type\", name = \"$name\", data = \"$data\"");
272 if (! ((defined $type) && (defined $name) && (defined $data))) {
273 ERROR ("Usage: Collectd::plugin_register (type, name, data)");
277 if ((! defined $plugins[$type]) && (TYPE_DATASET != $type)
278 && (TYPE_CONFIG != $type)) {
279 ERROR ("Collectd::plugin_register: Invalid type \"$type\"");
283 if ((TYPE_DATASET == $type) && ("ARRAY" eq ref $data)) {
284 return plugin_register_data_set ($name, $data);
286 elsif ((TYPE_CONFIG == $type) && (! ref $data)) {
287 my $pkg = scalar caller;
289 if ($data !~ m/^$pkg\:\:/) {
290 $data = $pkg . "::" . $data;
294 $cf_callbacks{$name} = $data;
296 elsif ((TYPE_DATASET != $type) && (! ref $data)) {
297 my $pkg = scalar caller;
301 if ($data !~ m/^$pkg\:\:/) {
302 $data = $pkg . "::" . $data;
306 wait_time => $interval_g,
311 lock %{$plugins[$type]};
312 $plugins[$type]->{$name} = \%p;
315 ERROR ("Collectd::plugin_register: Invalid data.");
321 sub plugin_unregister {
325 DEBUG ("Collectd::plugin_unregister: type = \"$type\", name = \"$name\"");
327 if (! ((defined $type) && (defined $name))) {
328 ERROR ("Usage: Collectd::plugin_unregister (type, name)");
332 if (TYPE_DATASET == $type) {
333 return plugin_unregister_data_set ($name);
335 elsif (TYPE_CONFIG == $type) {
337 delete $cf_callbacks{$name};
339 elsif (defined $plugins[$type]) {
340 lock %{$plugins[$type]};
341 delete $plugins[$type]->{$name};
344 ERROR ("Collectd::plugin_unregister: Invalid type.");
356 if (! defined $args{'valuelists'}) {
357 ERROR ("Collectd::plugin_write: Missing 'valuelists' argument.");
361 DEBUG ("Collectd::plugin_write:"
362 . (defined ($args{'plugins'}) ? " plugins = $args{'plugins'}" : "")
363 . (defined ($args{'datasets'}) ? " datasets = $args{'datasets'}" : "")
364 . " valueslists = $args{'valuelists'}");
366 if (defined ($args{'plugins'})) {
367 if ("ARRAY" eq ref ($args{'plugins'})) {
368 @plugins = @{$args{'plugins'}};
371 @plugins = ($args{'plugins'});
378 if ("ARRAY" eq ref ($args{'valuelists'})) {
379 @valuelists = @{$args{'valuelists'}};
382 @valuelists = ($args{'valuelists'});
385 if (defined ($args{'datasets'})) {
386 if ("ARRAY" eq ref ($args{'datasets'})) {
387 @datasets = @{$args{'datasets'}};
390 @datasets = ($args{'datasets'});
394 @datasets = (undef) x scalar (@valuelists);
397 if ($#datasets != $#valuelists) {
398 ERROR ("Collectd::plugin_write: Invalid number of datasets.");
402 foreach my $plugin (@plugins) {
403 for (my $i = 0; $i < scalar (@valuelists); ++$i) {
404 _plugin_write ($plugin, $datasets[$i], $valuelists[$i]);
416 DEBUG ("Collectd::plugin_flush:"
417 . (defined ($args{'timeout'}) ? " timeout = $args{'timeout'}" : "")
418 . (defined ($args{'plugins'}) ? " plugins = $args{'plugins'}" : "")
419 . (defined ($args{'identifiers'})
420 ? " identifiers = $args{'identifiers'}" : ""));
422 if (defined ($args{'timeout'}) && ($args{'timeout'} > 0)) {
423 $timeout = $args{'timeout'};
426 if (defined ($args{'plugins'})) {
427 if ("ARRAY" eq ref ($args{'plugins'})) {
428 @plugins = @{$args{'plugins'}};
431 @plugins = ($args{'plugins'});
438 if (defined ($args{'identifiers'})) {
439 if ("ARRAY" eq ref ($args{'identifiers'})) {
440 @ids = @{$args{'identifiers'}};
443 @ids = ($args{'identifiers'});
450 foreach my $plugin (@plugins) {
451 foreach my $id (@ids) {
452 _plugin_flush($plugin, $timeout, $id);
457 sub plugin_flush_one {
461 WARNING ("Collectd::plugin_flush_one is deprecated - "
462 . "use Collectd::plugin_flush instead.");
464 if (! (defined ($timeout) && defined ($name))) {
465 ERROR ("Usage: Collectd::plugin_flush_one(timeout, name)");
469 plugin_flush (plugins => $name, timeout => $timeout);
472 sub plugin_flush_all {
475 WARNING ("Collectd::plugin_flush_all is deprecated - "
476 . "use Collectd::plugin_flush instead.");
478 if (! defined ($timeout)) {
479 ERROR ("Usage: Collectd::plugin_flush_all(timeout)");
483 plugin_flush (timeout => $timeout);
493 our $cb_name = undef;
496 if (! ((defined $type) && (defined $name) && (defined $cb_type))) {
497 ERROR ("Usage: Collectd::fc_call(type, name, cb_type, ...)");
501 if (! defined $fc_plugins[$type]) {
502 ERROR ("Collectd::fc_call: Invalid type \"$type\"");
506 if (! defined $fc_plugins[$type]->{$name}) {
507 ERROR ("Collectd::fc_call: Unknown "
508 . ($type == FC_MATCH ? "match" : "target")
513 DEBUG ("Collectd::fc_call: "
514 . "type = \"$type\", name = \"$name\", cb_type = \"$cb_type\"");
517 lock %{$fc_plugins[$type]};
518 %proc = %{$fc_plugins[$type]->{$name}};
521 if (FC_CB_EXEC == $cb_type) {
522 $cb_name = $proc{$fc_exec_names{$type}};
524 elsif (FC_CB_CREATE == $cb_type) {
525 if (defined $proc{'create'}) {
526 $cb_name = $proc{'create'};
532 elsif (FC_CB_DESTROY == $cb_type) {
533 if (defined $proc{'destroy'}) {
534 $cb_name = $proc{'destroy'};
541 $status = call_by_name (@_);
550 $err = "callback returned false";
553 ERROR ("Execution of fc callback \"$cb_name\" failed: $err");
566 DEBUG ("Collectd::fc_register: "
567 . "type = \"$type\", name = \"$name\", proc = \"$proc\"");
569 if (! ((defined $type) && (defined $name) && (defined $proc))) {
570 ERROR ("Usage: Collectd::fc_register(type, name, proc)");
574 if (! defined $fc_plugins[$type]) {
575 ERROR ("Collectd::fc_register: Invalid type \"$type\"");
579 if (("HASH" ne ref ($proc)) || (! defined $proc->{$fc_exec_names{$type}})
580 || ("" ne ref ($proc->{$fc_exec_names{$type}}))) {
581 ERROR ("Collectd::fc_register: Invalid proc.");
585 for my $p (qw( create destroy )) {
586 if ((defined $proc->{$p}) && ("" ne ref ($proc->{$p}))) {
587 ERROR ("Collectd::fc_register: Invalid proc.");
594 foreach my $p (keys %fc) {
595 my $pkg = scalar caller;
597 if ($p !~ m/^(create|destroy|$fc_exec_names{$type})$/) {
601 if ($fc{$p} !~ m/^$pkg\:\:/) {
602 $fc{$p} = $pkg . "::" . $fc{$p};
606 lock %{$fc_plugins[$type]};
607 if (defined $fc_plugins[$type]->{$name}) {
608 WARNING ("Collectd::fc_register: Overwriting previous "
609 . "definition of match \"$name\".");
612 if (! _fc_register ($type, $name)) {
613 ERROR ("Collectd::fc_register: Failed to register \"$name\".");
617 $fc_plugins[$type]->{$name} = \%fc;
621 sub _plugin_dispatch_config {
625 our $cb_name = undef;
627 if (! (defined ($plugin) && defined ($config))) {
631 if (! defined $cf_callbacks{$plugin}) {
632 WARNING ("Found a configuration for the \"$plugin\" plugin, but "
633 . "the plugin isn't loaded or didn't register "
634 . "a configuration callback.");
640 $cb_name = $cf_callbacks{$plugin};
642 call_by_name ($config);
647 # vim: set sw=4 ts=4 tw=78 noexpandtab :