From f679cec04cc7ff206b4ba385635b251b57174668 Mon Sep 17 00:00:00 2001
From: mdtrooper <tres.14159@gmail.com>
Date: Fri, 25 Jul 2014 13:15:24 +0000
Subject: [PATCH] 2014-07-25  Miguel de Dios <miguel.dedios@artica.es>

	* lib/PandoraFMS/GIS.pm, lib/PandoraFMS/DB.pm,
	lib/PandoraFMS/Core.pm, lib/PandoraFMS/ReconServer.pm,
	lib/PandoraFMS/DataServer.pm: tiny fixes for to improve the support
	of PostgreSQL.




git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@10356 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
---
 pandora_server/lib/PandoraFMS/Core.pm        | 129 +++++++++++++------
 pandora_server/lib/PandoraFMS/DB.pm          |  99 ++++++++------
 pandora_server/lib/PandoraFMS/DataServer.pm  |  33 ++---
 pandora_server/lib/PandoraFMS/GIS.pm         |  24 +++-
 pandora_server/lib/PandoraFMS/ReconServer.pm |  30 +++--
 5 files changed, 198 insertions(+), 117 deletions(-)

diff --git a/pandora_server/lib/PandoraFMS/Core.pm b/pandora_server/lib/PandoraFMS/Core.pm
index 267f31276e..1a92f16988 100644
--- a/pandora_server/lib/PandoraFMS/Core.pm
+++ b/pandora_server/lib/PandoraFMS/Core.pm
@@ -1201,8 +1201,10 @@ sub pandora_planned_downtime_disabled_once_stop($$) {
 	# Stop executed downtimes (enable agents and disable_agents_alerts)
 	my @downtimes = get_db_rows($dbh, 'SELECT *
 		FROM tplanned_downtime
-		WHERE type_downtime != "quiet" AND type_execution="once"
-			AND executed = 1 AND date_to <= ?', $utimestamp);
+		WHERE type_downtime != ' . $RDBMS_QUOTE_STRING. 'quiet' . $RDBMS_QUOTE_STRING. '
+			AND type_execution = ' . $RDBMS_QUOTE_STRING. 'once' . $RDBMS_QUOTE_STRING. '
+			AND executed = 1
+			AND date_to <= ?', $utimestamp);
 	
 	foreach my $downtime (@downtimes) {
 		
@@ -1234,7 +1236,8 @@ sub pandora_planned_downtime_disabled_once_start($$) {
 	# Start pending downtimes (disable agents and disable_agents_alerts)
 	my @downtimes = get_db_rows($dbh, 'SELECT *
 		FROM tplanned_downtime
-		WHERE type_downtime != "quiet" AND type_execution="once"
+		WHERE type_downtime != ' . $RDBMS_QUOTE_STRING . 'quiet' . $RDBMS_QUOTE_STRING . '
+			AND type_execution = ' . $RDBMS_QUOTE_STRING . 'once' . $RDBMS_QUOTE_STRING . '
 			AND executed = 0 AND date_from <= ?
 			AND date_to >= ?', $utimestamp, $utimestamp);
 	
@@ -1431,7 +1434,8 @@ sub pandora_planned_downtime_quiet_once_stop($$) {
 	# Stop pending downtimes
 	my @downtimes = get_db_rows($dbh, 'SELECT *
 		FROM tplanned_downtime
-		WHERE type_downtime = "quiet" AND type_execution="once"
+		WHERE type_downtime = ' . $RDBMS_QUOTE_STRING . 'quiet' . $RDBMS_QUOTE_STRING . '
+			AND type_execution = ' . $RDBMS_QUOTE_STRING. 'once' . $RDBMS_QUOTE_STRING . '
 			AND executed = 1 AND date_to <= ?', $utimestamp);
 	
 	foreach my $downtime (@downtimes) {
@@ -1471,7 +1475,8 @@ sub pandora_planned_downtime_quiet_once_start($$) {
 	# Start pending downtimes
 	my @downtimes = get_db_rows($dbh, 'SELECT *
 		FROM tplanned_downtime
-		WHERE type_downtime = "quiet" AND type_execution="once"
+		WHERE type_downtime = ' . $RDBMS_QUOTE_STRING . 'quiet' . $RDBMS_QUOTE_STRING . '
+			AND type_execution = ' . $RDBMS_QUOTE_STRING . 'once' . $RDBMS_QUOTE_STRING . '
 			AND executed = 0 AND date_from <= ?
 			AND date_to >= ?', $utimestamp, $utimestamp);
 	
@@ -1525,7 +1530,7 @@ sub pandora_planned_downtime_monthly_start($$) {
 	# Start pending downtimes
 	my @downtimes = get_db_rows($dbh, 'SELECT *
 		FROM tplanned_downtime
-		WHERE type_periodicity="monthly"
+		WHERE type_periodicity = ' . $RDBMS_QUOTE_STRING . 'monthly' . $RDBMS_QUOTE_STRING . '
 			AND executed = 0
 			AND periodically_day_from <= ?
 			AND periodically_day_to >= ?', 
@@ -1627,9 +1632,9 @@ sub pandora_planned_downtime_monthly_stop($$) {
 	# Start pending downtimes
 	my @downtimes = get_db_rows($dbh, 'SELECT *
 		FROM tplanned_downtime
-		WHERE type_periodicity = "monthly"
+		WHERE type_periodicity = ' . $RDBMS_QUOTE_STRING . 'monthly' . $RDBMS_QUOTE_STRING . '
 			AND executed = 1
-			AND type_execution <> "once"');
+			AND type_execution <> ' . $RDBMS_QUOTE_STRING . 'once' . $RDBMS_QUOTE_STRING);
 	
 	foreach my $downtime (@downtimes) {
 		#Convert to identical type.
@@ -1703,18 +1708,18 @@ sub pandora_planned_downtime_weekly_start($$) {
 	# Start pending downtimes
 	my @downtimes = get_db_rows($dbh, 'SELECT *
 		FROM tplanned_downtime
-		WHERE type_periodicity="weekly"
+		WHERE type_periodicity = ' . $RDBMS_QUOTE_STRING . 'weekly' . $RDBMS_QUOTE_STRING . '
 			AND executed = 0');
 	
 	foreach my $downtime (@downtimes) {
 		my $across_date = $downtime->{'periodically_time_from'} gt $downtime->{'periodically_time_to'} ? 1 : 0 ;
 		$found = 0;
-
+		
 		if ($across_date && ($time lt $downtime->{'periodically_time_to'})) {
-                        $number_day_week--;
-                        $number_day_week = 6 if ($number_day_week == -1);
-                }
-
+			$number_day_week--;
+			$number_day_week = 6 if ($number_day_week == -1);
+		}
+		
 		if (($number_day_week == 1) &&
 			($downtime->{'monday'})) {
 				$found = 1;
@@ -1825,8 +1830,8 @@ sub pandora_planned_downtime_weekly_stop($$) {
 	# Start pending downtimes
 	my @downtimes = get_db_rows($dbh, 'SELECT *
 		FROM tplanned_downtime
-		WHERE type_periodicity = "weekly"
-			AND type_execution <> "once"
+		WHERE type_periodicity = ' . $RDBMS_QUOTE_STRING . 'weekly' . $RDBMS_QUOTE_STRING . '
+			AND type_execution <> ' . $RDBMS_QUOTE_STRING . 'once' . $RDBMS_QUOTE_STRING . '
 			AND executed = 1');
 	
 	foreach my $downtime (@downtimes) {
@@ -2145,7 +2150,7 @@ sub pandora_update_gis_data ($$$$$$$$$) {
 Create a template module.
 
 =cut
-##########################################################################
+########################################################################
 sub pandora_create_template_module ($$$$;$$$) {
 	my ($pa_config, $dbh, $id_agent_module, $id_alert_template, $id_policy_alerts, $disabled, $standby) = @_;
 	
@@ -2154,16 +2159,27 @@ sub pandora_create_template_module ($$$$;$$$) {
 	$standby = 0 unless defined $standby;
 	
 	my $module_name = get_module_name($dbh, $id_agent_module);
-	return db_insert ($dbh, 'id', "INSERT INTO talert_template_modules (`id_agent_module`, `id_alert_template`, `id_policy_alerts`, `disabled`, `standby`, `last_reference`) VALUES (?, ?, ?, ?, ?, ?)", $id_agent_module, $id_alert_template, $id_policy_alerts, $disabled, $standby, time);
+	
+	return db_insert ($dbh,
+		'id',
+		"INSERT INTO talert_template_modules(
+			" . $RDBMS_QUOTE . "id_agent_module" . $RDBMS_QUOTE . ",
+			" . $RDBMS_QUOTE . "id_alert_template" . $RDBMS_QUOTE . ",
+			" . $RDBMS_QUOTE . "id_policy_alerts" . $RDBMS_QUOTE . ",
+			" . $RDBMS_QUOTE . "disabled" . $RDBMS_QUOTE . ",
+			" . $RDBMS_QUOTE . "standby" . $RDBMS_QUOTE . ",
+			" . $RDBMS_QUOTE . "last_reference" . $RDBMS_QUOTE . ")
+		VALUES (?, ?, ?, ?, ?, ?)",
+		$id_agent_module, $id_alert_template, $id_policy_alerts, $disabled, $standby, time);
 }
 
-##########################################################################
+########################################################################
 =head2 C<< pandora_update_template_module(I<$pa_config>, I<$dbh>, I<$id_alert>, I<$id_policy_alerts>, I<$disabled>, I<$standby>) >>
 
 Update a template module.
 
 =cut
-##########################################################################
+########################################################################
 
 sub pandora_update_template_module ($$$;$$$) {
 	my ($pa_config, $dbh, $id_alert, $id_policy_alerts, $disabled, $standby) = @_;
@@ -2172,16 +2188,22 @@ sub pandora_update_template_module ($$$;$$$) {
 	$disabled = 0 unless defined $disabled;
 	$standby = 0 unless defined $standby;
 	
-	db_do ($dbh, "UPDATE talert_template_modules SET `id_policy_alerts` = ?, `disabled` =  ?, `standby` = ? WHERE id = ?", $id_policy_alerts, $disabled, $standby, $id_alert);
+	db_do ($dbh,
+		"UPDATE talert_template_modules
+		SET " . $RDBMS_QUOTE . "id_policy_alerts" . $RDBMS_QUOTE . " = ?,
+			" . $RDBMS_QUOTE . "disabled" . $RDBMS_QUOTE . " =  ?,
+			" . $RDBMS_QUOTE . "standby" . $RDBMS_QUOTE . " = ?
+		WHERE id = ?",
+		$id_policy_alerts, $disabled, $standby, $id_alert);
 }
 
-##########################################################################
+########################################################################
 =head2 C<< pandora_create_template_module_action(I<$pa_config>, I<$parameters>, I<$dbh>) >>
 
 Create a template action.
 
 =cut
-##########################################################################
+########################################################################
 sub pandora_create_template_module_action ($$$) {
 	my ($pa_config, $parameters, $dbh) = @_;
 	
@@ -2192,26 +2214,26 @@ sub pandora_create_template_module_action ($$$) {
 	return $action_id;
 }
 
-##########################################################################
+########################################################################
 =head2 C<< pandora_delete_all_template_module_actions(I<$dbh>, I<$template_module_id>) >>
 
 Delete all actions of policy template module.
 
 =cut
-##########################################################################
+########################################################################
 sub pandora_delete_all_template_module_actions ($$) {
 	my ($dbh, $template_module_id) = @_;
 	
 	return db_do ($dbh, 'DELETE FROM talert_template_module_actions WHERE id_alert_template_module = ?', $template_module_id);
 }
 
-##########################################################################
+########################################################################
 =head2 C<< pandora_update_agent_address(I<$pa_config>, I<$agent_id>, I<$address>, I<$dbh>) >>
 
 Update the address of an agent.
 
 =cut
-##########################################################################
+########################################################################
 sub pandora_update_agent_address ($$$$$) {
 	my ($pa_config, $agent_id, $agent_name, $address, $dbh) = @_;
 	
@@ -2306,9 +2328,13 @@ sub pandora_create_module ($$$$$$$$$$) {
 		$status = 0;
 	}
 	
-	my $module_id = db_insert($dbh, 'id_agente_modulo', 'INSERT INTO tagente_modulo (id_agente, id_tipo_modulo, nombre, max, min, post_process, descripcion, module_interval, id_modulo)
-		VALUES (?, ?, ?, ?, ?, ?, ?, ?, 1)', $agent_id, $module_type_id, safe_input($module_name), $max, $min, $post_process, $description, $interval);
-	db_do ($dbh, 'INSERT INTO tagente_estado (id_agente_modulo, id_agente, estado, last_status, last_known_status, last_try) VALUES (?, ?, ?, ?, ?, \'1970-01-01 00:00:00\')', $module_id, $agent_id, $status, $status, $status);
+	my $module_id = db_insert($dbh, 'id_agente_modulo',
+		'INSERT INTO tagente_modulo (id_agente, id_tipo_modulo, nombre, max, min, post_process, descripcion, module_interval, id_modulo)
+		VALUES (?, ?, ?, ?, ?, ?, ?, ?, 1)',
+		$agent_id, $module_type_id, safe_input($module_name), $max, $min, $post_process, $description, $interval);
+	db_do ($dbh, 'INSERT INTO tagente_estado (id_agente_modulo, id_agente, estado, last_status, last_known_status, last_try)
+		VALUES (?, ?, ?, ?, ?, \'1970-01-01 00:00:00\')',
+		$module_id, $agent_id, $status, $status, $status);
 	
 	# Update the module status count. When the module is created disabled dont do it
 	pandora_mark_agent_for_module_update ($dbh, $agent_id);
@@ -2514,8 +2540,16 @@ sub pandora_create_module_tags ($$$$) {
 	}
 	
 	foreach my $tag_name (split (',', $serialized_tags)) {
-		my $tag_id = get_db_value ($dbh, "SELECT id_tag FROM ttag WHERE name = ?", $tag_name);
-		db_insert ($dbh, 'id_tag', "INSERT INTO ttag_module (`id_tag`, `id_agente_modulo`) VALUES (?, ?)", $tag_id, $id_agent_module);
+		my $tag_id = get_db_value ($dbh,
+			"SELECT id_tag FROM ttag WHERE name = ?", $tag_name);
+		
+		db_insert ($dbh,
+			'id_tag',
+			"INSERT INTO ttag_module(
+				" . $RDBMS_QUOTE . "id_tag" . $RDBMS_QUOTE . ",
+				" . $RDBMS_QUOTE . "id_agente_modulo" . $RDBMS_QUOTE . ")
+			VALUES (?, ?)",
+			$tag_id, $id_agent_module);
 	}
 }
 
@@ -3987,33 +4021,46 @@ sub pandora_self_monitoring ($$) {
 		                                       AND estado = 3");
 		$agents_unknown = 0 if (!defined($agents_unknown));
 	}
-
+	
 	my $queued_modules = get_db_value ($dbh, "SELECT SUM(queued_modules) FROM tserver WHERE name = '".$pa_config->{"servername"}."'");
-
-	if (!defined($queued_modules)){
+	
+	if (!defined($queued_modules)) {
 		$queued_modules = 0;
 	}
-
-	my $dbmaintance = get_db_value ($dbh, "SELECT COUNT(*) FROM tconfig WHERE token = 'db_maintance' AND value > UNIX_TIMESTAMP() - 86400");
-
+	
+	my $dbmaintance;
+	if ($RDBMS eq 'postgresql') {
+		$dbmaintance = get_db_value ($dbh,
+			"SELECT COUNT(*)
+			FROM tconfig
+			WHERE token = 'db_maintance'
+				AND NULLIF(value, '')::int > UNIX_TIMESTAMP() - 86400");
+	}
+	else {
+		$dbmaintance = get_db_value ($dbh,
+			"SELECT COUNT(*)
+			FROM tconfig
+			WHERE token = 'db_maintance' AND value > UNIX_TIMESTAMP() - 86400");
+	}
+	
 	$xml_output .=" <module>";
 	$xml_output .=" <name>Database Maintenance</name>";
 	$xml_output .=" <type>generic_proc</type>";
 	$xml_output .=" <data>$dbmaintance</data>";
 	$xml_output .=" </module>";
-
+	
 	$xml_output .=" <module>";
 	$xml_output .=" <name>Queued_Modules</name>";
 	$xml_output .=" <type>generic_data</type>";
 	$xml_output .=" <data>$queued_modules</data>";
 	$xml_output .=" </module>";
-
+	
 	$xml_output .=" <module>";
 	$xml_output .=" <name>Agents_Unknown</name>";
 	$xml_output .=" <type>generic_data</type>";
 	$xml_output .=" <data>$agents_unknown</data>";
 	$xml_output .=" </module>";
-
+	
 	$xml_output .=" <module>";
 	$xml_output .=" <name>System_Load_AVG</name>";
 	$xml_output .=" <type>generic_data</type>";
diff --git a/pandora_server/lib/PandoraFMS/DB.pm b/pandora_server/lib/PandoraFMS/DB.pm
index c5f93dc692..1a8ddc2b94 100644
--- a/pandora_server/lib/PandoraFMS/DB.pm
+++ b/pandora_server/lib/PandoraFMS/DB.pm
@@ -86,14 +86,18 @@ our @EXPORT = qw(
 		get_agentmodule_data
 		$RDBMS
 		$RDBMS_QUOTE
+		$RDBMS_QUOTE_STRING
 	);
 
 # Relational database management system in use
 our $RDBMS = '';
 
-# Character used to quote reserved words in the current RDBMS
+# For fields, character used to quote reserved words in the current RDBMS
 our $RDBMS_QUOTE = '';
 
+# For strings, Character used to quote in the current RDBMS
+our $RDBMS_QUOTE_STRING = '';
+
 ##########################################################################
 ## Connect to the DB.
 ##########################################################################
@@ -103,7 +107,8 @@ sub db_connect ($$$$$$) {
 	if ($rdbms eq 'mysql') {
 		$RDBMS = 'mysql';
 		$RDBMS_QUOTE = '`';
-	
+		$RDBMS_QUOTE_STRING = '"';
+		
 		# Connect to MySQL
 		my $dbh = DBI->connect("DBI:mysql:$db_name:$db_host:$db_port", $db_user, $db_pass, { RaiseError => 1, AutoCommit => 1 });
 		return undef unless defined ($dbh);
@@ -119,15 +124,18 @@ sub db_connect ($$$$$$) {
 	elsif ($rdbms eq 'postgresql') {
 		$RDBMS = 'postgresql';
 		$RDBMS_QUOTE = '"';
+		$RDBMS_QUOTE_STRING = "'";
 		
 		# Connect to PostgreSQL
 		my $dbh = DBI->connect("DBI:Pg:dbname=$db_name;host=$db_host;port=$db_port", $db_user, $db_pass, { RaiseError => 1, AutoCommit => 1 });
 		return undef unless defined ($dbh);
 		
 		return $dbh;
-	} elsif ($rdbms eq 'oracle') {
+	}
+	elsif ($rdbms eq 'oracle') {
 		$RDBMS = 'oracle';
 		$RDBMS_QUOTE = '"';
+		$RDBMS_QUOTE_STRING = '"';
 		
 		# Connect to Oracle
 		my $dbh = DBI->connect("DBI:Oracle:dbname=$db_name;host=$db_host;port=$db_port;sid=pandora", $db_user, $db_pass, { RaiseError => 1, AutoCommit => 1 });
@@ -641,6 +649,7 @@ sub db_insert ($$$;@) {
 	my ($dbh, $index, $query, @values) = @_;
 	my $insert_id = undef;
 	
+	
 	# MySQL
 	if ($RDBMS eq 'mysql') {
 		$dbh->do($query, undef, @values);
@@ -684,9 +693,9 @@ sub get_alert_template_module_id ($$$) {
 	return defined ($rc) ? $rc : -1;
 }
 
-##########################################################################
+########################################################################
 ## SQL insert. Returns the ID of the inserted row.
-##########################################################################
+########################################################################
 sub db_process_insert($$$$;@) {
 	my ($dbh, $index, $table, $parameters, @values) = @_;
 	
@@ -711,9 +720,13 @@ sub db_process_insert($$$$;@) {
 	}
 	$wildcards = '('.$wildcards.')';
 	
-	my $columns_string = join('`,`',@columns_array);
+	my $columns_string = join($RDBMS_QUOTE . ',' . $RDBMS_QUOTE,
+		@columns_array);
+	
+	my $res = db_insert ($dbh,
+		$index,
+		"INSERT INTO $table (" . $RDBMS_QUOTE . $columns_string . $RDBMS_QUOTE . ") VALUES " . $wildcards, @values_array);
 	
-	my $res = db_insert ($dbh, $index, "INSERT INTO $table (`".$columns_string."`) VALUES ".$wildcards, @values_array);
 	
 	return $res;
 }
@@ -740,7 +753,8 @@ sub db_process_update($$$$$;@) {
 		if ($i > 0 && $i <= $#values_array) {
 			$fields = $fields.',';
 		}
-		$fields = $fields." `$columns_array[$i]` = ?";
+		$fields = $fields .
+			" " . $RDBMS_QUOTE . "$columns_array[$i]" . $RDBMS_QUOTE . " = ?";
 	}
 	
 	push(@values_array, $where_value);
@@ -752,28 +766,30 @@ sub db_process_update($$$$$;@) {
 	return $res;
 }
 
-##########################################################################
+########################################################################
 # Add the given address to taddress.
-##########################################################################
+########################################################################
 sub add_address ($$) {
 	my ($dbh, $ip_address) = @_;
-
+	
 	return db_insert ($dbh, 'id_a', 'INSERT INTO taddress (ip) VALUES (?)', $ip_address);
 }
 
-##########################################################################
+########################################################################
 # Assign the new address to the agent
-##########################################################################
+########################################################################
 sub add_new_address_agent ($$$) {
 	my ($dbh, $addr_id, $agent_id) = @_;
 	
-	db_do ($dbh, 'INSERT INTO taddress_agent (`id_a`, `id_agent`)
+	db_do ($dbh, 'INSERT INTO taddress_agent (' .
+		$RDBMS_QUOTE . 'id_a' . $RDBMS_QUOTE . ', ' .
+		$RDBMS_QUOTE. 'id_agent' . $RDBMS_QUOTE. ')
 		VALUES (?, ?)', $addr_id, $agent_id);
 }
 
-##########################################################################
+########################################################################
 # Return the ID of the given address, -1 if it does not exist.
-##########################################################################
+########################################################################
 sub get_addr_id ($$) {
 	my ($dbh, $addr) = @_;
 	
@@ -825,9 +841,9 @@ sub is_agent_address ($$$) {
 	return (defined ($id_ag)) ? $id_ag : 0;
 }
 
-##########################################################################
+########################################################################
 ## Quote the given string. 
-##########################################################################
+########################################################################
 sub db_string ($) {
 	my $string = shift;
 	
@@ -856,7 +872,10 @@ sub db_text ($) {
 sub get_alert_template_name ($$) {
 	my ($dbh, $alert_id) = @_;
 	
-	return get_db_value ($dbh, "SELECT name FROM talert_templates, talert_template_modules WHERE talert_templates.id = talert_template_modules.id_alert_template AND talert_template_modules.id = ?", $alert_id);
+	return get_db_value ($dbh, "SELECT name
+		FROM talert_templates, talert_template_modules
+		WHERE talert_templates.id = talert_template_modules.id_alert_template
+			AND talert_template_modules.id = ?", $alert_id);
 }
 
 ########################################################################
@@ -906,18 +925,18 @@ sub get_priority_name ($) {
 ########################################################################
 sub db_update_get_values ($) {
 	my ($set_ref) = @_;
-
+	
 	my $set = '';
 	my @values;
 	while (my ($key, $value) = each (%{$set_ref})) {
-
-			# Not value for the given column
-			next if (! defined ($value));
-
-			$set .= "$key = ?,";
-			push (@values, $value);
+		
+		# Not value for the given column
+		next if (! defined ($value));
+		
+		$set .= "$key = ?,";
+		push (@values, $value);
 	}
-
+	
 	# Remove the last ,
 	chop ($set);
 	
@@ -929,34 +948,34 @@ sub db_update_get_values ($) {
 ########################################################################
 sub db_insert_get_values ($) {
 	my ($insert_ref) = @_;
-
+	
 	my $columns = '(';
 	my @values;
 	while (my ($key, $value) = each (%{$insert_ref})) {
-
-			# Not value for the given column
-			next if (! defined ($value));
-
-			$columns .= $PandoraFMS::DB::RDBMS_QUOTE . "$key" . $PandoraFMS::DB::RDBMS_QUOTE . ",";
-			push (@values, $value);
+		
+		# Not value for the given column
+		next if (! defined ($value));
+		
+		$columns .= $PandoraFMS::DB::RDBMS_QUOTE . "$key" . $PandoraFMS::DB::RDBMS_QUOTE . ",";
+		push (@values, $value);
 	}
-
+	
 	# Remove the last , and close the parentheses
 	chop ($columns);
 	$columns .= ')';
 	
-	# No columns		
+	# No columns
 	if ($columns eq '()') {
 		return;
 	}
-
+	
 	# Add placeholders for the values
 	$columns .= ' VALUES (' . ("?," x ($#values + 1));
-
+	
 	# Remove the last , and close the parentheses
 	chop ($columns);
 	$columns .= ')';
-		
+	
 	return ($columns, \@values);
 }
 
@@ -973,7 +992,7 @@ sub db_get_lock($$;$) {
 	my $sth = $dbh->prepare('SELECT GET_LOCK(?, ?)');
 	$sth->execute($lock_name, $lock_timeout);
 	my ($lock) = $sth->fetchrow;
-
+	
 	# Something went wrong
 	return 0 if (! defined ($lock));
 	
diff --git a/pandora_server/lib/PandoraFMS/DataServer.pm b/pandora_server/lib/PandoraFMS/DataServer.pm
index 73c24a0f06..f88437d0b6 100644
--- a/pandora_server/lib/PandoraFMS/DataServer.pm
+++ b/pandora_server/lib/PandoraFMS/DataServer.pm
@@ -528,7 +528,7 @@ sub process_module_data ($$$$$$$$$) {
 	            'str_warning' => '', 'str_critical' => '', 'critical_instructions' => '', 'warning_instructions' => '',
 	            'unknown_instructions' => '', 'tags' => '', 'critical_inverse' => 0, 'warning_inverse' => 0, 'quiet' => 0,
 	            'module_ff_interval' => 0};
-
+	
 	# Other tags will be saved here
 	$module_conf->{'extended_info'} = '';
 	
@@ -539,8 +539,8 @@ sub process_module_data ($$$$$$$$$) {
 		} else {
 			$module_conf->{'extended_info'} .= "$tag: " . get_tag_value ($data, $tag, '') . '<br/>';
 		}
-	}	
-		
+	}
+	
 	# Description XML tag and column name don't match
 	$module_conf->{'descripcion'} = $module_conf->{'description'};
 	$module_conf->{'descripcion'} = '' unless defined ($module_conf->{'descripcion'});
@@ -549,14 +549,14 @@ sub process_module_data ($$$$$$$$$) {
 	# Name XML tag and column name don't match
 	$module_conf->{'nombre'} = safe_input($module_name);
 	delete $module_conf->{'name'};
-
+	
 	# Calculate the module interval in seconds
 	$module_conf->{'module_interval'} = 1 unless defined ($module_conf->{'module_interval'});
 	$module_conf->{'module_interval'} *= $interval if (defined ($module_conf->{'module_interval'}));
-
+	
 	# Allow , as a decimal separator
 	$module_conf->{'post_process'} =~ s/,/./ if (defined ($module_conf->{'post_process'}));
-
+	
 	# Get module data or create it if it does not exist
 	$ModuleSem->down ();
 	my $module = get_db_single_row ($dbh, 'SELECT * FROM tagente_modulo WHERE id_agente = ? AND ' . db_text ('nombre') . ' = ?', $agent->{'id_agente'}, safe_input($module_name));
@@ -568,14 +568,14 @@ sub process_module_data ($$$$$$$$$) {
 			$ModuleSem->up ();
 			return;
 		}
-
+		
 		# Is the agent learning?
 		if ($agent->{'modo'} ne '1') {
 			logger($pa_config, "Learning mode disabled. Skipping module '$module_name' agent '$agent_name'.", 10);
 			$ModuleSem->up ();
 			return;
 		}
-
+		
 		# Get the module type
 		$module_conf->{'id_tipo_modulo'} = get_module_id ($dbh, $module_type);
 		if ($module_conf->{'id_tipo_modulo'} <= 0) {
@@ -583,16 +583,16 @@ sub process_module_data ($$$$$$$$$) {
 			$ModuleSem->up ();
 			return;
 		}
-	
+		
 		# The group name has to be translated to a group ID
 		if (defined $module_conf->{'module_group'}) {
 			$module_conf->{'id_module_group'} = get_module_group_id ($dbh, $module_conf->{'module_group'});
 			delete $module_conf->{'module_group'};
 		}
-
+		
 		$module_conf->{'id_modulo'} = 1;
 		$module_conf->{'id_agente'} = $agent->{'id_agente'};
-
+		
 		my $module_tags = undef;
 		if(defined ($module_conf->{'tags'})) {
 			$module_tags = $module_conf->{'tags'};
@@ -601,7 +601,7 @@ sub process_module_data ($$$$$$$$$) {
 		
 		# Create the module
 		my $module_id = pandora_create_module_from_hash ($pa_config, $module_conf, $dbh);
-
+		
 		$module = get_db_single_row ($dbh, 'SELECT * FROM tagente_modulo WHERE id_agente = ? AND ' . db_text('nombre') . ' = ?', $agent->{'id_agente'}, safe_input($module_name));
 		if (! defined ($module)) {
 			logger($pa_config, "Could not create module '$module_name' for agent '$agent_name'.", 3);
@@ -626,7 +626,8 @@ sub process_module_data ($$$$$$$$$) {
 			}
 		}
 		
-	} else {
+	}
+	else {
 		# Control NULL columns
 		$module->{'descripcion'} = '' unless defined ($module->{'descripcion'});
 		$module->{'extended_info'} = '' unless defined ($module->{'extended_info'});
@@ -649,15 +650,15 @@ sub process_module_data ($$$$$$$$$) {
 	if ($agent->{'modo'} eq '1' && $policy_linked == 0) {
 		update_module_configuration ($pa_config, $dbh, $module, $module_conf);
 	}
-
+	
 	$ModuleSem->up ();
-
+	
 	# Module disabled!
 	if ($module->{'disabled'} eq '1') {
 		logger($pa_config, "Skipping disabled module '$module_name' agent '$agent_name'.", 10);
 		return;
 	}
-
+	
 	# Parse the timestamp and process the module
 	if ($timestamp !~ /(\d+)\/(\d+)\/(\d+) +(\d+):(\d+):(\d+)/ &&
 		$timestamp !~ /(\d+)\-(\d+)\-(\d+) +(\d+):(\d+):(\d+)/) {
diff --git a/pandora_server/lib/PandoraFMS/GIS.pm b/pandora_server/lib/PandoraFMS/GIS.pm
index 9376b2402d..809e8cb3df 100644
--- a/pandora_server/lib/PandoraFMS/GIS.pm
+++ b/pandora_server/lib/PandoraFMS/GIS.pm
@@ -146,12 +146,24 @@ B<Returns>: I<undef> if there is not information available or a B<hash> with:
 ##########################################################################
 sub get_reverse_geoip_sql($$$) {
 	my ($pa_config,$ip_addr, $dbh) = @_;
-
-	my $id_range =  get_db_value($dbh, 'SELECT `id_range` FROM tgis_reverse_geoip_ranges WHERE INET_ATON(?) >= `first_IP_decimal` AND INET_ATON(?) <= `last_IP_decimal` LIMIT 1', $ip_addr, $ip_addr);
-    if (defined($id_range)) {
-    	logger($pa_config,"Range id of '$ip_addr' is '$id_range'", 8);
-        my $region_info = get_db_single_row($dbh, 'SELECT * FROM tgis_reverse_geoip_info WHERE `id_range` = ?',$id_range);
-       	logger($pa_config, "region info of id_range '$id_range' is: country:".$region_info->{'country_name'}." region:".$region_info->{'region'}." city:".$region_info->{'city'}." longitude:".$region_info->{'longitude'}." latitude:".$region_info->{'longitude'}, 8);
+	
+	my $id_range =  get_db_value($dbh,
+		'SELECT ' . $RDBMS_QUOTE . 'id_range' . $RDBMS_QUOTE . '
+		FROM tgis_reverse_geoip_ranges
+		WHERE INET_ATON(?) >=  ' . $RDBMS_QUOTE . 'first_IP_decimal' . $RDBMS_QUOTE . '
+			AND INET_ATON(?) <=  ' . $RDBMS_QUOTE . 'last_IP_decimal ' . $RDBMS_QUOTE . '
+			LIMIT 1', $ip_addr, $ip_addr);
+	
+	if (defined($id_range)) {
+		logger($pa_config,"Range id of '$ip_addr' is '$id_range'", 8);
+		my $region_info = get_db_single_row($dbh,
+			'SELECT *
+			FROM tgis_reverse_geoip_info
+			WHERE  ' . $RDBMS_QUOTE . 'id_range ' . $RDBMS_QUOTE . ' = ?',
+			$id_range);
+		
+		logger($pa_config, "region info of id_range '$id_range' is: country:".$region_info->{'country_name'}." region:".$region_info->{'region'}." city:".$region_info->{'city'}." longitude:".$region_info->{'longitude'}." latitude:".$region_info->{'longitude'}, 8);
+		
 		return $region_info;
 	}
 	return undef;
diff --git a/pandora_server/lib/PandoraFMS/ReconServer.pm b/pandora_server/lib/PandoraFMS/ReconServer.pm
index 8ef3f07dc3..2221c3a54c 100644
--- a/pandora_server/lib/PandoraFMS/ReconServer.pm
+++ b/pandora_server/lib/PandoraFMS/ReconServer.pm
@@ -262,7 +262,7 @@ sub data_consumer ($$) {
 			}
 		}
 		# End of GIS code -----------------------------      
-		else {	
+		else {
 			# Create a new agent
 			$agent_id = pandora_create_agent ($pa_config, $pa_config->{'servername'},
 					                                  $host_name, $addr, $task->{'id_group'},
@@ -274,7 +274,7 @@ sub data_consumer ($$) {
 			logger($pa_config, "Error creating agent '$host_name'.", 3);
 			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);
@@ -282,14 +282,15 @@ sub data_consumer ($$) {
 			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);
+			db_do ($dbh, 'INSERT INTO taddress_agent (' . $RDBMS_QUOTE . 'id_a' . $RDBMS_QUOTE . ', ' .
+				$RDBMS_QUOTE . 'id_agent' . $RDBMS_QUOTE . ')
+				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'});
 		
@@ -309,9 +310,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);
 }
@@ -388,12 +389,13 @@ sub get_host_parent {
 		if ($os_detect == 1) {
 			$id_os = guess_os ($pa_config, $dbh, $host_addr);
 		}
-	
+		
 		# Create the host
 		my $agent_id = pandora_create_agent ($pa_config, $pa_config->{'servername'}, $host_name, $host_addr, $group, $parent_id, $id_os, '', 300, $dbh);
 		$agent_id = 0 unless defined ($parent_id);
-		db_do ($dbh, 'INSERT INTO taddress_agent (`id_a`, `id_agent`)
-			          VALUES (?, ?)', $addr_id, $agent_id);
+		db_do ($dbh, 'INSERT INTO taddress_agent (' . $RDBMS_QUOTE . 'id_a' . $RDBMS_QUOTE . ',' .
+			$RDBMS_QUOTE . 'id_agent' . $RDBMS_QUOTE . ')
+			VALUES (?, ?)', $addr_id, $agent_id);
 		
 		# Move to the next host
 		$parent_id = $agent_id;
@@ -417,9 +419,9 @@ sub tcp_scan ($$$) {
 # Guess OS using xprobe2.
 ##########################################################################
 sub guess_os {
-    my ($pa_config, $dbh, $host) = @_;
-    
-    # Use xprobe2 if available
+	my ($pa_config, $dbh, $host) = @_;
+	
+	# Use xprobe2 if available
 	my $xprobe = $pa_config->{'xprobe2'};
 	if (-e $xprobe){
 			my $output = `$xprobe $host 2>$DEVNULL | grep 'Running OS' | head -1`;