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 {
177 our $cb_name = undef;
179 if (! defined $type) {
183 if (TYPE_LOG != $type) {
184 DEBUG ("Collectd::plugin_call: type = \"$type\" ("
185 . $types{$type} . "), args=\""
186 . join(', ', map { defined($_) ? $_ : '<undef>' } @_) . "\"");
189 if (! defined $plugins[$type]) {
190 ERROR ("Collectd::plugin_call: unknown type \"$type\"");
195 lock %{$plugins[$type]};
196 %plugins = %{$plugins[$type]};
199 $interval = plugin_get_interval ();
201 foreach my $plugin (keys %plugins) {
202 my $p = $plugins{$plugin};
206 if ($p->{'wait_left'} > 0) {
207 $p->{'wait_left'} -= $interval;
210 next if ($p->{'wait_left'} > 0);
212 $cb_name = $p->{'cb_name'};
213 $status = call_by_name (@_);
222 $err = "callback returned false";
225 if (TYPE_LOG != $type) {
226 ERROR ("Execution of callback \"$cb_name\" failed: $err");
233 $p->{'wait_left'} = 0;
234 $p->{'wait_time'} = $interval;
236 elsif (TYPE_READ == $type) {
237 if ($p->{'wait_time'} < $interval) {
238 $p->{'wait_time'} = $interval;
241 $p->{'wait_left'} = $p->{'wait_time'};
242 $p->{'wait_time'} *= 2;
244 if ($p->{'wait_time'} > 86400) {
245 $p->{'wait_time'} = 86400;
248 WARNING ("${plugin}->read() failed with status $status. "
249 . "Will suspend it for $p->{'wait_left'} seconds.");
251 elsif (TYPE_INIT == $type) {
252 ERROR ("${plugin}->init() failed with status $status. "
253 . "Plugin will be disabled.");
255 foreach my $type (keys %types) {
256 plugin_unregister ($type, $plugin);
259 elsif (TYPE_LOG != $type) {
260 WARNING ("${plugin}->$types{$type}() failed with status $status.");
266 # Collectd::plugin_register (type, name, data).
269 # init, read, write, shutdown, data set
275 # reference to the plugin's subroutine that does the work or the data set
277 sub plugin_register {
282 DEBUG ("Collectd::plugin_register: "
283 . "type = \"$type\" (" . $types{$type}
284 . "), name = \"$name\", data = \"$data\"");
286 if (! ((defined $type) && (defined $name) && (defined $data))) {
287 ERROR ("Usage: Collectd::plugin_register (type, name, data)");
291 if ((! defined $plugins[$type]) && (TYPE_DATASET != $type)
292 && (TYPE_CONFIG != $type)) {
293 ERROR ("Collectd::plugin_register: Invalid type \"$type\"");
297 if ((TYPE_DATASET == $type) && ("ARRAY" eq ref $data)) {
298 return plugin_register_data_set ($name, $data);
300 elsif ((TYPE_CONFIG == $type) && (! ref $data)) {
301 my $pkg = scalar caller;
303 if ($data !~ m/^$pkg\:\:/) {
304 $data = $pkg . "::" . $data;
308 $cf_callbacks{$name} = $data;
310 elsif ((TYPE_DATASET != $type) && (! ref $data)) {
311 my $pkg = scalar caller;
315 if ($data !~ m/^$pkg\:\:/) {
316 $data = $pkg . "::" . $data;
320 wait_time => plugin_get_interval (),
325 lock %{$plugins[$type]};
326 $plugins[$type]->{$name} = \%p;
329 ERROR ("Collectd::plugin_register: Invalid data.");
335 sub plugin_unregister {
339 DEBUG ("Collectd::plugin_unregister: type = \"$type\" ("
340 . $types{$type} . "), name = \"$name\"");
342 if (! ((defined $type) && (defined $name))) {
343 ERROR ("Usage: Collectd::plugin_unregister (type, name)");
347 if (TYPE_DATASET == $type) {
348 return plugin_unregister_data_set ($name);
350 elsif (TYPE_CONFIG == $type) {
352 delete $cf_callbacks{$name};
354 elsif (defined $plugins[$type]) {
355 lock %{$plugins[$type]};
356 delete $plugins[$type]->{$name};
359 ERROR ("Collectd::plugin_unregister: Invalid type.");
371 if (! defined $args{'valuelists'}) {
372 ERROR ("Collectd::plugin_write: Missing 'valuelists' argument.");
376 DEBUG ("Collectd::plugin_write:"
377 . (defined ($args{'plugins'}) ? " plugins = $args{'plugins'}" : "")
378 . (defined ($args{'datasets'}) ? " datasets = $args{'datasets'}" : "")
379 . " valueslists = $args{'valuelists'}");
381 if (defined ($args{'plugins'})) {
382 if ("ARRAY" eq ref ($args{'plugins'})) {
383 @plugins = @{$args{'plugins'}};
386 @plugins = ($args{'plugins'});
393 if ("ARRAY" eq ref ($args{'valuelists'})) {
394 @valuelists = @{$args{'valuelists'}};
397 @valuelists = ($args{'valuelists'});
400 if (defined ($args{'datasets'})) {
401 if ("ARRAY" eq ref ($args{'datasets'})) {
402 @datasets = @{$args{'datasets'}};
405 @datasets = ($args{'datasets'});
409 @datasets = (undef) x scalar (@valuelists);
412 if ($#datasets != $#valuelists) {
413 ERROR ("Collectd::plugin_write: Invalid number of datasets.");
417 foreach my $plugin (@plugins) {
418 for (my $i = 0; $i < scalar (@valuelists); ++$i) {
419 _plugin_write ($plugin, $datasets[$i], $valuelists[$i]);
431 DEBUG ("Collectd::plugin_flush:"
432 . (defined ($args{'timeout'}) ? " timeout = $args{'timeout'}" : "")
433 . (defined ($args{'plugins'}) ? " plugins = $args{'plugins'}" : "")
434 . (defined ($args{'identifiers'})
435 ? " identifiers = $args{'identifiers'}" : ""));
437 if (defined ($args{'timeout'}) && ($args{'timeout'} > 0)) {
438 $timeout = $args{'timeout'};
441 if (defined ($args{'plugins'})) {
442 if ("ARRAY" eq ref ($args{'plugins'})) {
443 @plugins = @{$args{'plugins'}};
446 @plugins = ($args{'plugins'});
453 if (defined ($args{'identifiers'})) {
454 if ("ARRAY" eq ref ($args{'identifiers'})) {
455 @ids = @{$args{'identifiers'}};
458 @ids = ($args{'identifiers'});
465 foreach my $plugin (@plugins) {
466 foreach my $id (@ids) {
467 _plugin_flush($plugin, $timeout, $id);
479 our $cb_name = undef;
482 if (! ((defined $type) && (defined $name) && (defined $cb_type))) {
483 ERROR ("Usage: Collectd::fc_call(type, name, cb_type, ...)");
487 if (! defined $fc_plugins[$type]) {
488 ERROR ("Collectd::fc_call: Invalid type \"$type\"");
492 if (! defined $fc_plugins[$type]->{$name}) {
493 ERROR ("Collectd::fc_call: Unknown "
494 . ($type == FC_MATCH ? "match" : "target")
499 DEBUG ("Collectd::fc_call: "
500 . "type = \"$type\" (" . $fc_types{$type}
501 . "), name = \"$name\", cb_type = \"$cb_type\" ("
502 . $fc_cb_types{$cb_type} . ")");
505 lock %{$fc_plugins[$type]};
506 %proc = %{$fc_plugins[$type]->{$name}};
509 if (FC_CB_EXEC == $cb_type) {
510 $cb_name = $proc{$fc_exec_names{$type}};
512 elsif (FC_CB_CREATE == $cb_type) {
513 if (defined $proc{'create'}) {
514 $cb_name = $proc{'create'};
520 elsif (FC_CB_DESTROY == $cb_type) {
521 if (defined $proc{'destroy'}) {
522 $cb_name = $proc{'destroy'};
529 $status = call_by_name (@_);
538 $err = "callback returned false";
541 ERROR ("Execution of fc callback \"$cb_name\" failed: $err");
554 DEBUG ("Collectd::fc_register: "
555 . "type = \"$type\" (" . $fc_types{$type}
556 . "), name = \"$name\", proc = \"$proc\"");
558 if (! ((defined $type) && (defined $name) && (defined $proc))) {
559 ERROR ("Usage: Collectd::fc_register(type, name, proc)");
563 if (! defined $fc_plugins[$type]) {
564 ERROR ("Collectd::fc_register: Invalid type \"$type\"");
568 if (("HASH" ne ref ($proc)) || (! defined $proc->{$fc_exec_names{$type}})
569 || ("" ne ref ($proc->{$fc_exec_names{$type}}))) {
570 ERROR ("Collectd::fc_register: Invalid proc.");
574 for my $p (qw( create destroy )) {
575 if ((defined $proc->{$p}) && ("" ne ref ($proc->{$p}))) {
576 ERROR ("Collectd::fc_register: Invalid proc.");
583 foreach my $p (keys %fc) {
584 my $pkg = scalar caller;
586 if ($p !~ m/^(create|destroy|$fc_exec_names{$type})$/) {
590 if ($fc{$p} !~ m/^$pkg\:\:/) {
591 $fc{$p} = $pkg . "::" . $fc{$p};
595 lock %{$fc_plugins[$type]};
596 if (defined $fc_plugins[$type]->{$name}) {
597 WARNING ("Collectd::fc_register: Overwriting previous "
598 . "definition of match \"$name\".");
601 if (! _fc_register ($type, $name)) {
602 ERROR ("Collectd::fc_register: Failed to register \"$name\".");
606 $fc_plugins[$type]->{$name} = \%fc;
610 sub _plugin_dispatch_config {
614 our $cb_name = undef;
616 if (! (defined ($plugin) && defined ($config))) {
620 if (! defined $cf_callbacks{$plugin}) {
621 WARNING ("Found a configuration for the \"$plugin\" plugin, but "
622 . "the plugin isn't loaded or didn't register "
623 . "a configuration callback.");
629 $cb_name = $cf_callbacks{$plugin};
631 call_by_name ($config);
636 # vim: set sw=4 ts=4 tw=78 noexpandtab :