From 9839ef76de70fa40a3f1bf1e2f29c008b19bf500 Mon Sep 17 00:00:00 2001
From: Ramon Novoa <rnovoa@artica.es>
Date: Tue, 16 Oct 2012 11:49:13 +0000
Subject: [PATCH] 2012-10-16  Ramon Novoa  <rnovoa@artica.es>

	* include/db/postgresql.php, include/db/mysql.php,
	  include/db/oracle.php, include/functions_db.php,
	  operation/agentes/datos_agente.php: Merged from 4.0 branch. Fixed
	  history database connection when the main database and the history
	  database are on the same host. Allow switching between the main
	  database and the history database to retrieve module data.



git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@7078 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
---
 pandora_console/ChangeLog                     |  9 +++++++
 pandora_console/include/db/mysql.php          |  2 +-
 pandora_console/include/db/oracle.php         |  2 +-
 pandora_console/include/db/postgresql.php     |  2 +-
 pandora_console/include/functions_db.php      | 16 +++++------
 .../operation/agentes/datos_agente.php        | 27 +++++++++++++++----
 6 files changed, 42 insertions(+), 16 deletions(-)

diff --git a/pandora_console/ChangeLog b/pandora_console/ChangeLog
index 552e294194..7e6c5e0d5d 100644
--- a/pandora_console/ChangeLog
+++ b/pandora_console/ChangeLog
@@ -1,3 +1,12 @@
+2012-10-16  Ramon Novoa  <rnovoa@artica.es>
+
+	* include/db/postgresql.php, include/db/mysql.php,
+	  include/db/oracle.php, include/functions_db.php,
+	  operation/agentes/datos_agente.php: Merged from 4.0 branch. Fixed
+	  history database connection when the main database and the history
+	  database are on the same host. Allow switching between the main
+	  database and the history database to retrieve module data.
+
 2012-10-15 Dario Roriguez <dario.rodriguez@artica.es>
 
 	* extensions/agents_alerts.php: Modified to show
