From 8d733398875d6cf83ba441b74eaa8fb42214c9f8 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Tue, 28 Apr 2020 10:37:09 +0200 Subject: [PATCH 1/5] Windows environment spaces in binary file names --- pandora_server/lib/PandoraFMS/Tools.pm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandora_server/lib/PandoraFMS/Tools.pm b/pandora_server/lib/PandoraFMS/Tools.pm index 10f57b1e15..c3182f9d41 100755 --- a/pandora_server/lib/PandoraFMS/Tools.pm +++ b/pandora_server/lib/PandoraFMS/Tools.pm @@ -1458,7 +1458,7 @@ sub pandora_block_ping($@) { if (-x $pa_config->{'fping'}) { # 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`; } else { # Ping scan From 744bac67362e0b431f1c268f6ae5c6e22edfe267 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Tue, 28 Apr 2020 11:21:33 +0200 Subject: [PATCH 2/5] avoid problems while encoding json --- .../lib/PandoraFMS/DiscoveryServer.pm | 26 ++++++++++++------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/pandora_server/lib/PandoraFMS/DiscoveryServer.pm b/pandora_server/lib/PandoraFMS/DiscoveryServer.pm index 8cee68d538..55764c5ddd 100644 --- a/pandora_server/lib/PandoraFMS/DiscoveryServer.pm +++ b/pandora_server/lib/PandoraFMS/DiscoveryServer.pm @@ -1684,17 +1684,23 @@ sub PandoraFMS::Recon::Base::update_progress ($$) { my ($self, $progress) = @_; my $stats = {}; - if (defined($self->{'summary'}) && $self->{'summary'} ne '') { - $stats->{'summary'} = $self->{'summary'}; + eval { + local $SIG{__DIE__}; + if (defined($self->{'summary'}) && $self->{'summary'} ne '') { + $stats->{'summary'} = $self->{'summary'}; + } + + $stats->{'step'} = $self->{'step'}; + $stats->{'c_network_name'} = $self->{'c_network_name'}; + $stats->{'c_network_percent'} = $self->{'c_network_percent'}; + + # 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($stats), $self->{'task_id'}); + }; + if ($@) { + $self->call('Message', "Problems updating progress $@"); } - - $stats->{'step'} = $self->{'step'}; - $stats->{'c_network_name'} = $self->{'c_network_name'}; - $stats->{'c_network_percent'} = $self->{'c_network_percent'}; - - # 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($stats), $self->{'task_id'}); } 1; From 3ad1b5f2e3a23b099e5064b4bd3e43798a80d0f8 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Tue, 28 Apr 2020 12:19:46 +0200 Subject: [PATCH 3/5] trace if failed to encode json (summary) --- pandora_server/lib/PandoraFMS/DiscoveryServer.pm | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pandora_server/lib/PandoraFMS/DiscoveryServer.pm b/pandora_server/lib/PandoraFMS/DiscoveryServer.pm index 55764c5ddd..631194e212 100644 --- a/pandora_server/lib/PandoraFMS/DiscoveryServer.pm +++ b/pandora_server/lib/PandoraFMS/DiscoveryServer.pm @@ -1694,12 +1694,16 @@ 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($stats), $self->{'task_id'}); + time (), $progress, encode_json(\%t), $self->{'task_id'}); }; if ($@) { - $self->call('Message', "Problems updating progress $@"); + $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'}); } } From 2bfee57a67efb1390876e5509bce9efc783dc973 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Tue, 28 Apr 2020 12:46:13 +0200 Subject: [PATCH 4/5] 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__ From dfa31d5227746e79de02e086fb0a8bd1787f7a9b Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Tue, 28 Apr 2020 13:26:57 +0200 Subject: [PATCH 5/5] updates --- pandora_server/lib/PandoraFMS/Recon/Base.pm | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pandora_server/lib/PandoraFMS/Recon/Base.pm b/pandora_server/lib/PandoraFMS/Recon/Base.pm index 4b3a920f9f..bbe7802097 100644 --- a/pandora_server/lib/PandoraFMS/Recon/Base.pm +++ b/pandora_server/lib/PandoraFMS/Recon/Base.pm @@ -2109,16 +2109,16 @@ sub snmp_get_value($$$) { my ($self, $device, $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`; - chomp($effective_oid); + $effective_oid =~ s/[\r\n]//g; } my @output = $self->snmp_get($device, $effective_oid); - + foreach my $line (@output) { - chomp($line); - return $1 if ($line =~ /^$effective_oid\s+=\s+\S+:\s+(.*)$/); + $line =~ s/[\r\n]//g; + return $1 if ($line =~ /^$effective_oid\s+=\s+\S+:\s+(.*)/); } return undef;