From: cebka@lenovo-laptop Date: Mon, 1 Mar 2010 17:39:42 +0000 (+0300) Subject: * Allow to specify hosts file for making batch operations on rspamd using rspamc... X-Git-Tag: 0.3.0~73 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=dda6a0c72bcfc98fd151df83f99cd3fbebb4ba76;p=rspamd.git * Allow to specify hosts file for making batch operations on rspamd using rspamc client --- diff --git a/rspamc.pl.in b/rspamc.pl.in index 86abbc1cd..f5a01d3ed 100755 --- a/rspamc.pl.in +++ b/rspamc.pl.in @@ -13,9 +13,7 @@ use Getopt::Std; my %cfg = ( 'conf_file' => '@CMAKE_INSTALL_PREFIX@/etc/rspamd.conf', 'command' => 'SYMBOLS', - 'host' => 'localhost', - 'port' => '11333', - 'is_unix' => 0, + 'hosts' => ['localhost:11333', ], 'require_input' => 0, 'password' => '', 'control' => 0, @@ -29,9 +27,9 @@ $main::VERSION = '@RSPAMD_VERSION@'; sub HELP_MESSAGE { print <)) { print $line; } + + return 1; } sub do_ctrl_auth { @@ -201,7 +217,8 @@ sub do_control_command { # Read greeting first if (defined (my $greeting = <$sock>)) { if ($greeting !~ /^Rspamd version/) { - die "not rspamd greeting line $greeting"; + print "Not rspamd greeting line $greeting"; + return 0; } } if ($cfg{'command'} =~ /^learn$/i) { @@ -216,14 +233,17 @@ sub do_control_command { if (defined (my $reply = <$sock>)) { if ($reply =~ /^learn ok, sum weight: ([0-9.]+)/) { print "Learn succeed. Sum weight: $1\n"; + return 1; } else { print "Learn failed\n"; + return 0; } } } else { print "Authentication failed\n"; + return 0; } } if ($cfg{'command'} =~ /^weights$/i) { @@ -249,6 +269,7 @@ sub do_control_command { } else { print "Authentication failed\n"; + return 0; } } elsif ($cfg{'command'} =~ /(fuzzy_add|fuzzy_del)/i) { @@ -260,14 +281,17 @@ sub do_control_command { if (defined (my $reply = <$sock>)) { if ($reply =~ /^OK/) { print $cfg{'command'} . " succeed\n"; + return 1; } else { print $cfg{'command'} . " failed\n"; + return 0; } } } else { print "Authentication failed\n"; + return 0; } } @@ -278,6 +302,8 @@ sub do_control_command { print $line; } } + + return 1; } sub process_file { @@ -471,17 +497,61 @@ sub process_path { # Do specified command for specified input sub do_cmd { my $input = shift; + my $res; - my $sock = connect_socket (); + print "*" x 20 . "\n"; + foreach my $hostdef (@{ $cfg{'hosts'} }) { + print "Do $cfg{command} on $hostdef\n"; + my $sock = connect_socket ($hostdef); + + if (! $sock) { + print "Result: failed (on connect stage)\n"; + print "*" x 20 . "\n"; + next; + } - if ($cfg{'control'}) { - do_control_command ($sock, $input); - } - else { - do_rspamc_command ($sock, $input); - } + if ($cfg{'control'}) { + $res = do_control_command ($sock, $input); + } + else { + $res = do_rspamc_command ($sock, $input); + } + + close ($sock); + if (! $res) { + print "Result: failed (on command stage)\n"; + } + else { + print "Result: OK\n"; + } + print "*" x 20 . "\n"; + } +} + +sub load_hosts_file { + my $file = shift; - close ($sock); + open (HOSTS, "< $file") or die "cannot open file $file"; + $cfg{'hosts'} = [ ]; + while () { + chomp; + next if $_ =~ /^\s*#/; + if ($_ =~ /^\s*(([^:]+):(\d+))\s*$/) { + push (@{ $cfg{'hosts'} }, $1); + } + elsif ($_ =~ /^\s*([^:]+)\s*$/) { + if ($cfg{'control'}) { + push (@{ $cfg{'hosts'} }, "$1:11334"); + } + else { + push (@{ $cfg{'hosts'} }, "$1:11333"); + } + } + elsif ($_ =~ /^\s*(\/\S*)\s*$/) { + push (@{ $cfg{'hosts'} }, "$1"); + } + } + close FILE; } ############################# Main part ########################################### @@ -489,7 +559,7 @@ my %args; HELP_MESSAGE() unless scalar @ARGV >= 1; -getopt('c:h:p:P:s:d:w:S:', \%args); +getopt('c:h:P:s:d:w:S:H:', \%args); my $cmd = shift; my @path = shift; @@ -522,15 +592,9 @@ if (defined ($args{s})) { } } if (defined ($args{h})) { - $cfg{'host'} = $args{h}; - if ($args{h} =~ /^\/.*$/) { - $cfg{'is_unix'} = 1; - } + $cfg{'hosts'} = [ $args{h} ]; $do_parse_config = 0; } -if (defined ($args{p})) { - $cfg{'port'} = $args{p}; -} if (defined ($args{P})) { $cfg{'password'} = $args{P}; } @@ -561,6 +625,10 @@ if ($do_parse_config != 0) { parse_config ($cfg{'control'}); } +if (defined ($args{H})) { + load_hosts_file ($args{H}); +} + if (!defined ($path[0]) || ! $cfg{'require_input'}) { my $input; if ($cfg{'require_input'}) {