perl multilib json workaround

This commit is contained in:
fbsanchez 2020-04-28 12:46:13 +02:00
parent 3ad1b5f2e3
commit 2bfee57a67
2 changed files with 61 additions and 9 deletions

View File

@ -26,7 +26,7 @@ use Thread::Semaphore;
use IO::Socket::INET;
use POSIX qw(strftime ceil);
use JSON qw(decode_json encode_json);
use JSON;
use Encode qw(encode_utf8);
use MIME::Base64;
@ -336,14 +336,14 @@ sub exec_recon_script ($$$) {
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/\r/\\r/g;
my $decoded_macros;
if ($macros) {
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;
eval {
local $SIG{__DIE__};
$data = decode_json(decode_base64($row->{'data'}));
$data = p_decode_json($self->{'pa_config'}, decode_base64($row->{'data'}));
};
if ($@) {
$self->call('message', "ERROR JSON: $@", 3);
@ -1245,7 +1245,7 @@ sub PandoraFMS::Recon::Base::report_scanned_agents($;$) {
eval {
local $SIG{__DIE__};
$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 {
local $SIG{__DIE__};
$encoded = encode_base64(
encode_json($self->{'agents_found'}->{$addr})
p_encode_json($self->{'pa_config'}, $self->{'agents_found'}->{$addr})
);
};
@ -1694,11 +1694,9 @@ sub PandoraFMS::Recon::Base::update_progress ($$) {
$stats->{'c_network_name'} = $self->{'c_network_name'};
$stats->{'c_network_percent'} = $self->{'c_network_percent'};
my %t = %{$stats};
# Store progress, last contact and overall status.
db_do ($self->{'dbh'}, 'UPDATE trecon_task SET utimestamp = ?, status = ?, summary = ? WHERE id_rt = ?',
time (), $progress, encode_json(\%t), $self->{'task_id'});
time (), $progress, p_encode_json($self->{'pa_config'}, $stats), $self->{'task_id'});
};
if ($@) {
$self->call('message', "Problems updating progress $@", 5);

View File

@ -31,6 +31,9 @@ use LWP::UserAgent;
use threads;
use threads::shared;
use JSON;
use Encode qw/decode_utf8 encode_utf8/;
use lib '/usr/lib/perl5';
use PandoraFMS::Sendmail;
@ -152,6 +155,8 @@ our @EXPORT = qw(
dateTimeToTimestamp
get_user_agent
ui_get_full_url
p_encode_json
p_decode_json
);
# ID of the different servers
@ -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;
__END__