From ad99ea5ee9d87b5e90e401b4a771fe5257621fea Mon Sep 17 00:00:00 2001
From: ramonn <noreply@pandorafms.org>
Date: Thu, 6 Jun 2013 18:33:38 +0000
Subject: [PATCH] 2013-06-06  Ramon Novoa  <rnovoa@artica.es>

	* lib/PandoraFMS/ReconServer.pm: Merged from 4.0 branch. Create agents
	  before trying to assign addresses.



git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@8266 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
---
 pandora_server/ChangeLog                     |  5 ++
 pandora_server/lib/PandoraFMS/ReconServer.pm | 73 +++++++++++---------
 2 files changed, 46 insertions(+), 32 deletions(-)

diff --git a/pandora_server/ChangeLog b/pandora_server/ChangeLog
index fee6d48082..6305aaa67f 100644
--- a/pandora_server/ChangeLog
+++ b/pandora_server/ChangeLog
@@ -1,3 +1,8 @@
+2013-06-06  Ramon Novoa  <rnovoa@artica.es>
+
+	* lib/PandoraFMS/ReconServer.pm: Merged from 4.0 branch. Create agents
+	  before trying to assign addresses.
+
 2013-05-28  Ramon Novoa  <rnovoa@artica.es>
 
 	* util/pandora_db.pl: Use $BIG_OPERATION_STEP for event deletion.
diff --git a/pandora_server/lib/PandoraFMS/ReconServer.pm b/pandora_server/lib/PandoraFMS/ReconServer.pm
index f3d0812e57..3b2acdfe26 100644
--- a/pandora_server/lib/PandoraFMS/ReconServer.pm
+++ b/pandora_server/lib/PandoraFMS/ReconServer.pm
@@ -126,8 +126,11 @@ sub data_consumer ($$) {
 
 	# Is it a recon script?
 	if (defined ($task->{'id_recon_script'}) && ($task->{'id_recon_script'} != 0)) {
+		logger($pa_config, 'Executing recon script ' . safe_output($task->{'name'}) . '.', 10);
 		exec_recon_script ($pa_config, $dbh, $task);
 		return;
+	} else {
+		logger($pa_config, 'Starting recon task for net ' . $task->{'subnet'} . '.', 10);
 	}
 
 	# Call nmap
@@ -175,7 +178,21 @@ sub data_consumer ($$) {
 			# Skip if not in learning mode
 			next if ($agent->{'modo'} != 1);
 		}
-		
+
+		# Get the parent host
+		my $parent_id = 0;
+		if ($task->{'parent_detection'} == 1) {
+			$parent_id = get_host_parent ($pa_config, $addr, $dbh, $task->{'id_group'}, $task->{'parent_recursion'}, $task->{'resolve_names'}, $task->{'os_detect'});
+		}
+
+		# If the agent already exists update parent and continue
+		if ($agent_id > 0) {
+			if ($parent_id > 0) {
+				db_do ($dbh, 'UPDATE tagente SET id_parent = ? WHERE id_agente = ?', $parent_id, $agent_id );
+			}
+			next;
+		}
+
 		# Filter by TCP port
 		if ((defined ($task->{'recon_ports'})) && ($task->{'recon_ports'} ne "")) {
 			next unless (tcp_scan ($pa_config, $addr, $task->{'recon_ports'}) > 0);
@@ -187,36 +204,7 @@ sub data_consumer ($$) {
 			$id_os = guess_os ($pa_config, $dbh, $addr);
 			next if ($task->{'id_os'} > 0 && $task->{'id_os'} != $id_os);
 		}
-
-		# Get the parent host
-		my $parent_id = 0;
-		if ($task->{'parent_detection'} == 1) {
-			$parent_id = get_host_parent ($pa_config, $addr, $dbh, $task->{'id_group'}, $task->{'parent_recursion'}, $task->{'resolve_names'}, $task->{'os_detect'});
-		}
 		
-		# Add the new address if it does not exist
-		my $addr_id = get_addr_id ($dbh, $addr);
-		$addr_id = add_address ($dbh, $addr) unless ($addr_id > 0);
-		if ($addr_id <= 0) {
-			logger($pa_config, "Could not add address '$addr' for host '$host_name'.", 3);
-			next;
-		}
-
-		# Assign the new address to the agent
-		my $agent_addr_id = get_agent_addr_id ($dbh, $addr_id, $agent_id);
-		if ($agent_addr_id <= 0) {
-			db_do ($dbh, 'INSERT INTO taddress_agent (`id_a`, `id_agent`)
-		                  VALUES (?, ?)', $addr_id, $agent_id);
-		}
-
-		# If the agent already exists update parent and continue
-		if ($agent_id > 0) {
-			if ($parent_id > 0) {
-				db_do ($dbh, 'UPDATE tagente SET id_parent = ? WHERE id_agente = ?', $parent_id, $agent_id );
-			}
-			next;
-		}
-
         # GIS Code -----------------------------
 
 		# If GIS is activated try to geolocate the ip address of the agent 
@@ -280,6 +268,21 @@ sub data_consumer ($$) {
 			next;
 		}
 
+		# Add the new address if it does not exist
+		my $addr_id = get_addr_id ($dbh, $addr);
+		$addr_id = add_address ($dbh, $addr) unless ($addr_id > 0);
+		if ($addr_id <= 0) {
+			logger($pa_config, "Could not add address '$addr' for host '$host_name'.", 3);
+			next;
+		}
+
+		# Assign the new address to the agent
+		my $agent_addr_id = get_agent_addr_id ($dbh, $addr_id, $agent_id);
+		if ($agent_addr_id <= 0) {
+			db_do ($dbh, 'INSERT INTO taddress_agent (`id_a`, `id_agent`)
+		                  VALUES (?, ?)', $addr_id, $agent_id);
+		}
+
 		# Create network profile modules for the agent
 		create_network_profile_modules ($pa_config, $dbh, $agent_id, $task->{'id_network_profile'}, $addr, $task->{'snmp_community'});
 		
@@ -299,7 +302,9 @@ sub data_consumer ($$) {
 		$text .= "\n\nThis is the list of IP addresses found: \n\n$added_hosts";
 		pandora_create_incident ($pa_config, $dbh, "[RECON] New hosts detected", $text, 0, 0, 'Pandora FMS Recon Server', $task->{'id_group'});
 	}
-	
+
+	logger($pa_config, "Finished recon task for net " . $task->{'subnet'} . ".", 10);
+
 	# Mark recon task as done
 	update_recon_task ($dbh, $task_id, -1);
 }
@@ -476,7 +481,11 @@ sub exec_recon_script ($$$) {
 	my $field3 = safe_output($task->{'field3'}); 
 	my $field4 = safe_output($task->{'field4'});
 	
-	`$command $task->{'id_rt'} $task->{'id_group'} $task->{'create_incident'} $field1 $field2 $field3 $field4`;
+	if (-x $command) {
+		`$command $task->{'id_rt'} $task->{'id_group'} $task->{'create_incident'} $field1 $field2 $field3 $field4`;
+	} else {
+		logger ($pa_config, "Cannot execute recon task command $command.");
+	}
 	
 	# Notify this recon task is ended
 	update_recon_task ($dbh, $task->{'id_rt'}, -1);