diff --git a/pandora_console/include/ajax/agent.php b/pandora_console/include/ajax/agent.php index 73c4660511..c8ad9e3791 100644 --- a/pandora_console/include/ajax/agent.php +++ b/pandora_console/include/ajax/agent.php @@ -185,12 +185,6 @@ if ($search_agents && ((!defined('METACONSOLE')) || $force_local)) { return; } elseif ($search_agents && ($config['metaconsole'] == 1) && defined('METACONSOLE')) { - $servers = db_get_all_rows_sql ("SELECT * - FROM tmetaconsole_setup - WHERE disabled = 0"); - if (!isset($servers)) { - return; - } $id_agent = (int) get_parameter ('id_agent'); $string = (string) get_parameter ('q'); /* q is what autocomplete plugin gives */ @@ -204,7 +198,7 @@ elseif ($search_agents && ($config['metaconsole'] == 1) && defined('METACONSOLE' } } - $filter = array (); + $filter = array(); if ($id_group != -1) { if ($id_group == 0) { @@ -217,90 +211,63 @@ elseif ($search_agents && ($config['metaconsole'] == 1) && defined('METACONSOLE' } } - $data = array(); - foreach ($servers as $server) { - if (metaconsole_load_external_db ($server) != NOERR) { - continue; - } + if (!empty($id_agent)) { + $filter['id_agente'] = $id_agent; + } + + if (!empty($string)) { + $search_filters = array(); //Get agents for only the name. - $filter_agents = $filter; switch ($config['dbtype']) { case "mysql": - $filter_agents[] = '(nombre COLLATE utf8_general_ci LIKE "%'.$string.'%")'; + //Get agents for only the name. + $search_filters[] = "(nombre COLLATE utf8_general_ci LIKE '%$string%')"; + //Get agents for only the address + $search_filters[] = "(direccion LIKE '%$string%')"; + //Get agents for only the description + $search_filters[] = "(comentarios LIKE '%$string%')"; break; case "postgresql": - $filter_agents[] = '(nombre LIKE \'%'.$string.'%\')'; + //Get agents for only the name. + $search_filters[] = "(nombre LIKE '%$string%')"; + //Get agents for only the address + $search_filters[] = "(direccion LIKE '%$string%')"; + //Get agents for only the description + $search_filters[] = "(comentarios LIKE '%$string%')"; break; case "oracle": - $filter_agents[] = '(UPPER(nombre) LIKE UPPER(\'%'.$string.'%\')'; + //Get agents for only the name. + $search_filters[] = "(UPPER(nombre) LIKE UPPER('%$string%')"; + //Get agents for only the address + $search_filters[] = "(UPPER(direccion) LIKE UPPER('%$string%'))"; + //Get agents for only the description + $search_filters[] = "(UPPER(comentarios) LIKE UPPER('%$string%'))"; break; } - $agents = agents_get_agents($filter_agents, array ('id_agente', 'nombre', 'direccion')); - if ($agents !== false) { - foreach ($agents as $agent) { - $data[] = array('id' => $agent['id_agente'], - 'name' => io_safe_output($agent['nombre']), - 'ip' => io_safe_output($agent['direccion']), - 'filter' => 'agent', - 'id_server' => $server['id']); - } - } - //Get agents for only the address - $filter_address = $filter; - switch ($config['dbtype']) { - case "mysql": - $filter_address[] = '(nombre COLLATE utf8_general_ci NOT LIKE "%'.$string.'%" AND direccion LIKE "%'.$string.'%")'; - break; - case "postgresql": - $filter_address[] = '(nombre NOT LIKE \'%'.$string.'%\' AND direccion LIKE \'%'.$string.'%\')'; - break; - case "oracle": - $filter_address[] = '(UPPER(nombre) NOT LIKE UPPER(\'%'.$string.'%\') AND UPPER(direccion) LIKE UPPER(\'%'.$string.'%\'))'; - break; - } - $agents = agents_get_agents($filter_address, array ('id_agente', 'nombre', 'direccion')); - if ($agents !== false) { - foreach ($agents as $agent) { - $data[] = array('id' => $agent['id_agente'], - 'name' => io_safe_output($agent['nombre']), - 'ip' => io_safe_output($agent['direccion']), - 'filter' => 'address', - 'id_server' => $server['id']); - } - } + $search_filters_str = implode($search_filters, ' OR '); - //Get agents for only the description - $filter_description = $filter; - switch ($config['dbtype']) { - case "mysql": - $filter_description[] = - '(nombre COLLATE utf8_general_ci NOT LIKE "%'.$string.'%" AND direccion NOT LIKE "%'.$string.'%" AND comentarios LIKE "%'.$string.'%")'; - break; - case "postgresql": - $filter_description[] = - '(nombre NOT LIKE \'%'.$string.'%\' AND direccion NOT LIKE \'%'.$string.'%\' AND comentarios LIKE \'%'.$string.'%\')'; - break; - case "oracle": - $filter_description[] = - '(UPPER(nombre) NOT LIKE UPPER(\'%'.$string.'%\') AND UPPER(direccion) NOT LIKE UPPER(\'%'.$string.'%\') AND UPPER(comentarios) LIKE UPPER(\'%'.$string.'%\'))'; - break; - } - $agents = agents_get_agents($filter_description, - array ('id_agente', 'nombre', 'direccion')); - if ($agents !== false) { - foreach ($agents as $agent) { - $data[] = array('id' => $agent['id_agente'], - 'name' => io_safe_output($agent['nombre']), - 'ip' => io_safe_output($agent['direccion']), - 'filter' => 'description', - 'id_server' => $server['id']); - } - } - //Restore db connection - metaconsole_restore_db(); + if (!empty($search_filters_str)) + $filter[] = "($search_filters_str)"; + } + $fields = array( + 'id_tagente AS id_agente', 'nombre', + 'direccion', 'id_tmetaconsole_setup AS id_server' + ); + $agents = db_get_all_rows_filter('tmetaconsole_agent', $filter, $fields); + + $data = array(); + + if ($agents !== false) { + foreach ($agents as $agent) { + $data[] = array('id' => $agent['id_agente'], + 'name' => io_safe_output($agent['nombre']), + 'ip' => io_safe_output($agent['direccion']), + 'filter' => 'description', + 'id_server' => $agent['id_server']); + } } echo json_encode($data); diff --git a/pandora_console/operation/agentes/ver_agente.php b/pandora_console/operation/agentes/ver_agente.php index 6f143e1bc1..572348659d 100644 --- a/pandora_console/operation/agentes/ver_agente.php +++ b/pandora_console/operation/agentes/ver_agente.php @@ -423,6 +423,9 @@ if (is_ajax ()) { ($filter != '' ? $filter : false), $indexed); } + if (empty($agent_modules)) + $agent_modules = array(); + foreach ($agent_modules as $key => $module) { $agent_modules[$key]['nombre'] = io_safe_output($module['nombre']); } @@ -438,28 +441,24 @@ if (is_ajax ()) { 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 + $metaconsole = (bool) get_parameter('metaconsole', false); + $id_server = (int) get_parameter('id_server', 0); //Metaconsole $server = null; if ($metaconsole) { - $strict_user = (bool) db_get_value('strict_acl', 'tusuario', 'id_user', $config['id_user']); - $server = db_get_row('tmetaconsole_setup', 'id', $id_server); + $filter = array(); + if (!empty($id_agent)) + $filter['id_tagente'] = $id_agent; + if (!empty($id_server)) + $filter['id_tmetaconsole_setup'] = $id_server; - if (metaconsole_connect($server) != NOERR) { - return; - } - - $agent = db_get_row ('tagente', 'id_agente', $id_agent); - - metaconsole_restore_db(); + $agent = db_get_row_filter('tmetaconsole_agent', $filter); } else { $agent = db_get_row ('tagente', 'id_agente', $id_agent); } - - + if ($agent === false) { return; } echo '

'.$agent['nombre'].'

'; echo ''.__('Main IP').': '.$agent['direccion'].'
'; @@ -474,130 +473,91 @@ if (is_ajax ()) { echo ''.__('Last contact').': '.human_time_comparation($agent['ultimo_contacto']).'
'; echo ''.__('Last remote contact').': '.human_time_comparation($agent['ultimo_contacto_remoto']).'
'; - - # Fix : Only show agents with module with tags of user profile - $_user_tags = tags_get_user_tags($config['id_user'], 'RR'); - $_sql_post = ''; - if (is_array($_user_tags) && !empty($_user_tags)) { + if (!$metaconsole) { + # Fix : Only show agents with module with tags of user profile + $_user_tags = tags_get_user_tags($config['id_user'], 'RR'); - $_tags = implode(',', array_keys($_user_tags)); - - $_sql_post .= ' AND tagente_modulo.id_agente_modulo IN (SELECT a.id_agente_modulo FROM tagente_modulo a, ttag_module b WHERE a.id_agente_modulo=b.id_agente_modulo AND b.id_tag IN (' . $_tags . ')) '; - - } - - $sql = sprintf ('SELECT tagente_modulo.descripcion, - tagente_modulo.nombre - FROM tagente_estado, tagente_modulo - WHERE tagente_modulo.id_agente = %d - AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo - AND tagente_modulo.disabled = 0 - AND tagente_estado.estado = 1', $id_agent); - - $sql .= $_sql_post; - - if ($metaconsole) { - if (metaconsole_connect($server) != NOERR) { - return; + $_sql_post = ''; + if (is_array($_user_tags) && !empty($_user_tags)) { + + $_tags = implode(',', array_keys($_user_tags)); + + $_sql_post .= ' AND tagente_modulo.id_agente_modulo IN (SELECT a.id_agente_modulo FROM tagente_modulo a, ttag_module b WHERE a.id_agente_modulo=b.id_agente_modulo AND b.id_tag IN (' . $_tags . ')) '; + } + $sql = sprintf ('SELECT tagente_modulo.descripcion, + tagente_modulo.nombre + FROM tagente_estado, tagente_modulo + WHERE tagente_modulo.id_agente = %d + AND tagente_estado.id_agente_modulo = tagente_modulo.id_agente_modulo + AND tagente_modulo.disabled = 0 + AND tagente_estado.estado = 1', $id_agent); + + $sql .= $_sql_post; + $bad_modules = db_get_all_rows_sql ($sql); - metaconsole_restore_db(); - } - else { - $bad_modules = db_get_all_rows_sql ($sql); - } - - $sql = sprintf ('SELECT COUNT(*) - FROM tagente_modulo - WHERE id_agente = %d - AND disabled = 0', $id_agent); - if ($metaconsole) { - if (metaconsole_connect($server) != NOERR) { - return; - } - + $sql = sprintf ('SELECT COUNT(*) + FROM tagente_modulo + WHERE id_agente = %d + AND disabled = 0', $id_agent); $total_modules = db_get_sql ($sql); - metaconsole_restore_db(); - } - else { - $total_modules = db_get_sql ($sql); - } - - if ($bad_modules === false) - $size_bad_modules = 0; - else - $size_bad_modules = sizeof ($bad_modules); - - // Modules down - if ($size_bad_modules > 0 && (!$metaconsole || !$strict_user)) { - echo ''.__('Monitors down').': '.$size_bad_modules.' / '.$total_modules; - echo ''; - } - - // Alerts (if present) - $sql = sprintf ('SELECT COUNT(talert_template_modules.id) - FROM talert_template_modules, tagente_modulo, tagente - WHERE tagente.id_agente = %d - AND tagente.disabled = 0 - AND tagente.id_agente = tagente_modulo.id_agente - AND tagente_modulo.disabled = 0 - AND tagente_modulo.id_agente_modulo = talert_template_modules.id_agent_module - AND talert_template_modules.times_fired > 0 ', - $id_agent); - if ($metaconsole) { - if (metaconsole_connect($server) != NOERR) { - return; - } + if ($bad_modules === false) + $size_bad_modules = 0; + else + $size_bad_modules = sizeof ($bad_modules); - $alert_modules = db_get_sql ($sql); - - metaconsole_restore_db(); - } - else { - $alert_modules = db_get_sql ($sql); - } - - if ($alert_modules > 0 && (!$metaconsole || !$strict_user)) { - $sql = sprintf ('SELECT tagente_modulo.nombre, talert_template_modules.last_fired - FROM talert_template_modules, tagente_modulo, tagente - WHERE tagente.id_agente = %d - AND tagente.disabled = 0 - AND tagente.id_agente = tagente_modulo.id_agente - AND tagente_modulo.disabled = 0 - AND tagente_modulo.id_agente_modulo = talert_template_modules.id_agent_module - AND talert_template_modules.times_fired > 0 ', - $id_agent); - if ($metaconsole) { - if (metaconsole_connect($server) != NOERR) { - return; + // Modules down + if ($size_bad_modules > 0) { + echo ''.__('Monitors down').': '.$size_bad_modules.' / '.$total_modules; + echo ''; + } + + // Alerts (if present) + $sql = sprintf ('SELECT COUNT(talert_template_modules.id) + FROM talert_template_modules, tagente_modulo, tagente + WHERE tagente.id_agente = %d + AND tagente.disabled = 0 + AND tagente.id_agente = tagente_modulo.id_agente + AND tagente_modulo.disabled = 0 + AND tagente_modulo.id_agente_modulo = talert_template_modules.id_agent_module + AND talert_template_modules.times_fired > 0 ', + $id_agent); + + $alert_modules = (int) db_get_sql ($sql); + + if ($alert_modules > 0) { + $sql = sprintf ('SELECT tagente_modulo.nombre, talert_template_modules.last_fired + FROM talert_template_modules, tagente_modulo, tagente + WHERE tagente.id_agente = %d + AND tagente.disabled = 0 + AND tagente.id_agente = tagente_modulo.id_agente + AND tagente_modulo.disabled = 0 + AND tagente_modulo.id_agente_modulo = talert_template_modules.id_agent_module + AND talert_template_modules.times_fired > 0 ', + $id_agent); $alerts = db_get_all_rows_sql ($sql); - metaconsole_restore_db(); + echo ''.__('Alerts fired').':'; + echo "'; } - else { - $alerts = db_get_all_rows_sql ($sql); - } - echo ''.__('Alerts fired').':'; - echo "'; } return;