Merge branch 'ent-5575-rebuild-recon-y-modulos-con-pen' into 'develop'

Windows environment spaces in binary file names

See merge request artica/pandorafms!3188
This commit is contained in:
Alejandro Fraguas 2020-04-28 14:15:14 +02:00
commit 75f5df874b
3 changed files with 84 additions and 22 deletions

View File

@ -26,7 +26,7 @@ use Thread::Semaphore;
use IO::Socket::INET; use IO::Socket::INET;
use POSIX qw(strftime ceil); use POSIX qw(strftime ceil);
use JSON qw(decode_json encode_json); use JSON;
use Encode qw(encode_utf8); use Encode qw(encode_utf8);
use MIME::Base64; use MIME::Base64;
@ -336,14 +336,14 @@ sub exec_recon_script ($$$) {
my $macros = safe_output($task->{'macros'}); my $macros = safe_output($task->{'macros'});
# \r and \n should be escaped for decode_json(). # \r and \n should be escaped for p_decode_json().
$macros =~ s/\n/\\n/g; $macros =~ s/\n/\\n/g;
$macros =~ s/\r/\\r/g; $macros =~ s/\r/\\r/g;
my $decoded_macros; my $decoded_macros;
if ($macros) { if ($macros) {
eval { eval {
$decoded_macros = decode_json(encode_utf8($macros)); $decoded_macros = p_decode_json($pa_config, $macros);
}; };
} }
@ -980,7 +980,7 @@ sub PandoraFMS::Recon::Base::report_scanned_agents($;$) {
my $data; my $data;
eval { eval {
local $SIG{__DIE__}; local $SIG{__DIE__};
$data = decode_json(decode_base64($row->{'data'})); $data = p_decode_json($self->{'pa_config'}, decode_base64($row->{'data'}));
}; };
if ($@) { if ($@) {
$self->call('message', "ERROR JSON: $@", 3); $self->call('message', "ERROR JSON: $@", 3);
@ -1245,7 +1245,7 @@ sub PandoraFMS::Recon::Base::report_scanned_agents($;$) {
eval { eval {
local $SIG{__DIE__}; local $SIG{__DIE__};
$encoded = encode_base64( $encoded = encode_base64(
encode_json($data) p_encode_json($self->{'pa_config'}, $data)
); );
}; };
@ -1344,7 +1344,7 @@ sub PandoraFMS::Recon::Base::report_scanned_agents($;$) {
eval { eval {
local $SIG{__DIE__}; local $SIG{__DIE__};
$encoded = encode_base64( $encoded = encode_base64(
encode_json($self->{'agents_found'}->{$addr}) p_encode_json($self->{'pa_config'}, $self->{'agents_found'}->{$addr})
); );
}; };
@ -1684,6 +1684,8 @@ sub PandoraFMS::Recon::Base::update_progress ($$) {
my ($self, $progress) = @_; my ($self, $progress) = @_;
my $stats = {}; my $stats = {};
eval {
local $SIG{__DIE__};
if (defined($self->{'summary'}) && $self->{'summary'} ne '') { if (defined($self->{'summary'}) && $self->{'summary'} ne '') {
$stats->{'summary'} = $self->{'summary'}; $stats->{'summary'} = $self->{'summary'};
} }
@ -1694,7 +1696,13 @@ sub PandoraFMS::Recon::Base::update_progress ($$) {
# Store progress, last contact and overall status. # Store progress, last contact and overall status.
db_do ($self->{'dbh'}, 'UPDATE trecon_task SET utimestamp = ?, status = ?, summary = ? WHERE id_rt = ?', db_do ($self->{'dbh'}, 'UPDATE trecon_task SET utimestamp = ?, status = ?, summary = ? WHERE id_rt = ?',
time (), $progress, encode_json($stats), $self->{'task_id'}); time (), $progress, p_encode_json($self->{'pa_config'}, $stats), $self->{'task_id'});
};
if ($@) {
$self->call('message', "Problems updating progress $@", 5);
db_do ($self->{'dbh'}, 'UPDATE trecon_task SET utimestamp = ?, status = ?, summary = ? WHERE id_rt = ?',
time (), $progress, "{}", $self->{'task_id'});
}
} }
1; 1;

View File

@ -2109,16 +2109,16 @@ sub snmp_get_value($$$) {
my ($self, $device, $oid) = @_; my ($self, $device, $oid) = @_;
my $effective_oid = $oid; my $effective_oid = $oid;
if (is_enabled($self->{'translate_snmp'})) { if (is_enabled($self->{'translate_snmp'}) && $oid !~ /^[\.\d]+$/) {
$effective_oid = `snmptranslate $oid -On 2>$DEVNULL`; $effective_oid = `snmptranslate $oid -On 2>$DEVNULL`;
chomp($effective_oid); $effective_oid =~ s/[\r\n]//g;
} }
my @output = $self->snmp_get($device, $effective_oid); my @output = $self->snmp_get($device, $effective_oid);
foreach my $line (@output) { foreach my $line (@output) {
chomp($line); $line =~ s/[\r\n]//g;
return $1 if ($line =~ /^$effective_oid\s+=\s+\S+:\s+(.*)$/); return $1 if ($line =~ /^$effective_oid\s+=\s+\S+:\s+(.*)/);
} }
return undef; return undef;

View File

@ -31,6 +31,9 @@ use LWP::UserAgent;
use threads; use threads;
use threads::shared; use threads::shared;
use JSON;
use Encode qw/decode_utf8 encode_utf8/;
use lib '/usr/lib/perl5'; use lib '/usr/lib/perl5';
use PandoraFMS::Sendmail; use PandoraFMS::Sendmail;
@ -152,6 +155,8 @@ our @EXPORT = qw(
dateTimeToTimestamp dateTimeToTimestamp
get_user_agent get_user_agent
ui_get_full_url ui_get_full_url
p_encode_json
p_decode_json
); );
# ID of the different servers # ID of the different servers
@ -1458,7 +1463,7 @@ sub pandora_block_ping($@) {
if (-x $pa_config->{'fping'}) { if (-x $pa_config->{'fping'}) {
# fping timeout in milliseconds # fping timeout in milliseconds
$cmd = $pa_config->{'fping'} . " -a -q -t " . (1000 * $pa_config->{'networktimeout'}) . " " . (join (' ', @hosts)); $cmd = '"'.$pa_config->{'fping'} . '" -a -q -t ' . (1000 * $pa_config->{'networktimeout'}) . " " . (join (' ', @hosts));
@output = `$cmd 2>$DEVNULL`; @output = `$cmd 2>$DEVNULL`;
} else { } else {
# Ping scan # Ping scan
@ -2353,6 +2358,55 @@ sub ui_get_full_url {
} }
################################################################################
# Encodes a json.
################################################################################
sub p_encode_json {
my ($pa_config, $data) = @_;
# Initialize JSON manager.
my $json = JSON->new->allow_nonref;
my $encoded_data;
eval {
local $SIG{__DIE__};
if ($JSON::VERSION > 2.90) {
$encoded_data = $json->encode($data);
} else {
$encoded_data = encode_utf8($json->encode($data));
}
};
if ($@){
if (defined($data)) {
logger($pa_config, 'Failed to encode data: '.$@, 5);
}
}
return $encoded_data;
}
################################################################################
# Dencodes a json.
################################################################################
sub p_decode_json {
my ($pa_config, $data) = @_;
# Initialize JSON manager.
my $json = JSON->new->allow_nonref;
my $decoded_data;
if ($JSON::VERSION > 2.90) {
$decoded_data = $json->decode($data);
} else {
if (!is_empty($decoded_data)) {
$decoded_data = decode_json($data);
}
}
return $decoded_data;
}
1; 1;
__END__ __END__