0) { $groups = array($id_group); if ($recursion === 'true' || $recursion == 1 ) { $groups = array_merge($groups, groups_get_id_recursive($id_group, true)); } } else { $groups_orig = users_get_groups(false, $privilege); $groups = array_keys($groups_orig); } // Build filter $filter = array(); $filter['id_grupo'] = $groups; if (!empty($id_os)) $filter['id_os'] = $id_os; if (!empty($agent_name)) $filter['nombre'] = '%' . $agent_name . '%'; if (!empty($agent_alias)) $filter['alias'] = '%' . $agent_alias . '%'; switch ($status_agents) { case AGENT_STATUS_NORMAL: $filter[] = "(normal_count = total_count)"; break; case AGENT_STATUS_WARNING: $filter[] = "(critical_count = 0 AND warning_count > 0)"; break; case AGENT_STATUS_CRITICAL: $filter[] = "(critical_count > 0)"; break; case AGENT_STATUS_UNKNOWN: $filter[] = "(critical_count = 0 AND warning_count = 0 AND unknown_count > 0)"; break; case AGENT_STATUS_NOT_NORMAL: $filter[] = "(normal_count <> total_count)"; break; case AGENT_STATUS_NOT_INIT: $filter[] = "(notinit_count = total_count)"; break; } $filter['order'] = "alias ASC"; if($cluster_mode){ $agent_id_os = db_get_all_rows_sql('select id_os from tconfig_os where id_os != 100'); foreach ($agent_id_os as $key => $value) { $agent_id_os_array[] = $agent_id_os[$key]['id_os']; } $filter['id_os'] = $agent_id_os_array; if($agents_inserted[0] != ''){ $agents_id_list = ''; foreach($agents_inserted as $elem) { if ($elem === end($agents_inserted)) { $agents_id_list .= $elem; } else{ $agents_id_list .= $elem.','; } } $agent_id_agente = db_get_all_rows_sql('select id_agente from tagente where id_agente not in ('.$agents_id_list.')'); foreach ($agent_id_agente as $key => $value) { $agent_id_agente_array[] = $agent_id_agente[$key]['id_agente']; } $filter['id_agente'] = $agent_id_agente_array; } } // Build fields $fields = array('id_agente', 'alias'); // Perform search $agents = db_get_all_rows_filter('tagente', $filter, $fields); if (empty($agents)) $agents = array(); foreach ($agents as $k => $v) { $agents[$k] = io_safe_output($v); } // Add keys prefix if ($keys_prefix !== '') { foreach ($agents as $k => $v) { $agents[$keys_prefix . $k] = io_safe_output($v); unset($agents[$k]); } } echo json_encode($agents); return; } if ($get_modules_group_json) { $id_group = (int) get_parameter('id_module_group'); $id_agents = get_parameter('id_agents'); $selection = get_parameter('selection'); select_modules_for_agent_group($id_group, $id_agents, $selection); } if ($get_agent_json) { $id_agent = (int) get_parameter ('id_agent'); $agent = db_get_row ('tagente', 'id_agente', $id_agent); echo json_encode ($agent); return; } if ($get_agent_modules_json_for_multiple_agents_id) { $idAgents = get_parameter('id_agent'); $modules = db_get_all_rows_sql(' SELECT nombre, id_agente_modulo FROM tagente_modulo WHERE id_agente IN (' . implode(',', $idAgents) . ')'); $return = array(); foreach ($modules as $module) { $return[$module['id_agente_modulo']] = io_safe_output($module['nombre']); } echo json_encode($return); return; } if ($get_agents_json_for_multiple_modules) { $nameModules = get_parameter('module_name'); $selection_mode = get_parameter('selection_mode','common'); $status_modulo = (int) get_parameter ('status_module', -1); $groups = users_get_groups ($config["id_user"], "AW", false); $group_id_list = ($groups ? join(",",array_keys($groups)):"0"); $sql = 'SELECT DISTINCT(t1.alias) as name FROM tagente t1, tagente_modulo t2 WHERE t1.id_agente = t2.id_agente AND t1.id_grupo IN (' . $group_id_list .') AND t2.nombre IN (\'' . implode('\',\'', $nameModules) . '\')'; // Status selector if ($status_modulo == AGENT_MODULE_STATUS_NORMAL) { //Normal $sql_conditions .= ' estado = 0 AND utimestamp > 0) OR (t2.id_tipo_modulo IN(21,22,23,100)) '; } elseif ($status_modulo == AGENT_MODULE_STATUS_CRITICAL_BAD) { //Critical $sql_conditions .= ' estado = 1 AND utimestamp > 0 )'; } elseif ($status_modulo == AGENT_MODULE_STATUS_WARNING) { //Warning $sql_conditions .= ' estado = 2 AND utimestamp > 0 )'; } elseif ($status_modulo == AGENT_MODULE_STATUS_NOT_NORMAL) { //Not normal $sql_conditions .= ' estado <> 0'; } elseif ($status_modulo == AGENT_MODULE_STATUS_UNKNOWN) { //Unknown $sql_conditions .= ' estado = 3 AND utimestamp <> 0 )'; } elseif ($status_modulo == AGENT_MODULE_STATUS_NOT_INIT) { //Not init $sql_conditions .= ' utimestamp = 0 ) AND t2.id_tipo_modulo NOT IN (21,22,23,100)'; } if ($status_modulo != -1) { $sql .= ' AND t2.id_agente_modulo IN (SELECT id_agente_modulo FROM tagente_estado where ' . $sql_conditions; } if ($selection_mode == 'common') { $sql .= 'AND ( SELECT count(t3.nombre) FROM tagente t3, tagente_modulo t4 WHERE t3.id_agente = t4.id_agente AND t1.nombre = t3.nombre AND t4.nombre IN (\'' . implode('\',\'', $nameModules) . '\')) = '.count($nameModules); } $sql .= ' ORDER BY t1.alias'; $nameAgents = db_get_all_rows_sql($sql); if ($nameAgents == false) $nameAgents = array(); foreach ($nameAgents as $nameAgent) { $names[] = io_safe_output($nameAgent['name']); } echo json_encode($names); return; } if ($get_agent_modules_alerts_json_for_multiple_agents) { $idAgents = (array) get_parameter('id_agent'); $templates = (array) get_parameter('templates'); $selection_mode = get_parameter('selection_mode','common'); $sql = 'SELECT DISTINCT(nombre) FROM tagente_modulo t1, talert_template_modules t2 WHERE t2.id_agent_module = t1.id_agente_modulo AND delete_pending = 0 AND id_alert_template IN (' . implode(',', $templates) . ') AND id_agente IN (' . implode(',', $idAgents) . ')'; if ($selection_mode == 'common') { $sql .= ' AND ( SELECT count(nombre) FROM tagente_modulo t3, talert_template_modules t4 WHERE t4.id_agent_module = t3.id_agente_modulo AND delete_pending = 0 AND t1.nombre = t3.nombre AND id_agente IN (' . implode(',', $idAgents) . ') AND id_alert_template IN (' . implode(',', $templates) . ')) = (' . count($idAgents) . ')'; } $sql .= ' ORDER BY t1.nombre'; $nameModules = db_get_all_rows_sql($sql); if ($nameModules == false) { $nameModules = array(); } $result = array(); foreach($nameModules as $nameModule) { $result[] = io_safe_output($nameModule['nombre']); } echo json_encode($result); return; } if ($get_agent_modules_multiple_alerts_json_for_multiple_agents) { $idAgents = get_parameter('id_agent'); $id_template = get_parameter('template'); $selection_mode = get_parameter('selection_mode','common'); $sql = 'SELECT DISTINCT(nombre) FROM tagente_modulo t1, talert_template_modules t2 WHERE t2.id_agent_module = t1.id_agente_modulo AND delete_pending = 0 AND id_alert_template = '.$id_template.' AND id_agente IN (' . implode(',', $idAgents) . ')'; if ($selection_mode == 'common') { $sql .= ' AND ( SELECT count(nombre) FROM tagente_modulo t3, talert_template_modules t4 WHERE t4.id_agent_module = t3.id_agente_modulo AND delete_pending = 0 AND t1.nombre = t3.nombre AND id_agente IN (' . implode(',', $idAgents) . ') AND id_alert_template = '.$id_template.') = (' . count($idAgents) . ')'; } $sql .= ' ORDER BY t1.nombre'; $nameModules = db_get_all_rows_sql($sql); if ($nameModules == false) { $nameModules = array(); } $result = array(); foreach($nameModules as $nameModule) { $result[] = io_safe_output($nameModule['nombre']); } echo json_encode($result); return; } if ($get_agent_modules_json_for_multiple_agents) { $idAgents = (array) get_parameter('id_agent'); $tags = get_parameter('tags', null); $module_types_excluded = get_parameter('module_types_excluded', array()); $module_name = (string) get_parameter('name'); $selection_mode = get_parameter('selection_mode', 'common'); $serialized = get_parameter('serialized', ''); $id_server = (int) get_parameter('id_server', 0); $status_modulo = (int) get_parameter ('status_module', -1); $metaconsole_server_name = null; if (!empty($id_server)) { $metaconsole_server_name = db_get_value('server_name', 'tmetaconsole_setup', 'id', $id_server); } $filter = '1 = 1'; $all = (string)get_parameter('all', 'all'); switch ($all) { default: case 'all': $filter .= ' AND 1 = 1'; break; case 'enabled': $filter .= ' AND t1.disabled = 0'; break; } if (!empty($module_types_excluded) && is_array($module_types_excluded)) $filter .= ' AND t1.id_tipo_modulo NOT IN (' . implode($module_types_excluded) . ')'; if (!empty($module_name)) { switch ($config['dbtype']) { case "mysql": $filter .= " AND t1.nombre COLLATE utf8_general_ci LIKE '%$module_name%'"; break; case "postgresql": $filter .= " AND t1.nombre LIKE '%$module_name%'"; break; case "oracle": $filter .= " AND UPPER(t1.nombre) LIKE UPPER('%$module_name%')"; break; } } // Status selector if ($status_modulo == AGENT_MODULE_STATUS_NORMAL) { //Normal $sql_conditions .= ' estado = 0 AND utimestamp > 0 ) OR (t1.id_tipo_modulo IN(21,22,23,100)) '; } elseif ($status_modulo == AGENT_MODULE_STATUS_CRITICAL_BAD) { //Critical $sql_conditions .= ' estado = 1 AND utimestamp > 0 )'; } elseif ($status_modulo == AGENT_MODULE_STATUS_WARNING) { //Warning $sql_conditions .= ' estado = 2 AND utimestamp > 0 )'; } elseif ($status_modulo == AGENT_MODULE_STATUS_NOT_NORMAL) { //Not normal $sql_conditions .= ' estado <> 0)'; } elseif ($status_modulo == AGENT_MODULE_STATUS_UNKNOWN) { //Unknown $sql_conditions .= ' estado = 3 AND utimestamp <> 0 )'; } elseif ($status_modulo == AGENT_MODULE_STATUS_NOT_INIT) { //Not init $sql_conditions .= ' utimestamp = 0 ) AND t1.id_tipo_modulo NOT IN (21,22,23,100)'; } if ($status_modulo != -1) { $filter .= ' AND t1.id_agente_modulo IN (SELECT id_agente_modulo FROM tagente_estado where ' . $sql_conditions; } if (is_metaconsole()) { $result = array(); $nameModules = array(); $temp = array(); $first = true; $temp_element = array(); $counter = 0; $first_elements = array(); $array_mapped = array_map(function($item) use ($metaconsole_server_name) { if (empty($metaconsole_server_name)) { if (strstr($item, "|@_@|")) { $row = explode ('|@_@|', $item); } else { $row = explode ('|', $item); } $server_name = array_shift($row); $id_agent = array_shift($row); } else { $server_name = $metaconsole_server_name; $id_agent = $item; } return array( 'server_name' => $server_name, 'id_agent' => $id_agent ); }, $idAgents); $array_reduced = array_reduce($array_mapped, function($carry, $item) { if (!isset($carry[$item['server_name']])) $carry[$item['server_name']] = array(); $carry[$item['server_name']][] = $item['id_agent']; return $carry; }, array()); $last_modules_set = array(); foreach ($array_reduced as $server_name => $id_agents) { //Metaconsole db connection // $server_name can be the server id (ugly hack, I know) if (is_numeric($server_name)) { $connection = metaconsole_get_connection_by_id($server_name); } else { $connection = metaconsole_get_connection($server_name); } if (metaconsole_load_external_db($connection) != NOERR) { continue; } //Get agent's modules $sql = sprintf('SELECT t1.id_agente, t1.id_agente_modulo, t1.nombre FROM tagente_modulo t1 WHERE %s AND t1.delete_pending = 0 AND t1.id_agente IN (%s) AND ( SELECT COUNT(nombre) FROM tagente_modulo t2 WHERE t2.delete_pending = 0 AND t1.nombre = t2.nombre AND t2.id_agente IN (%s)) = (%d)', $filter, implode(',', $id_agents), implode(',', $id_agents), count($id_agents)); $modules = db_get_all_rows_sql($sql); if (empty($modules)) $modules = array(); $modules_aux = array(); foreach ($modules as $key => $module) { // Don't change this order, is used in the serialization $module_data = array( 'id_module' => $module['id_agente_modulo'], 'id_agent' => $module['id_agente'], 'server_name' => $server_name ); if (!isset($modules_aux[$module['nombre']])) $modules_aux[$module['nombre']] = array(); $modules_aux[$module['nombre']][] = $module_data; } $modules = $modules_aux; // Build the next array using the common values if (!empty($last_modules_set)) { $modules = array_intersect_key($modules, $last_modules_set); array_walk($modules, function(&$module_data, $module_name) use ($last_modules_set) { $module_data = array_merge($module_data, $last_modules_set[$module_name]); }); } $last_modules_set = $modules; //Restore db connection metaconsole_restore_db(); } $result = array(); foreach ($last_modules_set as $module_name => $module_data) { $value = ui_print_truncate_text(io_safe_output($module_name), 'module_medium', false, true); $module_data_processed = array_map(function($item) { // data: -> id_module | id_agent | server_name; return implode('|', $item); }, $module_data); $key = implode(';', $module_data_processed); $result[$key] = $value; } asort($result); } else { if($idAgents[0] < 0){ if($selection_mode == 'common'){ $sql_agent_total = 'SELECT count(*) FROM tagente WHERE disabled=0'; $agent_total = db_get_value_sql($sql_agent_total); $sql = "SELECT tam.nombre, tam.id_agente_modulo FROM tagente_modulo tam JOIN ( SELECT COUNT(*) AS num_names, nombre FROM tagente_modulo WHERE disabled=0 AND delete_pending=0 GROUP BY nombre ) AS tj ON tj.num_names = $agent_total AND tj.nombre = tam.nombre "; } else{ $sql = 'SELECT nombre, id_agente_modulo FROM tagente_modulo'; } } else { $sql = 'SELECT DISTINCT nombre, id_agente_modulo FROM tagente_modulo t1 WHERE ' . $filter . ' AND t1.delete_pending = 0 AND t1.id_agente IN (' . implode(',', $idAgents) . ')'; if ($selection_mode == 'common') { $sql .= ' AND ( SELECT count(nombre) FROM tagente_modulo t2 WHERE t2.delete_pending = 0 AND t1.nombre = t2.nombre AND t2.id_agente IN (' . implode(',', $idAgents) . ')) = (' . count($idAgents) . ')'; } elseif ($selection_mode == 'unknown') { $sql .= 'AND t1.id_agente_modulo IN (SELECT id_agente_modulo FROM tagente_estado where estado = 3 OR estado = 4)'; } } $sql .= ' ORDER BY nombre'; $nameModules = db_get_all_rows_sql($sql); if ($tags != null) { if ((count($tags) >= 1) && ($tags[0] != "") && ($tags[0] != -1)) { $implode_tags = implode(",", $tags); $tag_modules = db_get_all_rows_sql("SELECT DISTINCT id_agente_modulo FROM ttag_module WHERE id_tag IN (" . $implode_tags . ")"); if ($tag_modules) { $final_modules = array(); foreach ($nameModules as $key => $module) { $in_array = false; foreach ($tag_modules as $t_module) { if ($module['id_agente_modulo'] == $t_module['id_agente_modulo']) { $in_array = true; } } if ($in_array) { $final_modules[] = $module; } } $nameModules = $final_modules; } else { $nameModules = array(); } } } if ($nameModules == false) { $nameModules = array(); } $result = array(); foreach ($nameModules as $nameModule) { if (empty($serialized)) $result[io_safe_output($nameModule['nombre'])] = ui_print_truncate_text( io_safe_output($nameModule['nombre']), 'module_medium', false, true); else $result[io_safe_output($nameModule['nombre']).'$*$'.implode('|', $idAgents)] = ui_print_truncate_text(io_safe_output($nameModule['nombre']), 'module_medium', false, true); } } echo json_encode($result); return; } if ($get_agent_modules_json) { $id_agent = (int) get_parameter ('id_agent'); // Use -1 as not received $disabled = (int) get_parameter ('disabled', -1); $delete_pending = (int) get_parameter ('delete_pending', -1); // Use 0 as not received $id_tipo_modulo = (int) get_parameter ('id_tipo_modulo', 0); $status_modulo = (int) get_parameter ('status_module', -1); $tags = (array) get_parameter ('tags', array()); // Filter $filter = array(); if ($disabled !== -1) $filter['disabled'] = $disabled; if ($delete_pending !== -1) $filter['delete_pending'] = $delete_pending; if (!empty($id_tipo_modulo)) $filter['id_tipo_modulo'] = $id_tipo_modulo; if (empty($filter)) $filter = false; $get_only_string_modules = get_parameter('get_only_string_modules', false); if ($get_only_string_modules) { $filter['tagente_modulo.id_tipo_modulo IN'] = "(17,23,3,10,33)"; } // Status selector if ($status_modulo == AGENT_MODULE_STATUS_NORMAL) { //Normal $sql_conditions .= ' estado = 0 AND utimestamp > 0 ) OR (tagente_modulo.id_tipo_modulo IN(21,22,23,100)) '; } elseif ($status_modulo == AGENT_MODULE_STATUS_CRITICAL_BAD) { //Critical $sql_conditions .= ' estado = 1 AND utimestamp > 0 )'; } elseif ($status_modulo == AGENT_MODULE_STATUS_WARNING) { //Warning $sql_conditions .= ' estado = 2 AND utimestamp > 0 )'; } elseif ($status_modulo == AGENT_MODULE_STATUS_NOT_NORMAL) { //Not normal $sql_conditions .= ' estado <> 0 )'; } elseif ($status_modulo == AGENT_MODULE_STATUS_UNKNOWN) { //Unknown $sql_conditions .= ' estado = 3 AND utimestamp <> 0 )'; } elseif ($status_modulo == AGENT_MODULE_STATUS_NOT_INIT) { //Not init $sql_conditions .= ' utimestamp = 0 ) AND tagente_modulo.id_tipo_modulo NOT IN (21,22,23,100)'; } if ($status_modulo != -1) { $filter['id_agente_modulo IN'] = ' (SELECT id_agente_modulo FROM tagente_estado where ' . $sql_conditions; } $get_id_and_name = (bool) get_parameter ('get_id_and_name'); $get_distinct_name = (bool) get_parameter ('get_distinct_name'); // Fields $fields = '*'; if ($get_id_and_name) $fields = array('id_agente_modulo', 'nombre'); if ($get_distinct_name) $fields = array('DISTINCT(nombre)'); $indexed = (bool) get_parameter ('indexed', true); $agentName = (string) get_parameter ('agent_name', null); $server_name = (string) get_parameter ('server_name', null); $server_id = (int) get_parameter ('server_id', 0); /* This will force to get local modules although metaconsole is active, by default get all modules from all nodes */ $force_local_modules = (int) get_parameter ('force_local_modules', 0); if ($agentName != null) { $search = array(); $search['alias'] = io_safe_output($agentName); } else $search = false; if (is_metaconsole() && !$force_local_modules) { if (enterprise_include_once ('include/functions_metaconsole.php') !== ENTERPRISE_NOT_HOOK) { $connection = metaconsole_get_connection($server_name); if ($server_id > 0) { $connection = metaconsole_get_connection_by_id($server_id); } if (metaconsole_load_external_db($connection) == NOERR) { /* Get all agents if no agent was given */ if ($id_agent == 0) $id_agent = array_keys( agents_get_group_agents( array_keys (users_get_groups ()), $search, "none")); $agent_modules = agents_get_modules ($id_agent, $fields, $filter, $indexed, true, false, $tags); } // Restore db connection metaconsole_restore_db(); } } else { /* Get all agents if no agent was given */ if ($id_agent == 0) $id_agent = array_keys( agents_get_group_agents( array_keys(users_get_groups ()), $search, "none")); $agent_modules = agents_get_modules ($id_agent, $fields, $filter, $indexed, true, false, $tags); } if (empty($agent_modules)) $agent_modules = array(); if (!empty($tags)) { $implode_tags = implode(",", $tags); $tag_modules = db_get_all_rows_sql("SELECT DISTINCT id_agente_modulo FROM ttag_module WHERE id_tag IN (" . $implode_tags . ")"); if ($tag_modules) { $final_modules = array(); foreach ($agent_modules as $key => $module) { $in_array = false; foreach ($tag_modules as $t_module) { if ($module['id_agente_modulo'] == $t_module['id_agente_modulo']) { $in_array = true; } } if ($in_array) { $final_modules[] = $module; } } $agent_modules = $final_modules; } else { $agent_modules = array(); } } foreach ($agent_modules as $key => $module) { $agent_modules[$key]['nombre'] = io_safe_output($module['nombre']); } $get_order_json = (bool)get_parameter('get_order_json', false); if ($get_order_json) { $new_elements = array(); $index = 0; foreach ($agent_modules as $key => $module) { $new_elements[$index]['id_agente_modulo'] = $module['id_agente_modulo']; $new_elements[$index]['nombre'] = io_safe_output($module['nombre']); $index++; } $agent_modules = $new_elements; } echo json_encode ($agent_modules); return; } if ($get_agent_status_tooltip) { $id_agent = (int) get_parameter ('id_agent'); $metaconsole = (bool) get_parameter('metaconsole', false); $id_server = (int) get_parameter('id_server', 0); //Metaconsole $server = null; if ($metaconsole) { $filter = array(); if (!empty($id_agent)) $filter['id_tagente'] = $id_agent; if (!empty($id_server)) $filter['id_tmetaconsole_setup'] = $id_server; $agent = db_get_row_filter('tmetaconsole_agent', $filter); } else { $agent = db_get_row ('tagente', 'id_agente', $id_agent); } if ($agent === false) { return; } echo '