diff --git a/pandora_agents/unix/pandora_agent b/pandora_agents/unix/pandora_agent index c76059472b..b1d4e9ef1d 100755 --- a/pandora_agents/unix/pandora_agent +++ b/pandora_agents/unix/pandora_agent @@ -906,14 +906,13 @@ sub fix_directory ($) { ################################################################################ # Sends a file to the server. ################################################################################ -#sub send_file ($;$) { +#sub send_file ($;$$$) { sub send_file { - my ($file, $secondary) = @_; + my ($file, $secondary, $rc_primary, $flag_always) = @_; my $output; - my $pid = fork(); return 1 unless defined $pid; - + if ($pid == 0) { # execute the transfer program by child process. eval { @@ -942,14 +941,14 @@ sub send_file { }; if ($@) { - log_message ('error', "Error sending file '$file': File transfer command is not responding."); + log_message ('error', "Error sending file '$file' to '" . $Conf{'server_ip'} . ":" . $Conf{'server_port'}. "': File transfer command is not responding."); exit 1; } # Get the errorlevel my $rc = $? >> 8; if ($rc != 0) { - log_message ('error', "Error sending file '$file': $output"); + log_message ('error', "Error sending file '$file' to '" . $Conf{'server_ip'} . ":" . $Conf{'server_port'}. "': $output"); } exit $rc; } @@ -958,39 +957,104 @@ sub send_file { waitpid ($pid, 0); my $rc = $? >> 8; - return $rc unless (defined ($secondary)); + if( ($Conf{'secondary_mode'} eq 'always') && ( !defined($flag_always) ) ){ + # Send the file to the secondary server + return $rc unless ($Conf{'secondary_mode'} eq 'always'); + + if(defined ($secondary)){ + if( ($rc != 0 && ($file =~ /\.data/)) ){ + $rc_primary = 1; + } + swap_servers (); + $rc = send_file ($file, undef, $rc_primary); + swap_servers (); - # Send the file to the secondary server - return $rc unless ($Conf{'secondary_mode'} eq 'always' || ($Conf{'secondary_mode'} eq 'on_error' && $rc != 0)); - - swap_servers (); - $rc = send_file ($file); - swap_servers (); - return $rc; + return $rc; + } + else{ + my $rc_secondary = 0; + if( ($rc != 0) && ($file =~ /\.data/)){ + $rc_secondary = 1; + } + + if ( $rc_secondary == 1 && defined($rc_primary) ){ + return 1; + } + + if ( $rc_secondary == 1 ){ + if (! -d "$Conf{'temporal'}/secondary"){ + mkdir "$Conf{'temporal'}/secondary"; + } + eval { + copy("$file", "$Conf{'temporal'}/secondary/"); + }; + if ($@) { + # We shouldn't reach this point... + die ("Cannot write on $Conf{'temporal'}/secondary/"); + } + return 0; + } + + if ( defined($rc_primary) ){ + if (! -d "$Conf{'temporal'}/primary"){ + mkdir "$Conf{'temporal'}/primary"; + } + eval { + copy("$file", "$Conf{'temporal'}/primary/"); + }; + if ($@) { + # We shouldn't reach this point... + die ("Cannot write on $Conf{'temporal'}/primary/"); + } + return 0; + } + + if ( $rc_secondary == 0 && !defined($rc_primary) ){ + return 0; + } + } + } + elsif ( ($Conf{'secondary_mode'} eq 'always') && defined($flag_always) ){ + return $rc; + } + else{ + return $rc unless (defined ($secondary)); + + # Send the file to the secondary server + return $rc unless ($Conf{'secondary_mode'} eq 'always' || ($Conf{'secondary_mode'} eq 'on_error' && $rc != 0)); + + swap_servers (); + $rc = send_file ($file); + swap_servers (); + return $rc; + } } ################################################################################ # Send buffered XML files. ################################################################################ -sub send_buffered_xml_files () { - +sub send_buffered_xml_files ($;$) { + my ($temporal_file, $flag_always) = @_; # Read XML files from the temporal directory - opendir(TEMPORAL, $Conf{'temporal'}) or return; - while (my $xml_file = readdir(TEMPORAL)) { - + opendir(TEMPORAL, $temporal_file) or return; + if (defined($flag_always) && ($flag_always == 2)){ + swap_servers (); + } + while (my $xml_file = readdir(TEMPORAL)) { # Skip non data files and symlinks - next if ($xml_file !~ m/^$Conf{'agent_name'}\.[0-9]+\.data$/ || -l "$Conf{'temporal'}/$xml_file"); - - my $rc = send_file ("$Conf{'temporal'}/$xml_file", 1); - + next if ($xml_file !~ m/^$Conf{'agent_name'}\.[0-9]+\.data$/ || -l "$temporal_file/$xml_file"); + my $rc = send_file ("$temporal_file/$xml_file", 1, undef, $flag_always); if ($rc == 0) { if ($Conf{'debug'} eq '1') { - rename "$Conf{'temporal'}/$xml_file", "$Conf{'temporal'}/$xml_file". "sent"; + rename "$temporal_file/$xml_file", "$temporal_file/$xml_file". "sent"; } else { - unlink ("$Conf{'temporal'}/$xml_file"); + unlink ("$temporal_file/$xml_file"); } } } + if (defined($flag_always) && ($flag_always == 2)){ + swap_servers (); + } } ################################################################################ @@ -2769,7 +2833,20 @@ while (1) { # Send buffered XML data files if ($Conf{'xml_buffer'} == 1) { - send_buffered_xml_files (); + if($Conf{'secondary_mode'} eq 'always'){ + $Conf{'__temporal_primary'} = "$Conf{'temporal'}/primary"; + $Conf{'__temporal_secondary'} = "$Conf{'temporal'}/secondary"; + if (-d "$Conf{'__temporal_primary'}"){ + send_buffered_xml_files ($Conf{'__temporal_primary'}, 1); + } + if (-d "$Conf{'__temporal_secondary'}"){ + send_buffered_xml_files ($Conf{'__temporal_secondary'}, 2); + } + send_buffered_xml_files ($Conf{'temporal'}); + } + else{ + send_buffered_xml_files ($Conf{'temporal'}); + } } }