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

* include/db/postgresql.php,
	  include/db/mysql.php,
	  include/db/oracle.php,
	  include/config_process.php,
	  include/functions_db.php,
	  operation/agentes/datos_agente.php: Merged from 4.0 branch. Fixed and
	  improved the history database feature.

	* include/graphs/fgraph.php: Merged from 4.0 branch. Protect against
	  accidental output that might corrupt an image.



git-svn-id: https://svn.code.sf.net/p/pandora/code/trunk@6460 c3f86ba8-e40f-0410-aaad-9ba5e7f4b01f
This commit is contained in:
ramonn 2012-06-08 12:18:03 +00:00
parent d1fdb6d2d1
commit c05e8c9f98
8 changed files with 242 additions and 94 deletions

View File

@ -1,3 +1,16 @@
2012-06-08 Ramon Novoa <rnovoa@artica.es>
* include/db/postgresql.php,
include/db/mysql.php,
include/db/oracle.php,
include/config_process.php,
include/functions_db.php,
operation/agentes/datos_agente.php: Merged from 4.0 branch. Fixed and
improved the history database feature.
* include/graphs/fgraph.php: Merged from 4.0 branch. Protect against
accidental output that might corrupt an image.
2012-06-08 Dario Rodriguez <dario.rodriguez@artica.es>
* include/functions_modules.php,

View File

@ -93,7 +93,7 @@ require_once ($ownDir . 'functions_db.php');
require_once ($ownDir . 'functions.php');
db_select_engine();
db_connect();
$config['dbconnection'] = db_connect();
if (! defined ('EXTENSIONS_DIR'))
@ -168,20 +168,6 @@ else {
$config['enterprise_installed'] = 0;
}
// Connect to the history DB
if (isset($config['history_db_enabled'])) {
if ($config['history_db_enabled']) {
/*$config['history_db_connection'] =*/ db_connect(
$config['history_db_host'] . ':' . $config['history_db_port'],
$config['history_db_name'],
$config['history_db_user'],
$config['history_db_pass'], true);
}
}
// Make dbconnection the default connection again (the link identifier of the already opened link will be returned)
db_connect();
// Function include_graphs_dependencies() it's called in the code below
require_once("include_graph_dependencies.php");

View File

