diff --git a/pandora_console/ChangeLog b/pandora_console/ChangeLog index 232613c5bf..3409f56380 100644 --- a/pandora_console/ChangeLog +++ b/pandora_console/ChangeLog @@ -1,3 +1,15 @@ +2009-12-18 Ramon Novoa + + * include/functions_reporting.php: Fixed SLA calculation. Was not working. + + * include/functions_fsgraph.php: Fixed bug #2913813. + + * include/functions_config.php, include/config_process.php, + include/fgraph.php, include/functions_db.php, + operation/events/export_csv.php, operation/agentes/export_csv.php, + operation/agentes/datos_agente.php, godmode/menu.ph: Added history + database support. + 2009-12-17 Miguel de Dios * pandora_console/godmode/reporting/graph_builder.php: fix blank row when diff --git a/pandora_console/godmode/menu.php b/pandora_console/godmode/menu.php index 6807b40c62..506e862c17 100644 --- a/pandora_console/godmode/menu.php +++ b/pandora_console/godmode/menu.php @@ -165,6 +165,7 @@ if (give_acl ($config['id_user'], 0, "PM")) { $sub["godmode/setup/file_manager"]["text"] = __('File manager'); $sub["godmode/setup/links"]["text"] = __('Links'); $sub["godmode/setup/news"]["text"] = __('Site news'); + enterprise_hook ('historydb_submenu'); $menu["gsetup"]["sub"] = $sub; } diff --git a/pandora_console/include/config_process.php b/pandora_console/include/config_process.php index 2b8c6ca404..3a5342d704 100644 --- a/pandora_console/include/config_process.php +++ b/pandora_console/include/config_process.php @@ -97,4 +97,13 @@ require_once ('functions_extensions.php'); $config['extensions'] = get_extensions (); +// Connect to the history DB +if ($config['history_db_enabled']) { + $config['history_db_connection'] = mysql_connect ($config['history_db_host'] . ':' . $config['history_db_port'], $config['history_db_user'], $config['history_db_pass']); + mysql_select_db ($config['history_db_name'], $config['history_db_connection']); +} + +// Make dbconnection the default connection again (the link identifier of the already opened link will be returned) +$config['dbconnection'] = mysql_connect ($config["dbhost"], $config["dbuser"], $config["dbpass"]); + ?> diff --git a/pandora_console/include/fgraph.php b/pandora_console/include/fgraph.php index a55171179a..ea52f336d5 100644 --- a/pandora_console/include/fgraph.php +++ b/pandora_console/include/fgraph.php @@ -197,7 +197,7 @@ function graphic_combined_module ($module_list, $weight_list, $period, $width, $ "utimestamp > $datelimit", "utimestamp < $date", 'order' => 'utimestamp ASC'), - array ('datos', 'utimestamp')); + array ('datos', 'utimestamp'), 'AND', true); if ($result === false) { if (! $graphic_type) { @@ -341,7 +341,7 @@ function grafico_modulo_sparse ($id_agente_modulo, $period, $show_event, "utimestamp > $datelimit", "utimestamp < $date", 'order' => 'utimestamp ASC'), - array ('datos', 'utimestamp')); + array ('datos', 'utimestamp'), 'AND', true); if ($all_data === false) { if (! $graphic_type) { @@ -1165,17 +1165,17 @@ function grafico_db_agentes_purge ($id_agent, $width, $height) { // Three months ago $time["3month"] = $time["all"] - 7776000; - $data[__("Today")] = get_db_sql (sprintf ("SELECT COUNT(*) FROM tagente_datos WHERE utimestamp > %d %s", $time["1day"], $query)); - $data["1 ".__("Week")] = get_db_sql (sprintf ("SELECT COUNT(*) FROM tagente_datos WHERE utimestamp > %d %s", $time["1week"], $query)); - $data["1 ".__("Month")] = get_db_sql (sprintf ("SELECT COUNT(*) FROM tagente_datos WHERE utimestamp > %d %s", $time["1month"], $query)); - $data["3 ".__("Months")] = get_db_sql (sprintf ("SELECT COUNT(*) FROM tagente_datos WHERE utimestamp > %d %s", $time["3month"], $query)); + $data[__("Today")] = get_db_sql (sprintf ("SELECT COUNT(*) FROM tagente_datos WHERE utimestamp > %d %s", $time["1day"], $query), 0, true); + $data["1 ".__("Week")] = get_db_sql (sprintf ("SELECT COUNT(*) FROM tagente_datos WHERE utimestamp > %d %s", $time["1week"], $query), 0, true); + $data["1 ".__("Month")] = get_db_sql (sprintf ("SELECT COUNT(*) FROM tagente_datos WHERE utimestamp > %d %s", $time["1month"], $query), 0, true); + $data["3 ".__("Months")] = get_db_sql (sprintf ("SELECT COUNT(*) FROM tagente_datos WHERE utimestamp > %d %s", $time["3month"], $query), 0, true); $data[__("Older")] = get_db_sql (sprintf ("SELECT COUNT(*) FROM tagente_datos WHERE 1=1 %s", $query)); - $data[__("Today")] += get_db_sql (sprintf ("SELECT COUNT(*) FROM tagente_datos_string WHERE utimestamp > %d %s", $time["1day"], $query)); - $data["1 ".__("Week")] += get_db_sql (sprintf ("SELECT COUNT(*) FROM tagente_datos_string WHERE utimestamp > %d %s", $time["1week"], $query)); - $data["1 ".__("Month")] += get_db_sql (sprintf ("SELECT COUNT(*) FROM tagente_datos_string WHERE utimestamp > %d %s", $time["1month"], $query)); - $data["3 ".__("Months")] += get_db_sql (sprintf ("SELECT COUNT(*) FROM tagente_datos_string WHERE utimestamp > %d %s", $time["3month"], $query)); - $data[__("Older")] += get_db_sql (sprintf ("SELECT COUNT(*) FROM tagente_datos_string WHERE 1=1 %s", $query)); + $data[__("Today")] += get_db_sql (sprintf ("SELECT COUNT(*) FROM tagente_datos_string WHERE utimestamp > %d %s", $time["1day"], $query), 0, true); + $data["1 ".__("Week")] += get_db_sql (sprintf ("SELECT COUNT(*) FROM tagente_datos_string WHERE utimestamp > %d %s", $time["1week"], $query), 0, true); + $data["1 ".__("Month")] += get_db_sql (sprintf ("SELECT COUNT(*) FROM tagente_datos_string WHERE utimestamp > %d %s", $time["1month"], $query), 0, true); + $data["3 ".__("Months")] += get_db_sql (sprintf ("SELECT COUNT(*) FROM tagente_datos_string WHERE utimestamp > %d %s", $time["3month"], $query), 0, true); + $data[__("Older")] += get_db_sql (sprintf ("SELECT COUNT(*) FROM tagente_datos_string WHERE 1=1 %s", $query), 0, true); $data[__("Older")] = $data[__("Older")] - $data["3 ".__("Months")]; @@ -1267,8 +1267,8 @@ function grafico_modulo_boolean ($id_agente_modulo, $period, $show_event, $alert_low = false; // If we want to show alerts limits - $alert_high = get_db_value ('MAX(max_value)', 'talert_template_modules', 'id_agent_module', (int) $id_agente_modulo); - $alert_low = get_db_value ('MIN(min_value)', 'talert_template_modules', 'id_agent_module', (int) $id_agente_modulo); + $alert_high = get_db_value ('MAX(max_value)', 'talert_template_modules', 'id_agent_module', (int) $id_agente_modulo, true); + $alert_low = get_db_value ('MIN(min_value)', 'talert_template_modules', 'id_agent_module', (int) $id_agente_modulo, true); // if no valid alert defined to render limits, disable it if (($alert_low === false || $alert_low === NULL) && @@ -1313,7 +1313,7 @@ function grafico_modulo_boolean ($id_agente_modulo, $period, $show_event, "utimestamp > $datelimit", "utimestamp < $date", 'order' => 'utimestamp ASC'), - array ('datos', 'utimestamp')); + array ('datos', 'utimestamp'), 'AND', true); if ($all_data === false) $all_data = array (); @@ -1447,11 +1447,13 @@ function grafico_modulo_string ($id_agente_modulo, $period, $show_event, $sql1="SELECT utimestamp FROM tagente_datos_string WHERE id_agente_modulo = ".$id_agente_modulo." and utimestamp > '".$fechatope."'"; + $result = get_db_all_rows_sql ($sql1, true); + if ($result === false) + $result = array (); - $result=mysql_query($sql1); - while ($row=mysql_fetch_array($result)){ + foreach ($result as $row){ for ($i = 0; $i < $resolution; $i++){ - if (($row[0] < $valores[$i][3]) and ($row[0] >= $valores[$i][2]) ){ + if (($row['utimestamp'] < $valores[$i][3]) and ($row['utimestamp'] >= $valores[$i][2]) ){ // entra en esta fila $valores[$i][0]++; } diff --git a/pandora_console/include/functions_config.php b/pandora_console/include/functions_config.php index 2c35346ae9..b9c4b67363 100644 --- a/pandora_console/include/functions_config.php +++ b/pandora_console/include/functions_config.php @@ -163,6 +163,16 @@ function update_config () { update_config_value ('list_ACL_IPs_for_API', (string) get_parameter('list_ACL_IPs_for_API', implode("\n", $config['list_ACL_IPs_for_API']))); update_config_value ('custom_logo', (string) get_parameter ('custom_logo', $config["custom_logo"])); + update_config_value ('history_db_enabled', (bool) get_parameter ('history_db_enabled', $config['history_db_enabled'])); + update_config_value ('history_db_host', (string) get_parameter ('history_db_host', $config['history_db_host'])); + update_config_value ('history_db_port', (int) get_parameter ('history_db_port', $config['history_db_port'])); + update_config_value ('history_db_name', (string) get_parameter ('history_db_name', $config['history_db_name'])); + update_config_value ('history_db_user', (string) get_parameter ('history_db_user', $config['history_db_user'])); + update_config_value ('history_db_pass', (string) get_parameter ('history_db_pass', $config['history_db_pass'])); + update_config_value ('history_db_days', (string) get_parameter ('history_db_days', $config['history_db_days'])); + update_config_value ('history_db_step', (string) get_parameter ('history_db_step', $config['history_db_step'])); + update_config_value ('history_db_delay', (string) get_parameter ('history_db_delay', $config['history_db_delay'])); + } /** @@ -309,6 +319,42 @@ function process_config () { update_config_value ('custom_logo', 'none.png'); } + if (!isset ($config['history_db_enabled'])) { + update_config_value ( 'history_db_enabled', false); + } + + if (!isset ($config['history_db_host'])) { + update_config_value ( 'history_db_host', ''); + } + + if (!isset ($config['history_db_port'])) { + update_config_value ( 'history_db_port', 3306); + } + + if (!isset ($config['history_db_name'])) { + update_config_value ( 'history_db_name', 'pandora'); + } + + if (!isset ($config['history_db_user'])) { + update_config_value ( 'history_db_user', 'pandora'); + } + + if (!isset ($config['history_db_pass'])) { + update_config_value ( 'history_db_pass', ''); + } + + if (!isset ($config['history_db_days'])) { + update_config_value ( 'history_db_days', 0); + } + + if (!isset ($config['history_db_step'])) { + update_config_value ( 'history_db_step', 0); + } + + if (!isset ($config['history_db_delay'])) { + update_config_value ( 'history_db_delay', 0); + } + /* Finally, check if any value was overwritten in a form */ update_config (); } diff --git a/pandora_console/include/functions_db.php b/pandora_console/include/functions_db.php index 261c37e187..0070524be7 100644 --- a/pandora_console/include/functions_db.php +++ b/pandora_console/include/functions_db.php @@ -1127,7 +1127,7 @@ function get_agent_modules_data_count ($id_agent = 0) { $modules = array_keys (get_agent_modules ($agent_id)); foreach ($query as $sql) { //Add up each table's data - $count[$agent_id] += (int) get_db_sql ($sql." WHERE id_agente_modulo IN (".implode (",", $modules).")"); + $count[$agent_id] += (int) get_db_sql ($sql." WHERE id_agente_modulo IN (".implode (",", $modules).")", 0, true); } //Add total agent count to total count $count["total"] += $count[$agent_id]; @@ -1135,7 +1135,7 @@ function get_agent_modules_data_count ($id_agent = 0) { if ($count["total"] == 0) { foreach ($query as $sql) { - $count["total"] += (int) get_db_sql ($sql); + $count["total"] += (int) get_db_sql ($sql, 0, true); } } @@ -1602,7 +1602,7 @@ $sql_cache = array ('saved' => 0); * * @return mixed Value of first column of the first row. False if there were no row. */ -function get_db_value ($field, $table, $field_search = 1, $condition = 1) { +function get_db_value ($field, $table, $field_search = 1, $condition = 1, $search_history_db = false) { if (is_int ($condition)) { $sql = sprintf ("SELECT %s FROM %s WHERE %s = %d LIMIT 1", $field, $table, $field_search, $condition); @@ -1613,7 +1613,7 @@ function get_db_value ($field, $table, $field_search = 1, $condition = 1) { $sql = sprintf ("SELECT %s FROM %s WHERE %s = '%s' LIMIT 1", $field, $table, $field_search, $condition); } - $result = get_db_all_rows_sql ($sql); + $result = get_db_all_rows_sql ($sql, $search_history_db); if ($result === false) return false; @@ -1691,9 +1691,9 @@ function get_db_value_sql ($sql) { * * @return mixed The first row of the result or false */ -function get_db_row_sql ($sql) { +function get_db_row_sql ($sql, $search_history_db = false) { $sql .= " LIMIT 1"; - $result = get_db_all_rows_sql ($sql); + $result = get_db_all_rows_sql ($sql, $search_history_db); if($result === false) return false; @@ -1796,8 +1796,8 @@ function get_db_row_filter ($table, $filter, $fields = false, $where_join = 'AND * * @return mixed The selected field of the first row in a select statement. */ -function get_db_sql ($sql, $field = 0) { - $result = get_db_all_rows_sql ($sql); +function get_db_sql ($sql, $field = 0, $search_history_db = false) { + $result = get_db_all_rows_sql ($sql, $search_history_db); if($result === false) return false; @@ -1812,11 +1812,34 @@ function get_db_sql ($sql, $field = 0) { * @return mixed A matrix with all the values returned from the SQL statement or * false in case of empty result */ -function get_db_all_rows_sql ($sql) { - $return = process_sql ($sql); - - if (! empty ($return)) - return $return; +function get_db_all_rows_sql ($sql, $search_history_db = false) { + global $config; + $cache = true; + $history = array (); + + // Read from the history DB if necessary + if ($search_history_db) { + $cache = false; + $history = process_sql ($sql, 'affected_rows', $config['history_db_connection'], false); + if ($history === false) { + $history = array (); + } + } + + $return = process_sql ($sql, 'affected_rows', $config['dbconnection'], $cache); + if ($return === false) { + return false; + } + + // Append result to the history DB data + if (! empty ($return)) { + foreach ($return as $row) { + array_push ($history, $row); + } + } + + if (! empty ($history)) + return $history; //Return false, check with === or !== return false; } @@ -1845,7 +1868,7 @@ function get_db_all_rows_sql ($sql) { * * @return mixed Array of the row or false in case of error. */ -function get_db_all_rows_filter ($table, $filter, $fields = false, $where_join = 'AND', $returnSQL = false) { +function get_db_all_rows_filter ($table, $filter, $fields = false, $where_join = 'AND', $search_history_db = false, $returnSQL = false) { //TODO: Validate and clean fields if (empty ($fields)) { $fields = '*'; @@ -1868,7 +1891,7 @@ function get_db_all_rows_filter ($table, $filter, $fields = false, $where_join = if ($returnSQL) return $sql; else - return get_db_all_rows_sql ($sql); + return get_db_all_rows_sql ($sql, $search_history_db); } /** @@ -1971,7 +1994,7 @@ function add_database_debug_trace ($sql, $result = false, $affected = false, $ex * * @return mixed An array with the rows, columns and values in a multidimensional array or false in error */ -function process_sql ($sql, $rettype = "affected_rows") { +function process_sql ($sql, $rettype = "affected_rows", $dbconnection = '', $cache = true) { global $config; global $sql_cache; @@ -1980,14 +2003,19 @@ function process_sql ($sql, $rettype = "affected_rows") { if ($sql == '') return false; - if (! empty ($sql_cache[$sql])) { + if ($cache && ! empty ($sql_cache[$sql])) { $retval = $sql_cache[$sql]; $sql_cache['saved']++; add_database_debug_trace ($sql); } else { $start = microtime (true); - $result = mysql_query ($sql); + if ($dbconnection == '') { + $result = mysql_query ($sql); + } + else { + $result = mysql_query ($sql, $dbconnection); + } $time = microtime (true) - $start; if ($result === false) { $backtrace = debug_backtrace (); @@ -2395,7 +2423,7 @@ function get_previous_data ($id_agent_module, $utimestamp = 0) { ORDER BY utimestamp DESC', $id_agent_module, $utimestamp, $utimestamp - $interval); - return get_db_row_sql ($sql); + return get_db_row_sql ($sql, true); } /** @@ -2418,7 +2446,7 @@ function get_agentmodule_data ($id_agent_module, $period, $date = 0) { AND utimestamp > %d AND utimestamp <= %d ORDER BY utimestamp ASC", $id_agent_module, $datelimit, $date); - $values = get_db_all_rows_sql ($sql); + $values = get_db_all_rows_sql ($sql, true); if ($values === false) { return array (); @@ -2459,7 +2487,7 @@ function get_agentmodule_data_average ($id_agent_module, $period, $date = 0) { ORDER BY utimestamp ASC", $id_agent_module, $datelimit, $date); - $values = get_db_row_sql ($sql); + $values = get_db_row_sql ($sql, true); $sum = (float) $values[0]; $total = (int) $values[1]; @@ -2494,7 +2522,7 @@ function get_agentmodule_data_max ($id_agent_module, $period, $date = 0) { WHERE id_agente_modulo = %d AND utimestamp > %d AND utimestamp <= %d", $id_agent_module, $datelimit, $date); - $max = (float) get_db_sql ($sql); + $max = (float) get_db_sql ($sql, 0, true); /* Get also the previous report before the selected interval. */ $previous_data = get_previous_data ($id_agent_module, $datelimit); @@ -2522,7 +2550,7 @@ function get_agentmodule_data_min ($id_agent_module, $period, $date = 0) { WHERE id_agente_modulo = %d AND utimestamp > %d AND utimestamp <= %d", $id_agent_module, $datelimit, $date); - $min = (float) get_db_sql ($sql); + $min = (float) get_db_sql ($sql, 0, true); /* Get also the previous data before the selected interval. */ $previous_data = get_previous_data ($id_agent_module, $datelimit); @@ -2557,7 +2585,7 @@ function get_agentmodule_data_sum ($id_agent_module, $period, $date = 0) { AND utimestamp > %d AND utimestamp <= %d ORDER BY utimestamp ASC', $id_agent_module, $datelimit, $date); - $datas = get_db_all_rows_sql ($sql); + $datas = get_db_all_rows_sql ($sql, true); /* Get also the previous data before the selected interval. */ $previous_data = get_previous_data ($id_agent_module, $datelimit); diff --git a/pandora_console/include/functions_fsgraph.php b/pandora_console/include/functions_fsgraph.php index cb38c08a61..a60ce05096 100644 --- a/pandora_console/include/functions_fsgraph.php +++ b/pandora_console/include/functions_fsgraph.php @@ -192,7 +192,7 @@ function fs_module_chart ($data, $width, $height, $avg_only = 1, $step = 10, $ti $output .= '