diff --git a/pandora_console/ChangeLog b/pandora_console/ChangeLog index ccae7bf6c6..046eb7f8d1 100644 --- a/pandora_console/ChangeLog +++ b/pandora_console/ChangeLog @@ -1,3 +1,15 @@ +2009-09-16 Miguel de Dios + + * include/functions_reporting.php, include/functions_agents.php: cleanup + sourcecode, and important optimize, because the old querys was very huge + when the query have consult with all agent ids. That affect when login + as admin into Pandora Console in systems with few resources (more or + less 1Gb RAM in a desktop computer). + * include/functions_db.php: add function "isAllGroups" that test if the + array pass as parameter is all groups in DB, and return true o false. And + cleanup source code. + Fixes: 2824327 + 2009-09-16 Miguel de Dios * include/functions_db.php: cleanup the source code. diff --git a/pandora_console/include/functions_agents.php b/pandora_console/include/functions_agents.php index ea8cc17336..d7e130378f 100644 --- a/pandora_console/include/functions_agents.php +++ b/pandora_console/include/functions_agents.php @@ -106,34 +106,42 @@ function create_agent ($name, $id_group, $interval, $ip_address, $values = false * @return array All simple alerts defined for an agent. Empty array if no * alerts found. */ -function get_agent_alerts_simple ($id_agent, $filter = '', $options = false, $where = '') { +function get_agent_alerts_simple ($id_agent = false, $filter = '', $options = false, $where = '') { switch ($filter) { - case "notfired": - $filter = ' AND times_fired = 0 AND disabled = 0'; - break; - case "fired": - $filter = ' AND times_fired > 0 AND disabled = 0'; - break; - case "disabled": - $filter = ' AND disabled = 1'; - break; - default: - $filter = ''; + case "notfired": + $filter = ' AND times_fired = 0 AND disabled = 0'; + break; + case "fired": + $filter = ' AND times_fired > 0 AND disabled = 0'; + break; + case "disabled": + $filter = ' AND disabled = 1'; + break; + default: + $filter = ''; } - $id_agent = (array) $id_agent; - $id_modules = array_keys (get_agent_modules ($id_agent)); - if (empty ($id_modules)) - return array (); - if (is_array ($options)) { $filter .= format_array_to_where_clause_sql ($options); } + if ($id_agent === false) { + $subQuery = 'SELECT id_agente_modulo + FROM tagente_modulo WHERE disabled = 0'; + } + else { + $id_agent = (array) $id_agent; + $id_modules = array_keys (get_agent_modules ($id_agent)); + if (empty ($id_modules)) + return array (); + + $subQuery = implode (",", $id_modules); + } + $sql = sprintf ("SELECT talert_template_modules.* - FROM talert_template_modules - WHERE id_agent_module in (%s) %s %s", - implode (",", $id_modules), $where, $filter); + FROM talert_template_modules + WHERE id_agent_module in (%s) %s %s", + $subQuery, $where, $filter); $alerts = get_db_all_rows_sql ($sql); @@ -152,7 +160,7 @@ function get_agent_alerts_simple ($id_agent, $filter = '', $options = false, $wh * * @return array An array with all combined alerts defined for an agent. */ -function get_agent_alerts_compound ($id_agent, $filter = '', $options = false) { +function get_agent_alerts_compound ($id_agent = false, $filter = '', $options = false) { switch ($filter) { case "notfired": $filter = ' AND times_fired = 0 AND disabled = 0'; @@ -171,11 +179,19 @@ function get_agent_alerts_compound ($id_agent, $filter = '', $options = false) { $filter .= format_array_to_where_clause_sql ($options); } - $id_agent = (array) $id_agent; + if ($id_agent === false) { + $subQuery = 'SELECT id_agente + FROM tagente WHERE disabled = 0'; + } + else { + $id_agent = (array) $id_agent; + + $subQuery = implode (',', $id_agent); + } $sql = sprintf ("SELECT * FROM talert_compound WHERE id_agent IN (%s)%s", - implode (',', $id_agent), $filter); + $subQuery, $filter); $alerts = get_db_all_rows_sql ($sql); @@ -249,9 +265,9 @@ function get_agents ($filter = false, $fields = false, $access = 'AR') { * * @return array An array with all alerts defined for an agent. */ -function get_agent_alerts ($id_agent, $filter = false, $options = false) { - $simple_alerts = get_agent_alerts_simple ($id_agent, $filter, $options); +function get_agent_alerts ($id_agent = false, $filter = false, $options = false) { $combined_alerts = get_agent_alerts_compound ($id_agent, $filter, $options); + $simple_alerts = get_agent_alerts_simple ($id_agent, $filter, $options); return array ('simple' => $simple_alerts, 'compounds' => $combined_alerts); } diff --git a/pandora_console/include/functions_db.php b/pandora_console/include/functions_db.php index b1339745a1..f72f8788ca 100644 --- a/pandora_console/include/functions_db.php +++ b/pandora_console/include/functions_db.php @@ -319,6 +319,30 @@ function give_disabled_group ($id_group) { return (bool) get_db_value ('disabled', 'tgrupo', 'id_grupo', (int) $id_group); } +/** + * Test if the param array is all groups in db. + * + * @param array $id_groups + * + * @return bool It's true when the array is all groups in db. + */ +function isAllGroups($idGroups) { + if (!is_array($idGroups)) + $arrayGroups = array($idGroups); + + $groupsDB = get_db_all_rows_in_table ('tgrupo'); + + $returnVar = true; + foreach ($groupsDB as $group) { + if (!in_array($group['id_grupo'],$idGroups)) { + $returnVar = false; + break; + } + } + + return $returnVar; +} + /** * Get all the agents within a group(s). * @@ -1891,7 +1915,8 @@ function process_sql ($sql, $rettype = "affected_rows") { $retval = $sql_cache[$sql]; $sql_cache['saved']++; add_database_debug_trace ($sql); - } else { + } + else { $start = microtime (true); $result = mysql_query ($sql); $time = microtime (true) - $start; @@ -1904,19 +1929,23 @@ function process_sql ($sql, $rettype = "affected_rows") { trigger_error ($error); restore_error_handler (); return false; - } elseif ($result === true) { + } + elseif ($result === true) { if ($rettype == "insert_id") { $result = mysql_insert_id (); - } elseif ($rettype == "info") { + } + elseif ($rettype == "info") { $result = mysql_info (); - } else { + } + else { $result = mysql_affected_rows (); } add_database_debug_trace ($sql, $result, mysql_affected_rows (), array ('time' => $time)); return $result; - } else { + } + else { add_database_debug_trace ($sql, 0, mysql_affected_rows (), array ('time' => $time)); while ($row = mysql_fetch_array ($result)) { diff --git a/pandora_console/include/functions_reporting.php b/pandora_console/include/functions_reporting.php index 8cf5a3c57d..e66d5a5a37 100644 --- a/pandora_console/include/functions_reporting.php +++ b/pandora_console/include/functions_reporting.php @@ -124,14 +124,27 @@ function get_group_stats ($id_group = 0) { if ($id_group == 0) { $id_group = array_keys (get_user_groups ()); } - $agents = array_keys (get_group_agents ($id_group)); - if (empty ($agents)) { - //No agents in this group, means no data - return $data; + if (isAllGroups($id_group)) { + $filter = ' 1 = 1 '; + $total_agents = get_db_value_sql('SELECT count(id_agente) FROM tagente WHERE disabled = 0'); + $alerts = get_agent_alerts (); } - - $filter = 'id_agente IN ('.implode (",", $agents).') '; + else { + $agents = array_keys (get_group_agents ($id_group)); + $total_agents = count ($agents); + + if (empty ($agents)) { + //No agents in this group, means no data + return $data; + } + $filter = 'id_agente IN ('.implode (",", $agents).') '; + + $alerts = get_agent_alerts ($agents); + } + + if (empty ($alerts)) + $alerts = array (); $data["monitor_checks"] = (int) get_db_sql ("SELECT COUNT(*) FROM tagente_estado WHERE ".$filter); $data["monitor_not_init"] = (int) get_db_sql ("SELECT COUNT(*) FROM tagente_estado WHERE ".$filter."AND utimestamp = 0"); @@ -140,11 +153,6 @@ function get_group_stats ($id_group = 0) { $data["monitor_warning"] = (int) get_db_sql ("SELECT COUNT(*) FROM tagente_estado WHERE ".$filter."AND utimestamp > 0 AND estado = 2 AND UNIX_TIMESTAMP() - utimestamp < current_interval * 2"); $data["monitor_ok"] = $data["monitor_checks"] - $data["monitor_not_init"] - $data["monitor_unknown"] - $data["monitor_critical"] - $data["monitor_warning"]; - $alerts = get_agent_alerts ($agents); - - if (empty ($alerts)) - $alerts = array (); - $data["monitor_alerts"] = 0; foreach ($alerts as $alert_type) { $data["monitor_alerts"] += count ($alert_type); @@ -156,7 +164,7 @@ function get_group_stats ($id_group = 0) { } } - $data["total_agents"] = count ($agents); + $data["total_agents"] = $total_agents; $data["total_checks"] = $data["monitor_checks"]; $data["total_ok"] = $data["monitor_ok"]; //TODO: count SNMP Alerts and Inventory alerts here