Merge branch '375_Agente_xml_buffer_y_modo_secundario_always' into 'develop'

fixed errors in agent buffered always

See merge request !686
This commit is contained in:
vgilc 2017-07-24 11:11:01 +02:00
commit 65a1ebae77

View File

@ -906,14 +906,13 @@ sub fix_directory ($) {
################################################################################ ################################################################################
# Sends a file to the server. # Sends a file to the server.
################################################################################ ################################################################################
#sub send_file ($;$) { #sub send_file ($;$$$) {
sub send_file { sub send_file {
my ($file, $secondary) = @_; my ($file, $secondary, $rc_primary, $flag_always) = @_;
my $output; my $output;
my $pid = fork(); my $pid = fork();
return 1 unless defined $pid; return 1 unless defined $pid;
if ($pid == 0) { if ($pid == 0) {
# execute the transfer program by child process. # execute the transfer program by child process.
eval { eval {
@ -942,14 +941,14 @@ sub send_file {
}; };
if ($@) { 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; exit 1;
} }
# Get the errorlevel # Get the errorlevel
my $rc = $? >> 8; my $rc = $? >> 8;
if ($rc != 0) { 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; exit $rc;
} }
@ -958,39 +957,104 @@ sub send_file {
waitpid ($pid, 0); waitpid ($pid, 0);
my $rc = $? >> 8; 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;
return $rc unless ($Conf{'secondary_mode'} eq 'always' || ($Conf{'secondary_mode'} eq 'on_error' && $rc != 0)); }
else{
swap_servers (); my $rc_secondary = 0;
$rc = send_file ($file); if( ($rc != 0) && ($file =~ /\.data/)){
swap_servers (); $rc_secondary = 1;
return $rc; }
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. # 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 # Read XML files from the temporal directory
opendir(TEMPORAL, $Conf{'temporal'}) or return; opendir(TEMPORAL, $temporal_file) or return;
while (my $xml_file = readdir(TEMPORAL)) { if (defined($flag_always) && ($flag_always == 2)){
swap_servers ();
}
while (my $xml_file = readdir(TEMPORAL)) {
# Skip non data files and symlinks # Skip non data files and symlinks
next if ($xml_file !~ m/^$Conf{'agent_name'}\.[0-9]+\.data$/ || -l "$Conf{'temporal'}/$xml_file"); 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);
my $rc = send_file ("$Conf{'temporal'}/$xml_file", 1);
if ($rc == 0) { if ($rc == 0) {
if ($Conf{'debug'} eq '1') { 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 { } 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 # Send buffered XML data files
if ($Conf{'xml_buffer'} == 1) { 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'});
}
} }
} }