diff --git a/pandora_console/include/db/mysql.php b/pandora_console/include/db/mysql.php
index 98ae714d7a..3452048ca5 100644
--- a/pandora_console/include/db/mysql.php
+++ b/pandora_console/include/db/mysql.php
@@ -30,7 +30,7 @@ function mysql_connect_db($host = null, $db = null, $user = null, $pass = null,
 	
 	// Non-persistent connection: This will help to avoid mysql errors like "has gone away" or locking problems
 	// If you want persistent connections change it to mysql_pconnect(). 
-	$connect_id = mysql_connect($host . ":" . $port, $user, $pass);
+	$connect_id = mysql_connect($host . ":" . $port, $user, $pass, true);
 	if (! $connect_id) {
 		return false;
 	}
diff --git a/pandora_console/include/db/oracle.php b/pandora_console/include/db/oracle.php
index 997e25f51c..edcd0e770e 100644
--- a/pandora_console/include/db/oracle.php
+++ b/pandora_console/include/db/oracle.php
@@ -30,7 +30,7 @@ function oracle_connect_db($host = null, $db = null, $user = null, $pass = null,
 	
 	// Non-persistent connection: This will help to avoid mysql errors like "has gone away" or locking problems
 	// If you want persistent connections change it to oci_pconnect().
-	$connect_id = oci_connect($user, $pass, '//' . $host . ':' . $port . '/' . $db);
+	$connect_id = oci_new_connect($user, $pass, '//' . $host . ':' . $port . '/' . $db);
 	if (! $connect_id) {
 		return false;
 	}
diff --git a/pandora_console/include/db/postgresql.php b/pandora_console/include/db/postgresql.php
index 06eb268ed7..8460891915 100644
--- a/pandora_console/include/db/postgresql.php
+++ b/pandora_console/include/db/postgresql.php
@@ -32,7 +32,7 @@ function postgresql_connect_db($host = null, $db = null, $user = null, $pass = n
 		" port=" . $port .
 		" dbname='" . $db . "'" .
 		" user='" . $user . "'" .
-		" password='" . $pass . "'");
+		" password='" . $pass . "'", PGSQL_CONNECT_FORCE_NEW);
 	
 	if (! $connect_id) {
 		return false;
diff --git a/pandora_console/include/functions_db.php b/pandora_console/include/functions_db.php
index 6cbd35375b..f98c8a3d14 100644
--- a/pandora_console/include/functions_db.php
+++ b/pandora_console/include/functions_db.php
@@ -278,18 +278,18 @@ function db_get_value_filter ($field, $table, $filter, $where_join = 'AND') {
  * @return the first value of the first row of a table result from query.
  *
  */
-function db_get_value_sql($sql) {
+function db_get_value_sql($sql, $dbconnection = false) {
 	global $config;
 	
 	switch ($config["dbtype"]) {
 		case "mysql":
-			return mysql_db_get_value_sql($sql);
+			return mysql_db_get_value_sql($sql, $dbconnection);
 			break;
 		case "postgresql":
-			return postgresql_db_get_value_sql($sql);
+			return postgresql_db_get_value_sql($sql, $dbconnection);
 			break;
 		case "oracle":
-			return oracle_db_get_value_sql($sql);
+			return oracle_db_get_value_sql($sql, $dbconnection);
 			break;
 	}
 }
@@ -420,18 +420,18 @@ function db_get_sql ($sql, $field = 0, $search_history_db = false) {
  * @return mixed A matrix with all the values returned from the SQL statement or
  * false in case of empty result
  */
-function db_get_all_rows_sql($sql, $search_history_db = false, $cache = true) {
+function db_get_all_rows_sql($sql, $search_history_db = false, $cache = true, $dbconnection = false) {
 	global $config;
 	
 	switch ($config["dbtype"]) {
 		case "mysql":
-			return mysql_db_get_all_rows_sql($sql, $search_history_db, $cache);
+			return mysql_db_get_all_rows_sql($sql, $search_history_db, $cache, $dbconnection);
 			break;
 		case "postgresql":
-			return postgresql_db_get_all_rows_sql($sql, $search_history_db, $cache);
+			return postgresql_db_get_all_rows_sql($sql, $search_history_db, $cache, $dbconnection);
 			break;
 		case "oracle":
-			return oracle_db_get_all_rows_sql($sql, $search_history_db, $cache);
+			return oracle_db_get_all_rows_sql($sql, $search_history_db, $cache, $dbconnection);
 			break;
 	}
 }
diff --git a/pandora_console/operation/agentes/datos_agente.php b/pandora_console/operation/agentes/datos_agente.php
index a16b495240..93650a6fb7 100644
--- a/pandora_console/operation/agentes/datos_agente.php
+++ b/pandora_console/operation/agentes/datos_agente.php
@@ -28,6 +28,17 @@ $group = agents_get_agentmodule_group ($module_id);
 $agentId = get_parameter("id_agente"); 
 $freestring = get_parameter ("freestring");
 
+// Select active connection
+$connection = get_parameter ("connection", 'main');
+if ($connection == 'history' && $config['history_db_enabled'] == 1) {
+	if (! isset ($config['history_db_connection']) || $config['history_db_connection'] === false) {
+		$config['history_db_connection'] = db_connect($config['history_db_host'], $config['history_db_name'], $config['history_db_user'], $config['history_db_pass'], $config['history_db_port'], false);
+	}
+	$connection_handler = $config['history_db_connection'];
+} else {
+	$connection_handler = $config['dbconnection'];
+}
+
 $selection_mode = get_parameter('selection_mode', 'fromnow');
 $date_from = (string) get_parameter ('date_from', date ('Y-m-j'));
 $time_from = (string) get_parameter ('time_from', date ('h:iA'));
@@ -125,7 +136,7 @@ $sql_body = io_safe_output($sql_body);
 $sql = "SELECT * " . $sql_body;
 $sql_count = "SELECT count(*) " . $sql_body;
 
-$count = db_get_table_count($sql_count, true);
+$count = db_get_value_sql ($sql_count, $connection_handler);
 
 switch ($config["dbtype"]) {
 	case "mysql":
@@ -142,7 +153,7 @@ switch ($config["dbtype"]) {
 		break;
 }
 
-$result = db_get_all_rows_sql ($sql, true);
+$result = db_get_all_rows_sql ($sql, false, true, $connection_handler);
 if ($result === false) {
 	$result = array ();
 }
@@ -155,7 +166,15 @@ if (($config['dbtype'] == 'oracle') && ($result !== false)) {
 
 $header_title = __('Received data from')." ".modules_get_agentmodule_agent_name ($module_id)." / ".modules_get_agentmodule_name ($module_id); 
 
-echo "<h4>".$header_title. "</h4>";
+echo "<form method='post' action='index.php?sec=estado&sec2=operation/agentes/ver_agente&id_agente=" . $agentId . "&tab=data_view&id=" . $module_id . "'>";
+
+echo "<h4>".$header_title;
+if ($config['history_db_enabled'] == 1) {
+	echo "&nbsp;";
+	html_print_select (array ('main' => __('Main database'), 'history' => __('History database')), "connection", $connection);
+	ui_print_help_tip (__('Switch between the main database and the history database to retrieve module data'));
+}
+echo "</h4>";
 
 $formtable->width = '98%';
 $formtable->class = "databox";
@@ -165,8 +184,6 @@ $formtable->size[0] = '40%';
 $formtable->size[1] = '20%';
 $formtable->size[2] = '30%';
 
-echo "<form method='post' action='index.php?sec=estado&sec2=operation/agentes/ver_agente&id_agente=" . $agentId . "&tab=data_view&id=" . $module_id . "'>";
-
 $formtable->data[0][0] = html_print_radio_button_extended ("selection_mode", 'fromnow', '', $selection_mode, false, '', 'style="margin-right: 15px;"', true) . __("Choose a time from now");
 $formtable->data[0][1] = html_print_extended_select_for_time ('period', $period, '', '', '0', 10, true);