[collectd] [PATCH] SA plugin: Added reconnect support (collectd_retries), code cleanup

ALexander Wirt formorer<span style="display: none;">.trailing-username</span>(a)<span style="display: none;">leading-domain.</span>formorer.de
Sat Dec 9 19:43:25 CET 2006


The plugin now tries to reconnect if connection isn't possible
(default is 3 tries), this can adjusted with the collectd_retries
config parameter.

Signed-off-by: Alexander Wirt <formorer<span style="display: none;">.trailing-username</span>(a)<span style="display: none;">leading-domain.</span>formorer.de>
---
 contrib/SpamAssassin/Collectd.pm |   39 ++++++++++++++++++++++++++-----------
 1 files changed, 27 insertions(+), 12 deletions(-)

diff --git a/contrib/SpamAssassin/Collectd.pm b/contrib/SpamAssassin/Collectd.pm
index efc356f..292d4d0 100644
--- a/contrib/SpamAssassin/Collectd.pm
+++ b/contrib/SpamAssassin/Collectd.pm
@@ -1,5 +1,4 @@
 #!/usr/bin/perl
-# $Id: Collectd.pm 7 2006-12-07 06:13:12Z formorer $
 
 =head1 NAME
 
@@ -68,6 +67,7 @@ use Mail::SpamAssassin::Logger;
 use strict;
 use bytes; 
 use warnings;
+use Time::HiRes qw(usleep);
 use IO::Socket;
 
 use vars qw(@ISA);
@@ -112,6 +112,14 @@ sub set_config {
 			$Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC,
 	});
 
+	push (@cmds, {
+			setting => 'collectd_retries',
+			default => 3,
+			type =>
+			$Mail::SpamAssassin::Conf::CONF_TYPE_NUMERIC,
+	});
+
+
     $conf->{parser}->register_commands(\@cmds);
 }
 
@@ -120,20 +128,25 @@ sub check_end {
     my $message_status = $params->{permsgstatus};
 	#create  new connection to our socket
 	eval {
-		local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
+		local $SIG{ALRM} = sub { die "Sending to collectd timed out.\n" }; # NB: \n required
 
 		#generate a timeout
 		alarm $self->{main}->{conf}->{collectd_timeout};
 
-		my $sock = new IO::Socket::UNIX ( $self->{main}->{conf}->{collectd_socket});
-		# debug some informations if collectd is not running or anything else went
-		# wrong
-		if ( ! $sock ) {
-			dbg("collect: could not connect to " .
-				$self->{main}->{conf}->{collectd_socket} . ": $! - collectd plugin
-				disabled"); 
-			return 0; 
+		my $sock;
+		#try at least $self->{main}->{conf}->{collectd_retries} to get a
+		#connection
+		for (my $i = 0; $i < $self->{main}->{conf}->{collectd_retries} ; ++$i) {
+			last if $sock = new IO::Socket::UNIX
+				($self->{main}->{conf}->{collectd_socket});
+			#sleep a random value between 0 and 50 microsecs to try for a new
+			#thread
+			usleep(int(rand(50))); 
 		}
+
+		die("could not connect to " .
+				$self->{main}->{conf}->{collectd_socket} . ": $! - collectd plugin disabled") unless $sock; 
+
 		$sock->autoflush(1);
 
 		my $score = $message_status->{score};
@@ -177,8 +190,10 @@ sub check_end {
 		close($sock); 
 		alarm 0; 
 	};
-	if ($@ eq "alarm\n") {
-		info("Connection to collectd timed out");
+	if ($@) {
+		my $message = $@; 
+		chomp($message); 
+		info("collectd: $message");
 		return -1; 
 	}
 }
-- 
1.4.4.1

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mailman.verplant.org/pipermail/collectd/attachments/20061209/73e7017b/attachment.pgp


More information about the collectd mailing list