From dc92a00b48dab0024c28520f509a170a7cc776ee Mon Sep 17 00:00:00 2001 From: fermin831 Date: Tue, 2 Oct 2018 13:19:17 +0200 Subject: [PATCH] [Tags performance] Fixed performance of main agent view --- pandora_console/include/ajax/module.php | 174 ++++++--------------- pandora_console/include/functions_tags.php | 37 +++++ 2 files changed, 81 insertions(+), 130 deletions(-) diff --git a/pandora_console/include/ajax/module.php b/pandora_console/include/ajax/module.php index 24fd4c9c78..a2c675fd94 100755 --- a/pandora_console/include/ajax/module.php +++ b/pandora_console/include/ajax/module.php @@ -74,7 +74,7 @@ if ($search_modules) { $id_agents = json_decode(io_safe_output(get_parameter('id_agents'))); $filter = '%' . get_parameter('q', '') . '%'; $other_filter = json_decode(io_safe_output(get_parameter('other_filter')), true); - + // TODO TAGS agents_get_modules $modules = agents_get_modules($id_agents, false, (array('nombre' => $filter) + $other_filter)); @@ -550,11 +550,15 @@ if ($list_modules) { // Fix: for tag functionality groups have to be all user_groups (propagate ACL funct!) $groups = users_get_groups($config["id_user"], $access); - if($cluster_list != 1){ - // TODO revision tag - $tags_sql = tags_get_acl_tags($config['id_user'], - array_keys($groups), $access, 'module_condition', 'AND', - 'tagente_modulo', false, array(), true); + $tags_join = ""; + $tags_sql = ""; + if($cluster_list != 1) { + $tags = tags_get_user_applied_agent_tags ($id_agent, $access); + if (is_array($tags)) { + $tags_sql = " AND ttag_module.id_tag IN (" . implode(',', $tags) . ")"; + $tags_join = "LEFT JOIN ttag_module + ON ttag_module.id_agente_modulo = tagente_modulo.id_agente_modulo"; + } } $status_filter_monitor = (int)get_parameter('status_filter_monitor', -1); @@ -572,10 +576,10 @@ if ($list_modules) { } if ($status_module_group != -1) { - $status_module_group_filter = 'id_module_group = ' . $status_module_group; + $status_module_group_filter = 'tagente_modulo.id_module_group = ' . $status_module_group; } else { - $status_module_group_filter = 'id_module_group >= 0'; + $status_module_group_filter = 'tagente_modulo.id_module_group >= 0'; } $status_text_monitor_sql = '%'; @@ -591,66 +595,22 @@ if ($list_modules) { } //Count monitors/modules - switch ($config["dbtype"]) { - case "mysql": - $sql = sprintf(" - SELECT COUNT(*) - FROM tagente_estado, - (SELECT * - FROM tagente_modulo - WHERE id_agente = %d AND nombre LIKE \"%s\" AND delete_pending = 0 - AND disabled = 0 AND %s) tagente_modulo - LEFT JOIN tmodule_group - ON tagente_modulo.id_module_group = tmodule_group.id_mg - WHERE tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo - AND %s %s - AND tagente_estado.estado != %d - AND tagente_modulo.%s - ORDER BY tagente_modulo.id_module_group , %s %s", - $id_agente, $status_text_monitor_sql,$status_module_group_filter,$status_filter_sql, $tags_sql, $monitor_filter, - $status_module_group_filter, $order['field'], $order['order']); - break; - case "postgresql": - $sql = sprintf(" - SELECT COUNT(DISTINCT tagente_modulo.id_module_group) - FROM tagente_estado, - (SELECT * - FROM tagente_modulo - WHERE id_agente = %d AND nombre LIKE '%s' - AND delete_pending = 0 - AND disabled = 0 AND %s) tagente_modulo - LEFT JOIN tmodule_group - ON tagente_modulo.id_module_group = tmodule_group.id_mg - WHERE tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo - AND %s %s - AND tagente_estado.estado != %d - AND tagente_modulo.%s - GROUP BY tagente_modulo.id_module_group, - tagente_modulo.nombre - ORDER BY tagente_modulo.id_module_group , %s %s", - $id_agente, $status_text_monitor_sql,$status_module_group_filter,$status_filter_sql, - $tags_sql, $monitor_filter,$status_module_group_filter,$order['field'], - $order['order']); - break; - case "oracle": - $sql = sprintf (" - SELECT COUNT(*)" . - " FROM tagente_estado, tagente_modulo - LEFT JOIN tmodule_group - ON tmodule_group.id_mg = tagente_modulo.id_module_group - WHERE tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo - AND tagente_modulo.id_agente = %d - AND tagente_modulo.nombre LIKE '%s' - AND %s %s - AND tagente_modulo.delete_pending = 0 - AND tagente_modulo.disabled = 0 - AND tagente_estado.estado != %d - AND tagente_modulo.%s - ORDER BY tagente_modulo.id_module_group , %s %s - ", $id_agente, $status_text_monitor_sql, $status_filter_sql, $tags_sql, $monitor_filter, - $status_module_group_filter,$order['field'], $order['order']); - break; - } + $order_sql = $order['field'] . " " . $order['order']; + $sql = "SELECT COUNT(*) + FROM tagente_modulo + $tags_join + INNER JOIN tagente_estado + ON tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo + LEFT JOIN tmodule_group + ON tagente_modulo.id_module_group = tmodule_group.id_mg + WHERE tagente_modulo.id_agente = $id_agente + AND nombre LIKE '$status_text_monitor_sql' + AND delete_pending = 0 + AND $status_filter_sql + $tags_sql + AND tagente_estado.estado != $monitor_filter + GROUP BY tagente_modulo.id_agente_modulo + "; $count_modules = db_get_all_rows_sql($sql); if (isset($count_modules[0])) @@ -660,68 +620,22 @@ if ($list_modules) { //Get monitors/modules // Get all module from agent - switch ($config["dbtype"]) { - case "mysql": - $sql = sprintf(" - SELECT * - FROM tagente_estado, - (SELECT * - FROM tagente_modulo - WHERE id_agente = %d AND nombre LIKE \"%s\" AND delete_pending = 0 - AND disabled = 0 AND %s) tagente_modulo - LEFT JOIN tmodule_group - ON tagente_modulo.id_module_group = tmodule_group.id_mg - WHERE tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo - AND %s %s - AND tagente_estado.estado != %d - AND tagente_modulo.%s - ORDER BY tmodule_group.name , %s %s", - $id_agente, $status_text_monitor_sql,$status_module_group_filter,$status_filter_sql, $tags_sql, $monitor_filter, - $status_module_group_filter, $order['field'], $order['order']); - - break; - case "postgresql": - $sql = sprintf(" - SELECT * - FROM tagente_estado, - (SELECT * - FROM tagente_modulo - WHERE id_agente = %d AND nombre LIKE '%s' AND delete_pending = 0 - AND disabled = 0 AND %s) tagente_modulo - LEFT JOIN tmodule_group - ON tagente_modulo.id_module_group = tmodule_group.id_mg - WHERE tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo - AND %s %s - AND tagente_estado.estado != %d - AND tagente_modulo.%s - ORDER BY tmodule_group.name , %s %s", - $id_agente, $status_text_monitor_sql,$status_module_group_filter,$status_filter_sql, $tags_sql, $monitor_filter, - $status_module_group_filter, $order['field'], $order['order']); - break; - // If Dbms is Oracle then field_list in sql statement has to be recoded. See oracle_list_all_field_table() - case "oracle": - $fields_tagente_estado = oracle_list_all_field_table('tagente_estado', 'string'); - $fields_tagente_modulo = oracle_list_all_field_table('tagente_modulo', 'string'); - $fields_tmodule_group = oracle_list_all_field_table('tmodule_group', 'string'); - - $sql = sprintf (" - SELECT " . $fields_tagente_estado . ', ' . $fields_tagente_modulo . ', ' . $fields_tmodule_group . - " FROM tagente_estado, tagente_modulo - LEFT JOIN tmodule_group - ON tmodule_group.id_mg = tagente_modulo.id_module_group - WHERE tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo - AND tagente_modulo.id_agente = %d - AND tagente_modulo.nombre LIKE '%s' - AND %s %s - AND tagente_modulo.delete_pending = 0 - AND tagente_modulo.disabled = 0 - AND tagente_estado.estado != %d - AND tagente_modulo.%s - ORDER BY tmodule_group.name , %s %s - ", $id_agente, $status_text_monitor_sql, $tags_sql, $status_filter_sql, $monitor_filter, - $status_module_group_filter, $order['field'], $order['order']); - break; - } + $sql = "SELECT tagente_estado.*, tagente_modulo.*, tmodule_group.* + FROM tagente_modulo + $tags_join + INNER JOIN tagente_estado + ON tagente_modulo.id_agente_modulo = tagente_estado.id_agente_modulo + LEFT JOIN tmodule_group + ON tagente_modulo.id_module_group = tmodule_group.id_mg + WHERE tagente_modulo.id_agente = $id_agente + AND nombre LIKE '$status_text_monitor_sql' + AND delete_pending = 0 + AND $status_filter_sql + $tags_sql + AND tagente_estado.estado != $monitor_filter + GROUP BY tagente_modulo.id_agente_modulo + ORDER BY tmodule_group.name, $order_sql + "; if ($monitors_change_filter) { $limit = " LIMIT " . $config['block_size'] . " OFFSET 0"; diff --git a/pandora_console/include/functions_tags.php b/pandora_console/include/functions_tags.php index c785ee1297..75eef28bdd 100644 --- a/pandora_console/include/functions_tags.php +++ b/pandora_console/include/functions_tags.php @@ -2628,4 +2628,41 @@ function tags_get_module_policy_tags($id_tag, $id_module) { return $id_module_policy; } + +/** + * Get all tags configured to user associated to the agent. + * + * @param int $id_agent Agent to extract tags + * @param string $access Access to check + * + * @return mixed + * false if user has not permission on agent groups + * true if there is not any tag restriction + * array with all tags if there are tags configured + */ +function tags_get_user_applied_agent_tags ($id_agent, $access) { + global $config; + + $agent_groups = agents_get_all_groups_agent($id_agent); + $user_groups = users_get_groups(false, 'AR', false, true); + // Check global agent permissions + if (!check_acl_one_of_groups($config['id_user'], $agent_groups, $access)) { + return false; + } + + $acl_column = get_acl_column($access); + $tags = array(); + foreach ($agent_groups as $group) { + // If user has not permission to a single group, continue + if (!isset($user_groups[$group])) continue; + $group_tags = $user_groups[$group]["tags"][$acl_column]; + if (!empty($group_tags)) { + $tags = array_merge($tags, $group_tags); + } else { + // If an agent + return true; + } + } + return empty($tags) ? true : $tags; +} ?>