From 2bfee57a67efb1390876e5509bce9efc783dc973 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Tue, 28 Apr 2020 12:46:13 +0200 Subject: [PATCH] perl multilib json workaround --- .../lib/PandoraFMS/DiscoveryServer.pm | 16 +++--- pandora_server/lib/PandoraFMS/Tools.pm | 54 +++++++++++++++++++ 2 files changed, 61 insertions(+), 9 deletions(-) diff --git a/pandora_server/lib/PandoraFMS/DiscoveryServer.pm b/pandora_server/lib/PandoraFMS/DiscoveryServer.pm index 631194e212..b8b6f228e9 100644 --- a/pandora_server/lib/PandoraFMS/DiscoveryServer.pm +++ b/pandora_server/lib/PandoraFMS/DiscoveryServer.pm @@ -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); diff --git a/pandora_server/lib/PandoraFMS/Tools.pm b/pandora_server/lib/PandoraFMS/Tools.pm index c3182f9d41..4b1486f0ef 100755 --- a/pandora_server/lib/PandoraFMS/Tools.pm +++ b/pandora_server/lib/PandoraFMS/Tools.pm @@ -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__