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 = ();
122 TYPE_SHUTDOWN, "shutdown",
124 TYPE_NOTIF, "notify",
133 my %fc_exec_names = (
140 FC_CB_CREATE, "create",
141 FC_CB_DESTROY, "destroy"
144 foreach my $type (keys %types) {
145 $plugins[$type] = &share ({});
148 foreach my $type (keys %fc_types) {
149 $fc_plugins[$type] = &share ({});
157 if ("Collectd" eq $caller) {
160 return plugin_log ($lvl, $msg);
163 sub ERROR { _log (scalar caller, LOG_ERR, shift); }
164 sub WARNING { _log (scalar caller, LOG_WARNING, shift); }
165 sub NOTICE { _log (scalar caller, LOG_NOTICE, shift); }
166 sub INFO { _log (scalar caller, LOG_INFO, shift); }
167 sub DEBUG { _log (scalar caller, LOG_DEBUG, shift); }
169 sub plugin_call_all {
174 our $cb_name = undef;
176 if (! defined $type) {
180 if (TYPE_LOG != $type) {
181 DEBUG ("Collectd::plugin_call: type = \"$type\" ("
182 . $types{$type} . "), args=\""
183 . join(', ', map { defined($_) ? $_ : '<undef>' } @_) . "\"");
186 if (! defined $plugins[$type]) {
187 ERROR ("Collectd::plugin_call: unknown type \"$type\"");
192 lock %{$plugins[$type]};
193 %plugins = %{$plugins[$type]};
196 foreach my $plugin (keys %plugins) {
197 my $p = $plugins{$plugin};
201 if ($p->{'wait_left'} > 0) {
202 $p->{'wait_left'} -= $interval_g;
205 next if ($p->{'wait_left'} > 0);
207 $cb_name = $p->{'cb_name'};
208 $status = call_by_name (@_);
217 $err = "callback returned false";
220 if (TYPE_LOG != $type) {
221 ERROR ("Execution of callback \"$cb_name\" failed: $err");
228 $p->{'wait_left'} = 0;
229 $p->{'wait_time'} = $interval_g;
231 elsif (TYPE_READ == $type) {
232 if ($p->{'wait_time'} < $interval_g) {
233 $p->{'wait_time'} = $interval_g;
236 $p->{'wait_left'} = $p->{'wait_time'};
237 $p->{'wait_time'} *= 2;
239 if ($p->{'wait_time'} > 86400) {
240 $p->{'wait_time'} = 86400;
243 WARNING ("${plugin}->read() failed with status $status. "
244 . "Will suspend it for $p->{'wait_left'} seconds.");
246 elsif (TYPE_INIT == $type) {
247 ERROR ("${plugin}->init() failed with status $status. "
248 . "Plugin will be disabled.");
250 foreach my $type (keys %types) {
251 plugin_unregister ($type, $plugin);
254 elsif (TYPE_LOG != $type) {
255 WARNING ("${plugin}->$types{$type}() failed with status $status.");
261 # Collectd::plugin_register (type, name, data).
264 # init, read, write, shutdown, data set
270 # reference to the plugin's subroutine that does the work or the data set
272 sub plugin_register {
277 DEBUG ("Collectd::plugin_register: "
278 . "type = \"$type\" (" . $types{$type}
279 . "), name = \"$name\", data = \"$data\"");
281 if (! ((defined $type) && (defined $name) && (defined $data))) {
282 ERROR ("Usage: Collectd::plugin_register (type, name, data)");
286 if ((! defined $plugins[$type]) && (TYPE_DATASET != $type)
287 && (TYPE_CONFIG != $type)) {
288 ERROR ("Collectd::plugin_register: Invalid type \"$type\"");
292 if ((TYPE_DATASET == $type) && ("ARRAY" eq ref $data)) {
293 return plugin_register_data_set ($name, $data);
295 elsif ((TYPE_CONFIG == $type) && (! ref $data)) {
296 my $pkg = scalar caller;
298 if ($data !~ m/^$pkg\:\:/) {
299 $data = $pkg . "::" . $data;
303 $cf_callbacks{$name} = $data;
305 elsif ((TYPE_DATASET != $type) && (! ref $data)) {
306 my $pkg = scalar caller;
310 if ($data !~ m/^$pkg\:\:/) {
311 $data = $pkg . "::" . $data;
315 wait_time => $interval_g,
320 lock %{$plugins[$type]};
321 $plugins[$type]->{$name} = \%p;
324 ERROR ("Collectd::plugin_register: Invalid data.");
330 sub plugin_unregister {
334 DEBUG ("Collectd::plugin_unregister: type = \"$type\" ("
335 . $types{$type} . "), name = \"$name\"");
337 if (! ((defined $type) && (defined $name))) {
338 ERROR ("Usage: Collectd::plugin_unregister (type, name)");
342 if (TYPE_DATASET == $type) {
343 return plugin_unregister_data_set ($name);
345 elsif (TYPE_CONFIG == $type) {
347 delete $cf_callbacks{$name};
349 elsif (defined $plugins[$type]) {
350 lock %{$plugins[$type]};
351 delete $plugins[$type]->{$name};
354 ERROR ("Collectd::plugin_unregister: Invalid type.");
366 if (! defined $args{'valuelists'}) {
367 ERROR ("Collectd::plugin_write: Missing 'valuelists' argument.");
371 DEBUG ("Collectd::plugin_write:"
372 . (defined ($args{'plugins'}) ? " plugins = $args{'plugins'}" : "")
373 . (defined ($args{'datasets'}) ? " datasets = $args{'datasets'}" : "")
374 . " valueslists = $args{'valuelists'}");
376 if (defined ($args{'plugins'})) {
377 if ("ARRAY" eq ref ($args{'plugins'})) {
378 @plugins = @{$args{'plugins'}};
381 @plugins = ($args{'plugins'});
388 if ("ARRAY" eq ref ($args{'valuelists'})) {
389 @valuelists = @{$args{'valuelists'}};
392 @valuelists = ($args{'valuelists'});
395 if (defined ($args{'datasets'})) {
396 if ("ARRAY" eq ref ($args{'datasets'})) {
397 @datasets = @{$args{'datasets'}};
400 @datasets = ($args{'datasets'});
404 @datasets = (undef) x scalar (@valuelists);
407 if ($#datasets != $#valuelists) {
408 ERROR ("Collectd::plugin_write: Invalid number of datasets.");
412 foreach my $plugin (@plugins) {
413 for (my $i = 0; $i < scalar (@valuelists); ++$i) {
414 _plugin_write ($plugin, $datasets[$i], $valuelists[$i]);
426 DEBUG ("Collectd::plugin_flush:"
427 . (defined ($args{'timeout'}) ? " timeout = $args{'timeout'}" : "")
428 . (defined ($args{'plugins'}) ? " plugins = $args{'plugins'}" : "")
429 . (defined ($args{'identifiers'})
430 ? " identifiers = $args{'identifiers'}" : ""));
432 if (defined ($args{'timeout'}) && ($args{'timeout'} > 0)) {
433 $timeout = $args{'timeout'};
436 if (defined ($args{'plugins'})) {
437 if ("ARRAY" eq ref ($args{'plugins'})) {
438 @plugins = @{$args{'plugins'}};
441 @plugins = ($args{'plugins'});
448 if (defined ($args{'identifiers'})) {
449 if ("ARRAY" eq ref ($args{'identifiers'})) {
450 @ids = @{$args{'identifiers'}};
453 @ids = ($args{'identifiers'});
460 foreach my $plugin (@plugins) {
461 foreach my $id (@ids) {
462 _plugin_flush($plugin, $timeout, $id);
467 sub plugin_flush_one {
471 WARNING ("Collectd::plugin_flush_one is deprecated - "
472 . "use Collectd::plugin_flush instead.");
474 if (! (defined ($timeout) && defined ($name))) {
475 ERROR ("Usage: Collectd::plugin_flush_one(timeout, name)");
479 plugin_flush (plugins => $name, timeout => $timeout);
482 sub plugin_flush_all {
485 WARNING ("Collectd::plugin_flush_all is deprecated - "
486 . "use Collectd::plugin_flush instead.");
488 if (! defined ($timeout)) {
489 ERROR ("Usage: Collectd::plugin_flush_all(timeout)");
493 plugin_flush (timeout => $timeout);
503 our $cb_name = undef;
506 if (! ((defined $type) && (defined $name) && (defined $cb_type))) {
507 ERROR ("Usage: Collectd::fc_call(type, name, cb_type, ...)");
511 if (! defined $fc_plugins[$type]) {
512 ERROR ("Collectd::fc_call: Invalid type \"$type\"");
516 if (! defined $fc_plugins[$type]->{$name}) {
517 ERROR ("Collectd::fc_call: Unknown "
518 . ($type == FC_MATCH ? "match" : "target")
523 DEBUG ("Collectd::fc_call: "
524 . "type = \"$type\" (" . $fc_types{$type}
525 . "), name = \"$name\", cb_type = \"$cb_type\" ("
526 . $fc_cb_types{$cb_type} . ")");
529 lock %{$fc_plugins[$type]};
530 %proc = %{$fc_plugins[$type]->{$name}};
533 if (FC_CB_EXEC == $cb_type) {
534 $cb_name = $proc{$fc_exec_names{$type}};
536 elsif (FC_CB_CREATE == $cb_type) {
537 if (defined $proc{'create'}) {
538 $cb_name = $proc{'create'};
544 elsif (FC_CB_DESTROY == $cb_type) {
545 if (defined $proc{'destroy'}) {
546 $cb_name = $proc{'destroy'};
553 $status = call_by_name (@_);
562 $err = "callback returned false";
565 ERROR ("Execution of fc callback \"$cb_name\" failed: $err");
578 DEBUG ("Collectd::fc_register: "
579 . "type = \"$type\" (" . $fc_types{$type}
580 . "), name = \"$name\", proc = \"$proc\"");
582 if (! ((defined $type) && (defined $name) && (defined $proc))) {
583 ERROR ("Usage: Collectd::fc_register(type, name, proc)");
587 if (! defined $fc_plugins[$type]) {
588 ERROR ("Collectd::fc_register: Invalid type \"$type\"");
592 if (("HASH" ne ref ($proc)) || (! defined $proc->{$fc_exec_names{$type}})
593 || ("" ne ref ($proc->{$fc_exec_names{$type}}))) {
594 ERROR ("Collectd::fc_register: Invalid proc.");
598 for my $p (qw( create destroy )) {
599 if ((defined $proc->{$p}) && ("" ne ref ($proc->{$p}))) {
600 ERROR ("Collectd::fc_register: Invalid proc.");
607 foreach my $p (keys %fc) {
608 my $pkg = scalar caller;
610 if ($p !~ m/^(create|destroy|$fc_exec_names{$type})$/) {
614 if ($fc{$p} !~ m/^$pkg\:\:/) {
615 $fc{$p} = $pkg . "::" . $fc{$p};
619 lock %{$fc_plugins[$type]};
620 if (defined $fc_plugins[$type]->{$name}) {
621 WARNING ("Collectd::fc_register: Overwriting previous "
622 . "definition of match \"$name\".");
625 if (! _fc_register ($type, $name)) {
626 ERROR ("Collectd::fc_register: Failed to register \"$name\".");
630 $fc_plugins[$type]->{$name} = \%fc;
634 sub _plugin_dispatch_config {
638 our $cb_name = undef;
640 if (! (defined ($plugin) && defined ($config))) {
644 if (! defined $cf_callbacks{$plugin}) {
645 WARNING ("Found a configuration for the \"$plugin\" plugin, but "
646 . "the plugin isn't loaded or didn't register "
647 . "a configuration callback.");
653 $cb_name = $cf_callbacks{$plugin};
655 call_by_name ($config);
660 # vim: set sw=4 ts=4 tw=78 noexpandtab :