2014-08-06 Ramon Novoa <rnovoa@artica.es>

* lib/PandoraFMS/NetworkServer.pm: Rewrote TCP module socket code for
	  sending data (tcp_send). Fixes ticket #1096.



git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@10385 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
This commit is contained in:
ramonn 2014-08-06 15:48:59 +00:00
parent 9b7a8cec71
commit ded9780100
2 changed files with 90 additions and 81 deletions

View File

@ -1,3 +1,8 @@
2014-08-06 Ramon Novoa <rnovoa@artica.es>
* lib/PandoraFMS/NetworkServer.pm: Rewrote TCP module socket code for
sending data (tcp_send). Fixes ticket #1096.
2014-08-05 Hirofumi Kosaka <kosaka@rworks.jp> 2014-08-05 Hirofumi Kosaka <kosaka@rworks.jp>
* lib/PandoraFMS/Core.pm: Suppress "going to normal (warning, * lib/PandoraFMS/Core.pm: Suppress "going to normal (warning,

View File

@ -25,6 +25,7 @@ use threads::shared;
use Thread::Semaphore; use Thread::Semaphore;
use IO::Socket::INET6; use IO::Socket::INET6;
use IO::Select;
use HTML::Entities; use HTML::Entities;
use POSIX qw(strftime); use POSIX qw(strftime);
@ -170,100 +171,103 @@ sub pandora_query_tcp ($$$$$$$$$$) {
$retries = $pa_config->{'tcp_checks'}; $retries = $pa_config->{'tcp_checks'};
} }
$tcp_send = decode_entities($tcp_send); $tcp_send = decode_entities($tcp_send);
$tcp_rcv = decode_entities($tcp_rcv); $tcp_rcv = decode_entities($tcp_rcv);
my $counter; my $counter;
for ($counter =0; $counter < $retries; $counter++){ for ($counter =0; $counter < $retries; $counter++){
my $temp; my $temp2; my $temp; my $temp2;
my $tam; my $tam;
my $handle=IO::Socket::INET6->new( my $handle=IO::Socket::INET6->new(
Proto=>"tcp", Proto=>"tcp",
PeerAddr=>$ip_target, PeerAddr=>$ip_target,
Timeout=>$timeout, Timeout=>$timeout,
PeerPort=>$tcp_port, PeerPort=>$tcp_port,
Multihomed=>1, Multihomed=>1,
Blocking=>0 ); # Non blocking !!, very important ! Blocking=>0 ); # Non blocking !!, very important !
if (defined ($handle)){ if (defined ($handle)){
# Multi request patch, submitted by Glen Eustace (new zealand) # Multi request patch, submitted by Glen Eustace (new zealand)
my @tcp_send = split( /\|/, $tcp_send ); my @tcp_send = split( /\|/, $tcp_send );
my @tcp_rcv = split( /\|/, $tcp_rcv ); my @tcp_rcv = split( /\|/, $tcp_rcv );
# Add server socket to select queue
my $select = IO::Select->new ();
$select->add ($handle);
next_pair: next_pair:
$tcp_send = shift( @tcp_send ); $tcp_send = shift( @tcp_send );
$tcp_rcv = shift( @tcp_rcv ); $tcp_rcv = shift( @tcp_rcv );
if ((defined ($tcp_send)) && ($tcp_send ne "")){ # its Expected to sending data ? if ((defined ($tcp_send)) && ($tcp_send ne "")){ # its Expected to sending data ?
# Send data # Send data
$handle->autoflush(1); $handle->autoflush(1);
$tcp_send =~ s/\^M/\r\n/g; $tcp_send =~ s/\^M/\r\n/g;
# Replace Carriage rerturn and line feed # Replace Carriage rerturn and line feed
$handle->send($tcp_send); $handle->send($tcp_send);
} }
# we expect to receive data ? (non proc types) # we expect to receive data ? (non proc types)
if ((defined ($tcp_rcv) && $tcp_rcv ne "") || (($id_tipo_modulo == 10) || ($id_tipo_modulo ==8) || ($id_tipo_modulo == 11))) { if ((defined ($tcp_rcv) && $tcp_rcv ne "") || (($id_tipo_modulo == 10) || ($id_tipo_modulo ==8) || ($id_tipo_modulo == 11))) {
# Receive data, non-blocking !!!! (VERY IMPORTANT!) # Receive data, non-blocking !!!! (VERY IMPORTANT!)
$temp2 = ""; $temp2 = "";
for ($tam=0; $tam<$timeout; $tam++){ for ($tam = 0; $tam < $timeout; $tam ++) {
$handle->recv($temp,16000,0x40); if ($select->can_read (1)) {
$temp2 = $temp2.$temp; my $read = sysread ($handle, $temp, 16000);
if ($temp ne ""){ last if (! defined ($read) || $read == 0); # No more data or something went wrong
$tam++; # If doesnt receive data, increase counter $temp2 = $temp2.$temp;
} }
sleep(1); }
} if ($id_tipo_modulo == 9){ # only for TCP Proc
if ($id_tipo_modulo == 9){ # only for TCP Proc if ($temp2 =~ /$tcp_rcv/i){ # String match !
if ($temp2 =~ /$tcp_rcv/i){ # String match !
if ( @tcp_send ) { # still more pairs if ( @tcp_send ) { # still more pairs
goto next_pair; goto next_pair;
} }
$$module_data = 1; $$module_data = 1;
$$module_result = 0; $$module_result = 0;
$counter = $retries; $counter = $retries;
} else { } else {
$$module_data = 0; $$module_data = 0;
$$module_result = 0; $$module_result = 0;
$counter = $retries; $counter = $retries;
} }
} elsif ($id_tipo_modulo == 10 ){ # TCP String (no int conversion)! } elsif ($id_tipo_modulo == 10 ){ # TCP String (no int conversion)!
$$module_data = $temp2; $$module_data = $temp2;
$$module_result =0; $$module_result =0;
} else { # TCP Data numeric (inc or data) } else { # TCP Data numeric (inc or data)
if ($temp2 ne ""){ if ($temp2 ne ""){
if ($temp2 =~ /[A-Za-z\.\,\-\/\\\(\)\[\]]/){ if ($temp2 =~ /[A-Za-z\.\,\-\/\\\(\)\[\]]/){
$$module_result = 1; $$module_result = 1;
$$module_data = 0; # invalid data $$module_data = 0; # invalid data
$counter = $retries; $counter = $retries;
} else { } else {
$$module_data = int($temp2); $$module_data = int($temp2);
$$module_result = 0; # Successful $$module_result = 0; # Successful
$counter = $retries; $counter = $retries;
} }
} else { } else {
$$module_result = 1; $$module_result = 1;
$$module_data = 0; # invalid data $$module_data = 0; # invalid data
$counter = $retries; $counter = $retries;
} }
} }
} else { # No expected data to receive, if connected and tcp_proc type successful } else { # No expected data to receive, if connected and tcp_proc type successful
if ($id_tipo_modulo == 9){ # TCP Proc if ($id_tipo_modulo == 9){ # TCP Proc
$$module_result = 0; $$module_result = 0;
$$module_data = 1; $$module_data = 1;
$counter = $retries; $counter = $retries;
} }
} }
$handle->close(); $handle->close();
undef ($handle); undef ($handle);
} else { # Cannot connect (open sock failed) } else { # Cannot connect (open sock failed)
$$module_result = 1; # Fail $$module_result = 1; # Fail
if ($id_tipo_modulo == 9){ # TCP Proc if ($id_tipo_modulo == 9){ # TCP Proc
$$module_result = 0; $$module_result = 0;
$$module_data = 0; # Failed, but data exists $$module_data = 0; # Failed, but data exists
$counter = $retries; $counter = $retries;
} }
} }
} }
} }
############################################################################### ###############################################################################