@ -14,7 +14,7 @@
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
function mysql_connect_db($host = null, $db = null, $user = null, $pass = null, $history = null, $port = null) {
function mysql_connect_db($host = null, $db = null, $user = null, $pass = null, $port = null) {
global $config;
if ($host === null)
@ -30,59 +30,49 @@ 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);
mysql_select_db($db, $connect_id);
if (! $connect_id) {
include ($config["homedir"]."/general/error_authconfig.php");
exit;
return false;
}
if ($history){
$config['history_db_dbconnection'] = $connect_id;
}
else{
$config['dbconnection'] = $connect_id;
}
//$config['dbconnection'] = mysql_connect($host, $user, $pass);
//mysql_select_db($db, $config['dbconnection']);
//if (! $config['dbconnection']) {
// include ($config["homedir"]."/general/error_authconfig.php");
// exit;
//}
mysql_select_db($db, $connect_id);
return $connect_id;
//return $config['dbconnection'];
}
function mysql_db_get_all_rows_sql ($sql, $search_history_db = false, $cache = true) {
function mysql_db_get_all_rows_sql ($sql, $search_history_db = false, $cache = true, $dbconnection = false) {
global $config;
$history = array ();
if ($dbconnection === false) {
$dbconnection = $config['dbconnection'];
}
// To disable globally SQL cache depending on global variable.
// Used in several critical places like Metaconsole trans-server queries
if (isset($config["dbcache"]))
$cache = $config["dbcache"];
// Read from the history DB if necessary
if ($search_history_db) {
if ($search_history_db && $config['history_db_enabled'] == 1) {
$cache = false;
$history = false;
if (isset($config['history_db_connection']))
// Connect to the history DB
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);
}
if ($config['history_db_connection'] !== false) {
$history = mysql_db_process_sql ($sql, 'affected_rows', $config['history_db_connection'], false);
}
if ($history === false) {
$history = array ();
}
}
$return = mysql_db_process_sql ($sql, 'affected_rows', $config['dbconnection'], $cache);
$return = mysql_db_process_sql ($sql, 'affected_rows', $dbconnection, $cache);
if ($return === false) {
return false;
$return = array ();
}
// Append result to the history DB data
@ -578,9 +568,9 @@ function mysql_db_format_array_where_clause_sql ($values, $join = 'AND', $prefix
* @return the first value of the first row of a table result from query.
*
*/
function mysql_db_get_value_sql($sql) {
function mysql_db_get_value_sql($sql, $dbconnection = false) {
$sql .= " LIMIT 1";
$result = db_get_all_rows_sql ($sql);
$result = mysql_db_get_all_rows_sql ($sql, false, true, $dbconnection);
if($result === false)
return false;
@ -862,7 +852,6 @@ function mysql_db_process_sql_update($table, $values, $where = false, $where_joi
}
else if (is_array ($where)) {
$query .= db_format_array_where_clause_sql ($where, $where_join, ' WHERE ');
}
}
@ -1020,4 +1009,41 @@ function mysql_db_get_type_field_table($table, $field) {
return mysql_field_type($result, $field);
}
/**
* Get the element count of a table.
*
* @param string $sql SQL query to get the element count.
*
* @return int Return the number of elements in the table.
*/
function mysql_db_get_table_count($sql, $search_history_db = false) {
global $config;
$history_count = 0;
$count = mysql_db_get_value_sql ($sql);
if ($count === false) {
$count = 0;
}
// Search the history DB for matches
if ($search_history_db && $config['history_db_enabled'] == 1) {
// Connect to the history DB
if (! isset ($config['history_db_connection']) || $config['history_db_connection'] === false) {
$config['history_db_connection'] = mysql_connect_db ($config['history_db_host'], $config['history_db_name'], $config['history_db_user'], $config['history_db_pass'], $config['history_db_port'], false);
}
if ($config['history_db_connection'] !== false) {
$history_count = mysql_db_get_value_sql ($sql, $config['history_db_connection']);
if ($history_count === false) {
$history_count = 0;
}
}
}
$count += $history_count;
return $count;
}
?>

View File

@ -14,7 +14,7 @@
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
function oracle_connect_db($host = null, $db = null, $user = null, $pass = null, $history = null, $port = null) {
function oracle_connect_db($host = null, $db = null, $user = null, $pass = null, $port = null) {
global $config;
if ($host === null)
@ -31,10 +31,8 @@ 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);
if (! $connect_id) {
include ($config["homedir"]."/general/error_authconfig.php");
exit;
return false;
}
// Set date and timestamp formats for this session
@ -53,13 +51,6 @@ function oracle_connect_db($host = null, $db = null, $user = null, $pass = null,
oci_free_statement($date_format);
oci_free_statement($decimal_separator);
if ($history){
$config['history_db_dbconnection'] = $connect_id;
}
else{
$config['dbconnection'] = $connect_id;
}
return $connect_id;
}
@ -148,32 +139,41 @@ function oracle_db_get_row ($table, $field_search, $condition, $fields = false)
return $result[0];
}
function oracle_db_get_all_rows_sql ($sql, $search_history_db = false, $cache = true) {
function oracle_db_get_all_rows_sql ($sql, $search_history_db = false, $cache = true, $dbconnection = false) {
global $config;
$history = array ();
if ($dbconnection === false) {
$dbconnection = $config['dbconnection'];
}
// To disable globally SQL cache depending on global variable.
// Used in several critical places like Metaconsole trans-server queries
if (isset($config["dbcache"]))
$cache = $config["dbcache"];
// Read from the history DB if necessary
if ($search_history_db) {
if ($search_history_db && $config['history_db_enabled'] == 1) {
$cache = false;
$history = false;
if (isset($config['history_db_connection']))
// Connect to the history DB
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);
}
if ($config['history_db_connection'] !== false) {
$history = oracle_db_process_sql ($sql, 'affected_rows', $config['history_db_connection'], false);
}
if ($history === false) {
$history = array ();
}
}
$return = oracle_db_process_sql ($sql, 'affected_rows', $config['dbconnection'], $cache);
$return = oracle_db_process_sql ($sql, 'affected_rows', $dbconnection, $cache);
if ($return === false) {
return false;
$return = array ();
}
// Append result to the history DB data
@ -847,9 +847,9 @@ function oracle_recode_query ($sql, $values, $join = 'AND', $return = true) {
* @return the first value of the first row of a table result from query.
*
*/
function oracle_db_get_value_sql($sql) {
function oracle_db_get_value_sql($sql, $dbconnection = false) {
$sql = "SELECT * FROM (" . $sql . ") WHERE rownum < 2";
$result = db_get_all_rows_sql ($sql);
$result = oracle_db_get_all_rows_sql ($sql, false, true, $dbconnection);
if($result === false)
return false;
@ -867,7 +867,7 @@ function oracle_db_get_value_sql($sql) {
*/
function oracle_db_get_row_sql ($sql, $search_history_db = false) {
$sql = "SELECT * FROM (" . $sql . ") WHERE rownum < 2";
$result = db_get_all_rows_sql($sql, $search_history_db);
$result = oracle_db_get_all_rows_sql($sql, $search_history_db);
if($result === false)
return false;
@ -1413,4 +1413,40 @@ function oracle_list_all_field_table($table_name, $return_mode = 'array'){
}
}
/**
* Get the element count of a table.
*
* @param string $sql SQL query to get the element count.
*
* @return int Return the number of elements in the table.
*/
function oracle_db_get_table_count($sql, $search_history_db = false) {
global $config;
$history_count = 0;
$count = oracle_db_get_value_sql ($sql);
if ($count === false) {
$count = 0;
}
// Search the history DB for matches
if ($search_history_db && $config['history_db_enabled'] == 1) {
// Connect to the history DB
if (! isset ($config['history_db_connection']) || $config['history_db_connection'] === false) {
$config['history_db_connection'] = oracle_connect_db ($config['history_db_host'], $config['history_db_name'], $config['history_db_user'], $config['history_db_pass'], $config['history_db_port'], false);
}
if ($config['history_db_connection'] !== false) {
$history_count = oracle_db_get_value_sql ($sql, $config['history_db_connection']);
if ($history_count === false) {
$history_count = 0;
}
}
}
$count += $history_count;
return $count;
}
?>

View File

@ -14,7 +14,7 @@
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
function postgresql_connect_db($host = null, $db = null, $user = null, $pass = null, $history = null, $port = null) {
function postgresql_connect_db($host = null, $db = null, $user = null, $pass = null, $port = null) {
global $config;
if ($host === null)
@ -35,17 +35,9 @@ function postgresql_connect_db($host = null, $db = null, $user = null, $pass = n
" password='" . $pass . "'");
if (! $connect_id) {
include ($config["homedir"]."/general/error_authconfig.php");
exit;
return false;
}
if ($history){
$config['history_db_dbconnection'] = $connect_id;
}
else{
$config['dbconnection'] = $connect_id;
}
return $connect_id;
}
@ -135,32 +127,39 @@ function postgresql_db_get_row ($table, $field_search, $condition, $fields = fal
return $result[0];
}
function postgresql_db_get_all_rows_sql ($sql, $search_history_db = false, $cache = true) {
function postgresql_db_get_all_rows_sql ($sql, $search_history_db = false, $cache = true, $dbconnection = false) {
global $config;
$history = array ();
if ($dbconnection === false) {
$dbconnection = $config['dbconnection'];
}
// To disable globally SQL cache depending on global variable.
// Used in several critical places like Metaconsole trans-server queries
if (isset($config["dbcache"]))
$cache = $config["dbcache"];
// Read from the history DB if necessary
if ($search_history_db) {
if ($search_history_db && $config['history_db_enabled'] == 1) {
$cache = false;
$history = false;
if (isset($config['history_db_connection']))
// Connect to the history DB
$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);
if ($history_db_connection !== false) {
$history = postgresql_db_process_sql ($sql, 'affected_rows', $config['history_db_connection'], false);
}
if ($history === false) {
$history = array ();
}
}
$return = postgresql_db_process_sql ($sql, 'affected_rows', $config['dbconnection'], $cache);
$return = postgresql_db_process_sql ($sql, 'affected_rows', $dbconnection, $cache);
if ($return === false) {
return false;
$return = array ();
}
// Append result to the history DB data
@ -610,9 +609,9 @@ function postgresql_db_format_array_where_clause_sql ($values, $join = 'AND', $p
* @return the first value of the first row of a table result from query.
*
*/
function postgresql_db_get_value_sql($sql) {
function postgresql_db_get_value_sql($sql, $dbconnection = false) {
$sql .= " LIMIT 1";
$result = db_get_all_rows_sql ($sql);
$result = postgresql_db_get_all_rows_sql ($sql, false, true, $dbconnection);
if($result === false)
return false;
@ -630,7 +629,7 @@ function postgresql_db_get_value_sql($sql) {
*/
function postgresql_db_get_row_sql ($sql, $search_history_db = false) {
$sql .= " LIMIT 1";
$result = db_get_all_rows_sql($sql, $search_history_db);
$result = postgresql_db_get_all_rows_sql($sql, $search_history_db);
if($result === false)
return false;
@ -1047,4 +1046,41 @@ function postgresql_db_get_type_field_table($table, $field) {
return pg_field_type($result, $field);
}
/**
* Get the element count of a table.
*
* @param string $sql SQL query to get the element count.
*
* @return int Return the number of elements in the table.
*/
function postgresql_db_get_table_count($sql, $search_history_db = false) {
global $config;
$history_count = 0;
$count = postgresql_db_get_value_sql ($sql);
if ($count === false) {
$count = 0;
}
// Search the history DB for matches
if ($search_history_db && $config['history_db_enabled'] == 1) {
// Connect to the history DB
if (! isset ($config['history_db_connection']) || $config['history_db_connection'] === false) {
$config['history_db_connection'] = postgresql_connect_db ($config['history_db_host'], $config['history_db_name'], $config['history_db_user'], $config['history_db_pass'], $config['history_db_port'], false);
}
if ($config['history_db_connection'] !== false) {
$history_count = postgresql_db_get_value_sql ($sql, $config['history_db_connection']);
if ($history_count === false) {
$history_count = 0;
}
}
}
$count += $history_count;
return $count;
}
?>

View File

@ -25,6 +25,7 @@ include_once($config['homedir'] . "/include/functions_agents.php");
include_once($config['homedir'] . "/include/functions_modules.php");
include_once($config['homedir'] . "/include/functions_alerts.php");
include_once($config['homedir'] . "/include/functions_users.php");
include_once($config['homedir'] . "/include/functions_ui.php");
function db_select_engine() {
global $config;
@ -42,20 +43,37 @@ function db_select_engine() {
}
}
function db_connect($host = null, $db = null, $user = null, $pass = null, $history = null, $port = null) {
function db_connect($host = null, $db = null, $user = null, $pass = null, $port = null, $critical = true) {
global $config;
static $error = 0;
switch ($config["dbtype"]) {
case "mysql":
return mysql_connect_db($host, $db, $user, $pass, $history, $port);
$return = mysql_connect_db($host, $db, $user, $pass, $port);
break;
case "postgresql":
return postgresql_connect_db($host, $db, $user, $pass, $history, $port);
$return = postgresql_connect_db($host, $db, $user, $pass, $port);
break;
case "oracle":
return oracle_connect_db($host, $db, $user, $pass, $history, $port);
$return = oracle_connect_db($host, $db, $user, $pass, $port);
break;
default:
$return = false;
}
// Something went wrong
if ($return === false) {
if ($critical) {
include ($config["homedir"]."/general/error_authconfig.php");
exit;
} else if ($error == 0) {
// Display the error once even if multiple connection attempts are made
$error = 1;
ui_print_error_message (__("Error connecting to database %s at %s.", $db, $host));
}
}
return $return;
}
/**
@ -1111,4 +1129,29 @@ function db_get_type_field_table($table, $field) {
break;
}
}
/**
* Get the element count of a table.
*
* @param string $sql SQL query to get the element count.
*
* @return int Return the number of elements in the table.
*/
function db_get_table_count($table, $search_history_db = false) {
global $config;
switch ($config["dbtype"]) {
case "mysql":
return mysql_db_get_table_count($table, $search_history_db);
break;
case "postgresql":
return postgresql_db_get_table_count($table, $search_history_db);
break;
case "oracle":
return oracle_db_get_table_count($table, $search_history_db);
break;
}
}
?>

View File

@ -66,6 +66,11 @@ else $graph_type = '';
//$ttl_param = get_parameter('ttl', 1);
//$homeurl_param = get_parameter('homeurl', '');
// Turn on output buffering.
// The entire buffer will be discarded later so that any accidental output
// does not corrupt images generated by fgraph.
ob_start ();
if (!empty($graph_type)) {
include_once($homeurl . 'include/functions.php');
include_once($homeurl . 'include/functions_html.php');
@ -74,6 +79,9 @@ if (!empty($graph_type)) {
include_once($homeurl . 'include/graphs/functions_utils.php');
}
// Clean the output buffer and turn off output buffering
ob_end_clean ();
switch($graph_type) {
case 'histogram':
$width = get_parameter('width');

View File

@ -122,7 +122,7 @@ $sql_body = io_safe_output($sql_body);
$sql = "SELECT * " . $sql_body;
$sql_count = "SELECT count(*) " . $sql_body;
$count = db_get_value_sql($sql_count);
$count = db_get_table_count($sql_count, true);
switch ($config["dbtype"]) {
case "mysql":
@ -139,7 +139,7 @@ switch ($config["dbtype"]) {
break;
}
$result = db_get_all_rows_sql ($sql);
$result = db_get_all_rows_sql ($sql, true);
if ($result === false) {
$result = array ();
}