diff --git a/pandora_console/include/functions_custom_fields.php b/pandora_console/include/functions_custom_fields.php index dcc038b620..404fd2cfbf 100644 --- a/pandora_console/include/functions_custom_fields.php +++ b/pandora_console/include/functions_custom_fields.php @@ -193,6 +193,13 @@ function get_custom_fields_data ($custom_field_name) { function agent_counters_custom_fields($filters){ //filter by status agent $and_status = ""; + + $agent_state_normal = 0; + $agent_state_critical = 0; + $agent_state_warning = 0; + $agent_state_unknown = 0; + $agent_state_notinit = 0; + $agent_state_total = 0; if(is_array($filters['id_status'])){ if(!in_array(-1, $filters['id_status'])){ if(!in_array(AGENT_MODULE_STATUS_NOT_NORMAL, $filters['id_status'])){ @@ -200,34 +207,34 @@ function agent_counters_custom_fields($filters){ $and_status = " AND ( "; foreach ($filters['id_status'] as $key => $value) { $and_status .= ($key != 0) - ? " OR (" - : " ("; + ? " OR " + : " "; switch ($value) { default: case AGENT_STATUS_NORMAL: - $and_status .= " ta.critical_count = 0 - AND ta.warning_count = 0 - AND ta.unknown_count = 0 - AND ta.total_count <> ta.notinit_count ) "; + $agent_state_normal = agents_get_status_clause(AGENT_STATUS_NORMAL); + $and_status .= agents_get_status_clause(AGENT_STATUS_NORMAL); break; case AGENT_STATUS_CRITICAL: - $and_status .= " ta.critical_count > 0 ) "; + $and_status .= agents_get_status_clause(AGENT_STATUS_CRITICAL); + $agent_state_critical = agents_get_status_clause(AGENT_STATUS_CRITICAL); break; case AGENT_STATUS_WARNING: - $and_status .= " ta.critical_count = 0 - AND ta.warning_count > 0 ) "; + $and_status .= agents_get_status_clause(AGENT_STATUS_WARNING); + $agent_state_warning = agents_get_status_clause(AGENT_STATUS_WARNING); break; case AGENT_STATUS_UNKNOWN: - $and_status .= " ta.critical_count = 0 - AND ta.warning_count = 0 - AND ta.unknown_count > 0 ) "; + $and_status .= agents_get_status_clause(AGENT_STATUS_UNKNOWN); + $agent_state_unknown = agents_get_status_clause(AGENT_STATUS_UNKNOWN); break; case AGENT_STATUS_NOT_INIT: - $and_status .= " ta.total_count = ta.notinit_count ) "; + $and_status .= agents_get_status_clause(AGENT_STATUS_NOT_INIT); + $agent_state_notinit = agents_get_status_clause(AGENT_STATUS_NOT_INIT); break; } } $and_status .= " ) "; + $agent_state_total = agents_get_status_clause(AGENT_STATUS_ALL); } } else{ @@ -237,14 +244,45 @@ function agent_counters_custom_fields($filters){ OR ( ta.critical_count = 0 AND ta.warning_count = 0 AND ta.unknown_count > 0 ) OR ( ta.total_count = ta.notinit_count ) ) "; + + $agent_state_critical = agents_get_status_clause(AGENT_STATUS_CRITICAL); + $agent_state_warning = agents_get_status_clause(AGENT_STATUS_WARNING); + $agent_state_unknown = agents_get_status_clause(AGENT_STATUS_UNKNOWN); + $agent_state_notinit = agents_get_status_clause(AGENT_STATUS_NOT_INIT); + $agent_state_total = agents_get_status_clause(AGENT_STATUS_ALL); } } + else{ + $agent_state_normal = agents_get_status_clause(AGENT_STATUS_NORMAL); + $agent_state_critical = agents_get_status_clause(AGENT_STATUS_CRITICAL); + $agent_state_warning = agents_get_status_clause(AGENT_STATUS_WARNING); + $agent_state_unknown = agents_get_status_clause(AGENT_STATUS_UNKNOWN); + $agent_state_notinit = agents_get_status_clause(AGENT_STATUS_NOT_INIT); + $agent_state_total = agents_get_status_clause(AGENT_STATUS_ALL); + } } //filter by status module + $empty_agents_count = "UNION ALL + SELECT ta.id_agente, + 0 AS c_m_total, + 0 AS mm_normal, + 0 AS mm_critical, + 0 AS mm_warning, + 0 AS mm_unknown, + 0 AS mm_not_init, + 0 AS mm_total + FROM tagente ta + LEFT JOIN tagent_secondary_group tasg + ON ta.id_agente = tasg.id_agent + WHERE ta.disabled = 0 + AND ta.total_count = 0 + $groups_and"; + $and_module_status = ""; if(is_array($filters['module_status'])){ if(!in_array(-1, $filters['module_status'])){ + $empty_agents_count = ""; if(!in_array(AGENT_MODULE_STATUS_NOT_NORMAL, $filters['module_status'])){ if(count($filters['module_status']) > 0){ $and_module_status = " AND ( "; @@ -277,25 +315,15 @@ function agent_counters_custom_fields($filters){ else{ //not normal $and_module_status = "AND tae.estado <> 0 AND tae.estado <> 300 "; + $empty_agents_count = ""; } } } //filters module if($filters['module_search']){ - $and_module_search = 'AND nombre LIKE "%' . $filters['module_search'] . '%"'; - } - - $module_filter = ""; - if($and_module_search != '' || $and_module_status != ''){ - $module_filter = ' AND ( - SELECT count(*) AS n - FROM tagente_modulo tam - INNER JOIN tagente_estado tae - ON tae.id_agente_modulo = tam.id_agente_modulo - WHERE tam.id_agente=ta.id_agente - '. $and_module_search . ' ' . $and_module_status .' - ) > 0 '; + $and_module_search = 'AND tam.nombre LIKE "%' . $filters['module_search'] . '%"'; + $empty_agents_count = ""; } //filter group and check ACL groups @@ -359,42 +387,63 @@ function agent_counters_custom_fields($filters){ // Establishes connection if (metaconsole_load_external_db ($server_data) !== NOERR) continue; - $query = sprintf("SELECT - tcd.description as name_data, - SUM(ta.normal_count) AS m_normal, - SUM(ta.critical_count) AS m_critical, - SUM(ta.warning_count) AS m_warning, - SUM(ta.unknown_count) AS m_unknown, - SUM(ta.notinit_count) AS m_not_init, - SUM(ta.fired_count) AS m_alerts, - SUM(ta.total_count) AS m_total, - SUM(IF(ta.critical_count > 0, 1, 0)) AS a_critical, - SUM(IF(ta.critical_count = 0 AND ta.warning_count > 0, 1, 0)) AS a_warning, - SUM(IF(ta.critical_count = 0 AND ta.warning_count = 0 AND ta.unknown_count > 0, 1, 0)) AS a_unknown, - SUM(IF(ta.critical_count = 0 AND ta.warning_count = 0 AND ta.unknown_count = 0 AND ta.notinit_count <> ta.total_count, 1, 0)) AS a_normal, - SUM(IF(ta.total_count = ta.notinit_count, 1, 0)) AS a_not_init, - COUNT(ta.id_agente) AS a_agents, - GROUP_CONCAT(DISTINCT(ta.id_agente) SEPARATOR ',') as ids - FROM tagente ta - INNER JOIN tagent_custom_data tcd - ON tcd.id_agent = ta.id_agente + $query = sprintf( + "SELECT tcd.description AS name_data, + SUM(IF($agent_state_total, 1, 0)) AS a_agents, + SUM(IF($agent_state_critical, 1, 0)) AS a_critical, + SUM(IF($agent_state_warning, 1, 0)) AS a_warning, + SUM(IF($agent_state_unknown, 1, 0)) AS a_unknown, + SUM(IF($agent_state_normal, 1, 0)) AS a_normal, + SUM(IF($agent_state_notinit, 1, 0)) AS a_not_init, + SUM(tagent_counters.mm_normal) AS m_normal, + SUM(tagent_counters.mm_critical) AS m_critical, + SUM(tagent_counters.mm_warning) AS m_warning, + SUM(tagent_counters.mm_unknown) AS m_unknown, + SUM(tagent_counters.mm_not_init) AS m_not_init, + SUM(tagent_counters.mm_total) AS m_total + FROM tagent_custom_data tcd INNER JOIN tagent_custom_fields tcf ON tcd.id_field = tcf.id_field - LEFT JOIN tagent_secondary_group tasg - ON ta.id_agente = tasg.id_agent - WHERE ta.disabled = 0 - AND tcf.name = '%s' + INNER JOIN ( + SELECT ta.id_agente, + ta.total_count AS c_m_total, + SUM( IF(tae.estado = 0, 1, 0) ) AS mm_normal, + SUM( IF(tae.estado = 1, 1, 0) ) AS mm_critical, + SUM( IF(tae.estado = 2, 1, 0) ) AS mm_warning, + SUM( IF(tae.estado = 3, 1, 0) ) AS mm_unknown, + SUM( IF(tae.estado = 4 OR tae.estado = 5, 1, 0) ) AS mm_not_init, + COUNT(tam.id_agente_modulo) AS mm_total + FROM tagente ta + LEFT JOIN tagent_secondary_group tasg + ON ta.id_agente = tasg.id_agent + INNER JOIN tagente_modulo tam + ON ta.id_agente = tam.id_agente + INNER JOIN tagente_estado tae + ON tam.id_agente = tae.id_agente + AND tam.id_agente_modulo = tae.id_agente_modulo + WHERE ta.disabled = 0 + AND tam.disabled = 0 + %s + %s + %s + %s + GROUP by ta.id_agente + %s + ) AS tagent_counters + ON tcd.id_agent = tagent_counters.id_agente + INNER JOIN tagente ta + ON ta.id_agente = tagent_counters.id_agente + WHERE tcf.name = '%s' AND tcd.description <> '' %s - %s - %s - %s GROUP BY tcd.description", - $custom_field_name, - $custom_data_and, $groups_and, $and_status, - $module_filter + $and_module_search, + $and_module_status, + $empty_agents_count, + $custom_field_name, + $custom_data_and ); $result_meta[$server_data['id']] = db_get_all_rows_sql($query); @@ -406,6 +455,11 @@ function agent_counters_custom_fields($filters){ FROM tagente ta LEFT JOIN tagent_secondary_group tasg ON ta.id_agente = tasg.id_agent + INNER JOIN tagente_modulo tam + ON ta.id_agente = tam.id_agente + INNER JOIN tagente_estado tae + ON tam.id_agente = tae.id_agente + AND tam.id_agente_modulo = tae.id_agente_modulo INNER JOIN tagent_custom_data tcd ON tcd.id_agent = ta.id_agente INNER JOIN tagent_custom_fields tcf @@ -413,22 +467,28 @@ function agent_counters_custom_fields($filters){ WHERE ta.disabled = 0 AND tcf.name = '%s' AND tcd.description <> '' + AND tam.disabled = 0 %s %s %s %s + %s + GROUP BY ta.id_agente ", $server_data['id'], $custom_field_name, $custom_data_and, $groups_and, $and_status, - $module_filter + $and_module_search, + $and_module_status ); $node_result = db_get_all_rows_sql($query_data); + if (empty($node_result)) $node_result = array(); + $data = array_merge($data, $node_result); // Restore connection to root node metaconsole_restore_db(); @@ -455,7 +515,6 @@ function agent_counters_custom_fields($filters){ 't_a_not_init' => 0, 't_a_agents' => 0 ); - foreach ($result_meta as $k => $nodo) { if(isset($nodo) && is_array($nodo)){ foreach ($nodo as $key => $value) {