Collectd.pm: Improved error handling of failed callbacks.
authorSebastian Harl <sh@tokkee.org>
Sat, 17 Nov 2007 17:45:58 +0000 (18:45 +0100)
committerFlorian Forster <octo@huhu.verplant.org>
Tue, 20 Nov 2007 08:04:43 +0000 (09:04 +0100)
If call_by_name() failed, it will be handled the same way as if the callback
returned "false". The $@ variable may now be used by plugins as well to
describe errors in greater detail.

Signed-off-by: Sebastian Harl <sh@tokkee.org>
Signed-off-by: Florian Forster <octo@huhu.verplant.org>
bindings/perl/Collectd.pm
src/collectd-perl.pod

index 8c3159c..2de9885 100644 (file)
@@ -143,12 +143,21 @@ sub plugin_call_all {
                $cb_name = $p->{'cb_name'};
                $status = call_by_name (@_);
 
-               if (! defined $status) {
+               if (! $status) {
+                       my $err = undef;
+
+                       if ($@) {
+                               $err = $@;
+                       }
+                       else {
+                               $err = "callback returned false";
+                       }
+
                        if (TYPE_LOG != $type) {
-                               ERROR ("Could not execute callback \"$cb_name\": $@");
+                               ERROR ("Execution of callback \"$cb_name\" failed: $err");
                        }
 
-                       next;
+                       $status = 0;
                }
 
                if ($status) {
@@ -156,23 +165,23 @@ sub plugin_call_all {
                        $p->{'wait_time'} = 10;
                }
                elsif (TYPE_READ == $type) {
+                       WARNING ("${plugin}->read() failed with status $status. "
+                               . "Will suspend it for $p->{'wait_left'} seconds.");
+
                        $p->{'wait_left'} = $p->{'wait_time'};
                        $p->{'wait_time'} *= 2;
 
                        if ($p->{'wait_time'} > 86400) {
                                $p->{'wait_time'} = 86400;
                        }
-
-                       WARNING ("${plugin}->read() failed with status $status. "
-                               . "Will suspend it for $p->{'wait_left'} seconds.");
                }
                elsif (TYPE_INIT == $type) {
+                       ERROR ("${plugin}->init() failed with status $status. "
+                               . "Plugin will be disabled.");
+
                        foreach my $type (keys %types) {
                                plugin_unregister ($type, $plugin);
                        }
-
-                       ERROR ("${plugin}->init() failed with status $status. "
-                               . "Plugin will be disabled.");
                }
                elsif (TYPE_LOG != $type) {
                        WARNING ("${plugin}->$types{$type}() failed with status $status.");
index 2b77667..73bacc6 100644 (file)
@@ -103,6 +103,9 @@ be used to clean up the plugin (e.g. close sockets, ...).
 
 =back
 
+Any function may set the B<$@> variable to describe errors in more detail. The
+message will be passed on to the user using collectd's logging mechanism.
+
 See the documentation of the B<plugin_register> method in the section
 "METHODS" below for the number and types of arguments passed to each
 B<callback function>. This section also explains how to register B<callback