X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=contrib%2FSpamAssassin%2FCollectd.pm;h=b53cf953fcf201f7fb21c98bf830afda70e8908b;hb=96b920e6b7bf70560eb95c911202afbeaab2b965;hp=6f1c0dc719f0823c5e6ab06f7d2a2df837250ce2;hpb=5571810a9095ac2bc3e681910b6e08a5ed3bc3e8;p=collectd.git diff --git a/contrib/SpamAssassin/Collectd.pm b/contrib/SpamAssassin/Collectd.pm index 6f1c0dc7..b53cf953 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 @@ -19,7 +18,7 @@ add a loadplugin call into your init.pre file. =over 4 -=item collectd_socket [ socket path ] (default: /tmp/.collectd-email) +=item collectd_socket [ socket path ] (default: /var/run/collectd-email) Where the collectd socket is @@ -33,6 +32,21 @@ If you have changed this setting please get it in sync with the SA Plugin config. =cut + +=item collectd_timeout [ sec ] (default: 2) + +if sending data to to collectd takes too long the connection will be aborted. + +=cut + +=item collectd_retries [ tries ] (default: 3) + +the collectd plugin uses a tread pool, if this is empty the connection fails, +the SA Plugin then tries to reconnect. With this variable you can indicate how +often it should try. + +=cut + =head1 DESCRIPTION This modules uses the email plugin of collectd from Sebastian Harl to @@ -55,8 +69,7 @@ Alexander Wirt or - b) the "Artistic License" which comes with perl - (http://www.perl.com/pub/a/language/misc/Artistic.html) + b) the GPL (http://www.gnu.org/copyleft/gpl.html) use whatever you like more. @@ -69,6 +82,7 @@ use Mail::SpamAssassin::Logger; use strict; use bytes; use warnings; +use Time::HiRes qw(usleep); use IO::Socket; use vars qw(@ISA); @@ -102,7 +116,7 @@ sub set_config { push (@cmds, { setting => 'collectd_socket', - default => '/tmp/.collectd-email', + default => '/var/run/collectd-email', type => $Mail::SpamAssassin::Conf::CONF_TYPE_STRING, }); @@ -113,6 +127,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); } @@ -121,20 +143,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 - die "alarm\n"; + 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) { + my ($socket_path) = $self->{main}->{conf}->{collectd_socket} =~ /(.*)/; # Untaint path, which can contain any characters. + last if $sock = new IO::Socket::UNIX $socket_path; + #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}; @@ -178,8 +205,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; } }