diff --git a/pandora_console/include/class/Tree.class.php b/pandora_console/include/class/Tree.class.php index 1ef7129f9d..e18cfb599e 100644 --- a/pandora_console/include/class/Tree.class.php +++ b/pandora_console/include/class/Tree.class.php @@ -155,23 +155,23 @@ class Tree { return array( 'warning' => array( 'header' => "0 AS x_critical, SUM(total) AS x_warning, 0 AS x_normal, 0 AS x_unknown, 0 AS x_not_init, 0 AS x_alerts, 0 AS x_total, g", - 'condition' => "AND ta.warning_count > 0 AND ta.critical_count = 0" + 'condition' => "AND " . agents_get_status_clause(AGENT_STATUS_WARNING, $this->filter['show_not_init_agents']) ), 'critical' => array( 'header' => "SUM(total) AS x_critical, 0 AS x_warning, 0 AS x_normal, 0 AS x_unknown, 0 AS x_not_init, 0 AS x_alerts, 0 AS x_total, g", - 'condition' => "AND ta.critical_count > 0" + 'condition' => "AND " . agents_get_status_clause(AGENT_STATUS_CRITICAL, $this->filter['show_not_init_agents']) ), 'normal' => array( 'header' => "0 AS x_critical, 0 AS x_warning, SUM(total) AS x_normal, 0 AS x_unknown, 0 AS x_not_init, 0 AS x_alerts, 0 AS x_total, g", - 'condition' => "AND ta.critical_count = 0 AND ta.warning_count = 0 AND ta.unknown_count = 0 AND ta.normal_count > 0" + 'condition' => "AND " . agents_get_status_clause(AGENT_STATUS_NORMAL, $this->filter['show_not_init_agents']) ), 'unknown' => array( 'header' => "0 AS x_critical, 0 AS x_warning, 0 AS x_normal, SUM(total) AS x_unknown, 0 AS x_not_init, 0 AS x_alerts, 0 AS x_total, g", - 'condition' => "AND ta.critical_count = 0 AND ta.warning_count = 0 AND ta.unknown_count > 0" + 'condition' => "AND " . agents_get_status_clause(AGENT_STATUS_UNKNOWN, $this->filter['show_not_init_agents']) ), 'not_init' => array( 'header' => "0 AS x_critical, 0 AS x_warning, 0 AS x_normal, 0 AS x_unknown, SUM(total) AS x_not_init, 0 AS x_alerts, 0 AS x_total, g", - 'condition' => $this->filter['show_not_init_agents'] ? "AND ta.total_count = ta.notinit_count" : " AND 1=0" + 'condition' => "AND " . agents_get_status_clause(AGENT_STATUS_NOT_INIT, $this->filter['show_not_init_agents']) ), 'alerts' => array( 'header' => "0 AS x_critical, 0 AS x_warning, 0 AS x_normal, 0 AS x_unknown, 0 AS x_not_init, SUM(total) AS x_alerts, 0 AS x_total, g", @@ -179,7 +179,7 @@ class Tree { ), 'total' => array( 'header' => "0 AS x_critical, 0 AS x_warning, 0 AS x_normal, 0 AS x_unknown, 0 AS x_not_init, 0 AS x_alerts, SUM(total) AS x_total, g", - 'condition' => $this->filter['show_not_init_agents'] ? "" : "AND ta.total_count <> ta.notinit_count" + 'condition' => "AND " . agents_get_status_clause(AGENT_STATUS_ALL, $this->filter['show_not_init_agents']) ) ); } @@ -232,43 +232,9 @@ class Tree { ? $state : $this->filter['statusModule']; - $filter = array(); - switch ($selected_status) { - case AGENT_MODULE_STATUS_CRITICAL_ALERT: - case AGENT_MODULE_STATUS_CRITICAL_BAD: - $filter[] = "( - tae.estado = ".AGENT_MODULE_STATUS_CRITICAL_ALERT." - OR tae.estado = ".AGENT_MODULE_STATUS_CRITICAL_BAD." - )"; - break; - case AGENT_MODULE_STATUS_WARNING_ALERT: - case AGENT_MODULE_STATUS_WARNING: - $filter[] = "( - tae.estado = ".AGENT_MODULE_STATUS_WARNING_ALERT." - OR tae.estado = ".AGENT_MODULE_STATUS_WARNING." - )"; - break; - case AGENT_MODULE_STATUS_UNKNOWN: - $filter[] = "tae.estado = ".AGENT_MODULE_STATUS_UNKNOWN." "; - break; - case AGENT_MODULE_STATUS_NO_DATA: - case AGENT_MODULE_STATUS_NOT_INIT: - $filter[] = "( - tae.estado = ".AGENT_MODULE_STATUS_NO_DATA." - OR tae.estado = ".AGENT_MODULE_STATUS_NOT_INIT." - )"; - break; - case AGENT_MODULE_STATUS_NORMAL_ALERT: - case AGENT_MODULE_STATUS_NORMAL: - $filter[] = "( - tae.estado = ".AGENT_MODULE_STATUS_NORMAL_ALERT." - OR tae.estado = ".AGENT_MODULE_STATUS_NORMAL." - )"; - break; - default: - $filter[] = "1=1"; - break; - } + $filter = array( + modules_get_state_condition($selected_status) + ); if (!$this->filter['show_not_init_modules'] && $state === false) { if (!empty($filter)) $filter[] = "( diff --git a/pandora_console/include/functions_agents.php b/pandora_console/include/functions_agents.php index e0b7f284cd..5bdd03a418 100644 --- a/pandora_console/include/functions_agents.php +++ b/pandora_console/include/functions_agents.php @@ -2742,4 +2742,33 @@ function agents_check_access_agent ($id_agent, $access = "AR") { // Return null otherwise return null; } + +function agents_get_status_clause($state, $show_not_init = true) { + switch ($state) { + case AGENT_STATUS_CRITICAL: + return "(ta.critical_count > 0)"; + case AGENT_STATUS_WARNING: + return "(ta.warning_count > 0 AND ta.critical_count = 0)"; + case AGENT_STATUS_UNKNOWN: + return "( + ta.critical_count = 0 AND ta.warning_count = 0 AND ta.unknown_count > 0 + )"; + case AGENT_STATUS_NOT_INIT: + return $show_not_init + ? "(ta.total_count = ta.notinit_count)" + : "1=0"; + case AGENT_STATUS_NORMAL: + return "( + ta.critical_count = 0 AND ta.warning_count = 0 + AND ta.unknown_count = 0 AND ta.normal_count > 0 + )"; + case AGENT_STATUS_ALL: + default: + return $show_not_init + ? "1=1" + : "(ta.total_count <> ta.notinit_count)"; + } + // If the state is not an expected state, return no condition + return "1=1"; +} ?> diff --git a/pandora_console/include/functions_groups.php b/pandora_console/include/functions_groups.php index 443d84c07f..98c13d5db7 100644 --- a/pandora_console/include/functions_groups.php +++ b/pandora_console/include/functions_groups.php @@ -1956,85 +1956,159 @@ function groups_get_not_init_monitors ($group, $agent_filter = array(), $module_ } // Get alerts defined for a given group, except disabled - -function groups_monitor_alerts ($group_array, $strict_user = false, $id_group_strict = false) { - - // If there are not groups to query, we jump to nextone - - if (empty ($group_array)) { - return 0; - - } - else if (!is_array ($group_array)) { - $group_array = array($group_array); - } - - $group_clause = implode (",", $group_array); - $group_clause = "(" . $group_clause . ")"; - - if ($strict_user) { - $sql = "SELECT COUNT(talert_template_modules.id) - FROM talert_template_modules, tagente_modulo, tagente_estado, tagente - WHERE tagente.id_grupo = $id_group_strict AND tagente_modulo.id_agente = tagente.id_agente - AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo - AND tagente_modulo.disabled = 0 AND tagente.disabled = 0 - AND talert_template_modules.disabled = 0 - AND talert_template_modules.id_agent_module = tagente_modulo.id_agente_modulo"; - $count = db_get_sql ($sql); - return $count; - } else { - //TODO REVIEW ORACLE AND POSTGRES - return db_get_sql ("SELECT COUNT(talert_template_modules.id) - FROM talert_template_modules, tagente_modulo, tagente_estado, tagente - WHERE tagente.id_grupo IN $group_clause AND tagente_modulo.id_agente = tagente.id_agente - AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo - AND tagente_modulo.disabled = 0 AND tagente.disabled = 0 - AND talert_template_modules.disabled = 0 - AND talert_template_modules.id_agent_module = tagente_modulo.id_agente_modulo"); - } +function groups_monitor_alerts ($group_array) { + $total = groups_monitor_alerts_total_counters($group_array); + return $total['total']; } // Get alert configured currently FIRED, except disabled +function groups_monitor_fired_alerts ($group_array) { + $total = groups_monitor_alerts_total_counters($group_array); + return $total['fired']; +} -function groups_monitor_fired_alerts ($group_array, $strict_user = false, $id_group_strict = false) { - +function groups_monitor_alerts_total_counters ($group_array) { // If there are not groups to query, we jump to nextone - + $default_total = array('total' => 0, 'fired' => 0); if (empty ($group_array)) { - return 0; - + return $default_total; } else if (!is_array ($group_array)) { $group_array = array($group_array); } - - $group_clause = implode (",", $group_array); - $group_clause = "(" . $group_clause . ")"; - - if ($strict_user) { - $sql = "SELECT COUNT(talert_template_modules.id) - FROM talert_template_modules, tagente_modulo, tagente_estado, tagente - WHERE tagente.id_grupo = $id_group_strict AND tagente_modulo.id_agente = tagente.id_agente - AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo - AND tagente_modulo.disabled = 0 AND tagente.disabled = 0 - AND talert_template_modules.disabled = 0 - AND talert_template_modules.id_agent_module = tagente_modulo.id_agente_modulo - AND times_fired > 0 "; - $count = db_get_sql ($sql); - return $count; - } else { - //TODO REVIEW ORACLE AND POSTGRES - return db_get_sql ("SELECT COUNT(talert_template_modules.id) - FROM talert_template_modules, tagente_modulo, tagente_estado, tagente - WHERE tagente.id_grupo IN $group_clause AND tagente_modulo.id_agente = tagente.id_agente - AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo - AND tagente_modulo.disabled = 0 AND tagente.disabled = 0 - AND talert_template_modules.disabled = 0 - AND talert_template_modules.id_agent_module = tagente_modulo.id_agente_modulo - AND times_fired > 0"); + $group_clause = implode (",", $group_array); + $group_clause = "(tasg.id_group IN ($group_clause) OR ta.id_grupo IN ($group_clause))"; + + $alerts = db_get_row_sql ("SELECT + COUNT(tatm.id) AS total, + SUM(IF(tatm.times_fired > 0, 1, 0)) AS fired + FROM talert_template_modules tatm + INNER JOIN tagente_modulo tam + ON tatm.id_agent_module = tam.id_agente_modulo + INNER JOIN tagente ta + ON ta.id_agente = tam.id_agente + WHERE ta.id_agente IN ( + SELECT ta.id_agente + FROM tagente ta + LEFT JOIN tagent_secondary_group tasg + ON ta.id_agente = tasg.id_agent + WHERE ta.disabled = 0 + AND $group_clause + ) AND tam.disabled = 0" + ); + + return ($alerts === false) ? $default_total : $alerts; +} + +function groups_monitor_total_counters ($group_array, $search_in_testado = false) { + $default_total = array( + 'ok' => 0, 'critical' => 0, 'warning' => 0, + 'unknown' => 0, 'not_init' => 0, 'total' => 0 + ); + if (empty ($group_array)) { + return $default_total; } - + else if (!is_array ($group_array)) { + $group_array = array($group_array); + } + $group_clause = implode (",", $group_array); + $group_clause = "(tasg.id_group IN ($group_clause) OR ta.id_grupo IN ($group_clause))"; + + if ($search_in_testado) { + $condition_critical = modules_get_state_condition(AGENT_MODULE_STATUS_CRITICAL_ALERT); + $condition_warning = modules_get_state_condition(AGENT_MODULE_STATUS_WARNING_ALERT); + $condition_unknown = modules_get_state_condition(AGENT_MODULE_STATUS_UNKNOWN); + $condition_not_init = modules_get_state_condition(AGENT_MODULE_STATUS_NO_DATA); + $condition_normal = modules_get_state_condition(AGENT_MODULE_STATUS_NORMAL); + $sql = + "SELECT SUM(IF($condition_normal, 1, 0)) AS ok, + SUM(IF($condition_critical, 1, 0)) AS critical, + SUM(IF($condition_warning, 1, 0)) AS warning, + SUM(IF($condition_unknown, 1, 0)) AS unknown, + SUM(IF($condition_not_init, 1, 0)) AS not_init, + COUNT(tam.id_agente_modulo) AS total + FROM tagente ta + INNER JOIN tagente_modulo tam + ON ta.id_agente = tam.id_agente + INNER JOIN tagente_estado tae + ON tam.id_agente_modulo = tae.id_agente_modulo + WHERE ta.disabled = 0 AND tam.disabled = 0 + AND ta.id_agente IN ( + SELECT ta.id_agente FROM tagente ta + LEFT JOIN tagent_secondary_group tasg + ON ta.id_agente = tasg.id_agent + WHERE ta.disabled = 0 + AND $group_clause + GROUP BY ta.id_agente + ) + "; + } else { + $sql = + "SELECT SUM(ta.normal_count) AS ok, + SUM(ta.critical_count) AS critical, + SUM(ta.warning_count) AS warning, + SUM(ta.unknown_count) AS unknown, + SUM(ta.notinit_count) AS not_init, + SUM(ta.total_count) AS total + FROM tagente ta + WHERE ta.disabled = 0 + AND ta.id_agente IN ( + SELECT ta.id_agente FROM tagente ta + LEFT JOIN tagent_secondary_group tasg + ON ta.id_agente = tasg.id_agent + WHERE ta.disabled = 0 + AND $group_clause + GROUP BY ta.id_agente + ) + "; + } + $monitors = db_get_row_sql($sql); + + return ($monitors === false) ? $default_total : $monitors; +} + +function groups_agents_total_counters ($group_array) { + $default_total = array( + 'ok' => 0, 'critical' => 0, 'warning' => 0, + 'unknown' => 0, 'not_init' => 0, 'total' => 0 + ); + if (empty ($group_array)) { + return $default_total; + } + else if (!is_array ($group_array)) { + $group_array = array($group_array); + } + $group_clause = implode (",", $group_array); + $group_clause = "(tasg.id_group IN ($group_clause) OR ta.id_grupo IN ($group_clause))"; + + $condition_critical = agents_get_status_clause(AGENT_STATUS_CRITICAL); + $condition_warning = agents_get_status_clause(AGENT_STATUS_WARNING); + $condition_unknown = agents_get_status_clause(AGENT_STATUS_UNKNOWN); + $condition_not_init = agents_get_status_clause(AGENT_STATUS_NOT_INIT); + $condition_normal = agents_get_status_clause(AGENT_STATUS_NORMAL); + $sql = + "SELECT SUM(IF($condition_normal, 1, 0)) AS ok, + SUM(IF($condition_critical, 1, 0)) AS critical, + SUM(IF($condition_warning, 1, 0)) AS warning, + SUM(IF($condition_unknown, 1, 0)) AS unknown, + SUM(IF($condition_not_init, 1, 0)) AS not_init, + COUNT(ta.id_agente) AS total + FROM tagente ta + WHERE ta.disabled = 0 + AND ta.id_agente IN ( + SELECT ta.id_agente FROM tagente ta + LEFT JOIN tagent_secondary_group tasg + ON ta.id_agente = tasg.id_agent + WHERE ta.disabled = 0 + AND $group_clause + GROUP BY ta.id_agente + ) + "; + + $agents = db_get_row_sql($sql); + + return ($agents === false) ? $default_total : $agents; } /** @@ -2771,7 +2845,7 @@ function group_get_data ($id_user = false, $user_strict = false, $acltags, $retu $list[$i]['_monitors_warning_'] = (int) groups_get_warning_monitors ($id, $agent_filter, $module_filter, $user_strict, $acltags, $config["realtimestats"]); $list[$i]['_monitors_unknown_'] = (int) groups_get_unknown_monitors ($id, $agent_filter, $module_filter, $user_strict, $acltags, $config["realtimestats"]); $list[$i]['_monitors_not_init_'] = (int) groups_get_not_init_monitors ($id, $agent_filter, $module_filter, $user_strict, $acltags, $config["realtimestats"]); - $list[$i]['_monitors_alerts_fired_'] = groups_monitor_fired_alerts ($id, $user_strict, $id); + $list[$i]['_monitors_alerts_fired_'] = groups_monitor_fired_alerts ($id); $list[$i]['_total_agents_'] = (int) groups_get_total_agents ($id, $agent_filter, $module_filter, $user_strict, $acltags, $config["realtimestats"]); $list[$i]['_agents_unknown_'] = (int) groups_get_unknown_agents ($id, $agent_filter, $module_filter, $user_strict, $acltags, $config["realtimestats"]); $list[$i]['_agents_not_init_'] = (int) groups_get_not_init_agents ($id, $agent_filter, $module_filter, $user_strict, $acltags, $config["realtimestats"]); @@ -2780,7 +2854,7 @@ function group_get_data ($id_user = false, $user_strict = false, $acltags, $retu $list[$i]['_agents_ok_'] = (int) groups_get_normal_agents ($id, $agent_filter, $module_filter, $user_strict, $acltags, $config["realtimestats"]); $list[$i]['_agents_warning_'] = (int) groups_get_warning_agents ($id, $agent_filter, $module_filter, $user_strict, $acltags, $config["realtimestats"]); $list[$i]['_agents_critical_'] = (int) groups_get_critical_agents ($id, $agent_filter, $module_filter, $user_strict, $acltags, $config["realtimestats"]); - $list[$i]['_monitors_alerts_'] = groups_monitor_alerts ($id, $user_strict, $id); + $list[$i]['_monitors_alerts_'] = groups_monitor_alerts ($id); // TODO //~ $list[$i]["_total_checks_"] diff --git a/pandora_console/include/functions_modules.php b/pandora_console/include/functions_modules.php index 00e643e7ca..f8e632dad4 100755 --- a/pandora_console/include/functions_modules.php +++ b/pandora_console/include/functions_modules.php @@ -2775,4 +2775,37 @@ function modules_get_counter_by_states($state) { // to not show any data return false; } -?> + +function modules_get_state_condition($state) { + switch ($state) { + case AGENT_MODULE_STATUS_CRITICAL_ALERT: + case AGENT_MODULE_STATUS_CRITICAL_BAD: + return "( + tae.estado = ".AGENT_MODULE_STATUS_CRITICAL_ALERT." + OR tae.estado = ".AGENT_MODULE_STATUS_CRITICAL_BAD." + )"; + case AGENT_MODULE_STATUS_WARNING_ALERT: + case AGENT_MODULE_STATUS_WARNING: + return "( + tae.estado = ".AGENT_MODULE_STATUS_WARNING_ALERT." + OR tae.estado = ".AGENT_MODULE_STATUS_WARNING." + )"; + case AGENT_MODULE_STATUS_UNKNOWN: + return "tae.estado = ".AGENT_MODULE_STATUS_UNKNOWN." "; + case AGENT_MODULE_STATUS_NO_DATA: + case AGENT_MODULE_STATUS_NOT_INIT: + return "( + tae.estado = ".AGENT_MODULE_STATUS_NO_DATA." + OR tae.estado = ".AGENT_MODULE_STATUS_NOT_INIT." + )"; + case AGENT_MODULE_STATUS_NORMAL_ALERT: + case AGENT_MODULE_STATUS_NORMAL: + return "( + tae.estado = ".AGENT_MODULE_STATUS_NORMAL_ALERT." + OR tae.estado = ".AGENT_MODULE_STATUS_NORMAL." + )"; + } + // If the state is not an expected state, return no condition + return "1=1"; +} +?> \ No newline at end of file diff --git a/pandora_console/include/functions_reporting.php b/pandora_console/include/functions_reporting.php index c5e8e53725..642dec50f4 100755 --- a/pandora_console/include/functions_reporting.php +++ b/pandora_console/include/functions_reporting.php @@ -7000,54 +7000,41 @@ function reporting_get_group_stats ($id_group = 0, $access = 'AR') { continue; } } - + if (!empty($group_array)) { + $monitors_info = groups_monitor_total_counters($group_array, true); // Get monitor NOT INIT, except disabled AND async modules - $data["monitor_not_init"] += (int) groups_get_not_init_monitors ($group_array, array(), array(), false, false, true); - - // Get monitor OK, except disabled and non-init - $data["monitor_ok"] += (int) groups_get_normal_monitors ($group_array, array(), array(), false, false, true); - - // Get monitor CRITICAL, except disabled and non-init - $data["monitor_critical"] += (int) groups_get_critical_monitors ($group_array, array(), array(), false, false, true); - - // Get monitor WARNING, except disabled and non-init - $data["monitor_warning"] += (int) groups_get_warning_monitors ($group_array, array(), array(), false, false, true); - - // Get monitor UNKNOWN, except disabled and non-init - $data["monitor_unknown"] += (int) groups_get_unknown_monitors ($group_array, array(), array(), false, false, true); - - // Get alerts configured, except disabled - $data["monitor_alerts"] += groups_monitor_alerts ($group_array) ; - - // Get alert configured currently FIRED, except disabled - $data["monitor_alerts_fired"] += groups_monitor_fired_alerts ($group_array); - - // Calculate totals using partial counts from above - - // Get TOTAL non-init modules, except disabled ones and async modules + $data["monitor_not_init"] += $monitors_info['not_init']; $data["total_not_init"] += $data["monitor_not_init"]; - + // Get monitor OK, except disabled and non-init + $data["monitor_ok"] += $monitors_info['ok']; + // Get monitor CRITICAL, except disabled and non-init + $data["monitor_critical"] += $monitors_info['critical']; + // Get monitor WARNING, except disabled and non-init + $data["monitor_warning"] += $monitors_info['warning']; + // Get monitor UNKNOWN, except disabled and non-init + $data["monitor_unknown"] += $monitors_info['unknown']; + $data["monitor_checks"] += $monitors_info['total']; + + // Get alerts configured, except disabled + $alerts_info = groups_monitor_alerts_total_counters ($group_array); + $data["monitor_alerts"] += $alerts_info['total']; + // Get alert configured currently FIRED, except disabled + $data["monitor_alerts_fired"] += $alerts_info['fired']; + + $agents_info = groups_agents_total_counters($group_array); // Get TOTAL agents in a group - $data["total_agents"] += (int) groups_get_total_agents ($group_array, array(), array(), false, false, true); - + $data["total_agents"] += $agents_info['total']; // Get Agents OK - $data["agent_ok"] += (int) groups_get_normal_agents ($group_array, array(), array(), false, false, true); - - // Get Agents Warning - $data["agent_warning"] += (int) groups_get_warning_agents ($group_array, array(), array(), false, false, true); - + $data["agent_ok"] += $agents_info['ok']; + // Get Agents Warning + $data["agent_warning"] += $agents_info['warning']; // Get Agents Critical - $data["agent_critical"] += (int) groups_get_critical_agents ($group_array, array(), array(), false, false, true); - + $data["agent_critical"] += $agents_info['critical']; // Get Agents Unknown - $data["agent_unknown"] += (int) groups_get_unknown_agents ($group_array, array(), array(), false, false, true); - + $data["agent_unknown"] += $agents_info['unknown']; // Get Agents Not init - $data["agent_not_init"] += (int) groups_get_not_init_agents ($group_array, array(), array(), false, false, true); - - // Get total count of monitors for this group, except disabled. - $data["monitor_checks"] += $data["monitor_not_init"] + $data["monitor_unknown"] + $data["monitor_warning"] + $data["monitor_critical"] + $data["monitor_ok"]; + $data["agent_not_init"] += $agents_info['not_init']; } } // Calculate not_normal monitors