From 3a5bbb22bdaa4b60015dbeb2e3803f8b8a420cae Mon Sep 17 00:00:00 2001 From: alejandro-campos Date: Thu, 17 Oct 2019 11:14:42 +0200 Subject: [PATCH] changed pandora api functions to include possibility of using agent alias when agent id is accepted as argument --- pandora_console/include/functions_api.php | 2073 +++++++++++----- pandora_server/lib/PandoraFMS/DB.pm | 14 +- pandora_server/lib/PandoraFMS/Tools.pm | 1 + pandora_server/util/pandora_manage.pl | 2723 +++++++++++++++------ 4 files changed, 3372 insertions(+), 1439 deletions(-) diff --git a/pandora_console/include/functions_api.php b/pandora_console/include/functions_api.php index 899add9ec0..5ecf2a11eb 100644 --- a/pandora_console/include/functions_api.php +++ b/pandora_console/include/functions_api.php @@ -401,10 +401,11 @@ function api_get_groups($thrash1, $thrash2, $other, $returnType, $user_in_db) function api_get_agent_module_name_last_value($agentName, $moduleName, $other=';', $returnType) { $idAgent = agents_get_agent_id($agentName); + $sql = sprintf( 'SELECT id_agente_modulo - FROM tagente_modulo - WHERE id_agente = %d AND nombre LIKE "%s"', + FROM tagente_modulo + WHERE id_agente = %d AND nombre LIKE "%s"', $idAgent, $moduleName ); @@ -418,8 +419,8 @@ function api_get_agent_module_name_last_value_alias($alias, $moduleName, $other= { $sql = sprintf( 'SELECT tagente_modulo.id_agente_modulo FROM tagente_modulo - INNER JOIN tagente ON tagente_modulo.id_agente = tagente.id_agente - WHERE tagente.alias LIKE "%s" AND tagente_modulo.nombre LIKE "%s"', + INNER JOIN tagente ON tagente_modulo.id_agente = tagente.id_agente + WHERE tagente.alias LIKE "%s" AND tagente_modulo.nombre LIKE "%s"', $alias, $moduleName ); @@ -444,8 +445,8 @@ function api_get_module_last_value($idAgentModule, $trash1, $other=';', $returnT $sql = sprintf( 'SELECT datos - FROM tagente_estado - WHERE id_agente_modulo = %d', + FROM tagente_estado + WHERE id_agente_modulo = %d', $idAgentModule ); $value = db_get_value_sql($sql); @@ -892,9 +893,9 @@ function api_get_tree_agents($trash1, $trahs2, $other, $returnType) $id_group = $group['group_id']; $agents = db_get_all_rows_sql( "SELECT id_agente AS agent_id, id_grupo AS agent_id_group , alias $agent_additional_columns - FROM tagente ta LEFT JOIN tagent_secondary_group tasg - ON ta.id_agente = tasg.id_agent - WHERE ta.id_grupo = $id_group OR tasg.id_group = $id_group" + FROM tagente ta LEFT JOIN tagent_secondary_group tasg + ON ta.id_agente = tasg.id_agent + WHERE ta.id_grupo = $id_group OR tasg.id_group = $id_group" ); if ($agents === false) { $agents = []; @@ -916,13 +917,13 @@ function api_get_tree_agents($trash1, $trahs2, $other, $returnType) $modules = db_get_all_rows_sql( 'SELECT * - FROM (SELECT id_agente_modulo as module_id_agent_modulo '.$module_additional_columns.' - FROM tagente_modulo - WHERE id_agente = '.$agent['agent_id'].') t1 - INNER JOIN (SELECT id_agente_modulo as module_id_agent_modulo '.$estado_additional_columns.' - FROM tagente_estado - WHERE id_agente = '.$agent['agent_id'].') t2 - ON t1.module_id_agent_modulo = t2.module_id_agent_modulo' + FROM (SELECT id_agente_modulo as module_id_agent_modulo '.$module_additional_columns.' + FROM tagente_modulo + WHERE id_agente = '.$agent['agent_id'].') t1 + INNER JOIN (SELECT id_agente_modulo as module_id_agent_modulo '.$estado_additional_columns.' + FROM tagente_estado + WHERE id_agente = '.$agent['agent_id'].') t2 + ON t1.module_id_agent_modulo = t2.module_id_agent_modulo' ); if ($modules === false) { @@ -947,16 +948,16 @@ function api_get_tree_agents($trash1, $trahs2, $other, $returnType) $alerts = db_get_all_rows_sql( 'SELECT t1.id_agent_module as alert_id_agent_module '.$alert_additional_columns.' - FROM (SELECT * FROM talert_template_modules - WHERE id_agent_module = '.$module['module_id_agent_modulo'].') t1 - INNER JOIN talert_templates t2 - ON t1.id_alert_template = t2.id - LEFT JOIN talert_template_module_actions t3 - ON t1.id = t3.id_alert_template_module - LEFT JOIN talert_actions t4 - ON t3.id_alert_action = t4.id - LEFT JOIN talert_commands t5 - ON t4.id_alert_command = t5.id' + FROM (SELECT * FROM talert_template_modules + WHERE id_agent_module = '.$module['module_id_agent_modulo'].') t1 + INNER JOIN talert_templates t2 + ON t1.id_alert_template = t2.id + LEFT JOIN talert_template_module_actions t3 + ON t1.id = t3.id_alert_template_module + LEFT JOIN talert_actions t4 + ON t3.id_alert_action = t4.id + LEFT JOIN talert_commands t5 + ON t4.id_alert_command = t5.id' ); if ($alerts === false) { @@ -1141,8 +1142,8 @@ function api_get_module_properties_by_alias($alias, $module_name, $other, $retur $sql = sprintf( 'SELECT tagente_modulo.id_agente_modulo, tagente.id_agente FROM tagente_modulo - INNER JOIN tagente ON tagente_modulo.id_agente = tagente.id_agente - WHERE tagente.alias LIKE "%s" AND tagente_modulo.nombre LIKE "%s"', + INNER JOIN tagente ON tagente_modulo.id_agente = tagente.id_agente + WHERE tagente.alias LIKE "%s" AND tagente_modulo.nombre LIKE "%s"', $alias, $module_name ); @@ -1258,13 +1259,13 @@ function get_module_properties($id_module, $fields, $separator, $returnType, $re $returnVar = []; $modules = db_get_all_rows_sql( 'SELECT * - FROM (SELECT id_agente_modulo as module_id_agent_modulo '.$module_additional_columns.' - FROM tagente_modulo - WHERE id_agente_modulo = '.$id_module.') t1 - INNER JOIN (SELECT id_agente_modulo as module_id_agent_modulo '.$estado_additional_columns.' - FROM tagente_estado - WHERE id_agente_modulo = '.$id_module.') t2 - ON t1.module_id_agent_modulo = t2.module_id_agent_modulo' + FROM (SELECT id_agente_modulo as module_id_agent_modulo '.$module_additional_columns.' + FROM tagente_modulo + WHERE id_agente_modulo = '.$id_module.') t1 + INNER JOIN (SELECT id_agente_modulo as module_id_agent_modulo '.$estado_additional_columns.' + FROM tagente_estado + WHERE id_agente_modulo = '.$id_module.') t2 + ON t1.module_id_agent_modulo = t2.module_id_agent_modulo' ); if ($modules === false) { @@ -1328,8 +1329,8 @@ function api_set_update_agent($id_agent, $thrash2, $other, $thrash3) if ($cascadeProtection == 1) { if (($idParent != 0) && (db_get_value_sql( 'SELECT id_agente_modulo - FROM tagente_modulo - WHERE id_agente = '.$idParent.' AND id_agente_modulo = '.$cascadeProtectionModule + FROM tagente_modulo + WHERE id_agente = '.$idParent.' AND id_agente_modulo = '.$cascadeProtectionModule ) === false) ) { returnError('parent_agent_not_exist', 'Is not a parent module to do cascade protection.'); @@ -1368,7 +1369,7 @@ function api_set_update_agent($id_agent, $thrash2, $other, $thrash3) ); $tpolicy_group_old = db_get_all_rows_sql( 'SELECT id_policy FROM tpolicy_groups - WHERE id_group = '.$values_old['id_grupo'] + WHERE id_group = '.$values_old['id_grupo'] ); $return = db_process_sql_update( @@ -1413,7 +1414,7 @@ function api_set_update_agent($id_agent, $thrash2, $other, $thrash3) foreach ($tpolicy_group_old as $key => $value) { $tpolicy_agents_old = db_get_sql( 'SELECT * FROM tpolicy_agents - WHERE id_policy = '.$value['id_policy'].' AND id_agent = '.$id_agent + WHERE id_policy = '.$value['id_policy'].' AND id_agent = '.$id_agent ); if ($tpolicy_agents_old) { @@ -1431,14 +1432,14 @@ function api_set_update_agent($id_agent, $thrash2, $other, $thrash3) $tpolicy_group = db_get_all_rows_sql( 'SELECT id_policy FROM tpolicy_groups - WHERE id_group = '.$idGroup + WHERE id_group = '.$idGroup ); if ($tpolicy_group) { foreach ($tpolicy_group as $key => $value) { $tpolicy_agents = db_get_sql( 'SELECT * FROM tpolicy_agents - WHERE id_policy = '.$value['id_policy'].' AND id_agent ='.$id_agent + WHERE id_policy = '.$value['id_policy'].' AND id_agent ='.$id_agent ); if (!$tpolicy_agents) { @@ -1528,8 +1529,8 @@ function api_set_new_agent($thrash1, $thrash2, $other, $thrash3) if ($cascadeProtection == 1) { if (($idParent != 0) && (db_get_value_sql( 'SELECT id_agente_modulo - FROM tagente_modulo - WHERE id_agente = '.$idParent.' AND id_agente_modulo = '.$cascadeProtectionModule + FROM tagente_modulo + WHERE id_agente = '.$idParent.' AND id_agente_modulo = '.$cascadeProtectionModule ) === false) ) { returnError('parent_agent_not_exist', 'Is not a parent module to do cascade protection.'); @@ -1541,13 +1542,13 @@ function api_set_new_agent($thrash1, $thrash2, $other, $thrash3) switch ($config['dbtype']) { case 'mysql': $sql1 = 'SELECT name - FROM tserver WHERE BINARY name LIKE "'.$nameServer.'"'; + FROM tserver WHERE BINARY name LIKE "'.$nameServer.'"'; break; case 'postgresql': case 'oracle': $sql1 = 'SELECT name - FROM tserver WHERE name LIKE \''.$nameServer.'\''; + FROM tserver WHERE name LIKE \''.$nameServer.'\''; break; } @@ -1577,15 +1578,15 @@ function api_set_new_agent($thrash1, $thrash2, $other, $thrash3) returnError('agent_name_exist', 'The name of agent yet exist in DB.'); } else if (db_get_value_sql( 'SELECT id_grupo - FROM tgrupo - WHERE id_grupo = '.$idGroup + FROM tgrupo + WHERE id_grupo = '.$idGroup ) === false ) { returnError('id_grupo_not_exist', 'The group don`t exist.'); } else if (db_get_value_sql( 'SELECT id_os - FROM tconfig_os - WHERE id_os = '.$idOS + FROM tconfig_os + WHERE id_os = '.$idOS ) === false ) { returnError('id_os_not_exist', 'The OS don`t exist.'); @@ -1620,7 +1621,7 @@ function api_set_new_agent($thrash1, $thrash2, $other, $thrash3) if ($idGroup && !empty($idAgente)) { $tpolicy_group_old = db_get_all_rows_sql( 'SELECT id_policy FROM tpolicy_groups - WHERE id_group = '.$idGroup + WHERE id_group = '.$idGroup ); if ($tpolicy_group_old) { @@ -1803,17 +1804,23 @@ function api_get_custom_field_id($t1, $t2, $other, $returnType) /** - * Delete a agent with the name pass as parameter. + * Delete an agent with the name as parameter. * * @param string $id Name of agent to delete. * @param $thrash1 Don't use. * @param $thrast2 Don't use. * @param $thrash3 Don't use. */ -function api_set_delete_agent($id, $thrash1, $thrast2, $thrash3) +function api_set_delete_agent($id, $thrash1, $other, $thrash3) { global $config; + $agent_by_alias = false; + + if ($other['data'][0] === '1') { + $agent_by_alias = true; + } + if (is_metaconsole()) { if (!check_acl($config['id_user'], 0, 'PM')) { returnError('forbidden', 'string'); @@ -1822,8 +1829,8 @@ function api_set_delete_agent($id, $thrash1, $thrast2, $thrash3) $servers = db_get_all_rows_sql( 'SELECT * - FROM tmetaconsole_setup - WHERE disabled = 0' + FROM tmetaconsole_setup + WHERE disabled = 0' ); if ($servers === false) { @@ -1832,19 +1839,47 @@ function api_set_delete_agent($id, $thrash1, $thrast2, $thrash3) foreach ($servers as $server) { if (metaconsole_connect($server) == NOERR) { - $idAgent[0] = agents_get_agent_id($id, true); + if ($other['data'][0] === '1') { + $idAgent[0] = agents_get_agent_id_by_alias($id); + } else { + $idAgent[0] = agents_get_agent_id($id, true); + } + if ($idAgent[0]) { $result = agents_delete_agent($idAgent, true); } + + metaconsole_restore_db(); } } } else { - $idAgent = agents_get_agent_id($id); - if (!util_api_check_agent_and_print_error($idAgent, 'string', 'AD')) { - return; + if ($agent_by_alias) { + $idsAgents = agents_get_agent_id_by_alias(io_safe_input($id)); + } else { + $idAgent = agents_get_agent_id($id, true); } - $result = agents_delete_agent($idAgent, true); + if (!$agent_by_alias) { + if (!util_api_check_agent_and_print_error($idAgent, 'string', 'AD')) { + return; + } + } + + if ($agent_by_alias) { + foreach ($idsAgents as $id) { + if (!util_api_check_agent_and_print_error($id['id_agente'], 'string', 'AD')) { + continue; + } + + $result = agents_delete_agent($id['id_agente'], true); + + if (!$result) { + break; + } + } + } else { + $result = agents_delete_agent($idAgent, true); + } } if (!$result) { @@ -1931,20 +1966,20 @@ function api_get_all_agents($thrash1, $thrash2, $other, $returnType) // Filter by state if (defined('METACONSOLE')) { $sql = "SELECT id_agente, alias, direccion, comentarios, - tconfig_os.name, url_address, nombre - FROM tconfig_os, tmetaconsole_agent - LEFT JOIN tagent_secondary_group - ON tmetaconsole_agent.id_agente = tagent_secondary_group.id_agent - WHERE tmetaconsole_agent.id_os = tconfig_os.id_os - AND disabled = 0 $where AND $groups"; + tconfig_os.name, url_address, nombre + FROM tconfig_os, tmetaconsole_agent + LEFT JOIN tagent_secondary_group + ON tmetaconsole_agent.id_agente = tagent_secondary_group.id_agent + WHERE tmetaconsole_agent.id_os = tconfig_os.id_os + AND disabled = 0 $where AND $groups"; } else { $sql = "SELECT id_agente, alias, direccion, comentarios, - tconfig_os.name, url_address, nombre - FROM tconfig_os, tagente - LEFT JOIN tagent_secondary_group - ON tagente.id_agente = tagent_secondary_group.id_agent - WHERE tagente.id_os = tconfig_os.id_os - AND disabled = 0 $where AND $groups"; + tconfig_os.name, url_address, nombre + FROM tconfig_os, tagente + LEFT JOIN tagent_secondary_group + ON tagente.id_agente = tagent_secondary_group.id_agent + WHERE tagente.id_os = tconfig_os.id_os + AND disabled = 0 $where AND $groups"; } $all_agents = db_get_all_rows_sql($sql); @@ -2068,9 +2103,9 @@ function api_get_agent_modules($thrash1, $thrash2, $other, $thrash3) $sql = sprintf( 'SELECT id_agente, id_agente_modulo, nombre - FROM tagente_modulo - WHERE id_agente = %d AND disabled = 0 - AND delete_pending = 0', + FROM tagente_modulo + WHERE id_agente = %d AND disabled = 0 + AND delete_pending = 0', $other['data'][0] ); @@ -2417,9 +2452,9 @@ function api_get_module_id($id, $thrash1, $name, $thrash3) $sql = sprintf( 'SELECT id_agente_modulo - FROM tagente_modulo WHERE id_agente = %d - AND nombre = "%s" AND disabled = 0 - AND delete_pending = 0', + FROM tagente_modulo WHERE id_agente = %d + AND nombre = "%s" AND disabled = 0 + AND delete_pending = 0', $id, $name['data'] ); @@ -2459,10 +2494,10 @@ function api_get_group_agent($thrash1, $thrash2, $other, $thrash3) $sql = sprintf( 'SELECT groups.nombre nombre - FROM tagente agents, tgrupo groups - WHERE id_agente = %d AND agents.disabled = 0 - AND groups.disabled = 0 - AND agents.id_grupo = groups.id_grupo', + FROM tagente agents, tgrupo groups + WHERE id_agente = %d AND agents.disabled = 0 + AND groups.disabled = 0 + AND agents.id_grupo = groups.id_grupo', $other['data'][0] ); @@ -2501,8 +2536,8 @@ function api_get_group_agent_by_name($thrash1, $thrash2, $other, $thrash3) if (is_metaconsole()) { $servers = db_get_all_rows_sql( 'SELECT * - FROM tmetaconsole_setup - WHERE disabled = 0' + FROM tmetaconsole_setup + WHERE disabled = 0' ); if ($servers === false) { @@ -2516,9 +2551,9 @@ function api_get_group_agent_by_name($thrash1, $thrash2, $other, $thrash3) if ($agent_id) { $sql = sprintf( 'SELECT groups.nombre nombre - FROM tagente agents, tgrupo groups - WHERE id_agente = %d - AND agents.id_grupo = groups.id_grupo', + FROM tagente agents, tgrupo groups + WHERE id_agente = %d + AND agents.id_grupo = groups.id_grupo', $agent_id ); $group_server_names = db_get_all_rows_sql($sql); @@ -2541,9 +2576,9 @@ function api_get_group_agent_by_name($thrash1, $thrash2, $other, $thrash3) $sql = sprintf( 'SELECT groups.nombre nombre - FROM tagente agents, tgrupo groups - WHERE id_agente = %d - AND agents.id_grupo = groups.id_grupo', + FROM tagente agents, tgrupo groups + WHERE id_agente = %d + AND agents.id_grupo = groups.id_grupo', $agent_id ); $group_names = db_get_all_rows_sql($sql); @@ -2593,8 +2628,8 @@ function api_get_group_agent_by_alias($thrash1, $thrash2, $other, $thrash3) if (is_metaconsole()) { $servers = db_get_all_rows_sql( 'SELECT * - FROM tmetaconsole_setup - WHERE disabled = 0' + FROM tmetaconsole_setup + WHERE disabled = 0' ); if ($servers === false) { @@ -2609,9 +2644,9 @@ function api_get_group_agent_by_alias($thrash1, $thrash2, $other, $thrash3) foreach ($agent_id as &$id) { $sql = sprintf( 'SELECT groups.nombre nombre - FROM tagente agents, tgrupo groups - WHERE id_agente = %d - AND agents.id_grupo = groups.id_grupo', + FROM tagente agents, tgrupo groups + WHERE id_agente = %d + AND agents.id_grupo = groups.id_grupo', $id['id_agente'] ); $group_server_names = db_get_all_rows_sql($sql); @@ -2637,9 +2672,9 @@ function api_get_group_agent_by_alias($thrash1, $thrash2, $other, $thrash3) $sql = sprintf( 'SELECT groups.nombre nombre - FROM tagente agents, tgrupo groups - WHERE id_agente = %d - AND agents.id_grupo = groups.id_grupo', + FROM tagente agents, tgrupo groups + WHERE id_agente = %d + AND agents.id_grupo = groups.id_grupo', $id['id_agente'] ); $group_name = db_get_all_rows_sql($sql); @@ -2682,8 +2717,8 @@ function api_get_locate_agent($id, $thrash1, $thrash2, $thrash3) $servers = db_get_all_rows_sql( 'SELECT * - FROM tmetaconsole_setup - WHERE disabled = 0' + FROM tmetaconsole_setup + WHERE disabled = 0' ); if ($servers === false) { @@ -2740,8 +2775,8 @@ function api_get_id_group_agent_by_name($thrash1, $thrash2, $other, $thrash3) if (is_metaconsole()) { $servers = db_get_all_rows_sql( 'SELECT * - FROM tmetaconsole_setup - WHERE disabled = 0' + FROM tmetaconsole_setup + WHERE disabled = 0' ); if ($servers === false) { @@ -2755,9 +2790,9 @@ function api_get_id_group_agent_by_name($thrash1, $thrash2, $other, $thrash3) if ($agent_id) { $sql = sprintf( 'SELECT groups.id_grupo id_group - FROM tagente agents, tgrupo groups - WHERE id_agente = %d - AND agents.id_grupo = groups.id_grupo', + FROM tagente agents, tgrupo groups + WHERE id_agente = %d + AND agents.id_grupo = groups.id_grupo', $agent_id ); $group_server_names = db_get_all_rows_sql($sql); @@ -2781,9 +2816,9 @@ function api_get_id_group_agent_by_name($thrash1, $thrash2, $other, $thrash3) $sql = sprintf( 'SELECT groups.id_grupo id_group - FROM tagente agents, tgrupo groups - WHERE id_agente = %d - AND agents.id_grupo = groups.id_grupo', + FROM tagente agents, tgrupo groups + WHERE id_agente = %d + AND agents.id_grupo = groups.id_grupo', $agent_id ); $group_names = db_get_all_rows_sql($sql); @@ -2833,8 +2868,8 @@ function api_get_id_group_agent_by_alias($thrash1, $thrash2, $other, $thrash3) if (is_metaconsole()) { $servers = db_get_all_rows_sql( 'SELECT * - FROM tmetaconsole_setup - WHERE disabled = 0' + FROM tmetaconsole_setup + WHERE disabled = 0' ); if ($servers === false) { @@ -2849,9 +2884,9 @@ function api_get_id_group_agent_by_alias($thrash1, $thrash2, $other, $thrash3) foreach ($agent_id as &$id) { $sql = sprintf( 'SELECT groups.id_grupo id_group - FROM tagente agents, tgrupo groups - WHERE id_agente = %d - AND agents.id_grupo = groups.id_grupo', + FROM tagente agents, tgrupo groups + WHERE id_agente = %d + AND agents.id_grupo = groups.id_grupo', $id['id_agente'] ); $group_server_names = db_get_all_rows_sql($sql); @@ -2877,9 +2912,9 @@ function api_get_id_group_agent_by_alias($thrash1, $thrash2, $other, $thrash3) $sql = sprintf( 'SELECT groups.id_grupo id_group - FROM tagente agents, tgrupo groups - WHERE id_agente = %d - AND agents.id_grupo = groups.id_grupo', + FROM tagente agents, tgrupo groups + WHERE id_agente = %d + AND agents.id_grupo = groups.id_grupo', $id['id_agente'] ); $group_name = db_get_all_rows_sql($sql); @@ -2937,8 +2972,8 @@ function api_get_policies($thrash1, $thrash2, $other, $thrash3) $sql = sprintf( 'SELECT policy.id, name, id_agent - FROM tpolicies AS policy, tpolicy_agents AS pol_agents - WHERE policy.id = pol_agents.id_policy %s AND id_group IN (%s)', + FROM tpolicies AS policy, tpolicy_agents AS pol_agents + WHERE policy.id = pol_agents.id_policy %s AND id_group IN (%s)', $where, $user_groups ); @@ -3036,12 +3071,22 @@ function api_set_create_network_module($id, $thrash1, $other, $thrash3) return; } - $agentName = $id; + $agent_by_alias = false; - $idAgent = agents_get_agent_id($agentName); + if ($other['data'][30] === '1') { + $agent_by_alias = true; + } - if (!util_api_check_agent_and_print_error($idAgent, 'string', 'AW')) { - return; + if ($agent_by_alias) { + $idsAgents = agents_get_agent_id_by_alias($id); + } else { + $idAgent = agents_get_agent_id($id); + } + + if (!$agent_by_alias) { + if (!util_api_check_agent_and_print_error($idAgent, 'string', 'AW')) { + return; + } } if ($other['data'][2] < 6 or $other['data'][2] > 18) { @@ -3059,7 +3104,6 @@ function api_set_create_network_module($id, $thrash1, $other, $thrash3) $disabled_types_event = json_encode($disabled_types_event); $values = [ - 'id_agente' => $idAgent, 'disabled' => $other['data'][1], 'id_tipo_modulo' => $other['data'][2], 'id_module_group' => $other['data'][3], @@ -3102,7 +3146,27 @@ function api_set_create_network_module($id, $thrash1, $other, $thrash3) // Column 'module_macros' cannot be null } - $idModule = modules_create_agent_module($idAgent, $name, $values, true); + if ($agent_by_alias) { + $agents_affected = 0; + + foreach ($idsAgents as $id) { + if (!util_api_check_agent_and_print_error($id['id_agente'], 'string', 'AW')) { + continue; + } + + $idModule = modules_create_agent_module($id['id_agente'], $name, $values, true); + + if (!is_error($idModule)) { + $agents_affected++; + } + } + + returnData('string', ['type' => 'string', 'data' => $agents_affected.' agents affected']); + + return; + } else { + $idModule = modules_create_agent_module($idAgent, $name, $values, true); + } if (is_error($idModule)) { // TODO: Improve the error returning more info @@ -3270,17 +3334,27 @@ function api_set_create_plugin_module($id, $thrash1, $other, $thrash3) return; } - $agentName = $id; - if ($other['data'][22] == '') { returnError('error_create_plugin_module', __('Error in creation plugin module. Id_plugin cannot be left blank.')); return; } - $idAgent = agents_get_agent_id($agentName); + $agent_by_alias = false; - if (!util_api_check_agent_and_print_error($idAgent, 'string', 'AW')) { - return; + if ($other['data'][36] === '1') { + $agent_by_alias = true; + } + + if ($agent_by_alias) { + $idsAgents = agents_get_agent_id_by_alias($id); + } else { + $idAgent = agents_get_agent_id($id); + } + + if (!$agent_by_alias) { + if (!util_api_check_agent_and_print_error($idAgent, 'string', 'AW')) { + return; + } } $disabled_types_event = []; @@ -3290,7 +3364,6 @@ function api_set_create_plugin_module($id, $thrash1, $other, $thrash3) $name = $other['data'][0]; $values = [ - 'id_agente' => $idAgent, 'disabled' => $other['data'][1], 'id_tipo_modulo' => $other['data'][2], 'id_module_group' => $other['data'][3], @@ -3338,7 +3411,27 @@ function api_set_create_plugin_module($id, $thrash1, $other, $thrash3) // Column 'module_macros' cannot be null } - $idModule = modules_create_agent_module($idAgent, $name, $values, true); + if ($agent_by_alias) { + $agents_affected = 0; + + foreach ($idsAgents as $id) { + if (!util_api_check_agent_and_print_error($id['id_agente'], 'string', 'AW')) { + continue; + } + + $idModule = modules_create_agent_module($id['id_agente'], $name, $values, true); + + if (!is_error($idModule)) { + $agents_affected++; + } + } + + returnData('string', ['type' => 'string', 'data' => $agents_affected.' agents affected']); + + return; + } else { + $idModule = modules_create_agent_module($idAgent, $name, $values, true); + } if (is_error($idModule)) { // TODO: Improve the error returning more info @@ -3499,17 +3592,27 @@ function api_set_create_data_module($id, $thrash1, $other, $thrash3) return; } - $agentName = $id; - if ($other['data'][0] == '') { returnError('error_create_data_module', __('Error in creation data module. Module_name cannot be left blank.')); return; } - $idAgent = agents_get_agent_id($agentName); + $agent_by_alias = false; - if (!util_api_check_agent_and_print_error($idAgent, 'string', 'AW')) { - return; + if ($other['data'][27] === '1') { + $agent_by_alias = true; + } + + if ($agent_by_alias) { + $idsAgents = agents_get_agent_id_by_alias($id); + } else { + $idAgent = agents_get_agent_id($id); + } + + if (!$agent_by_alias) { + if (!util_api_check_agent_and_print_error($idAgent, 'string', 'AW')) { + return; + } } $name = $other['data'][0]; @@ -3519,7 +3622,6 @@ function api_set_create_data_module($id, $thrash1, $other, $thrash3) $disabled_types_event = json_encode($disabled_types_event); $values = [ - 'id_agente' => $idAgent, 'disabled' => $other['data'][1], 'id_tipo_modulo' => $other['data'][2], 'descripcion' => $other['data'][3], @@ -3558,7 +3660,27 @@ function api_set_create_data_module($id, $thrash1, $other, $thrash3) // Column 'module_macros' cannot be null } - $idModule = modules_create_agent_module($idAgent, $name, $values, true); + if ($agent_by_alias) { + $agents_affected = 0; + + foreach ($idsAgents as $id) { + if (!util_api_check_agent_and_print_error($id['id_agente'], 'string', 'AW')) { + continue; + } + + $idModule = modules_create_agent_module($id['id_agente'], $name, $values, true); + + if (!is_error($idModule)) { + $agents_affected++; + } + } + + returnData('string', ['type' => 'string', 'data' => $agents_affected.' agents affected']); + + return; + } else { + $idModule = modules_create_agent_module($idAgent, $name, $values, true); + } if (is_error($idModule)) { // TODO: Improve the error returning more info @@ -3583,7 +3705,7 @@ function api_set_create_data_module($id, $thrash1, $other, $thrash3) * * @param $thrash3 Don't use */ -function api_set_create_synthetic_module($id, $thrash1, $other, $thrash3) +function api_set_create_synthetic_module($id, $agent_by_alias, $other, $thrash3) { if (defined('METACONSOLE')) { return; @@ -3591,8 +3713,6 @@ function api_set_create_synthetic_module($id, $thrash1, $other, $thrash3) global $config; - $agentName = $id; - io_safe_input_array($other); if ($other['data'][0] == '') { @@ -3600,15 +3720,34 @@ function api_set_create_synthetic_module($id, $thrash1, $other, $thrash3) return; } - $idAgent = agents_get_agent_id(io_safe_output($agentName), true); + if ($agent_by_alias == '1') { + $ids_agents = agents_get_agent_id_by_alias(io_safe_output($id)); - if (!util_api_check_agent_and_print_error($idAgent, 'string', 'AW')) { - return; + foreach ($ids_agents as $id) { + if (!util_api_check_agent_and_print_error($id['id_agente'], 'string', 'AW')) { + return; + } + } + } else { + $idAgent = agents_get_agent_id(io_safe_output($id), true); + + if (!util_api_check_agent_and_print_error($idAgent, 'string', 'AW')) { + return; + } } - if (!$idAgent) { - returnError('error_create_data_module', __('Error in creation synthetic module. Agent name doesn\'t exist.')); - return; + if ($agent_by_alias) { + foreach ($ids_agents as $id) { + if (!$id['id_agente']) { + returnError('error_create_data_module', __('Error in creation synthetic module. Agent name doesn\'t exist.')); + return; + } + } + } else { + if (!$idAgent) { + returnError('error_create_data_module', __('Error in creation synthetic module. Agent name doesn\'t exist.')); + return; + } } $name = io_safe_output($other['data'][0]); @@ -3616,7 +3755,6 @@ function api_set_create_synthetic_module($id, $thrash1, $other, $thrash3) $id_tipo_modulo = db_get_row_sql("SELECT id_tipo FROM ttipo_modulo WHERE nombre = 'generic_data'"); $values = [ - 'id_agente' => $idAgent, 'id_modulo' => 5, 'custom_integer_1' => 0, 'custom_integer_2' => 0, @@ -3629,151 +3767,312 @@ function api_set_create_synthetic_module($id, $thrash1, $other, $thrash3) // Column 'descripcion' cannot be null } - $idModule = modules_create_agent_module($idAgent, $name, $values, true); + if ($agent_by_alias) { + foreach ($ids_agents as $id) { + $idAgent = $id['id_agente']; - if (is_error($idModule)) { - // TODO: Improve the error returning more info - returnError('error_create_data_module', __('Error in creation data module.')); - } else { - $synthetic_type = $other['data'][1]; - unset($other['data'][0]); - unset($other['data'][1]); + $idModule = modules_create_agent_module($idAgent, $name, $values, true); - $filterdata = []; - foreach ($other['data'] as $data) { - $data = str_replace(['ADD', 'SUB', 'MUL', 'DIV'], ['+', '-', '*', '/'], $data); - $split_data = explode(';', $data); + if (is_error($idModule)) { + // TODO: Improve the error returning more info + returnError('error_create_data_module', __('Error in creation data module.')); + } else { + $synthetic_type = $other['data'][1]; + unset($other['data'][0]); + unset($other['data'][1]); - if (preg_match('/[x\/+*-]/', $split_data[0]) && strlen($split_data[0]) == 1) { - if (preg_match('/[\/|+|*|-]/', $split_data[0]) && $synthetic_type === 'average') { - returnError('', "[ERROR] With this type: $synthetic_type only be allow use this operator: 'x' \n\n"); + $filterdata = []; + foreach ($other['data'] as $data) { + $data = str_replace(['ADD', 'SUB', 'MUL', 'DIV'], ['+', '-', '*', '/'], $data); + $data = io_safe_output($data); + // Double safe output is necessary. + $split_data = explode(';', io_safe_output($data)); + + if (preg_match('/[x\/+*-]/', $split_data[0]) && strlen($split_data[0]) == 1) { + if (preg_match('/[\/|+|*|-]/', $split_data[0]) && $synthetic_type === 'average') { + returnError('', "[ERROR] With this type: $synthetic_type only be allow use this operator: 'x' \n\n"); + } + + $operator = strtolower($split_data[0]); + $data_module = [ + '', + $operator, + $split_data[1], + ]; + + $text_data = implode('_', $data_module); + array_push($filterdata, $text_data); + } else { + if (count($split_data) == 2) { + $idAgent = agents_get_agent_id($split_data[0], true); + $data_module = [ + $idAgent, + '', + $split_data[1], + ]; + $text_data = implode('_', $data_module); + array_push($filterdata, $text_data); + } else { + if (strlen($split_data[1]) > 1 && $synthetic_type != 'average') { + returnError('', "[ERROR] You can only use +, -, *, / or x, and you use this: @split_data[1] \n\n"); + return; + } + + if (preg_match('/[\/|+|*|-]/', $split_data[1]) && $synthetic_type === 'average') { + returnError('', "[ERROR] With this type: $synthetic_type only be allow use this operator: 'x' \n\n"); + return; + } + + $idAgent = agents_get_agent_id(io_safe_output($split_data[0]), true); + $operator = strtolower($split_data[1]); + $data_module = [ + $idAgent, + $operator, + $split_data[2], + ]; + $text_data = implode('_', $data_module); + array_push($filterdata, $text_data); + } + } } - $operator = strtolower($split_data[0]); - $data_module = [ - '', - $operator, - $split_data[1], - ]; + $serialize_ops = implode(',', $filterdata); - $text_data = implode('_', $data_module); - array_push($filterdata, $text_data); - } else { - if (count($split_data) == 2) { - $idAgent = agents_get_agent_id(io_safe_output($split_data[0]), true); - $data_module = [ - $idAgent, - '', - $split_data[1], - ]; - $text_data = implode('_', $data_module); - array_push($filterdata, $text_data); + // modules_create_synthetic_operations + $synthetic = enterprise_hook( + 'modules_create_synthetic_operations', + [ + $idModule, + $serialize_ops, + ] + ); + + if ($synthetic === ENTERPRISE_NOT_HOOK) { + returnError('error_synthetic_modules', 'Error Synthetic modules.'); + db_process_sql_delete( + 'tagente_modulo', + ['id_agente_modulo' => $idModule] + ); + + return; } else { - if (strlen($split_data[1]) > 1 && $synthetic_type != 'average') { - returnError('', "[ERROR] You can only use +, -, *, / or x, and you use this: @split_data[1] \n\n"); - return; + $status = AGENT_MODULE_STATUS_NO_DATA; + switch ($config['dbtype']) { + case 'mysql': + $result = db_process_sql_insert( + 'tagente_estado', + [ + 'id_agente_modulo' => $idModule, + 'datos' => 0, + 'timestamp' => '01-01-1970 00:00:00', + 'estado' => $status, + 'id_agente' => (int) $idAgent, + 'utimestamp' => 0, + 'status_changes' => 0, + 'last_status' => $status, + 'last_known_status' => $status, + ] + ); + break; + + case 'postgresql': + $result = db_process_sql_insert( + 'tagente_estado', + [ + 'id_agente_modulo' => $idModule, + 'datos' => 0, + 'timestamp' => null, + 'estado' => $status, + 'id_agente' => (int) $idAgent, + 'utimestamp' => 0, + 'status_changes' => 0, + 'last_status' => $status, + 'last_known_status' => $status, + ] + ); + break; + + case 'oracle': + $result = db_process_sql_insert( + 'tagente_estado', + [ + 'id_agente_modulo' => $idModule, + 'datos' => 0, + 'timestamp' => '#to_date(\'1970-01-01 00:00:00\', \'YYYY-MM-DD HH24:MI:SS\')', + 'estado' => $status, + 'id_agente' => (int) $idAgent, + 'utimestamp' => 0, + 'status_changes' => 0, + 'last_status' => $status, + 'last_known_status' => $status, + ] + ); + break; } - if (preg_match('/[\/|+|*|-]/', $split_data[1]) && $synthetic_type === 'average') { - returnError('', "[ERROR] With this type: $synthetic_type only be allow use this operator: 'x' \n\n"); - return; + if ($result === false) { + db_process_sql_delete( + 'tagente_modulo', + ['id_agente_modulo' => $idModule] + ); + returnError('error_synthetic_modules', 'Error Synthetic modules.'); + } else { + db_process_sql('UPDATE tagente SET total_count=total_count+1, notinit_count=notinit_count+1 WHERE id_agente='.(int) $idAgent); + returnData('string', ['type' => 'string', 'data' => __('Synthetic module created ID: '.$idModule)]); } - - $idAgent = agents_get_agent_id(io_safe_output($split_data[0]), true); - $operator = strtolower($split_data[1]); - $data_module = [ - $idAgent, - $operator, - $split_data[2], - ]; - $text_data = implode('_', $data_module); - array_push($filterdata, $text_data); } } } + } else { + $idModule = modules_create_agent_module($idAgent, $name, $values, true); - $serialize_ops = implode(',', $filterdata); - - // modules_create_synthetic_operations - $synthetic = enterprise_hook( - 'modules_create_synthetic_operations', - [ - $idModule, - $serialize_ops, - ] - ); - - if ($synthetic === ENTERPRISE_NOT_HOOK) { - returnError('error_synthetic_modules', 'Error Synthetic modules.'); - db_process_sql_delete( - 'tagente_modulo', - ['id_agente_modulo' => $idModule] - ); - return; + if (is_error($idModule)) { + // TODO: Improve the error returning more info + returnError('error_create_data_module', __('Error in creation data module.')); } else { - $status = AGENT_MODULE_STATUS_NO_DATA; - switch ($config['dbtype']) { - case 'mysql': - $result = db_process_sql_insert( - 'tagente_estado', - [ - 'id_agente_modulo' => $idModule, - 'datos' => 0, - 'timestamp' => '01-01-1970 00:00:00', - 'estado' => $status, - 'id_agente' => (int) $idAgent, - 'utimestamp' => 0, - 'status_changes' => 0, - 'last_status' => $status, - 'last_known_status' => $status, - ] - ); - break; + $synthetic_type = $other['data'][1]; + unset($other['data'][0]); + unset($other['data'][1]); - case 'postgresql': - $result = db_process_sql_insert( - 'tagente_estado', - [ - 'id_agente_modulo' => $idModule, - 'datos' => 0, - 'timestamp' => null, - 'estado' => $status, - 'id_agente' => (int) $idAgent, - 'utimestamp' => 0, - 'status_changes' => 0, - 'last_status' => $status, - 'last_known_status' => $status, - ] - ); - break; + $filterdata = []; + foreach ($other['data'] as $data) { + $data = str_replace(['ADD', 'SUB', 'MUL', 'DIV'], ['+', '-', '*', '/'], $data); + $data = io_safe_output($data); + // Double safe output is necessary. + $split_data = explode(';', io_safe_output($data)); - case 'oracle': - $result = db_process_sql_insert( - 'tagente_estado', - [ - 'id_agente_modulo' => $idModule, - 'datos' => 0, - 'timestamp' => '#to_date(\'1970-01-01 00:00:00\', \'YYYY-MM-DD HH24:MI:SS\')', - 'estado' => $status, - 'id_agente' => (int) $idAgent, - 'utimestamp' => 0, - 'status_changes' => 0, - 'last_status' => $status, - 'last_known_status' => $status, - ] - ); - break; + if (preg_match('/[x\/+*-]/', $split_data[0]) && strlen($split_data[0]) == 1) { + if (preg_match('/[\/|+|*|-]/', $split_data[0]) && $synthetic_type === 'average') { + returnError('', "[ERROR] With this type: $synthetic_type only be allow use this operator: 'x' \n\n"); + } + + $operator = strtolower($split_data[0]); + $data_module = [ + '', + $operator, + $split_data[1], + ]; + + $text_data = implode('_', $data_module); + array_push($filterdata, $text_data); + } else { + if (count($split_data) == 2) { + $idAgent = agents_get_agent_id($split_data[0], true); + $data_module = [ + $idAgent, + '', + $split_data[1], + ]; + $text_data = implode('_', $data_module); + array_push($filterdata, $text_data); + } else { + if (strlen($split_data[1]) > 1 && $synthetic_type != 'average') { + returnError('', "[ERROR] You can only use +, -, *, / or x, and you use this: @split_data[1] \n\n"); + return; + } + + if (preg_match('/[\/|+|*|-]/', $split_data[1]) && $synthetic_type === 'average') { + returnError('', "[ERROR] With this type: $synthetic_type only be allow use this operator: 'x' \n\n"); + return; + } + + $idAgent = agents_get_agent_id(io_safe_output($split_data[0]), true); + $operator = strtolower($split_data[1]); + $data_module = [ + $idAgent, + $operator, + $split_data[2], + ]; + $text_data = implode('_', $data_module); + array_push($filterdata, $text_data); + } + } } - if ($result === false) { + $serialize_ops = implode(',', $filterdata); + + // modules_create_synthetic_operations + $synthetic = enterprise_hook( + 'modules_create_synthetic_operations', + [ + $idModule, + $serialize_ops, + ] + ); + + if ($synthetic === ENTERPRISE_NOT_HOOK) { + returnError('error_synthetic_modules', 'Error Synthetic modules.'); db_process_sql_delete( 'tagente_modulo', ['id_agente_modulo' => $idModule] ); - returnError('error_synthetic_modules', 'Error Synthetic modules.'); + + return; } else { - db_process_sql('UPDATE tagente SET total_count=total_count+1, notinit_count=notinit_count+1 WHERE id_agente='.(int) $idAgent); - returnData('string', ['type' => 'string', 'data' => __('Synthetic module created ID: '.$idModule)]); + $status = AGENT_MODULE_STATUS_NO_DATA; + switch ($config['dbtype']) { + case 'mysql': + $result = db_process_sql_insert( + 'tagente_estado', + [ + 'id_agente_modulo' => $idModule, + 'datos' => 0, + 'timestamp' => '01-01-1970 00:00:00', + 'estado' => $status, + 'id_agente' => (int) $idAgent, + 'utimestamp' => 0, + 'status_changes' => 0, + 'last_status' => $status, + 'last_known_status' => $status, + ] + ); + break; + + case 'postgresql': + $result = db_process_sql_insert( + 'tagente_estado', + [ + 'id_agente_modulo' => $idModule, + 'datos' => 0, + 'timestamp' => null, + 'estado' => $status, + 'id_agente' => (int) $idAgent, + 'utimestamp' => 0, + 'status_changes' => 0, + 'last_status' => $status, + 'last_known_status' => $status, + ] + ); + break; + + case 'oracle': + $result = db_process_sql_insert( + 'tagente_estado', + [ + 'id_agente_modulo' => $idModule, + 'datos' => 0, + 'timestamp' => '#to_date(\'1970-01-01 00:00:00\', \'YYYY-MM-DD HH24:MI:SS\')', + 'estado' => $status, + 'id_agente' => (int) $idAgent, + 'utimestamp' => 0, + 'status_changes' => 0, + 'last_status' => $status, + 'last_known_status' => $status, + ] + ); + break; + } + + if ($result === false) { + db_process_sql_delete( + 'tagente_modulo', + ['id_agente_modulo' => $idModule] + ); + returnError('error_synthetic_modules', 'Error Synthetic modules.'); + } else { + db_process_sql('UPDATE tagente SET total_count=total_count+1, notinit_count=notinit_count+1 WHERE id_agente='.(int) $idAgent); + returnData('string', ['type' => 'string', 'data' => __('Synthetic module created ID: '.$idModule)]); + } } } } @@ -3923,8 +4222,6 @@ function api_set_create_snmp_module($id, $thrash1, $other, $thrash3) return; } - $agentName = $id; - if ($other['data'][0] == '') { returnError('error_create_snmp_module', __('Error in creation SNMP module. Module_name cannot be left blank.')); return; @@ -3935,10 +4232,22 @@ function api_set_create_snmp_module($id, $thrash1, $other, $thrash3) return; } - $idAgent = agents_get_agent_id($agentName); + $agent_by_alias = false; - if (!util_api_check_agent_and_print_error($idAgent, 'string', 'AW')) { - return; + if ($other['data'][35] === '1') { + $agent_by_alias = true; + } + + if ($agent_by_alias) { + $idsAgents = agents_get_agent_id_by_alias($id); + } else { + $idAgent = agents_get_agent_id($id); + } + + if (!$agent_by_alias) { + if (!util_api_check_agent_and_print_error($idAgent, 'string', 'AW')) { + return; + } } $name = $other['data'][0]; @@ -3965,7 +4274,6 @@ function api_set_create_snmp_module($id, $thrash1, $other, $thrash3) } $values = [ - 'id_agente' => $idAgent, 'disabled' => $other['data'][1], 'id_tipo_modulo' => $other['data'][2], 'id_module_group' => $other['data'][3], @@ -4003,7 +4311,6 @@ function api_set_create_snmp_module($id, $thrash1, $other, $thrash3) ]; } else { $values = [ - 'id_agente' => $idAgent, 'disabled' => $other['data'][1], 'id_tipo_modulo' => $other['data'][2], 'id_module_group' => $other['data'][3], @@ -4040,7 +4347,27 @@ function api_set_create_snmp_module($id, $thrash1, $other, $thrash3) // Column 'descripcion' cannot be null } - $idModule = modules_create_agent_module($idAgent, $name, $values, true); + if ($agent_by_alias) { + $agents_affected = 0; + + foreach ($idsAgents as $id) { + if (!util_api_check_agent_and_print_error($id['id_agente'], 'string', 'AW')) { + continue; + } + + $idModule = modules_create_agent_module($id['id_agente'], $name, $values, true); + + if (!is_error($idModule)) { + $agents_affected++; + } + } + + returnData('string', ['type' => 'string', 'data' => $agents_affected.' agents affected']); + + return; + } else { + $idModule = modules_create_agent_module($idAgent, $name, $values, true); + } if (is_error($idModule)) { // TODO: Improve the error returning more info @@ -4743,9 +5070,9 @@ function api_get_module_value_all_agents($id, $thrash1, $other, $thrash2) $sql = sprintf( "SELECT agent.id_agente, agent.alias, module_state.datos, agent.nombre - FROM tagente agent LEFT JOIN tagent_secondary_group tasg ON agent.id_agente = tasg.id_agent, tagente_modulo module, tagente_estado module_state - WHERE agent.id_agente = module.id_agente AND module.id_agente_modulo=module_state.id_agente_modulo AND module.nombre = '%s' - AND %s", + FROM tagente agent LEFT JOIN tagent_secondary_group tasg ON agent.id_agente = tasg.id_agent, tagente_modulo module, tagente_estado module_state + WHERE agent.id_agente = module.id_agente AND module.id_agente_modulo=module_state.id_agente_modulo AND module.nombre = '%s' + AND %s", $id, $groups ); @@ -5372,20 +5699,48 @@ function api_get_plugins($thrash1, $thrash2, $other, $thrash3) * @param $thrash1 Don't use * @param $thrash2 Don't use */ -function api_set_create_network_module_from_component($agent_name, $component_name, $thrash1, $thrash2) +function api_set_create_network_module_from_component($agent_name, $component_name, $other, $thrash2) { if (defined('METACONSOLE')) { return; } - $agent_id = agents_get_agent_id($agent_name); - if (!util_api_check_agent_and_print_error($agent_id, 'string', 'AW')) { - return; + $agent_by_alias = false; + + if ($other['data'][0] === '1') { + $agent_by_alias = true; } - if (!$agent_id) { - returnError('error_network_module_from_component', __('Error creating module from network component. Agent doesn\'t exist.')); - return; + if ($agent_by_alias) { + $ids_agents = agents_get_agent_id_by_alias($agent_name); + } else { + $agent_id = agents_get_agent_id($agent_name); + } + + if ($agent_by_alias) { + foreach ($ids_agents as $id) { + if (!util_api_check_agent_and_print_error($id['id_agente'], 'string', 'AW')) { + return; + } + } + } else { + if (!util_api_check_agent_and_print_error($agent_id, 'string', 'AW')) { + return; + } + } + + if ($agent_by_alias) { + foreach ($ids_agents as $id) { + if (!$id['id_agente']) { + returnError('error_network_module_from_component', __('Error creating module from network component. Agent doesn\'t exist.')); + return; + } + } + } else { + if (!$agent_id) { + returnError('error_network_module_from_component', __('Error creating module from network component. Agent doesn\'t exist.')); + return; + } } $component = db_get_row('tnetwork_component', 'name', $component_name); @@ -5406,14 +5761,29 @@ function api_set_create_network_module_from_component($agent_name, $component_na $component['ip_target'] = agents_get_address($agent_id); // Create module - $module_id = modules_create_agent_module($agent_id, $component_name, $component, true); + if ($agent_by_alias) { + $agents_affected = 0; - if (!$module_id) { - returnError('error_network_module_from_component', __('Error creating module from network component. Error creating module.')); + foreach ($ids_agents as $id) { + $module_id = modules_create_agent_module($id['id_agente'], $component_name, $component, true); + + if ($module_id) { + $agents_affected++; + } + } + + returnData('string', ['type' => 'string', 'data' => __('%d agents affected', $agents_affected)]); return; - } + } else { + $module_id = modules_create_agent_module($agent_id, $component_name, $component, true); - return $module_id; + if (!$module_id) { + returnError('error_network_module_from_component', __('Error creating module from network component. Error creating module.')); + return; + } + + return $module_id; + } } @@ -5552,7 +5922,7 @@ function api_set_delete_module_template($id, $thrash1, $other, $thrash3) /** - * Delete an module assigned to a template. And return a message with the result of the operation. + * Delete a module assigned to a template and return a message with the result of the operation. * * @param $id Agent Name * @param $id2 Alert Template Name @@ -5573,11 +5943,6 @@ function api_set_delete_module_template_by_names($id, $id2, $other, $trash1) $result = 0; - if ($other['type'] != 'string') { - returnError('error_parameter', 'Error in the parameters.'); - return; - } - if (! check_acl($config['id_user'], 0, 'AD') && ! check_acl($config['id_user'], 0, 'LM') ) { @@ -5585,10 +5950,26 @@ function api_set_delete_module_template_by_names($id, $id2, $other, $trash1) return; } - $idAgent = agents_get_agent_id($id); + $agent_by_alias = false; - if (!util_api_check_agent_and_print_error($idAgent, 'string', 'AD')) { - return; + if ($other['data'][1] === '1') { + $agent_by_alias = true; + } + + if ($agent_by_alias) { + $idsAgents = agents_get_agent_id_by_alias($id); + + foreach ($idsAgents as $id) { + if (!util_api_check_agent_and_print_error($id['id_agente'], 'string', 'AD')) { + return; + } + } + } else { + $idAgent = agents_get_agent_id($id); + + if (!util_api_check_agent_and_print_error($idAgent, 'string', 'AD')) { + return; + } } $row = db_get_row_filter('talert_templates', ['name' => $id2]); @@ -5601,25 +5982,52 @@ function api_set_delete_module_template_by_names($id, $id2, $other, $trash1) $idTemplate = $row['id']; $idActionTemplate = $row['id_alert_action']; - $idAgentModule = db_get_value_filter('id_agente_modulo', 'tagente_modulo', ['id_agente' => $idAgent, 'nombre' => $other['data']]); + $delete_count = 0; - if ($idAgentModule === false) { - returnError('error_parameter', 'Error in the parameters.'); - return; - } + if ($agent_by_alias) { + foreach ($idsAgents as $id) { + $idAgentModule = db_get_value_filter('id_agente_modulo', 'tagente_modulo', ['id_agente' => $id['id_agente'], 'nombre' => $other['data'][0]]); - $values = [ - 'id_agent_module' => $idAgentModule, - 'id_alert_template' => $idTemplate, - ]; + if ($idAgentModule === false) { + continue; + } - $result = db_process_sql_delete('talert_template_modules', $values); + $values = [ + 'id_agent_module' => $idAgentModule, + 'id_alert_template' => $idTemplate, + ]; - if ($result == 0) { - // TODO: Improve the error returning more info - returnError('error_delete_module_template_by_name', __('Error deleting module template.')); + $result = db_process_sql_delete('talert_template_modules', $values); + + $delete_count++; + + if ($result != 0) { + $delete_count++; + } + } + + returnError('error_delete_module_template_by_name', __('Module template have been deleted in %d agents.', $delete_count)); } else { - returnData('string', ['type' => 'string', 'data' => __('Correct deleting of module template.')]); + $idAgentModule = db_get_value_filter('id_agente_modulo', 'tagente_modulo', ['id_agente' => $idAgent, 'nombre' => $other['data'][0]]); + + if ($idAgentModule === false) { + returnError('error_parameter', 'Error in the parameters1.'); + return; + } + + $values = [ + 'id_agent_module' => $idAgentModule, + 'id_alert_template' => $idTemplate, + ]; + + $result = db_process_sql_delete('talert_template_modules', $values); + + if ($result == 0) { + // TODO: Improve the error returning more info + returnError('error_delete_module_template_by_name', __('Error deleting module template.')); + } else { + returnData('string', ['type' => 'string', 'data' => __('Correct deleting of module template.')]); + } } } @@ -5662,15 +6070,15 @@ function api_set_validate_all_alerts($id, $thrash1, $other, $thrash3) $sql = sprintf( ' - SELECT talert_template_modules.id - FROM talert_template_modules - INNER JOIN tagente_modulo t2 - ON talert_template_modules.id_agent_module = t2.id_agente_modulo - INNER JOIN tagente t3 - ON t2.id_agente = t3.id_agente - INNER JOIN talert_templates t4 - ON talert_template_modules.id_alert_template = t4.id - WHERE t3.id_agente in (%s)', + SELECT talert_template_modules.id + FROM talert_template_modules + INNER JOIN tagente_modulo t2 + ON talert_template_modules.id_agent_module = t2.id_agente_modulo + INNER JOIN tagente t3 + ON t2.id_agente = t3.id_agente + INNER JOIN talert_templates t4 + ON talert_template_modules.id_alert_template = t4.id + WHERE t3.id_agente in (%s)', $agents_string ); @@ -5758,9 +6166,9 @@ function api_set_validate_all_policy_alerts($id, $thrash1, $other, $thrash3) if (count($result_pol_alerts) != 0) { $sql = sprintf( ' - SELECT id - FROM talert_template_modules - WHERE id_policy_alerts IN (%s)', + SELECT id + FROM talert_template_modules + WHERE id_policy_alerts IN (%s)', $id_pol_alerts ); @@ -7645,8 +8053,8 @@ function api_set_create_group($id, $thrash1, $other, $thrash3) if (defined('METACONSOLE')) { $servers = db_get_all_rows_sql( 'SELECT * - FROM tmetaconsole_setup - WHERE disabled = 0' + FROM tmetaconsole_setup + WHERE disabled = 0' ); if ($servers === false) { @@ -7935,10 +8343,10 @@ function api_get_module_data($id, $thrash1, $other, $returnType) $sql = sprintf( 'SELECT utimestamp, datos - FROM tagente_datos - WHERE id_agente_modulo = %d AND utimestamp > %d - AND utimestamp < %d - ORDER BY utimestamp DESC', + FROM tagente_datos + WHERE id_agente_modulo = %d AND utimestamp > %d + AND utimestamp < %d + ORDER BY utimestamp DESC', $id, $date_start, $date_end @@ -7947,17 +8355,17 @@ function api_get_module_data($id, $thrash1, $other, $returnType) if ($periodSeconds == null) { $sql = sprintf( 'SELECT utimestamp, datos - FROM tagente_datos - WHERE id_agente_modulo = %d - ORDER BY utimestamp DESC', + FROM tagente_datos + WHERE id_agente_modulo = %d + ORDER BY utimestamp DESC', $id ); } else { $sql = sprintf( 'SELECT utimestamp, datos - FROM tagente_datos - WHERE id_agente_modulo = %d AND utimestamp > %d - ORDER BY utimestamp DESC', + FROM tagente_datos + WHERE id_agente_modulo = %d AND utimestamp > %d + ORDER BY utimestamp DESC', $id, (get_system_time() - $periodSeconds) ); @@ -8276,7 +8684,7 @@ function api_set_enable_disable_user($id, $thrash2, $other, $thrash3) } -function otherParameter2Filter($other, $return_as_array=false) +function otherParameter2Filter($other, $return_as_array=false, $use_agent_name=false) { $filter = []; @@ -8286,12 +8694,22 @@ function otherParameter2Filter($other, $return_as_array=false) $idAgent = null; if (isset($other['data'][2]) && $other['data'][2] != '') { - $idAgents = agents_get_agent_id_by_alias($other['data'][2]); + if ($use_agent_name === true) { + $idAgentByName = agents_get_agent_id($other['data'][2]); - if (!empty($idAgent)) { - $filter[] = 'id_agente IN ('.explode(',', $idAgents).')'; + if (!empty($idAgentByName)) { + $filter[] = 'id_agente = '.$idAgentByName; + } else { + $filter['sql'] = '1=0'; + } } else { - $filter['sql'] = '1=0'; + $idAgents = agents_get_agent_id_by_alias($other['data'][2]); + + if (!empty($idAgent)) { + $filter[] = 'id_agente IN ('.explode(',', $idAgents).')'; + } else { + $filter['sql'] = '1=0'; + } } } @@ -8427,7 +8845,7 @@ function otherParameter2Filter($other, $return_as_array=false) if ($event_type == 'not_normal') { $filterString .= " AND ( event_type LIKE '%warning%' - OR event_type LIKE '%critical%' OR event_type LIKE '%unknown%' ) "; + OR event_type LIKE '%critical%' OR event_type LIKE '%unknown%' ) "; } else { $filterString .= ' AND event_type LIKE "%'.$event_type.'%"'; } @@ -8463,48 +8881,91 @@ function api_set_new_alert_template($id, $id2, $other, $trash1) } if ($other['type'] == 'string') { - returnError('error_parameter', 'Error in the parameters.'); + returnError('error_parameter', 'Error in the parameters0.'); return; } else if ($other['type'] == 'array') { - $idAgent = agents_get_agent_id($id); + $agent_by_alias = false; - if (!util_api_check_agent_and_print_error($idAgent, 'string', 'AW')) { - return; + if ($other['data'][1] === '1') { + $agent_by_alias = true; + } + + if ($agent_by_alias) { + $idsAgents = agents_get_agent_id_by_alias($id); + } else { + $idAgent = agents_get_agent_id($id); + } + + if ($agent_by_alias) { + foreach ($idsAgents as $id) { + if (!util_api_check_agent_and_print_error($id['id_agente'], 'string', 'AW')) { + return; + } + } + } else { + if (!util_api_check_agent_and_print_error($idAgent, 'string', 'AW')) { + return; + } } $row = db_get_row_filter('talert_templates', ['name' => $id2]); if ($row === false) { - returnError('error_parameter', 'Error in the parameters.'); + returnError('error_parameter', 'Error in the parameters1.'); return; } $idTemplate = $row['id']; $idActionTemplate = $row['id_alert_action']; - $idAgentModule = db_get_value_filter('id_agente_modulo', 'tagente_modulo', ['id_agente' => $idAgent, 'nombre' => $other['data'][0]]); + $inserted_count = 0; - if ($idAgentModule === false) { - returnError('error_parameter', 'Error in the parameters.'); + if ($agent_by_alias) { + foreach ($idsAgents as $id) { + $idAgentModule = db_get_value_filter('id_agente_modulo', 'tagente_modulo', ['id_agente' => $id['id_agente'], 'nombre' => $other['data'][0]]); + + if ($idAgentModule === false) { + continue; + } + + $values = [ + 'id_agent_module' => $idAgentModule, + 'id_alert_template' => $idTemplate, + ]; + + $return = db_process_sql_insert('talert_template_modules', $values); + + if ($return != false) { + $inserted_count++; + } + } + + returnData('string', ['type' => 'string', 'data' => __('Template have been inserted in %d agents.', $inserted_count)]); + } else { + $idAgentModule = db_get_value_filter('id_agente_modulo', 'tagente_modulo', ['id_agente' => $idAgent, 'nombre' => $other['data'][0]]); + + if ($idAgentModule === false) { + returnError('error_parameter', 'Error in the parameter2s.'); + return; + } + + $values = [ + 'id_agent_module' => $idAgentModule, + 'id_alert_template' => $idTemplate, + ]; + + $return = db_process_sql_insert('talert_template_modules', $values); + + $data['type'] = 'string'; + if ($return === false) { + $data['data'] = 0; + } else { + $data['data'] = $return; + } + + returnData('string', $data); return; } - - $values = [ - 'id_agent_module' => $idAgentModule, - 'id_alert_template' => $idTemplate, - ]; - - $return = db_process_sql_insert('talert_template_modules', $values); - - $data['type'] = 'string'; - if ($return === false) { - $data['data'] = 0; - } else { - $data['data'] = $return; - } - - returnData('string', $data); - return; } } @@ -8517,38 +8978,83 @@ function api_set_delete_module($id, $id2, $other, $trash1) if ($other['type'] == 'string') { $simulate = false; - if ($other['data'] == 'simulate') { + if ($other['data'][0] == 'simulate') { $simulate = true; } - $idAgent = agents_get_agent_id($id); + $agent_by_alias = false; - if (!util_api_check_agent_and_print_error($idAgent, 'string', 'AD')) { + if ($other['data'][1] === '1') { + $agent_by_alias = true; + } + + if ($agent_by_alias) { + $idsAgents = agents_get_agent_id_by_alias($id); + } else { + $idAgent = agents_get_agent_id($id); + } + + if ($agent_by_alias) { + foreach ($idsAgents as $id) { + if (!util_api_check_agent_and_print_error($id['id_agente'], 'string', 'AD')) { + return; + } + } + } else { + if (!util_api_check_agent_and_print_error($idAgent, 'string', 'AD')) { + return; + } + } + + if ($agent_by_alias) { + foreach ($idsAgents as $id) { + $idAgentModule = db_get_value_filter('id_agente_modulo', 'tagente_modulo', ['id_agente' => $id['id_agente'], 'nombre' => $id2]); + + if ($idAgentModule === false) { + continue; + } + + if (!$simulate) { + $return = modules_delete_agent_module($idAgentModule); + } else { + $return = true; + } + + $data['type'] = 'string'; + if ($return === false) { + $data['data'] = 0; + } else { + $data['data'] = $return; + } + + returnData('string', $data); + } + + return; + } else { + $idAgentModule = db_get_value_filter('id_agente_modulo', 'tagente_modulo', ['id_agente' => $idAgent, 'nombre' => $id2]); + + if ($idAgentModule === false) { + returnError('error_parameter', 'Error in the parameters.'); + return; + } + + if (!$simulate) { + $return = modules_delete_agent_module($idAgentModule); + } else { + $return = true; + } + + $data['type'] = 'string'; + if ($return === false) { + $data['data'] = 0; + } else { + $data['data'] = $return; + } + + returnData('string', $data); return; } - - $idAgentModule = db_get_value_filter('id_agente_modulo', 'tagente_modulo', ['id_agente' => $idAgent, 'nombre' => $id2]); - - if ($idAgentModule === false) { - returnError('error_parameter', 'Error in the parameters.'); - return; - } - - if (!$simulate) { - $return = modules_delete_agent_module($idAgentModule); - } else { - $return = true; - } - - $data['type'] = 'string'; - if ($return === false) { - $data['data'] = 0; - } else { - $data['data'] = $return; - } - - returnData('string', $data); - return; } else { returnError('error_parameter', 'Error in the parameters.'); return; @@ -8582,14 +9088,14 @@ function api_set_module_data($id, $thrash2, $other, $trash1) $agent = db_get_row_filter('tagente', ['id_agente' => $agentModule['id_agente']]); $xmlTemplate = " - - - - - - - - "; + + + + + + + + "; $xml = sprintf( $xmlTemplate, @@ -8631,9 +9137,29 @@ function api_set_new_module($id, $id2, $other, $trash1) return; } else if ($other['type'] == 'array') { $values = []; - $values['id_agente'] = agents_get_agent_id($id); - if (!util_api_check_agent_and_print_error($values['id_agente'], 'string', 'AW')) { - return; + + $agent_by_alias = false; + + if ($other['data'][15] === '1') { + $agent_by_alias = true; + } + + if ($agent_by_alias) { + $idsAgents = agents_get_agent_id_by_alias($id); + } else { + $values['id_agente'] = agents_get_agent_id($id); + } + + if ($agent_by_alias) { + foreach ($idsAgents as $id) { + if (!util_api_check_agent_and_print_error($id['id_agente'], 'string', 'AW')) { + return; + } + } + } else { + if (!util_api_check_agent_and_print_error($values['id_agente'], 'string', 'AW')) { + return; + } } $values['nombre'] = $id2; @@ -8713,11 +9239,30 @@ function api_set_new_module($id, $id2, $other, $trash1) $values['id_modulo'] = 2; - $return = modules_create_agent_module( - $values['id_agente'], - $values['nombre'], - $values - ); + if ($agent_by_alias) { + $agents_module_created = 0; + + foreach ($idsAgents as $id) { + $return = modules_create_agent_module( + $id['id_agente'], + $values['nombre'], + $values + ); + + if ($return != false) { + $agents_module_created++; + } + } + + returnData('string', ['type' => 'string', 'data' => __('Module has been created in %d agents.', $agents_module_created)]); + return; + } else { + $return = modules_create_agent_module( + $values['id_agente'], + $values['nombre'], + $values + ); + } $data['type'] = 'string'; if ($return === false) { @@ -8753,66 +9298,133 @@ function api_set_alert_actions($id, $id2, $other, $trash1) } if ($other['type'] == 'string') { - returnError('error_parameter', 'Error in the parameters.'); + returnError('error_parameter', 'Error in the parameters0.'); return; } else if ($other['type'] == 'array') { - $idAgent = agents_get_agent_id($id); - if (!util_api_check_agent_and_print_error($idAgent, 'string', 'AW')) { - return; + $agent_by_alias = false; + + if ($other['data'][4] === '1') { + $agent_by_alias = true; + } + + if ($agent_by_alias) { + $idsAgents = agents_get_agent_id_by_alias($id); + } else { + $idAgent = agents_get_agent_id($id); + } + + if ($agent_by_alias) { + foreach ($idsAgents as $id) { + if (!util_api_check_agent_and_print_error($id['id_agente'], 'string', 'AW')) { + return; + } + } + } else { + if (!util_api_check_agent_and_print_error($idAgent, 'string', 'AW')) { + return; + } } $row = db_get_row_filter('talert_templates', ['name' => $id2]); if ($row === false) { - returnError('error_parameter', 'Error in the parameters.'); + returnError('error_parameter', 'Error in the parameters1.'); return; } $idTemplate = $row['id']; - $idAgentModule = db_get_value_filter('id_agente_modulo', 'tagente_modulo', ['id_agente' => $idAgent, 'nombre' => $other['data'][0]]); - if ($idAgentModule === false) { - returnError('error_parameter', 'Error in the parameters.'); - return; - } + if ($agent_by_alias) { + $actions_set = 0; - $idAlertTemplateModule = db_get_value_filter('id', 'talert_template_modules', ['id_alert_template' => $idTemplate, 'id_agent_module' => $idAgentModule]); - if ($idAlertTemplateModule === false) { - returnError('error_parameter', 'Error in the parameters.'); - return; - } + foreach ($idsAgents as $id) { + $idAgentModule = db_get_value_filter('id_agente_modulo', 'tagente_modulo', ['id_agente' => $id['id_agente'], 'nombre' => $other['data'][0]]); + if ($idAgentModule === false) { + continue; + } - if ($other['data'][1] != '') { - $idAction = db_get_value_filter('id', 'talert_actions', ['name' => $other['data'][1]]); - if ($idAction === false) { + $idAlertTemplateModule = db_get_value_filter('id', 'talert_template_modules', ['id_alert_template' => $idTemplate, 'id_agent_module' => $idAgentModule]); + if ($idAlertTemplateModule === false) { + returnError('error_parameter', 'Error in the parameters.'); + return; + } + + if ($other['data'][1] != '') { + $idAction = db_get_value_filter('id', 'talert_actions', ['name' => $other['data'][1]]); + if ($idAction === false) { + returnError('error_parameter', 'Error in the parameters.'); + return; + } + } else { + returnError('error_parameter', 'Error in the parameters.'); + return; + } + + $firesMin = $other['data'][2]; + $firesMax = $other['data'][3]; + + $values = [ + 'id_alert_template_module' => $idAlertTemplateModule, + 'id_alert_action' => $idAction, + 'fires_min' => $firesMin, + 'fires_max' => $firesMax, + ]; + + $return = db_process_sql_insert('talert_template_module_actions', $values); + + if ($return != false) { + $actions_set++; + } + } + + returnData('string', ['type' => 'string', 'data' => __('Action has been set for %d agents.', $actions_set)]); + + return; + } else { + $idAgentModule = db_get_value_filter('id_agente_modulo', 'tagente_modulo', ['id_agente' => $idAgent, 'nombre' => $other['data'][0]]); + if ($idAgentModule === false) { returnError('error_parameter', 'Error in the parameters.'); return; } - } else { - returnError('error_parameter', 'Error in the parameters.'); + + $idAlertTemplateModule = db_get_value_filter('id', 'talert_template_modules', ['id_alert_template' => $idTemplate, 'id_agent_module' => $idAgentModule]); + if ($idAlertTemplateModule === false) { + returnError('error_parameter', 'Error in the parameters.'); + return; + } + + if ($other['data'][1] != '') { + $idAction = db_get_value_filter('id', 'talert_actions', ['name' => $other['data'][1]]); + if ($idAction === false) { + returnError('error_parameter', 'Error in the parameters.'); + return; + } + } else { + returnError('error_parameter', 'Error in the parameters.'); + return; + } + + $firesMin = $other['data'][2]; + $firesMax = $other['data'][3]; + + $values = [ + 'id_alert_template_module' => $idAlertTemplateModule, + 'id_alert_action' => $idAction, + 'fires_min' => $firesMin, + 'fires_max' => $firesMax, + ]; + + $return = db_process_sql_insert('talert_template_module_actions', $values); + + $data['type'] = 'string'; + if ($return === false) { + $data['data'] = 0; + } else { + $data['data'] = $return; + } + + returnData('string', $data); return; } - - $firesMin = $other['data'][2]; - $firesMax = $other['data'][3]; - - $values = [ - 'id_alert_template_module' => $idAlertTemplateModule, - 'id_alert_action' => $idAction, - 'fires_min' => $firesMin, - 'fires_max' => $firesMax, - ]; - - $return = db_process_sql_insert('talert_template_module_actions', $values); - - $data['type'] = 'string'; - if ($return === false) { - $data['data'] = 0; - } else { - $data['data'] = $return; - } - - returnData('string', $data); - return; } } @@ -9141,11 +9753,11 @@ function api_set_new_event($trash1, $trash2, $other, $trash3) } else { $idAlert = db_get_value_sql( "SELECT t1.id - FROM talert_template_modules t1 - INNER JOIN talert_templates t2 - ON t1.id_alert_template = t2.id - WHERE t1.id_agent_module = 1 - AND t2.name LIKE '".$other['data'][7]."'" + FROM talert_template_modules t1 + INNER JOIN talert_templates t2 + ON t1.id_alert_template = t2.id + WHERE t1.id_agent_module = 1 + AND t2.name LIKE '".$other['data'][7]."'" ); if ($idAlert === false) { @@ -9299,7 +9911,9 @@ function api_set_event_validate_filter($trash1, $trash2, $other, $trash3) } } - $filterString = otherParameter2Filter($other); + $use_agent_name = ($other['data'][8] === '1') ? true : false; + + $filterString = otherParameter2Filter($other, false, $use_agent_name); if (!users_can_manage_group_all('EW')) { $user_groups = implode( @@ -9382,9 +9996,9 @@ function api_get_gis_agent($id_agent, $trash1, $tresh2, $return_type, $user_in_d $agent_gis_data = db_get_row_sql( ' - SELECT * - FROM tgis_data_status - WHERE tagente_id_agente = '.$id_agent + SELECT * + FROM tgis_data_status + WHERE tagente_id_agente = '.$id_agent ); if ($agent_gis_data) { @@ -9616,7 +10230,9 @@ function get_events_with_user($trash1, $trash2, $other, $returnType, $user_in_db $utimestamp_upper = 0; $utimestamp_bottom = 0; - $filter = otherParameter2Filter($other, true); + $use_agent_name = ($other['data'][14] === '1') ? true : false; + + $filter = otherParameter2Filter($other, true, $use_agent_name); if (isset($filter['criticity'])) { $severity = $filter['criticity']; @@ -9751,7 +10367,7 @@ function get_events_with_user($trash1, $trash2, $other, $returnType, $user_in_db $sql_post .= " AND event_type LIKE '%$event_type%' "; } else if ($event_type == 'not_normal') { $sql_post .= " AND ( event_type LIKE '%warning%' - OR event_type LIKE '%critical%' OR event_type LIKE '%unknown%' ) "; + OR event_type LIKE '%critical%' OR event_type LIKE '%unknown%' ) "; } else { $sql_post .= " AND event_type = '".$event_type."'"; } @@ -9810,48 +10426,48 @@ function get_events_with_user($trash1, $trash2, $other, $returnType, $user_in_db case 'mysql': if ($filter['total']) { $sql = 'SELECT COUNT(*) - FROM '.$table_events.' - WHERE 1=1 '.$sql_post; + FROM '.$table_events.' + WHERE 1=1 '.$sql_post; } else if ($filter['more_criticity']) { $sql = 'SELECT criticity - FROM '.$table_events.' - WHERE 1=1 '.$sql_post.' - ORDER BY criticity DESC - LIMIT 1'; + FROM '.$table_events.' + WHERE 1=1 '.$sql_post.' + ORDER BY criticity DESC + LIMIT 1'; } else { if (defined('METACONSOLE')) { $sql = 'SELECT *, - (SELECT t2.nombre - FROM tgrupo t2 - WHERE t2.id_grupo = '.$table_events.'.id_grupo) AS group_name, - (SELECT t2.icon - FROM tgrupo t2 - WHERE t2.id_grupo = '.$table_events.'.id_grupo) AS group_icon - FROM '.$table_events.' - WHERE 1=1 '.$sql_post.' - ORDER BY utimestamp DESC - LIMIT '.$offset.','.$pagination; + (SELECT t2.nombre + FROM tgrupo t2 + WHERE t2.id_grupo = '.$table_events.'.id_grupo) AS group_name, + (SELECT t2.icon + FROM tgrupo t2 + WHERE t2.id_grupo = '.$table_events.'.id_grupo) AS group_icon + FROM '.$table_events.' + WHERE 1=1 '.$sql_post.' + ORDER BY utimestamp DESC + LIMIT '.$offset.','.$pagination; } else { $sql = 'SELECT *, - (SELECT t1.alias - FROM tagente t1 - WHERE t1.id_agente = tevento.id_agente) AS agent_name, - (SELECT t2.nombre - FROM tgrupo t2 - WHERE t2.id_grupo = tevento.id_grupo) AS group_name, - (SELECT t2.icon - FROM tgrupo t2 - WHERE t2.id_grupo = tevento.id_grupo) AS group_icon, - (SELECT tmodule.name - FROM tmodule - WHERE id_module IN ( - SELECT tagente_modulo.id_modulo - FROM tagente_modulo - WHERE tagente_modulo.id_agente_modulo=tevento.id_agentmodule)) AS module_name - FROM '.$table_events.' - WHERE 1=1 '.$sql_post.' - ORDER BY utimestamp DESC - LIMIT '.$offset.','.$pagination; + (SELECT t1.alias + FROM tagente t1 + WHERE t1.id_agente = tevento.id_agente) AS agent_name, + (SELECT t2.nombre + FROM tgrupo t2 + WHERE t2.id_grupo = tevento.id_grupo) AS group_name, + (SELECT t2.icon + FROM tgrupo t2 + WHERE t2.id_grupo = tevento.id_grupo) AS group_icon, + (SELECT tmodule.name + FROM tmodule + WHERE id_module IN ( + SELECT tagente_modulo.id_modulo + FROM tagente_modulo + WHERE tagente_modulo.id_agente_modulo=tevento.id_agentmodule)) AS module_name + FROM '.$table_events.' + WHERE 1=1 '.$sql_post.' + ORDER BY utimestamp DESC + LIMIT '.$offset.','.$pagination; } } break; @@ -9859,25 +10475,25 @@ function get_events_with_user($trash1, $trash2, $other, $returnType, $user_in_db case 'postgresql': // TODO TOTAL $sql = 'SELECT *, - (SELECT t1.alias - FROM tagente t1 - WHERE t1.id_agente = tevento.id_agente) AS agent_name, - (SELECT t2.nombre - FROM tgrupo t2 - WHERE t2.id_grupo = tevento.id_grupo) AS group_name, - (SELECT t2.icon - FROM tgrupo t2 - WHERE t2.id_grupo = tevento.id_grupo) AS group_icon, - (SELECT tmodule.name - FROM tmodule - WHERE id_module IN ( - SELECT tagente_modulo.id_modulo - FROM tagente_modulo - WHERE tagente_modulo.id_agente_modulo=tevento.id_agentmodule)) AS module_name - FROM tevento - WHERE 1=1 '.$sql_post.' - ORDER BY utimestamp DESC - LIMIT '.$pagination.' OFFSET '.$offset; + (SELECT t1.alias + FROM tagente t1 + WHERE t1.id_agente = tevento.id_agente) AS agent_name, + (SELECT t2.nombre + FROM tgrupo t2 + WHERE t2.id_grupo = tevento.id_grupo) AS group_name, + (SELECT t2.icon + FROM tgrupo t2 + WHERE t2.id_grupo = tevento.id_grupo) AS group_icon, + (SELECT tmodule.name + FROM tmodule + WHERE id_module IN ( + SELECT tagente_modulo.id_modulo + FROM tagente_modulo + WHERE tagente_modulo.id_agente_modulo=tevento.id_agentmodule)) AS module_name + FROM tevento + WHERE 1=1 '.$sql_post.' + ORDER BY utimestamp DESC + LIMIT '.$pagination.' OFFSET '.$offset; break; case 'oracle': @@ -9887,26 +10503,26 @@ function get_events_with_user($trash1, $trash2, $other, $returnType, $user_in_db $set['offset'] = $offset; $sql = 'SELECT *, - (SELECT t1.alias - FROM tagente t1 - WHERE t1.id_agente = tevento.id_agente) AS alias, - (SELECT t1.nombre - FROM tagente t1 - WHERE t1.id_agente = tevento.id_agente) AS agent_name, - (SELECT t2.nombre - FROM tgrupo t2 - WHERE t2.id_grupo = tevento.id_grupo) AS group_name, - (SELECT t2.icon - FROM tgrupo t2 - WHERE t2.id_grupo = tevento.id_grupo) AS group_icon, - (SELECT tmodule.name - FROM tmodule - WHERE id_module IN ( - SELECT tagente_modulo.id_modulo - FROM tagente_modulo - WHERE tagente_modulo.id_agente_modulo=tevento.id_agentmodule)) AS module_name - FROM tevento - WHERE 1=1 '.$sql_post.' ORDER BY utimestamp DESC'; + (SELECT t1.alias + FROM tagente t1 + WHERE t1.id_agente = tevento.id_agente) AS alias, + (SELECT t1.nombre + FROM tagente t1 + WHERE t1.id_agente = tevento.id_agente) AS agent_name, + (SELECT t2.nombre + FROM tgrupo t2 + WHERE t2.id_grupo = tevento.id_grupo) AS group_name, + (SELECT t2.icon + FROM tgrupo t2 + WHERE t2.id_grupo = tevento.id_grupo) AS group_icon, + (SELECT tmodule.name + FROM tmodule + WHERE id_module IN ( + SELECT tagente_modulo.id_modulo + FROM tagente_modulo + WHERE tagente_modulo.id_agente_modulo=tevento.id_agentmodule)) AS module_name + FROM tevento + WHERE 1=1 '.$sql_post.' ORDER BY utimestamp DESC'; $sql = oracle_recode_query($sql, $set); break; } @@ -9916,26 +10532,26 @@ function get_events_with_user($trash1, $trash2, $other, $returnType, $user_in_db db_process_sql('SET group_concat_max_len = 9999999'); $sql = "SELECT *, MAX(id_evento) AS id_evento, - GROUP_CONCAT(DISTINCT user_comment SEPARATOR '') AS user_comment, - MIN(estado) AS min_estado, MAX(estado) AS max_estado, - COUNT(*) AS event_rep, MAX(utimestamp) AS timestamp_rep - FROM ".$table_events.' - WHERE 1=1 '.$sql_post.' - GROUP BY evento, id_agentmodule - ORDER BY timestamp_rep DESC - LIMIT '.$offset.','.$pagination; + GROUP_CONCAT(DISTINCT user_comment SEPARATOR '') AS user_comment, + MIN(estado) AS min_estado, MAX(estado) AS max_estado, + COUNT(*) AS event_rep, MAX(utimestamp) AS timestamp_rep + FROM ".$table_events.' + WHERE 1=1 '.$sql_post.' + GROUP BY evento, id_agentmodule + ORDER BY timestamp_rep DESC + LIMIT '.$offset.','.$pagination; break; case 'postgresql': $sql = "SELECT *, MAX(id_evento) AS id_evento, - array_to_string(array_agg(DISTINCT user_comment), '') AS user_comment, - MIN(estado) AS min_estado, MAX(estado) AS max_estado, - COUNT(*) AS event_rep, MAX(utimestamp) AS timestamp_rep - FROM ".$table_events.' - WHERE 1=1 '.$sql_post.' - GROUP BY evento, id_agentmodule - ORDER BY timestamp_rep DESC - LIMIT '.$pagination.' OFFSET '.$offset; + array_to_string(array_agg(DISTINCT user_comment), '') AS user_comment, + MIN(estado) AS min_estado, MAX(estado) AS max_estado, + COUNT(*) AS event_rep, MAX(utimestamp) AS timestamp_rep + FROM ".$table_events.' + WHERE 1=1 '.$sql_post.' + GROUP BY evento, id_agentmodule + ORDER BY timestamp_rep DESC + LIMIT '.$pagination.' OFFSET '.$offset; break; case 'oracle': @@ -9944,21 +10560,21 @@ function get_events_with_user($trash1, $trash2, $other, $returnType, $user_in_db $set['offset'] = $offset; // TODO: Remove duplicate user comments $sql = 'SELECT a.*, b.event_rep, b.timestamp_rep - FROM (SELECT * - FROM tevento - WHERE 1=1 '.$sql_post.") a, - (SELECT MAX (id_evento) AS id_evento, - to_char(evento) AS evento, id_agentmodule, - COUNT(*) AS event_rep, MIN(estado) AS min_estado, - MAX(estado) AS max_estado, - LISTAGG(user_comment, '') AS user_comment, - MAX(utimestamp) AS timestamp_rep - FROM ".$table_events.' - WHERE 1=1 '.$sql_post.' - GROUP BY to_char(evento), id_agentmodule) b - WHERE a.id_evento=b.id_evento AND - to_char(a.evento)=to_char(b.evento) AND - a.id_agentmodule=b.id_agentmodule'; + FROM (SELECT * + FROM tevento + WHERE 1=1 '.$sql_post.") a, + (SELECT MAX (id_evento) AS id_evento, + to_char(evento) AS evento, id_agentmodule, + COUNT(*) AS event_rep, MIN(estado) AS min_estado, + MAX(estado) AS max_estado, + LISTAGG(user_comment, '') AS user_comment, + MAX(utimestamp) AS timestamp_rep + FROM ".$table_events.' + WHERE 1=1 '.$sql_post.' + GROUP BY to_char(evento), id_agentmodule) b + WHERE a.id_evento=b.id_evento AND + to_char(a.evento)=to_char(b.evento) AND + a.id_agentmodule=b.id_agentmodule'; $sql = oracle_recode_query($sql, $set); break; } @@ -10092,7 +10708,9 @@ function api_get_events($trash1, $trash2, $other, $returnType, $user_in_db=null) } else if ($other['type'] == 'array') { $separator = $other['data'][0]; - $filterString = otherParameter2Filter($other); + $use_agent_name = ($other['data'][14] === '1') ? true : false; + + $filterString = otherParameter2Filter($other, false, $use_agent_name); } if (defined('METACONSOLE')) { @@ -10576,25 +11194,58 @@ function api_set_new_note_incident($id, $id2, $other, $thrash2) */ -function api_set_disable_module($agent_name, $module_name, $thrast3, $thrash4) +function api_set_disable_module($agent_name, $module_name, $other, $thrash4) { if (defined('METACONSOLE')) { return; } - $id_agent = agents_get_agent_id($agent_name); - if (!util_api_check_agent_and_print_error($id_agent, 'string', 'AD')) { - return; + $agent_by_alias = false; + + if ($other['data'][0] === '1') { + $agent_by_alias = true; } - $id_agent_module = db_get_value_filter('id_agente_modulo', 'tagente_modulo', ['id_agente' => $id_agent, 'nombre' => $module_name]); + if ($agent_by_alias) { + $ids_agents = agents_get_agent_id_by_alias($agent_name); - $result = modules_change_disabled($id_agent_module, 1); - - if ($result === NOERR) { - returnData('string', ['type' => 'string', 'data' => __('Correct module disable')]); + foreach ($ids_agents as $id) { + if (!util_api_check_agent_and_print_error($id['id_agente'], 'string', 'AD')) { + return; + } + } } else { - returnData('string', ['type' => 'string', 'data' => __('Error disabling module')]); + $id_agent = agents_get_agent_id($agent_name); + + if (!util_api_check_agent_and_print_error($id_agent, 'string', 'AD')) { + return; + } + } + + if ($agent_by_alias) { + $agents_affected = 0; + + foreach ($ids_agents as $id) { + $id_agent_module = db_get_value_filter('id_agente_modulo', 'tagente_modulo', ['id_agente' => $id['id_agente'], 'nombre' => $module_name]); + + $result = modules_change_disabled($id_agent_module, 1); + + if ($result === NOERR) { + $agents_affected++; + } + } + + returnData('string', ['type' => 'string', 'data' => __('%d agents affected', $agents_affected)]); + } else { + $id_agent_module = db_get_value_filter('id_agente_modulo', 'tagente_modulo', ['id_agente' => $id_agent, 'nombre' => $module_name]); + + $result = modules_change_disabled($id_agent_module, 1); + + if ($result === NOERR) { + returnData('string', ['type' => 'string', 'data' => __('Correct module disable')]); + } else { + returnData('string', ['type' => 'string', 'data' => __('Error disabling module')]); + } } } @@ -10609,25 +11260,58 @@ function api_set_disable_module($agent_name, $module_name, $thrast3, $thrash4) */ -function api_set_enable_module($agent_name, $module_name, $thrast3, $thrash4) +function api_set_enable_module($agent_name, $module_name, $other, $thrash4) { if (defined('METACONSOLE')) { return; } - $id_agent = agents_get_agent_id($agent_name); - if (!util_api_check_agent_and_print_error($id_agent, 'string', 'AD')) { - return; + $agent_by_alias = false; + + if ($other['data'][0] === '1') { + $agent_by_alias = true; } - $id_agent_module = db_get_value_filter('id_agente_modulo', 'tagente_modulo', ['id_agente' => $id_agent, 'nombre' => $module_name]); + if ($agent_by_alias) { + $ids_agents = agents_get_agent_id_by_alias($agent_name); - $result = modules_change_disabled($id_agent_module, 0); - - if ($result === NOERR) { - returnData('string', ['type' => 'string', 'data' => __('Correct module enable')]); + foreach ($ids_agents as $id) { + if (!util_api_check_agent_and_print_error($id['id_agente'], 'string', 'AD')) { + return; + } + } } else { - returnData('string', ['type' => 'string', 'data' => __('Error enabling module')]); + $id_agent = agents_get_agent_id($agent_name); + + if (!util_api_check_agent_and_print_error($id_agent, 'string', 'AD')) { + return; + } + } + + if ($agent_by_alias) { + $agents_affected = 0; + + foreach ($ids_agents as $id) { + $id_agent_module = db_get_value_filter('id_agente_modulo', 'tagente_modulo', ['id_agente' => $id['id_agente'], 'nombre' => $module_name]); + + $result = modules_change_disabled($id_agent_module, 0); + + if ($result === NOERR) { + $agents_affected++; + } + } + + returnData('string', ['type' => 'string', 'data' => __('%d agents affected', $agents_affected)]); + } else { + $id_agent_module = db_get_value_filter('id_agente_modulo', 'tagente_modulo', ['id_agente' => $id_agent, 'nombre' => $module_name]); + + $result = modules_change_disabled($id_agent_module, 0); + + if ($result === NOERR) { + returnData('string', ['type' => 'string', 'data' => __('Correct module enable')]); + } else { + returnData('string', ['type' => 'string', 'data' => __('Error enabling module')]); + } } } @@ -10667,8 +11351,8 @@ function api_set_disable_alert($agent_name, $module_name, $template_name, $thras $result = db_process_sql( "UPDATE talert_template_modules - SET disabled = 1 - WHERE id_agent_module = $id_agent_module AND id_alert_template = $id_template" + SET disabled = 1 + WHERE id_agent_module = $id_agent_module AND id_alert_template = $id_template" ); if ($result) { @@ -10716,8 +11400,8 @@ function api_set_disable_alert_alias($agent_alias, $module_name, $template_name, $result = db_process_sql( "UPDATE talert_template_modules - SET disabled = 1 - WHERE id_agent_module = $id_agent_module AND id_alert_template = $id_template" + SET disabled = 1 + WHERE id_agent_module = $id_agent_module AND id_alert_template = $id_template" ); if ($result) { @@ -10767,8 +11451,8 @@ function api_set_enable_alert($agent_name, $module_name, $template_name, $thrash $result = db_process_sql( "UPDATE talert_template_modules - SET disabled = 0 - WHERE id_agent_module = $id_agent_module AND id_alert_template = $id_template" + SET disabled = 0 + WHERE id_agent_module = $id_agent_module AND id_alert_template = $id_template" ); if ($result) { @@ -10816,8 +11500,8 @@ function api_set_enable_alert_alias($agent_alias, $module_name, $template_name, $result = db_process_sql( "UPDATE talert_template_modules - SET disabled = 0 - WHERE id_agent_module = $id_agent_module AND id_alert_template = $id_template" + SET disabled = 0 + WHERE id_agent_module = $id_agent_module AND id_alert_template = $id_template" ); if ($result) { @@ -10844,7 +11528,7 @@ function api_set_enable_alert_alias($agent_alias, $module_name, $template_name, */ -function api_set_disable_module_alerts($agent_name, $module_name, $thrash3, $thrash4) +function api_set_disable_module_alerts($agent_name, $module_name, $other, $thrash4) { global $config; @@ -10857,20 +11541,59 @@ function api_set_disable_module_alerts($agent_name, $module_name, $thrash3, $thr return; } - $id_agent = agents_get_agent_id($agent_name); - if (!util_api_check_agent_and_print_error($id_agent, 'string', 'AW')) { - return; + $agent_by_alias = false; + + if ($other['data'][0] === '1') { + $agent_by_alias = true; } - $id_agent_module = db_get_value_filter('id_agente_modulo', 'tagente_modulo', ['id_agente' => $id_agent, 'nombre' => $module_name]); + if ($agent_by_alias) { + $ids_agents = agents_get_agent_id_by_alias($agent_name); + } else { + $id_agent = agents_get_agent_id($agent_name); + } - db_process_sql( - "UPDATE talert_template_modules - SET disabled = 1 - WHERE id_agent_module = $id_agent_module" - ); + if ($agent_by_alias) { + foreach ($ids_agents as $id) { + if (!util_api_check_agent_and_print_error($id['id_agente'], 'string', 'AW')) { + return; + } + } + } else { + if (!util_api_check_agent_and_print_error($id_agent, 'string', 'AW')) { + return; + } + } - returnData('string', ['type' => 'string', 'data' => 'Correct alerts disable']); + if ($agent_by_alias) { + $agents_affected = 0; + + foreach ($ids_agents as $id) { + $id_agent_module = db_get_value_filter('id_agente_modulo', 'tagente_modulo', ['id_agente' => $id['id_agente'], 'nombre' => $module_name]); + + $return_value = db_process_sql( + "UPDATE talert_template_modules + SET disabled = 1 + WHERE id_agent_module = $id_agent_module" + ); + + if ($return_value != false) { + $agents_affected++; + } + } + + returnData('string', ['type' => 'string', 'data' => __('%d agents affected', $agents_affected)]); + } else { + $id_agent_module = db_get_value_filter('id_agente_modulo', 'tagente_modulo', ['id_agente' => $id_agent, 'nombre' => $module_name]); + + db_process_sql( + "UPDATE talert_template_modules + SET disabled = 1 + WHERE id_agent_module = $id_agent_module" + ); + + returnData('string', ['type' => 'string', 'data' => 'Correct alerts disable']); + } } @@ -10886,7 +11609,7 @@ function api_set_disable_module_alerts($agent_name, $module_name, $thrash3, $thr */ -function api_set_enable_module_alerts($agent_name, $module_name, $thrash3, $thrash4) +function api_set_enable_module_alerts($agent_name, $module_name, $other, $thrash4) { global $config; @@ -10899,20 +11622,59 @@ function api_set_enable_module_alerts($agent_name, $module_name, $thrash3, $thra return; } - $id_agent = agents_get_agent_id($agent_name); - if (!util_api_check_agent_and_print_error($id_agent, 'string', 'AW')) { - return; + $agent_by_alias = false; + + if ($other['data'][0] === '1') { + $agent_by_alias = true; } - $id_agent_module = db_get_value_filter('id_agente_modulo', 'tagente_modulo', ['id_agente' => $id_agent, 'nombre' => $module_name]); + if ($agent_by_alias) { + $ids_agents = agents_get_agent_id_by_alias($agent_name); + } else { + $id_agent = agents_get_agent_id($agent_name); + } - db_process_sql( - "UPDATE talert_template_modules - SET disabled = 0 - WHERE id_agent_module = $id_agent_module" - ); + if ($agent_by_alias) { + foreach ($ids_agents as $id) { + if (!util_api_check_agent_and_print_error($id['id_agente'], 'string', 'AW')) { + return; + } + } + } else { + if (!util_api_check_agent_and_print_error($id_agent, 'string', 'AW')) { + return; + } + } - returnData('string', ['type' => 'string', 'data' => 'Correct alerts enable']); + if ($agent_by_alias) { + $agents_affected = 0; + + foreach ($ids_agents as $id) { + $id_agent_module = db_get_value_filter('id_agente_modulo', 'tagente_modulo', ['id_agente' => $id['id_agente'], 'nombre' => $module_name]); + + $return_value = db_process_sql( + "UPDATE talert_template_modules + SET disabled = 0 + WHERE id_agent_module = $id_agent_module" + ); + + if ($return_value != false) { + $agents_affected++; + } + } + + returnData('string', ['type' => 'string', 'data' => __('%d agents affected', $agents_affected)]); + } else { + $id_agent_module = db_get_value_filter('id_agente_modulo', 'tagente_modulo', ['id_agente' => $id_agent, 'nombre' => $module_name]); + + db_process_sql( + "UPDATE talert_template_modules + SET disabled = 0 + WHERE id_agent_module = $id_agent_module" + ); + + returnData('string', ['type' => 'string', 'data' => 'Correct alerts enable']); + } } @@ -10984,11 +11746,11 @@ function api_get_total_modules($id_group, $trash1, $trash2, $returnType) } $sql = "SELECT COUNT(DISTINCT(id_agente_modulo)) - FROM tagente_modulo tam, tagente ta - LEFT JOIN tagent_secondary_group tasg - ON ta.id_agente = tasg.id_agent - WHERE tam.id_agente = ta.id_agente AND id_module_group = $id_group - AND delete_pending = 0 AND $groups_clause"; + FROM tagente_modulo tam, tagente ta + LEFT JOIN tagent_secondary_group tasg + ON ta.id_agente = tasg.id_agent + WHERE tam.id_agente = ta.id_agente AND id_module_group = $id_group + AND delete_pending = 0 AND $groups_clause"; $total = db_get_value_sql($sql); @@ -11049,8 +11811,8 @@ function api_get_agent_name($id_agent, $trash1, $trash2, $returnType) $sql = sprintf( 'SELECT nombre - FROM tagente - WHERE id_agente = %d', + FROM tagente + WHERE id_agente = %d', $id_agent ); $value = db_get_value_sql($sql); @@ -11126,8 +11888,8 @@ function api_get_agent_alias($id_agent, $id_node, $trash1, $returnType) $sql = sprintf( 'SELECT alias - FROM '.$table_agent_alias.' - WHERE id_agente = %d', + FROM '.$table_agent_alias.' + WHERE id_agente = %d', $id_agent ); $value = db_get_value_sql($sql); @@ -11159,8 +11921,8 @@ function api_get_module_name($id_module, $trash1, $trash2, $returnType) $sql = sprintf( 'SELECT nombre - FROM tagente_modulo - WHERE id_agente_modulo = %d', + FROM tagente_modulo + WHERE id_agente_modulo = %d', $id_module ); @@ -11201,15 +11963,15 @@ function api_get_alert_action_by_group($id_group, $id_action, $trash2, $returnTy } $sql = "SELECT SUM(internal_counter) - FROM - talert_template_modules tatm, - tagente ta LEFT JOIN tagent_secondary_group tasg - ON ta.id_agente = tasg.id_agent, - tagente_modulo tam - WHERE tam.id_agente = ta.id_agente - AND tatm.id_agent_module = tam.id_agente_modulo - AND ta.disabled = 0 - AND $filter_groups"; + FROM + talert_template_modules tatm, + tagente ta LEFT JOIN tagent_secondary_group tasg + ON ta.id_agente = tasg.id_agent, + tagente_modulo tam + WHERE tam.id_agente = ta.id_agente + AND tatm.id_agent_module = tam.id_agente_modulo + AND ta.disabled = 0 + AND $filter_groups"; $value = db_get_value_sql($sql); @@ -11239,8 +12001,8 @@ function api_get_event_info($id_event, $trash1, $trash, $returnType) } $sql = 'SELECT * - FROM '.$table_events." - WHERE id_evento=$id_event"; + FROM '.$table_events." + WHERE id_evento=$id_event"; $event_data = db_get_row_sql($sql); // Check the access to group @@ -13768,8 +14530,8 @@ function api_set_create_event_filter($name, $thrash1, $other, $thrash3) } else { $sql = sprintf( 'SELECT alias - FROM tagente - WHERE id_agente = %d', + FROM tagente + WHERE id_agente = %d', $agent[0]['id_agente'] ); @@ -13812,8 +14574,8 @@ function api_set_create_event_filter($name, $thrash1, $other, $thrash3) $agents = db_get_all_rows_sql( 'SELECT id_agente - FROM tagente - WHERE id_grupo IN ('.$id_groups.')' + FROM tagente + WHERE id_grupo IN ('.$id_groups.')' ); if ($agents === false) { @@ -13982,8 +14744,8 @@ function api_set_update_event_filter($id_event_filter, $thrash1, $other, $thrash } else { $sql = sprintf( 'SELECT alias - FROM tagente - WHERE id_agente = %d', + FROM tagente + WHERE id_agente = %d', $agent[0]['id_agente'] ); @@ -14025,12 +14787,10 @@ function api_set_update_event_filter($id_event_filter, $thrash1, $other, $thrash break; case 14: - print_r('14444444'); $values['tag_with'] = (preg_match('/^\[(("\d+"((,|\])("\d+"))+)|"\d+")\]$/', io_safe_output($other['data'][14]))) ? $other['data'][14] : '[]'; break; case 15: - print_r('1555555555'); $values['tag_without'] = (preg_match('/^\[(("\d+"((,|\])("\d+"))+)|"\d+")\]$/', io_safe_output($other['data'][15]))) ? $other['data'][15] : '[]'; break; @@ -14055,8 +14815,8 @@ function api_set_update_event_filter($id_event_filter, $thrash1, $other, $thrash $agents = db_get_all_rows_sql( 'SELECT id_agente - FROM tagente - WHERE id_grupo IN ('.$id_groups.')' + FROM tagente + WHERE id_grupo IN ('.$id_groups.')' ); if ($agents === false) { @@ -14089,7 +14849,6 @@ function api_set_update_event_filter($id_event_filter, $thrash1, $other, $thrash break; case 20: - print_r('adadadasds'); $values['user_comment'] = $other['data'][20]; break; } @@ -14478,7 +15237,7 @@ function api_get_group_id_by_name($thrash1, $thrash2, $other, $thrash3) $sql = sprintf( 'SELECT id_grupo - FROM tgrupo WHERE nombre = "'.$other['data'].'"' + FROM tgrupo WHERE nombre = "'.$other['data'].'"' ); $group_id = db_get_all_rows_sql($sql); @@ -14504,7 +15263,7 @@ function api_get_timezone($thrash1, $thrash2, $other, $thrash3) $sql = sprintf( 'SELECT value - FROM tconfig WHERE token = "timezone"' + FROM tconfig WHERE token = "timezone"' ); $timezone = db_get_all_rows_sql($sql); @@ -14530,7 +15289,7 @@ function api_get_language($thrash1, $thrash2, $other, $thrash3) $sql = sprintf( 'SELECT value - FROM tconfig WHERE token = "language"' + FROM tconfig WHERE token = "language"' ); $language = db_get_all_rows_sql($sql); @@ -14556,7 +15315,7 @@ function api_get_session_timeout($thrash1, $thrash2, $other, $thrash3) $sql = sprintf( 'SELECT value - FROM tconfig WHERE token = "session_timeout"' + FROM tconfig WHERE token = "session_timeout"' ); $language = db_get_all_rows_sql($sql); diff --git a/pandora_server/lib/PandoraFMS/DB.pm b/pandora_server/lib/PandoraFMS/DB.pm index a257d5df07..63c756e5ae 100644 --- a/pandora_server/lib/PandoraFMS/DB.pm +++ b/pandora_server/lib/PandoraFMS/DB.pm @@ -57,6 +57,7 @@ our @EXPORT = qw( get_addr_id get_agent_addr_id get_agent_id + get_agent_ids_from_alias get_agent_address get_agent_alias get_agent_group @@ -232,6 +233,17 @@ sub get_agent_id ($$) { return defined ($rc) ? $rc : -1; } +######################################################################## +## Return agent IDs given an agent alias. +######################################################################## +sub get_agent_ids_from_alias ($$) { + my ($dbh, $agent_alias) = @_; + + my @rc = get_db_rows ($dbh, "SELECT id_agente, nombre FROM tagente WHERE alias = ?", safe_input($agent_alias)); + + return @rc; +} + ######################################################################## ## Return server ID given the name of server. ######################################################################## @@ -956,7 +968,7 @@ sub db_process_insert($$$$;@) { } } my $columns_string = join(',', @columns_array); - + my $res = db_insert ($dbh, $index, "INSERT INTO $table ($columns_string) VALUES " . $wildcards, @values_array); diff --git a/pandora_server/lib/PandoraFMS/Tools.pm b/pandora_server/lib/PandoraFMS/Tools.pm index e07a4e9ae6..ad769c9655 100755 --- a/pandora_server/lib/PandoraFMS/Tools.pm +++ b/pandora_server/lib/PandoraFMS/Tools.pm @@ -141,6 +141,7 @@ our @EXPORT = qw( long_to_ip ip_to_long get_enabled_servers + dateTimeToTimestamp ); # ID of the different servers diff --git a/pandora_server/util/pandora_manage.pl b/pandora_server/util/pandora_manage.pl index 878ec570ad..a23959d124 100755 --- a/pandora_server/util/pandora_manage.pl +++ b/pandora_server/util/pandora_manage.pl @@ -1135,19 +1135,26 @@ sub cli_create_agent() { ############################################################################## sub cli_delete_agent() { - my $agent_name = @ARGV[2]; + my ($agent_name,$use_alias) = @ARGV[2..3]; + + my @id_agents; + my $id_agent; $agent_name = decode_entities($agent_name); - + if (is_metaconsole($conf) == 1) { - my $agents_groups = enterprise_hook('get_metaconsole_agent',[$dbh, $agent_name]); - - if (scalar(@{$agents_groups}) != 0) { - foreach my $agent (@{$agents_groups}) { - my $return = enterprise_hook('delete_metaconsole_agent',[$dbh,$agent->{'id_agente'}]); - print_log "[INFO] Deleting agent '$agent_name' \n\n"; + if (not defined $use_alias) { + my $agents_groups = enterprise_hook('get_metaconsole_agent',[$dbh, $agent_name]); + + if (scalar(@{$agents_groups}) != 0) { + foreach my $agent (@{$agents_groups}) { + my $return = enterprise_hook('delete_metaconsole_agent',[$dbh,$agent->{'id_agente'}]); + print_log "[INFO] Deleting agent '$agent_name' \n\n"; + } } } + + my $servers = enterprise_hook('get_metaconsole_setup_servers',[$dbh]); my @servers_id = split(',',$servers); my @list_servers; @@ -1155,24 +1162,58 @@ sub cli_delete_agent() { foreach my $server (@servers_id) { my $dbh_metaconsole = enterprise_hook('get_node_dbh',[$conf, $server, $dbh]); - my $id_agent = get_agent_id($dbh_metaconsole,$agent_name); + my @id_agents; + my $id_agent; + + if (defined $use_alias and $use_alias eq 'use_alias') { + @id_agents = get_agent_ids_from_alias($dbh_metaconsole,$agent_name); + + foreach my $id (@id_agents) { + + if ($id->{'id_agente'} == -1) { + next; + } + else { + print_log "[INFO] Deleting agent '$id->{'nombre'}' in ID server: '$server'\n\n"; + pandora_delete_agent($dbh_metaconsole,$id->{'id_agente'},$conf); + } + } + } else { + $id_agent = get_agent_id($dbh_metaconsole,$agent_name); + + if ($id_agent == -1) { + next; + } + else { + print_log "[INFO] Deleting agent '$agent_name' in ID server: '$server'\n\n"; + pandora_delete_agent($dbh_metaconsole,$id_agent,$conf); + } + } + - if ($id_agent == -1) { - next; - } - else { - print_log "[INFO] Deleting agent '$agent_name' in ID server: '$server'\n\n"; - pandora_delete_agent($dbh_metaconsole,$id_agent,$conf); - } } } else { - - my $id_agent = get_agent_id($dbh,$agent_name); - exist_check($id_agent,'agent',$agent_name); - - print_log "[INFO] Deleting agent '$agent_name'\n\n"; - pandora_delete_agent($dbh,$id_agent,$conf); + my @id_agents; + my $id_agent; + + if (defined $use_alias and $use_alias eq 'use_alias') { + @id_agents = get_agent_ids_from_alias($dbh,$agent_name); + } else { + $id_agent = get_agent_id($dbh,$agent_name); + exist_check($id_agent,'agent',$agent_name); + } + + if (defined $use_alias and $use_alias eq 'use_alias') { + foreach my $id (@id_agents) { + exist_check($id->{'id_agente'},'agent',$agent_name); + print_log "[INFO] Deleting agent '$id->{'nombre'}'\n\n"; + pandora_delete_agent($dbh,$id->{'id_agente'},$conf); + } + } else { + print_log "[INFO] Deleting agent '$agent_name'\n\n"; + pandora_delete_agent($dbh,$id_agent,$conf); + } } } @@ -1290,14 +1331,14 @@ sub cli_create_data_module($) { $min,$max,$post_process, $interval, $warning_min, $warning_max, $critical_min, $critical_max, $history_data, $definition_file, $configuration_data, $warning_str, $critical_str, $enable_unknown_events, $ff_threshold, $each_ff, $ff_threshold_normal, $ff_threshold_warning, $ff_threshold_critical, $ff_timeout, - $warning_inverse, $critical_inverse, $critical_instructions, $warning_instructions, $unknown_instructions); + $warning_inverse, $critical_inverse, $critical_instructions, $warning_instructions, $unknown_instructions, $use_alias); if ($in_policy == 0) { ($module_name, $module_type, $agent_name, $description, $module_group, $min,$max,$post_process, $interval, $warning_min, $warning_max, $critical_min, $critical_max, $history_data, $definition_file, $warning_str, $critical_str, $enable_unknown_events, $ff_threshold, $each_ff, $ff_threshold_normal, $ff_threshold_warning, $ff_threshold_critical, $ff_timeout, - $warning_inverse, $critical_inverse, $critical_instructions, $warning_instructions, $unknown_instructions) = @ARGV[2..30]; + $warning_inverse, $critical_inverse, $critical_instructions, $warning_instructions, $unknown_instructions, $use_alias) = @ARGV[2..31]; } else { ($policy_name, $module_name, $module_type, $description, $module_group, @@ -1311,6 +1352,7 @@ sub cli_create_data_module($) { my $module_type_def; my $agent_id; + my @id_agents; my $policy_id; my $disabled_types_event = {}; @@ -1323,13 +1365,22 @@ sub cli_create_data_module($) { my $disabled_types_event_json = encode_json($disabled_types_event); if ($in_policy == 0) { - $agent_id = get_agent_id($dbh,$agent_name); - exist_check($agent_id,'agent',$agent_name); - - my $module_exists = get_agent_module_id($dbh, $module_name, $agent_id); - non_exist_check($module_exists, 'module name', $module_name); - - #~ print_log "[INFO] Adding module '$module_name' to agent '$agent_name'\n\n"; + if (defined $use_alias and $use_alias eq 'use_alias') { + @id_agents = get_agent_ids_from_alias($dbh,$agent_name); + + foreach my $id (@id_agents) { + exist_check($id->{'id_agente'},'agent',$agent_name); + + my $module_exists = get_agent_module_id($dbh, $module_name, $id->{'id_agente'}); + non_exist_check($module_exists, 'module name', $module_name); + } + } else { + $agent_id = get_agent_id($dbh,$agent_name); + exist_check($agent_id,'agent',$agent_name); + + my $module_exists = get_agent_module_id($dbh, $module_name, $agent_id); + non_exist_check($module_exists, 'module name', $module_name); + } } else { $policy_id = enterprise_hook('get_policy_id',[$dbh, safe_input($policy_name)]); @@ -1343,44 +1394,91 @@ sub cli_create_data_module($) { $module_name_def = $module_name; $module_type_def = $module_type; - # If the module is local and is not to policy, we add it to the conf file - if (defined($definition_file) && (-e $definition_file) && (-e $conf->{incomingdir}.'/conf/'.md5($agent_name).'.conf')){ - open (FILE, $definition_file); - my @file = ; - my $definition = join("", @file); - close (FILE); - - # If the parameter name or type and the definition file name or type - # dont match will be set the file definitions - open (FILE, $definition_file); - while () { - chomp; - my ($key, $val) = split / /,2; - if ($key eq 'module_name') { - $module_name_def = $val; - } - if ($key eq 'module_type') { - $module_type_def = $val; + + if (defined $use_alias and $use_alias eq 'use_alias') { + foreach my $id (@id_agents) { + # If the module is local and is not to policy, we add it to the conf file + if (defined($definition_file) && (-e $definition_file) && (-e $conf->{incomingdir}.'/conf/'.md5($id->{'nombre'}).'.conf')){ + open (FILE, $definition_file); + my @file = ; + my $definition = join("", @file); + close (FILE); + + # If the parameter name or type and the definition file name or type + # dont match will be set the file definitions + open (FILE, $definition_file); + while () { + chomp; + my ($key, $val) = split / /,2; + if ($key eq 'module_name') { + $module_name_def = $val; + } + if ($key eq 'module_type') { + $module_type_def = $val; + } + } + close (FILE); + + open (FILE, $conf->{incomingdir}.'/conf/'.md5($id->{'nombre'}).'.conf'); + my @file = ; + my $conf_file = join("", @file); + close(FILE); + + open FILE, "> ".$conf->{incomingdir}.'/conf/'.md5($id->{'nombre'}).'.conf'; + print FILE "$conf_file\n$definition"; + close(FILE); + + enterprise_hook('pandora_update_md5_file', [$conf, $id->{'nombre'}]); } } - close (FILE); - - open (FILE, $conf->{incomingdir}.'/conf/'.md5($agent_name).'.conf'); - my @file = ; - my $conf_file = join("", @file); - close(FILE); - - open FILE, "> ".$conf->{incomingdir}.'/conf/'.md5($agent_name).'.conf'; - print FILE "$conf_file\n$definition"; - close(FILE); - - enterprise_hook('pandora_update_md5_file', [$conf, $agent_name]); + } else { + # If the module is local and is not to policy, we add it to the conf file + if (defined($definition_file) && (-e $definition_file) && (-e $conf->{incomingdir}.'/conf/'.md5($agent_name).'.conf')){ + open (FILE, $definition_file); + my @file = ; + my $definition = join("", @file); + close (FILE); + + # If the parameter name or type and the definition file name or type + # dont match will be set the file definitions + open (FILE, $definition_file); + while () { + chomp; + my ($key, $val) = split / /,2; + if ($key eq 'module_name') { + $module_name_def = $val; + } + if ($key eq 'module_type') { + $module_type_def = $val; + } + } + close (FILE); + + open (FILE, $conf->{incomingdir}.'/conf/'.md5($agent_name).'.conf'); + my @file = ; + my $conf_file = join("", @file); + close(FILE); + + open FILE, "> ".$conf->{incomingdir}.'/conf/'.md5($agent_name).'.conf'; + print FILE "$conf_file\n$definition"; + close(FILE); + + enterprise_hook('pandora_update_md5_file', [$conf, $agent_name]); + } } if ($in_policy == 0) { - my $module_exists = get_agent_module_id($dbh, $module_name_def, $agent_id); - non_exist_check($module_exists, 'module name', $module_name_def); - print_log "[INFO] Adding module '$module_name' to agent '$agent_name'\n\n"; + if (defined $use_alias and $use_alias eq 'use_alias') { + foreach my $id (@id_agents) { + my $module_exists = get_agent_module_id($dbh, $module_name_def, $id->{'id_agente'}); + non_exist_check($module_exists, 'module name', $module_name_def); + print_log "[INFO] Adding module '$module_name' to agent '$id->{'nombre'}'\n\n"; + } + } else { + my $module_exists = get_agent_module_id($dbh, $module_name_def, $agent_id); + non_exist_check($module_exists, 'module name', $module_name_def); + print_log "[INFO] Adding module '$module_name' to agent '$agent_name'\n\n"; + } } else { my $policy_module_exist = enterprise_hook('get_policy_module_id',[$dbh, $policy_id, $module_name_def]); @@ -1398,7 +1496,6 @@ sub cli_create_data_module($) { print_log "[INFO] The module name has been forced to '$module_name' by the definition file\n\n"; } - # The get_module_id has wrong name. Change in future my $module_type_id = get_module_id($dbh,$module_type); exist_check($module_type_id,'module type',$module_type); @@ -1417,10 +1514,13 @@ sub cli_create_data_module($) { my %parameters; $parameters{'id_tipo_modulo'} = $module_type_id; - + if ($in_policy == 0) { $parameters{'nombre'} = safe_input($module_name); - $parameters{'id_agente'} = $agent_id; + + if (not defined $use_alias) { + $parameters{'id_agente'} = $agent_id; + } } else { $parameters{'name'} = safe_input($module_name); @@ -1464,7 +1564,14 @@ sub cli_create_data_module($) { $parameters{'unknown_instructions'} = $unknown_instructions unless !defined ($unknown_instructions); if ($in_policy == 0) { - pandora_create_module_from_hash ($conf, \%parameters, $dbh); + if (defined $use_alias and $use_alias eq 'use_alias') { + foreach my $id (@id_agents) { + $parameters{'id_agente'} = $id->{'id_agente'}; + pandora_create_module_from_hash ($conf, \%parameters, $dbh); + } + } else { + pandora_create_module_from_hash ($conf, \%parameters, $dbh); + } } else { enterprise_hook('pandora_create_policy_module_from_hash', [$conf, \%parameters, $dbh]); @@ -1483,7 +1590,7 @@ sub cli_create_web_module($) { $critical_max, $history_data, $retries, $requests, $agent_browser_id, $auth_server, $auth_realm, $definition_file, $proxy_url, $proxy_auth_login, $proxy_auth_password, $configuration_data, $warning_str, $critical_str, $enable_unknown_events, $ff_threshold, $each_ff, $ff_threshold_normal, $ff_threshold_warning, $ff_threshold_critical, $ff_timeout, - $warning_inverse, $critical_inverse, $critical_instructions, $warning_instructions, $unknown_instructions); + $warning_inverse, $critical_inverse, $critical_instructions, $warning_instructions, $unknown_instructions, $use_alias); if ($in_policy == 0) { ($module_name, $module_type, $agent_name, $description, $module_group, @@ -1491,7 +1598,7 @@ sub cli_create_web_module($) { $critical_max, $history_data, $retries, $requests, $agent_browser_id, $auth_server, $auth_realm, $definition_file, $proxy_url, $proxy_auth_login, $proxy_auth_password, $warning_str, $critical_str, $enable_unknown_events, $ff_threshold, $each_ff, $ff_threshold_normal, $ff_threshold_warning, $ff_threshold_critical, $ff_timeout, - $warning_inverse, $critical_inverse, $critical_instructions, $warning_instructions, $unknown_instructions) = @ARGV[2..38]; + $warning_inverse, $critical_inverse, $critical_instructions, $warning_instructions, $unknown_instructions, $use_alias) = @ARGV[2..39]; } else { ($policy_name, $module_name, $module_type, $description, $module_group, @@ -1506,6 +1613,7 @@ sub cli_create_web_module($) { my $module_type_def; my $agent_id; + my @id_agents; my $policy_id; my $disabled_types_event = {}; @@ -1518,13 +1626,22 @@ sub cli_create_web_module($) { my $disabled_types_event_json = encode_json($disabled_types_event); if ($in_policy == 0) { - $agent_id = get_agent_id($dbh,$agent_name); - exist_check($agent_id,'agent',$agent_name); - - my $module_exists = get_agent_module_id($dbh, $module_name, $agent_id); - non_exist_check($module_exists, 'module name', $module_name); - - #~ print_log "[INFO] Adding module '$module_name' to agent '$agent_name'\n\n"; + if (defined $use_alias and $use_alias eq 'use_alias') { + @id_agents = get_agent_ids_from_alias($dbh,$agent_name); + + foreach my $id (@id_agents) { + exist_check($id->{'id_agente'},'agent',$agent_name); + + my $module_exists = get_agent_module_id($dbh, $module_name, $id->{'id_agente'}); + non_exist_check($module_exists, 'module name', $module_name); + } + } else { + $agent_id = get_agent_id($dbh,$agent_name); + exist_check($agent_id,'agent',$agent_name); + + my $module_exists = get_agent_module_id($dbh, $module_name, $agent_id); + non_exist_check($module_exists, 'module name', $module_name); + } } else { $policy_id = enterprise_hook('get_policy_id',[$dbh, safe_input($policy_name)]); @@ -1538,44 +1655,90 @@ sub cli_create_web_module($) { $module_name_def = $module_name; $module_type_def = $module_type; + # If the module is local and is not to policy, we add it to the conf file - if (defined($definition_file) && (-e $definition_file) && (-e $conf->{incomingdir}.'/conf/'.md5($agent_name).'.conf')){ - open (FILE, $definition_file); - my @file = ; - my $definition = join("", @file); - close (FILE); - - # If the parameter name or type and the definition file name or type - # dont match will be set the file definitions - open (FILE, $definition_file); - while () { - chomp; - my ($key, $val) = split / /; - if ($key eq 'module_name') { - $module_name_def = $val; - } - if ($key eq 'module_type') { - $module_type_def = $val; + if (defined $use_alias and $use_alias eq 'use_alias') { + foreach my $id (@id_agents) { + if (defined($definition_file) && (-e $definition_file) && (-e $conf->{incomingdir}.'/conf/'.md5($agent_name).'.conf')){ + open (FILE, $definition_file); + my @file = ; + my $definition = join("", @file); + close (FILE); + + # If the parameter name or type and the definition file name or type + # dont match will be set the file definitions + open (FILE, $definition_file); + while () { + chomp; + my ($key, $val) = split / /; + if ($key eq 'module_name') { + $module_name_def = $val; + } + if ($key eq 'module_type') { + $module_type_def = $val; + } + } + close (FILE); + + #open (FILE, $conf->{incomingdir}.'/conf/'.md5($agent_name).'.conf'); + #my @file = ; + #my $conf_file = join("", @file); + #close(FILE); + + #open FILE, "> ".$conf->{incomingdir}.'/conf/'.md5($agent_name).'.conf'; + #print FILE "$conf_file\n$definition"; + #close(FILE); + + enterprise_hook('pandora_update_md5_file', [$conf, $agent_name]); } } - close (FILE); - - #open (FILE, $conf->{incomingdir}.'/conf/'.md5($agent_name).'.conf'); - #my @file = ; - #my $conf_file = join("", @file); - #close(FILE); - - #open FILE, "> ".$conf->{incomingdir}.'/conf/'.md5($agent_name).'.conf'; - #print FILE "$conf_file\n$definition"; - #close(FILE); - - enterprise_hook('pandora_update_md5_file', [$conf, $agent_name]); + } else { + if (defined($definition_file) && (-e $definition_file) && (-e $conf->{incomingdir}.'/conf/'.md5($agent_name).'.conf')){ + open (FILE, $definition_file); + my @file = ; + my $definition = join("", @file); + close (FILE); + + # If the parameter name or type and the definition file name or type + # dont match will be set the file definitions + open (FILE, $definition_file); + while () { + chomp; + my ($key, $val) = split / /; + if ($key eq 'module_name') { + $module_name_def = $val; + } + if ($key eq 'module_type') { + $module_type_def = $val; + } + } + close (FILE); + + #open (FILE, $conf->{incomingdir}.'/conf/'.md5($agent_name).'.conf'); + #my @file = ; + #my $conf_file = join("", @file); + #close(FILE); + + #open FILE, "> ".$conf->{incomingdir}.'/conf/'.md5($agent_name).'.conf'; + #print FILE "$conf_file\n$definition"; + #close(FILE); + + enterprise_hook('pandora_update_md5_file', [$conf, $agent_name]); + } } - + if ($in_policy == 0) { - my $module_exists = get_agent_module_id($dbh, $module_name_def, $agent_id); - non_exist_check($module_exists, 'module name', $module_name_def); - print_log "[INFO] Adding module '$module_name' to agent '$agent_name'\n\n"; + if (defined $use_alias and $use_alias eq 'use_alias') { + foreach my $id (@id_agents) { + my $module_exists = get_agent_module_id($dbh, $module_name_def, $id->{'id_agente'}); + non_exist_check($module_exists, 'module name', $module_name_def); + print_log "[INFO] Adding module '$module_name' to agent '$id->{'nombre'}'\n\n"; + } + } else { + my $module_exists = get_agent_module_id($dbh, $module_name_def, $agent_id); + non_exist_check($module_exists, 'module name', $module_name_def); + print_log "[INFO] Adding module '$module_name' to agent '$agent_name'\n\n"; + } } else { my $policy_module_exist = enterprise_hook('get_policy_module_id',[$dbh, $policy_id, $module_name_def]); @@ -1615,7 +1778,10 @@ sub cli_create_web_module($) { if ($in_policy == 0) { $parameters{'nombre'} = safe_input($module_name); - $parameters{'id_agente'} = $agent_id; + + if (not defined $use_alias) { + $parameters{'id_agente'} = $agent_id; + } } else { $parameters{'name'} = safe_input($module_name); @@ -1672,14 +1838,21 @@ sub cli_create_web_module($) { if ($in_policy == 0) { - pandora_create_module_from_hash ($conf, \%parameters, $dbh); + if (defined $use_alias and $use_alias eq 'use_alias') { + foreach my $id (@id_agents) { + $parameters{'id_agente'} = $id->{'id_agente'}; + pandora_create_module_from_hash ($conf, \%parameters, $dbh); + } + } else { + pandora_create_module_from_hash ($conf, \%parameters, $dbh); + } } else { enterprise_hook('pandora_create_policy_module_from_hash', [$conf, \%parameters, $dbh]); } #Begin Insert module definition from file_definition in bd - if (defined($definition_file)){ + if (defined($definition_file)){ open(my $fh, '<', $definition_file) or die($!); my @lines = <$fh>; @@ -1766,21 +1939,50 @@ sub cli_module_group_synch() { ############################################################################## sub cli_create_network_module_from_component() { - my ($agent_name, $component_name) = @ARGV[2..3]; - - my $agent_id = get_agent_id($dbh,$agent_name); - exist_check($agent_id,'agent',$agent_name); - - my $nc_id = pandora_get_network_component_id($dbh, $component_name); - exist_check($nc_id,'network component',$component_name); - - my $module_exists = get_agent_module_id($dbh, $component_name, $agent_id); - non_exist_check($module_exists, 'module name', $component_name); - - # Get network component data - my $component = get_db_single_row ($dbh, 'SELECT * FROM tnetwork_component WHERE id_nc = ?', $nc_id); - - pandora_create_module_from_network_component ($conf, $component, $agent_id, $dbh); + my ($agent_name, $component_name, $use_alias) = @ARGV[2..4]; + + if (defined $use_alias and $use_alias eq 'use_alias') { + my @id_agents = get_agent_ids_from_alias($dbh,$agent_name); + my $agent_id; + my $module_exists; + my $component; + my $nc_id; + + foreach my $id (@id_agents) { + $agent_id = $id->{'id_agente'}; + exist_check($agent_id,'agent',$agent_name); + + $nc_id = pandora_get_network_component_id($dbh, $component_name); + exist_check($nc_id,'network component',$component_name); + + # Get network component data + $component = get_db_single_row ($dbh, 'SELECT * FROM tnetwork_component WHERE id_nc = ?', $nc_id); + + my $module_exists = get_agent_module_id($dbh, $component_name, $agent_id); + non_exist_check($module_exists, 'module name', $component_name); + + print_log "[INFO] Creating module from component '$component_name'\n\n"; + + pandora_create_module_from_network_component ($conf, $component, $agent_id, $dbh); + } + } else { + my $nc_id = pandora_get_network_component_id($dbh, $component_name); + exist_check($nc_id,'network component',$component_name); + + # Get network component data + my $component = get_db_single_row ($dbh, 'SELECT * FROM tnetwork_component WHERE id_nc = ?', $nc_id); + + my $agent_id = get_agent_id($dbh,$agent_name); + exist_check($agent_id,'agent',$agent_name); + + my $module_exists = get_agent_module_id($dbh, $component_name, $agent_id); + non_exist_check($module_exists, 'module name', $component_name); + + + print_log "[INFO] Creating module from component '$component_name'\n\n"; + + pandora_create_module_from_network_component ($conf, $component, $agent_id, $dbh); + } } ############################################################################## @@ -1831,7 +2033,7 @@ sub cli_create_network_module($) { $module_group, $min, $max, $post_process, $interval, $warning_min, $warning_max, $critical_min, $critical_max, $history_data, $ff_threshold, $warning_str, $critical_str, $enable_unknown_events, $each_ff, $ff_threshold_normal, $ff_threshold_warning, $ff_threshold_critical, $timeout, $retries, $critical_instructions, - $warning_instructions, $unknown_instructions, $warning_inverse, $critical_inverse); + $warning_instructions, $unknown_instructions, $warning_inverse, $critical_inverse, $use_alias); if ($in_policy == 0) { ($module_name, $module_type, $agent_name, $module_address, $module_port, $description, @@ -1839,7 +2041,7 @@ sub cli_create_network_module($) { $critical_max, $history_data, $ff_threshold, $warning_str, $critical_str, $enable_unknown_events, $each_ff, $ff_threshold_normal, $ff_threshold_warning, $ff_threshold_critical, $timeout, $retries,$critical_instructions, $warning_instructions, $unknown_instructions, - $warning_inverse, $critical_inverse) = @ARGV[2..32]; + $warning_inverse, $critical_inverse, $use_alias) = @ARGV[2..33]; } else { ($policy_name, $module_name, $module_type, $module_port, $description, @@ -1849,10 +2051,12 @@ sub cli_create_network_module($) { $ff_threshold_critical, $timeout, $retries, $critical_instructions, $warning_instructions, $unknown_instructions, $warning_inverse, $critical_inverse) = @ARGV[2..31]; } - + my $module_name_def; my $module_type_def; + my $agent_id; + my @id_agents; my $policy_id; my $disabled_types_event = {}; @@ -1865,13 +2069,26 @@ sub cli_create_network_module($) { my $disabled_types_event_json = encode_json($disabled_types_event); if ($in_policy == 0) { - $agent_id = get_agent_id($dbh,$agent_name); - exist_check($agent_id,'agent',$agent_name); - - my $module_exists = get_agent_module_id($dbh, $module_name, $agent_id); - non_exist_check($module_exists, 'module name', $module_name); - - print_log "[INFO] Adding module '$module_name' to agent '$agent_name'\n\n"; + if (defined $use_alias and $use_alias eq 'use_alias') { + @id_agents = get_agent_ids_from_alias($dbh,$agent_name); + + foreach my $id (@id_agents) { + exist_check($id->{'id_agente'},'agent',$agent_name); + + my $module_exists = get_agent_module_id($dbh, $module_name, $id->{'id_agente'}); + non_exist_check($module_exists, 'module name', $module_name); + + print_log "[INFO] Adding module '$module_name' to agent '$id->{'nombre'}'\n\n"; + } + } else { + $agent_id = get_agent_id($dbh,$agent_name); + exist_check($agent_id,'agent',$agent_name); + + my $module_exists = get_agent_module_id($dbh, $module_name, $agent_id); + non_exist_check($module_exists, 'module name', $module_name); + + print_log "[INFO] Adding module '$module_name' to agent '$agent_name'\n\n"; + } } else { $policy_id = enterprise_hook('get_policy_id',[$dbh, safe_input($policy_name)]); @@ -1921,7 +2138,11 @@ sub cli_create_network_module($) { if ($in_policy == 0) { $parameters{'nombre'} = safe_input($module_name); - $parameters{'id_agente'} = $agent_id; + + if (not defined $use_alias) { + $parameters{'id_agente'} = $agent_id; + } + $parameters{'ip_target'} = $module_address; } else { @@ -1966,7 +2187,14 @@ sub cli_create_network_module($) { $parameters{'warning_inverse'} = $warning_inverse unless !defined ($warning_inverse); if ($in_policy == 0) { - pandora_create_module_from_hash ($conf, \%parameters, $dbh); + if (defined $use_alias and $use_alias eq 'use_alias') { + foreach my $id (@id_agents) { + $parameters{'id_agente'} = $id->{'id_agente'}; + pandora_create_module_from_hash ($conf, \%parameters, $dbh); + } + } else { + pandora_create_module_from_hash ($conf, \%parameters, $dbh); + } } else { enterprise_hook('pandora_create_policy_module_from_hash', [$conf, \%parameters, $dbh]); @@ -1985,7 +2213,7 @@ sub cli_create_snmp_module($) { $warning_max, $critical_min, $critical_max, $history_data, $snmp3_priv_method, $snmp3_priv_pass, $snmp3_sec_level, $snmp3_auth_method, $snmp3_auth_user, $snmp3_auth_pass, $ff_threshold, $warning_str, $critical_str, $enable_unknown_events, $each_ff, $ff_threshold_normal, $ff_threshold_warning, $ff_threshold_critical, $timeout, $retries, - $critical_instructions, $warning_instructions, $unknown_instructions, $warning_inverse, $critical_inverse); + $critical_instructions, $warning_instructions, $unknown_instructions, $warning_inverse, $critical_inverse, $use_alias); if ($in_policy == 0) { ($module_name, $module_type, $agent_name, $module_address, $module_port, $version, $community, @@ -1993,7 +2221,7 @@ sub cli_create_snmp_module($) { $warning_max, $critical_min, $critical_max, $history_data, $snmp3_priv_method, $snmp3_priv_pass, $snmp3_sec_level, $snmp3_auth_method, $snmp3_auth_user, $snmp3_auth_pass, $ff_threshold, $warning_str, $critical_str, $enable_unknown_events, $each_ff, $ff_threshold_normal, $ff_threshold_warning, $ff_threshold_critical, $timeout, $retries, - $critical_instructions, $warning_instructions, $unknown_instructions, $warning_inverse, $critical_inverse) = @ARGV[2..41]; + $critical_instructions, $warning_instructions, $unknown_instructions, $warning_inverse, $critical_inverse, $use_alias) = @ARGV[2..42]; } else { ($policy_name, $module_name, $module_type, $module_port, $version, $community, @@ -2006,7 +2234,9 @@ sub cli_create_snmp_module($) { my $module_name_def; my $module_type_def; + my $agent_id; + my @id_agents; my $policy_id; my $disabled_types_event = {}; @@ -2019,13 +2249,26 @@ sub cli_create_snmp_module($) { my $disabled_types_event_json = encode_json($disabled_types_event); if ($in_policy == 0) { - $agent_id = get_agent_id($dbh,$agent_name); - exist_check($agent_id,'agent',$agent_name); - - my $module_exists = get_agent_module_id($dbh, $module_name, $agent_id); - non_exist_check($module_exists, 'module name', $module_name); - - print_log "[INFO] Adding module '$module_name' to agent '$agent_name'\n\n"; + if (defined $use_alias and $use_alias eq 'use_alias') { + @id_agents = get_agent_ids_from_alias($dbh,$agent_name); + + foreach my $id (@id_agents) { + exist_check($id->{'id_agente'},'agent',$agent_name); + + my $module_exists = get_agent_module_id($dbh, $module_name, $id->{'id_agente'}); + non_exist_check($module_exists, 'module name', $module_name); + + print_log "[INFO] Adding module '$module_name' to agent '$id->{'nombre'}'\n\n"; + } + } else { + $agent_id = get_agent_id($dbh,$agent_name); + exist_check($agent_id,'agent',$agent_name); + + my $module_exists = get_agent_module_id($dbh, $module_name, $agent_id); + non_exist_check($module_exists, 'module name', $module_name); + + print_log "[INFO] Adding module '$module_name' to agent '$agent_name'\n\n"; + } } else { $policy_id = enterprise_hook('get_policy_id',[$dbh, safe_input($policy_name)]); @@ -2064,7 +2307,11 @@ sub cli_create_snmp_module($) { if ($in_policy == 0) { $parameters{'nombre'} = safe_input($module_name); - $parameters{'id_agente'} = $agent_id; + + if (not defined $use_alias) { + $parameters{'id_agente'} = $agent_id; + } + $parameters{'ip_target'} = $module_address; } else { @@ -2125,7 +2372,14 @@ sub cli_create_snmp_module($) { $parameters{'warning_inverse'} = $warning_inverse unless !defined ($warning_inverse); if ($in_policy == 0) { - pandora_create_module_from_hash ($conf, \%parameters, $dbh); + if (defined $use_alias and $use_alias eq 'use_alias') { + foreach my $id (@id_agents) { + $parameters{'id_agente'} = $id->{'id_agente'}; + pandora_create_module_from_hash ($conf, \%parameters, $dbh); + } + } else { + pandora_create_module_from_hash ($conf, \%parameters, $dbh); + } } else { enterprise_hook('pandora_create_policy_module_from_hash', [$conf, \%parameters, $dbh]); @@ -2144,7 +2398,7 @@ sub cli_create_plugin_module($) { $interval, $warning_min, $warning_max, $critical_min, $critical_max, $history_data, $ff_threshold, $warning_str, $critical_str, $enable_unknown_events, $each_ff, $ff_threshold_normal, $ff_threshold_warning, $ff_threshold_critical, $timeout, - $critical_instructions, $warning_instructions, $unknown_instructions, $warning_inverse, $critical_inverse); + $critical_instructions, $warning_instructions, $unknown_instructions, $warning_inverse, $critical_inverse, $use_alias); if ($in_policy == 0) { ($module_name, $module_type, $agent_name, $module_address, $module_port, $plugin_name, @@ -2152,7 +2406,7 @@ sub cli_create_plugin_module($) { $interval, $warning_min, $warning_max, $critical_min, $critical_max, $history_data, $ff_threshold, $warning_str, $critical_str, $enable_unknown_events, $each_ff, $ff_threshold_normal, $ff_threshold_warning, $ff_threshold_critical, $timeout, - $critical_instructions, $warning_instructions, $unknown_instructions, $warning_inverse, $critical_inverse) = @ARGV[2..35]; + $critical_instructions, $warning_instructions, $unknown_instructions, $warning_inverse, $critical_inverse, $use_alias) = @ARGV[2..36]; } else { ($policy_name, $module_name, $module_type, $module_port, $plugin_name, @@ -2162,10 +2416,12 @@ sub cli_create_plugin_module($) { $each_ff, $ff_threshold_normal, $ff_threshold_warning, $ff_threshold_critical, $timeout, $critical_instructions, $warning_instructions, $unknown_instructions, $warning_inverse, $critical_inverse) = @ARGV[2..34]; } - + my $module_name_def; my $module_type_def; + my $agent_id; + my @id_agents; my $policy_id; my $disabled_types_event = {}; @@ -2178,13 +2434,26 @@ sub cli_create_plugin_module($) { my $disabled_types_event_json = encode_json($disabled_types_event); if ($in_policy == 0) { - $agent_id = get_agent_id($dbh,$agent_name); - exist_check($agent_id,'agent',$agent_name); - - my $module_exists = get_agent_module_id($dbh, $module_name, $agent_id); - non_exist_check($module_exists, 'module name', $module_name); - - print_log "[INFO] Adding module '$module_name' to agent '$agent_name'\n\n"; + if (defined $use_alias and $use_alias eq 'use_alias') { + @id_agents = get_agent_ids_from_alias($dbh,$agent_name); + + foreach my $id (@id_agents) { + exist_check($id->{'id_agente'},'agent',$agent_name); + + my $module_exists = get_agent_module_id($dbh, $module_name, $id->{'id_agente'}); + non_exist_check($module_exists, 'module name', $module_name); + + print_log "[INFO] Adding module '$module_name' to agent '$id->{'nombre'}'\n\n"; + } + } else { + $agent_id = get_agent_id($dbh,$agent_name); + exist_check($agent_id,'agent',$agent_name); + + my $module_exists = get_agent_module_id($dbh, $module_name, $agent_id); + non_exist_check($module_exists, 'module name', $module_name); + + print_log "[INFO] Adding module '$module_name' to agent '$agent_name'\n\n"; + } } else { $policy_id = enterprise_hook('get_policy_id',[$dbh, safe_input($policy_name)]); @@ -2222,7 +2491,11 @@ sub cli_create_plugin_module($) { if ($in_policy == 0) { $parameters{'nombre'} = safe_input($module_name); - $parameters{'id_agente'} = $agent_id; + + if (not defined $use_alias) { + $parameters{'id_agente'} = $agent_id; + } + $parameters{'ip_target'} = $module_address; } else { @@ -2299,7 +2572,14 @@ sub cli_create_plugin_module($) { $parameters{'warning_inverse'} = $warning_inverse unless !defined ($warning_inverse); if ($in_policy == 0) { - pandora_create_module_from_hash ($conf, \%parameters, $dbh); + if (defined $use_alias and $use_alias eq 'use_alias') { + foreach my $id (@id_agents) { + $parameters{'id_agente'} = $id->{'id_agente'}; + pandora_create_module_from_hash ($conf, \%parameters, $dbh); + } + } else { + pandora_create_module_from_hash ($conf, \%parameters, $dbh); + } } else { enterprise_hook('pandora_create_policy_module_from_hash', [$conf, \%parameters, $dbh]); @@ -2312,16 +2592,36 @@ sub cli_create_plugin_module($) { ############################################################################## sub cli_delete_module() { - my ($module_name,$agent_name) = @ARGV[2..3]; + my ($module_name,$agent_name, $use_alias) = @ARGV[2..4]; - print_log "[INFO] Deleting module '$module_name' from agent '$agent_name' \n\n"; - - my $id_agent = get_agent_id($dbh,$agent_name); - exist_check($id_agent,'agent',$agent_name); - my $id_module = get_agent_module_id($dbh,$module_name,$id_agent); - exist_check($id_module,'module',$module_name); - - pandora_delete_module($dbh,$id_module,$conf); + my @id_agents; + + if (defined $use_alias and $use_alias eq 'use_alias') { + @id_agents = get_agent_ids_from_alias($dbh,$agent_name); + my $id_agent; + + foreach my $id (@id_agents) { + print_log "[INFO] Deleting module '$module_name' from agent '$id->{'nombre'}' \n\n"; + + $id_agent = $id->{'id_agente'}; + exist_check($id_agent,'agent',$agent_name); + my $id_module = get_agent_module_id($dbh,$module_name,$id_agent); + if ($id_module == -1) { + next; + } + + pandora_delete_module($dbh,$id_module,$conf); + } + } else { + print_log "[INFO] Deleting module '$module_name' from agent '$agent_name' \n\n"; + + my $id_agent = get_agent_id($dbh,$agent_name); + exist_check($id_agent,'agent',$agent_name); + my $id_module = get_agent_module_id($dbh,$module_name,$id_agent); + exist_check($id_module,'module',$module_name); + + pandora_delete_module($dbh,$id_module,$conf); + } } ############################################################################## @@ -2370,18 +2670,40 @@ sub cli_delete_not_policy_modules() { ############################################################################## sub cli_create_template_module() { - my ($template_name,$module_name,$agent_name) = @ARGV[2..4]; + my ($template_name,$module_name,$agent_name, $use_alias) = @ARGV[2..5]; - print_log "[INFO] Adding template '$template_name' to module '$module_name' from agent '$agent_name' \n\n"; - - my $id_agent = get_agent_id($dbh,$agent_name); - exist_check($id_agent,'agent',$agent_name); - my $module_id = get_agent_module_id($dbh,$module_name,$id_agent); - exist_check($module_id,'module',$module_name); - my $template_id = get_template_id($dbh,$template_name); - exist_check($template_id,'template',$template_name); - - pandora_create_template_module ($conf, $dbh, $module_id, $template_id); + my @id_agents; + + if (defined $use_alias and $use_alias eq 'use_alias') { + @id_agents = get_agent_ids_from_alias($dbh,$agent_name); + + foreach my $id (@id_agents) { + print_log "[INFO] Adding template '$template_name' to module '$module_name' from agent '$agent_name' \n\n"; + + my $id_agent = $id->{'id_agente'}; + exist_check($id_agent,'agent',$agent_name); + my $module_id = get_agent_module_id($dbh,$module_name,$id_agent); + if ($module_id == -1) { + next; + } + + my $template_id = get_template_id($dbh,$template_name); + exist_check($template_id,'template',$template_name); + + pandora_create_template_module ($conf, $dbh, $module_id, $template_id); + } + } else { + print_log "[INFO] Adding template '$template_name' to module '$module_name' from agent '$agent_name' \n\n"; + + my $id_agent = get_agent_id($dbh,$agent_name); + exist_check($id_agent,'agent',$agent_name); + my $module_id = get_agent_module_id($dbh,$module_name,$id_agent); + exist_check($module_id,'module',$module_name); + my $template_id = get_template_id($dbh,$template_name); + exist_check($template_id,'template',$template_name); + + pandora_create_template_module ($conf, $dbh, $module_id, $template_id); + } } ############################################################################## @@ -2390,21 +2712,45 @@ sub cli_create_template_module() { ############################################################################## sub cli_delete_template_module() { - my ($template_name,$module_name,$agent_name) = @ARGV[2..4]; + my ($template_name,$module_name,$agent_name, $use_alias) = @ARGV[2..5]; - print_log "[INFO] Delete template '$template_name' from module '$module_name' from agent '$agent_name' \n\n"; + if (defined $use_alias and $use_alias eq 'use_alias') { + my @id_agents = get_agent_ids_from_alias($dbh,$agent_name); - my $id_agent = get_agent_id($dbh,$agent_name); - exist_check($id_agent,'agent',$agent_name); - my $module_id = get_agent_module_id($dbh,$module_name,$id_agent); - exist_check($module_id,'module',$module_name); - my $template_id = get_template_id($dbh,$template_name); - exist_check($template_id,'template',$template_name); + my $id_agent; - my $template_module_id = get_template_module_id($dbh, $module_id, $template_id); - exist_check($template_module_id,"template '$template_name' on module",$module_name); + foreach my $id (@id_agents) { + print_log "[INFO] Delete template '$template_name' from module '$module_name' from agent '$agent_name' \n\n"; + + $id_agent = $id->{'id_agente'}; + exist_check($id_agent,'agent',$agent_name); + my $module_id = get_agent_module_id($dbh,$module_name,$id_agent); + if ($module_id eq -1) { + next; + } + my $template_id = get_template_id($dbh,$template_name); + exist_check($template_id,'template',$template_name); + + my $template_module_id = get_template_module_id($dbh, $module_id, $template_id); + exist_check($template_module_id,"template '$template_name' on module",$module_name); - pandora_delete_template_module ($dbh, $template_module_id); + pandora_delete_template_module ($dbh, $template_module_id); + } + } else { + print_log "[INFO] Delete template '$template_name' from module '$module_name' from agent '$agent_name' \n\n"; + + my $id_agent = get_agent_id($dbh,$agent_name); + exist_check($id_agent,'agent',$agent_name); + my $module_id = get_agent_module_id($dbh,$module_name,$id_agent); + exist_check($module_id,'module',$module_name); + my $template_id = get_template_id($dbh,$template_name); + exist_check($template_id,'template',$template_name); + + my $template_module_id = get_template_module_id($dbh, $module_id, $template_id); + exist_check($template_module_id,"template '$template_name' on module",$module_name); + + pandora_delete_template_module ($dbh, $template_module_id); + } } ############################################################################## @@ -2413,32 +2759,67 @@ sub cli_delete_template_module() { ############################################################################## sub cli_create_template_action() { - my ($action_name,$template_name,$module_name,$agent_name,$fires_min,$fires_max) = @ARGV[2..7]; + my ($action_name,$template_name,$module_name,$agent_name,$fires_min,$fires_max, $use_alias) = @ARGV[2..8]; - print_log "[INFO] Adding action '$action_name' to template '$template_name' in module '$module_name' from agent '$agent_name' with $fires_min min. fires and $fires_max max. fires\n\n"; - - my $id_agent = get_agent_id($dbh,$agent_name); - exist_check($id_agent,'agent',$agent_name); - my $module_id = get_agent_module_id($dbh,$module_name,$id_agent); - exist_check($module_id,'module',$module_name); - my $template_id = get_template_id($dbh,$template_name); - exist_check($template_id,'template',$template_name); - my $template_module_id = get_template_module_id($dbh,$module_id,$template_id); - exist_check($template_module_id,'template module',$template_name); - my $action_id = get_action_id($dbh,safe_input($action_name)); - exist_check($action_id,'action',$action_name); - - $fires_min = 0 unless defined ($fires_min); - $fires_max = 0 unless defined ($fires_max); - - my %parameters; - - $parameters{'id_alert_template_module'} = $template_module_id; - $parameters{'id_alert_action'} = $action_id; - $parameters{'fires_min'} = $fires_min; - $parameters{'fires_max'} = $fires_max; - - pandora_create_template_module_action ($conf, \%parameters, $dbh); + my @id_agents; + + if (defined $use_alias and $use_alias eq 'use_alias') { + @id_agents = get_agent_ids_from_alias($dbh,$agent_name); + + foreach my $id (@id_agents) { + print_log "[INFO] Adding action '$action_name' to template '$template_name' in module '$module_name' from agent '$agent_name' with $fires_min min. fires and $fires_max max. fires\n\n"; + + my $id_agent = $id->{'id_agente'}; + exist_check($id_agent,'agent',$agent_name); + my $module_id = get_agent_module_id($dbh,$module_name,$id_agent); + if ($module_id eq -1) { + next; + } + my $template_id = get_template_id($dbh,$template_name); + exist_check($template_id,'template',$template_name); + my $template_module_id = get_template_module_id($dbh,$module_id,$template_id); + exist_check($template_module_id,'template module',$template_name); + my $action_id = get_action_id($dbh,safe_input($action_name)); + exist_check($action_id,'action',$action_name); + + $fires_min = 0 unless defined ($fires_min); + $fires_max = 0 unless defined ($fires_max); + + my %parameters; + + $parameters{'id_alert_template_module'} = $template_module_id; + $parameters{'id_alert_action'} = $action_id; + $parameters{'fires_min'} = $fires_min; + $parameters{'fires_max'} = $fires_max; + + pandora_create_template_module_action ($conf, \%parameters, $dbh); + } + } else { + print_log "[INFO] Adding action '$action_name' to template '$template_name' in module '$module_name' from agent '$agent_name' with $fires_min min. fires and $fires_max max. fires\n\n"; + + my $id_agent = get_agent_id($dbh,$agent_name); + exist_check($id_agent,'agent',$agent_name); + my $module_id = get_agent_module_id($dbh,$module_name,$id_agent); + exist_check($module_id,'module',$module_name); + my $template_id = get_template_id($dbh,$template_name); + exist_check($template_id,'template',$template_name); + my $template_module_id = get_template_module_id($dbh,$module_id,$template_id); + exist_check($template_module_id,'template module',$template_name); + my $action_id = get_action_id($dbh,safe_input($action_name)); + exist_check($action_id,'action',$action_name); + + $fires_min = 0 unless defined ($fires_min); + $fires_max = 0 unless defined ($fires_max); + + my %parameters; + + $parameters{'id_alert_template_module'} = $template_module_id; + $parameters{'id_alert_action'} = $action_id; + $parameters{'fires_min'} = $fires_min; + $parameters{'fires_max'} = $fires_max; + + pandora_create_template_module_action ($conf, \%parameters, $dbh); + } } ############################################################################## @@ -2447,22 +2828,45 @@ sub cli_create_template_action() { ############################################################################## sub cli_delete_template_action() { - my ($action_name,$template_name,$module_name,$agent_name) = @ARGV[2..5]; + my ($action_name,$template_name,$module_name,$agent_name, $use_alias) = @ARGV[2..6]; - print_log "[INFO] Deleting action '$action_name' from template '$template_name' in module '$module_name' from agent '$agent_name')\n\n"; + if (defined $use_alias and $use_alias eq 'use_alias') { + my @id_agents = get_agent_ids_from_alias($dbh,$agent_name); - my $id_agent = get_agent_id($dbh,$agent_name); - exist_check($id_agent,'agent',$agent_name); - my $module_id = get_agent_module_id($dbh,$module_name,$id_agent); - exist_check($module_id,'module',$module_name); - my $template_id = get_template_id($dbh,$template_name); - exist_check($template_id,'template',$template_name); - my $template_module_id = get_template_module_id($dbh,$module_id,$template_id); - exist_check($template_module_id,'template module',$template_name); - my $action_id = get_action_id($dbh,safe_input($action_name)); - exist_check($action_id,'action',$action_name); + foreach my $id (@id_agents) { + print_log "[INFO] Deleting action '$action_name' from template '$template_name' in module '$module_name' from agent '$agent_name')\n\n"; - pandora_delete_template_module_action ($dbh, $template_module_id, $action_id); + my $id_agent = $id->{'id_agente'}; + exist_check($id_agent,'agent',$agent_name); + my $module_id = get_agent_module_id($dbh,$module_name,$id_agent); + if ($module_id eq -1) { + next; + } + my $template_id = get_template_id($dbh,$template_name); + exist_check($template_id,'template',$template_name); + my $template_module_id = get_template_module_id($dbh,$module_id,$template_id); + exist_check($template_module_id,'template module',$template_name); + my $action_id = get_action_id($dbh,safe_input($action_name)); + exist_check($action_id,'action',$action_name); + + pandora_delete_template_module_action ($dbh, $template_module_id, $action_id); + } + } else { + print_log "[INFO] Deleting action '$action_name' from template '$template_name' in module '$module_name' from agent '$agent_name')\n\n"; + + my $id_agent = get_agent_id($dbh,$agent_name); + exist_check($id_agent,'agent',$agent_name); + my $module_id = get_agent_module_id($dbh,$module_name,$id_agent); + exist_check($module_id,'module',$module_name); + my $template_id = get_template_id($dbh,$template_name); + exist_check($template_id,'template',$template_name); + my $template_module_id = get_template_module_id($dbh,$module_id,$template_id); + exist_check($template_module_id,'template module',$template_name); + my $action_id = get_action_id($dbh,safe_input($action_name)); + exist_check($action_id,'action',$action_name); + + pandora_delete_template_module_action ($dbh, $template_module_id, $action_id); + } } ############################################################################## @@ -2471,9 +2875,11 @@ sub cli_delete_template_action() { ############################################################################## sub cli_data_module() { - my ($server_name,$agent_name,$module_name,$module_type,$module_new_data,$datetime) = @ARGV[2..7]; + my ($server_name,$agent_name,$module_name,$module_type,$module_new_data,$datetime,$use_alias) = @ARGV[2..8]; my $utimestamp; + my @id_agents; + if(defined($datetime)) { if ($datetime !~ /([0-9]{2,4})\-([0-1][0-9])\-([0-3][0-9]) +([0-2][0-9]):([0-5][0-9])/) { print_log "[ERROR] Invalid datetime $datetime. (Correct format: YYYY-MM-DD HH:mm)\n"; @@ -2490,29 +2896,58 @@ sub cli_data_module() { # The get_module_id has wrong name. Change in future my $module_type_id = get_module_id($dbh,$module_type); exist_check($module_type_id,'module type',$module_type); - - my $id_agent = get_agent_id($dbh,$agent_name); - exist_check($id_agent,'agent',$agent_name); - - my $id_module = get_agent_module_id($dbh, $module_name, $id_agent); - exist_check($id_module, 'module name', $module_name); - + # Server_type 0 is dataserver my $server_id = get_server_id($dbh,$server_name,0); exist_check($server_id,'data server',$server_name); - - my $module = get_db_single_row ($dbh, 'SELECT * FROM tagente_modulo WHERE id_agente_modulo = ? AND id_tipo_modulo = ?', $id_module, $id_agent, $module_type_id); - - if(not defined($module->{'module_interval'})) { - print_log "[ERROR] No module found with this type. \n\n"; - exit; + + if (defined $use_alias and $use_alias eq 'use_alias') { + @id_agents = get_agent_ids_from_alias($dbh,$agent_name); + + my $id_agent; + + foreach my $id (@id_agents) { + $id_agent = $id->{'id_agente'}; + exist_check($id_agent,'agent',$agent_name); + + my $id_module = get_agent_module_id($dbh, $module_name, $id_agent); + if ($id_module == -1) { + next; + } + + my $module = get_db_single_row ($dbh, 'SELECT * FROM tagente_modulo WHERE id_agente_modulo = ? AND id_tipo_modulo = ?', $id_module, $module_type_id); + + if(not defined($module->{'module_interval'})) { + print_log "[ERROR] No module found with this type. \n\n"; + exit; + } + + my %data = ('data' => $module_new_data); + + pandora_process_module ($conf, \%data, '', $module, $module_type, '', $utimestamp, $server_id, $dbh); + + print_log "[INFO] Inserting data to module '$module_name'\n\n"; + } + } else { + my $id_agent = get_agent_id($dbh,$agent_name); + exist_check($id_agent,'agent',$agent_name); + + my $id_module = get_agent_module_id($dbh, $module_name, $id_agent); + exist_check($id_module, 'module name', $module_name); + + my $module = get_db_single_row ($dbh, 'SELECT * FROM tagente_modulo WHERE id_agente_modulo = ? AND id_tipo_modulo = ?', $id_module, $module_type_id); + + if(not defined($module->{'module_interval'})) { + print_log "[ERROR] No module found with this type. \n\n"; + exit; + } + + my %data = ('data' => $module_new_data); + + pandora_process_module ($conf, \%data, '', $module, $module_type, '', $utimestamp, $server_id, $dbh); + + print_log "[INFO] Inserting data to module '$module_name'\n\n"; } - - my %data = ('data' => $module_new_data); - - pandora_process_module ($conf, \%data, '', $module, $module_type, '', $utimestamp, $server_id, $dbh); - - print_log "[INFO] Inserting data to module '$module_name'\n\n"; } ############################################################################## @@ -2578,10 +3013,20 @@ sub cli_user_update() { ############################################################################## sub cli_agent_update() { - my ($agent_name,$field,$new_value) = @ARGV[2..4]; - - my $id_agent = get_agent_id($dbh,$agent_name); - exist_check($id_agent,'agent',$agent_name); + my ($agent_name,$field,$new_value,$use_alias) = @ARGV[2..5]; + + my @id_agents; + my $id_agent; + + if (defined $use_alias and $use_alias eq 'use_alias') { + @id_agents = get_agent_ids_from_alias($dbh,$agent_name); + foreach my $id (@id_agents) { + exist_check($id->{'id_agente'},'agent',$agent_name); + } + } else { + $id_agent = get_agent_id($dbh,$agent_name); + exist_check($id_agent,'agent',$agent_name); + } # agent_name, address, description, group_name, interval, os_name, disabled, parent_name, cascade_protection, icon_path, update_gis_data, custom_id @@ -2629,7 +3074,13 @@ sub cli_agent_update() { } # Add the address to the agent - add_new_address_agent ($dbh, $address_id, $id_agent); + if (defined $use_alias and $use_alias eq 'use_alias') { + foreach my $id (@id_agents) { + add_new_address_agent ($dbh, $address_id, $id->{'id_agente'}); + } + } else { + add_new_address_agent ($dbh, $address_id, $id_agent); + } $field = 'direccion'; } @@ -2637,14 +3088,24 @@ sub cli_agent_update() { print_log "[ERROR] Field '$field' doesnt exist\n\n"; exit; } - - print_log "[INFO] Updating field '$field' in agent '$agent_name'\n\n"; + + if (defined $use_alias and $use_alias eq 'use_alias') { + print_log "[INFO] Updating field '$field' in agents with alias '$agent_name'\n\n"; + } else { + print_log "[INFO] Updating field '$field' in agent '$agent_name'\n\n"; + } my $update; $update->{$field} = $new_value; - pandora_update_table_from_hash ($conf, $update, 'id_agente', safe_input($id_agent), 'tagente', $dbh); + if (defined $use_alias and $use_alias eq 'use_alias') { + foreach my $id (@id_agents) { + pandora_update_table_from_hash ($conf, $update, 'id_agente', safe_input($id->{'id_agente'}), 'tagente', $dbh); + } + } else { + pandora_update_table_from_hash ($conf, $update, 'id_agente', safe_input($id_agent), 'tagente', $dbh); + } } ############################################################################## @@ -2901,150 +3362,306 @@ sub pandora_check_plugin_module_fields($) { ############################################################################## sub cli_module_update() { - my ($module_name,$agent_name,$field,$new_value) = @ARGV[2..5]; - - my $id_agent = get_agent_id($dbh,$agent_name); - exist_check($id_agent,'agent',$agent_name); - my $id_agent_module = get_agent_module_id ($dbh, $module_name, $id_agent); - exist_check($id_agent_module,'agent module',$module_name); - - # Check and adjust parameters in common values - - if($field eq 'min' || $field eq 'max' || $field eq 'post_process' || $field eq 'history_data') { - # Fields admited, no changes - } - elsif($field eq 'interval') { - $field = 'module_interval'; - } - elsif($field eq 'warning_min') { - $field = 'min_warning'; - } - elsif($field eq 'warning_max') { - $field = 'max_warning'; - } - elsif($field eq 'critical_min') { - $field = 'min_critical'; - } - elsif($field eq 'critical_max') { - $field = 'max_critical'; - } - elsif($field eq 'warning_str') { - $field = 'str_warning'; - $new_value = safe_input($new_value); - } - elsif($field eq 'critical_str') { - $field = 'str_critical'; - $new_value = safe_input($new_value); - } - elsif($field eq 'agent_name') { - my $id_agent_change = get_agent_id($dbh,$new_value); - exist_check($id_agent_change,'agent',$new_value); - my $id_agent_module_exist = get_agent_module_id ($dbh, $module_name, $id_agent_change); - if($id_agent_module_exist != -1) { - print_log "[ERROR] A module called '$module_name' already exist in the agent '$new_value'\n\n"; - exit; - } - $field = 'id_agente'; - $new_value = $id_agent_change; - } - elsif ($field eq 'module_name') { - my $id_agent_module_change = get_agent_module_id ($dbh, $new_value, $id_agent); - if ($id_agent_module_change != -1) { - print_log "[ERROR] A module called '$new_value' already exist in the agent '$agent_name'\n\n"; - exit; - } - $field = 'nombre'; - $new_value = safe_input($new_value); - } - elsif ($field eq 'description') { - $field = 'descripcion'; - $new_value = safe_input($new_value); - } - elsif ($field eq 'module_group') { - my $module_group_id = get_module_group_id($dbh,$new_value); - - if ($module_group_id == -1) { - print_log "[ERROR] Module group '$new_value' doesnt exist\n\n"; - exit; - } - $field = 'id_module_group'; - $new_value = $module_group_id; - } - elsif ($field eq 'enable_unknown_events') { - my $disabled_types_event = {}; - if ($new_value) { - $disabled_types_event->{'going_unknown'} = 0; - } - else { - $disabled_types_event->{'going_unknown'} = 1; - } - $field = 'disabled_types_event'; - $new_value = encode_json($disabled_types_event); - } - elsif ($field eq 'ff_threshold') { - $field = 'min_ff_event'; - } - elsif ($field eq 'each_ff') { - $field = 'each_ff'; - } - elsif ($field eq 'ff_threshold_normal') { - $field = 'min_ff_event_normal'; - } - elsif ($field eq 'ff_threshold_warning') { - $field = 'min_ff_event_warning'; - } - elsif ($field eq 'ff_threshold_critical') { - $field = 'min_ff_event_critical'; - } - elsif ($field eq 'critical_instructions') { - $field = 'critical_instructions'; - } - elsif ($field eq 'warning_instructions') { - $field = 'warning_instructions'; - } - elsif ($field eq 'unknown_instructions') { - $field = 'unknown_instructions'; - } - else { - # If is not a common value, check type and call type update funtion - my $type = pandora_get_module_type($dbh,$id_agent_module); - - my %field_value; - $field_value{'field'} = $field; - $field_value{'new_value'} = $new_value; - - if($type eq 'data') { - pandora_check_data_module_fields(\%field_value); - } - elsif($type eq 'network') { - pandora_check_network_module_fields(\%field_value); - } - elsif($type eq 'snmp') { - pandora_check_snmp_module_fields(\%field_value); - } - elsif($type eq 'plugin') { - pandora_check_plugin_module_fields(\%field_value); - } - else { - print_log "[ERROR] The field '$field' is not available for this type of module\n\n"; - } - - $field = $field_value{'field'}; - $new_value = $field_value{'new_value'}; - } - - print_log "[INFO] Updating field '$field' in module '$module_name' of agent '$agent_name' with new value '$new_value'\n\n"; - - my $update; - - $update->{$field} = $new_value; + my ($module_name,$agent_name,$field,$new_value, $use_alias) = @ARGV[2..6]; - my $policy_id = enterprise_hook('get_id_policy_module_agent_module',[$dbh, safe_input($id_agent_module)]); - if ( $policy_id > 0) { - $update->{policy_linked} = 0; + if (defined $use_alias and $use_alias eq 'use_alias') { + my @id_agents = get_agent_ids_from_alias($dbh,$agent_name); + + my $save_initial_field = $field; + my $save_new_value = $new_value; + + foreach my $id (@id_agents) { + $field = $save_initial_field; + $new_value = $save_new_value; + my $id_agent = $id->{'id_agente'}; + exist_check($id_agent,'agent',$agent_name); + my $id_agent_module = get_agent_module_id ($dbh, $module_name, $id_agent); + if ($id_agent_module == -1) { + next; + } + + # Check and adjust parameters in common values + + if($field eq 'min' || $field eq 'max' || $field eq 'post_process' || $field eq 'history_data') { + # Fields admited, no changes + } + elsif($field eq 'interval') { + $field = 'module_interval'; + } + elsif($field eq 'warning_min') { + $field = 'min_warning'; + } + elsif($field eq 'warning_max') { + $field = 'max_warning'; + } + elsif($field eq 'critical_min') { + $field = 'min_critical'; + } + elsif($field eq 'critical_max') { + $field = 'max_critical'; + } + elsif($field eq 'warning_str') { + $field = 'str_warning'; + $new_value = safe_input($new_value); + } + elsif($field eq 'critical_str') { + $field = 'str_critical'; + $new_value = safe_input($new_value); + } + elsif($field eq 'agent_name') { + my $id_agent_change = get_agent_id($dbh,$new_value); + exist_check($id_agent_change,'agent',$new_value); + my $id_agent_module_exist = get_agent_module_id ($dbh, $module_name, $id_agent_change); + if($id_agent_module_exist != -1) { + print_log "[ERROR] A module called '$module_name' already exist in the agent '$new_value'\n\n"; + exit; + } + $field = 'id_agente'; + $new_value = $id_agent_change; + } + elsif ($field eq 'module_name') { + my $id_agent_module_change = get_agent_module_id ($dbh, $new_value, $id_agent); + if ($id_agent_module_change != -1) { + print_log "[ERROR] A module called '$new_value' already exist in the agent '$agent_name'\n\n"; + exit; + } + $field = 'nombre'; + $new_value = safe_input($new_value); + } + elsif ($field eq 'description') { + $field = 'descripcion'; + $new_value = safe_input($new_value); + } + elsif ($field eq 'module_group') { + my $module_group_id = get_module_group_id($dbh,$new_value); + + if ($module_group_id == -1) { + print_log "[ERROR] Module group '$new_value' doesnt exist\n\n"; + exit; + } + $field = 'id_module_group'; + $new_value = $module_group_id; + } + elsif ($field eq 'enable_unknown_events') { + my $disabled_types_event = {}; + if ($new_value) { + $disabled_types_event->{'going_unknown'} = 0; + } + else { + $disabled_types_event->{'going_unknown'} = 1; + } + $field = 'disabled_types_event'; + $new_value = encode_json($disabled_types_event); + } + elsif ($field eq 'ff_threshold') { + $field = 'min_ff_event'; + } + elsif ($field eq 'each_ff') { + $field = 'each_ff'; + } + elsif ($field eq 'ff_threshold_normal') { + $field = 'min_ff_event_normal'; + } + elsif ($field eq 'ff_threshold_warning') { + $field = 'min_ff_event_warning'; + } + elsif ($field eq 'ff_threshold_critical') { + $field = 'min_ff_event_critical'; + } + elsif ($field eq 'critical_instructions') { + $field = 'critical_instructions'; + } + elsif ($field eq 'warning_instructions') { + $field = 'warning_instructions'; + } + elsif ($field eq 'unknown_instructions') { + $field = 'unknown_instructions'; + } + else { + # If is not a common value, check type and call type update funtion + my $type = pandora_get_module_type($dbh,$id_agent_module); + print("TYPE EN ELSE".$type); + my %field_value; + $field_value{'field'} = $field; + $field_value{'new_value'} = $new_value; + + if($type eq 'data') { + pandora_check_data_module_fields(\%field_value); + } + elsif($type eq 'network') { + pandora_check_network_module_fields(\%field_value); + } + elsif($type eq 'snmp') { + pandora_check_snmp_module_fields(\%field_value); + } + elsif($type eq 'plugin') { + pandora_check_plugin_module_fields(\%field_value); + } + else { + print_log "[ERROR] The field '$field' is not available for this type of module\n\n"; + } + + $field = $field_value{'field'}; + $new_value = $field_value{'new_value'}; + } + + print_log "[INFO] Updating field '$field' in module '$module_name' of agent '$agent_name' with new value '$new_value'\n\n"; + + my $update; + + $update->{$field} = $new_value; + + my $policy_id = enterprise_hook('get_id_policy_module_agent_module',[$dbh, safe_input($id_agent_module)]); + if ( $policy_id > 0) { + $update->{policy_linked} = 0; + } + + pandora_update_module_from_hash ($conf, $update, 'id_agente_modulo', $id_agent_module, $dbh); + } + } else { + my $id_agent = get_agent_id($dbh,$agent_name); + exist_check($id_agent,'agent',$agent_name); + my $id_agent_module = get_agent_module_id ($dbh, $module_name, $id_agent); + exist_check($id_agent_module,'agent module',$module_name); + + # Check and adjust parameters in common values + + if($field eq 'min' || $field eq 'max' || $field eq 'post_process' || $field eq 'history_data') { + # Fields admited, no changes + } + elsif($field eq 'interval') { + $field = 'module_interval'; + } + elsif($field eq 'warning_min') { + $field = 'min_warning'; + } + elsif($field eq 'warning_max') { + $field = 'max_warning'; + } + elsif($field eq 'critical_min') { + $field = 'min_critical'; + } + elsif($field eq 'critical_max') { + $field = 'max_critical'; + } + elsif($field eq 'warning_str') { + $field = 'str_warning'; + $new_value = safe_input($new_value); + } + elsif($field eq 'critical_str') { + $field = 'str_critical'; + $new_value = safe_input($new_value); + } + elsif($field eq 'agent_name') { + my $id_agent_change = get_agent_id($dbh,$new_value); + exist_check($id_agent_change,'agent',$new_value); + my $id_agent_module_exist = get_agent_module_id ($dbh, $module_name, $id_agent_change); + if($id_agent_module_exist != -1) { + print_log "[ERROR] A module called '$module_name' already exist in the agent '$new_value'\n\n"; + exit; + } + $field = 'id_agente'; + $new_value = $id_agent_change; + } + elsif ($field eq 'module_name') { + my $id_agent_module_change = get_agent_module_id ($dbh, $new_value, $id_agent); + if ($id_agent_module_change != -1) { + print_log "[ERROR] A module called '$new_value' already exist in the agent '$agent_name'\n\n"; + exit; + } + $field = 'nombre'; + $new_value = safe_input($new_value); + } + elsif ($field eq 'description') { + $field = 'descripcion'; + $new_value = safe_input($new_value); + } + elsif ($field eq 'module_group') { + my $module_group_id = get_module_group_id($dbh,$new_value); + + if ($module_group_id == -1) { + print_log "[ERROR] Module group '$new_value' doesnt exist\n\n"; + exit; + } + $field = 'id_module_group'; + $new_value = $module_group_id; + } + elsif ($field eq 'enable_unknown_events') { + my $disabled_types_event = {}; + if ($new_value) { + $disabled_types_event->{'going_unknown'} = 0; + } + else { + $disabled_types_event->{'going_unknown'} = 1; + } + $field = 'disabled_types_event'; + $new_value = encode_json($disabled_types_event); + } + elsif ($field eq 'ff_threshold') { + $field = 'min_ff_event'; + } + elsif ($field eq 'each_ff') { + $field = 'each_ff'; + } + elsif ($field eq 'ff_threshold_normal') { + $field = 'min_ff_event_normal'; + } + elsif ($field eq 'ff_threshold_warning') { + $field = 'min_ff_event_warning'; + } + elsif ($field eq 'ff_threshold_critical') { + $field = 'min_ff_event_critical'; + } + elsif ($field eq 'critical_instructions') { + $field = 'critical_instructions'; + } + elsif ($field eq 'warning_instructions') { + $field = 'warning_instructions'; + } + elsif ($field eq 'unknown_instructions') { + $field = 'unknown_instructions'; + } + else { + # If is not a common value, check type and call type update funtion + my $type = pandora_get_module_type($dbh,$id_agent_module); + + my %field_value; + $field_value{'field'} = $field; + $field_value{'new_value'} = $new_value; + + if($type eq 'data') { + pandora_check_data_module_fields(\%field_value); + } + elsif($type eq 'network') { + pandora_check_network_module_fields(\%field_value); + } + elsif($type eq 'snmp') { + pandora_check_snmp_module_fields(\%field_value); + } + elsif($type eq 'plugin') { + pandora_check_plugin_module_fields(\%field_value); + } + else { + print_log "[ERROR] The field '$field' is not available for this type of module\n\n"; + } + + $field = $field_value{'field'}; + $new_value = $field_value{'new_value'}; + } + + print_log "[INFO] Updating field '$field' in module '$module_name' of agent '$agent_name' with new value '$new_value'\n\n"; + + my $update; + + $update->{$field} = $new_value; + + my $policy_id = enterprise_hook('get_id_policy_module_agent_module',[$dbh, safe_input($id_agent_module)]); + if ( $policy_id > 0) { + $update->{policy_linked} = 0; + } + + pandora_update_module_from_hash ($conf, $update, 'id_agente_modulo', $id_agent_module, $dbh); } - - pandora_update_module_from_hash ($conf, $update, 'id_agente_modulo', $id_agent_module, $dbh); } ############################################################################## @@ -3401,7 +4018,7 @@ sub cli_delete_profile() { ############################################################################## sub cli_create_event() { - my ($event,$event_type,$group_name,$agent_name,$module_name,$event_status,$severity,$template_name, $user_name, $comment, $source, $id_extra, $tags, $custom_data,$force_create_agent,$c_instructions,$w_instructions,$u_instructions) = @ARGV[2..19]; + my ($event,$event_type,$group_name,$agent_name,$module_name,$event_status,$severity,$template_name, $user_name, $comment, $source, $id_extra, $tags, $custom_data,$force_create_agent,$c_instructions,$w_instructions,$u_instructions, $use_alias) = @ARGV[2..20]; $event_status = 0 unless defined($event_status); $severity = 0 unless defined($severity); @@ -3427,58 +4044,108 @@ sub cli_create_event() { } my $id_agent; - - if (! $agent_name) { - $id_agent = 0; - } - else { - $id_agent = get_agent_id($dbh,$agent_name); - # exist_check($id_agent,'agent',$agent_name); - if($id_agent == -1){ - if($force_create_agent == 1){ - pandora_create_agent ($conf, '', $agent_name, '', '', '', '', 'Created by cli_create_event', '', $dbh); - print_log "[INFO] Adding agent '$agent_name' \n\n"; - $id_agent = get_agent_id($dbh,$agent_name); + + if (defined $use_alias and $use_alias eq 'use_alias') { + + my @id_agents = get_agent_ids_from_alias($dbh,$agent_name); + + foreach my $id (@id_agents) { + if (! $agent_name) { + $id_agent = 0; } - else{ - exist_check($id_agent,'agent',$agent_name); + else { + $id_agent = $id->{'id_agente'}; } + + my $id_agentmodule; + + if (! $module_name) { + $id_agentmodule = 0; + } + else { + $id_agentmodule = get_agent_module_id($dbh,$module_name,$id_agent); + if ($id_agentmodule eq -1) { + next; + } + } + + my $id_alert_agent_module; + + if(defined($template_name) && $template_name ne '') { + my $id_template = get_template_id($dbh,$template_name); + exist_check($id_template,'template',$template_name); + $id_alert_agent_module = get_template_module_id($dbh,$id_agentmodule,$id_template); + exist_check($id_alert_agent_module,'alert template module',$template_name); + } + else { + $id_alert_agent_module = 0; + } + + if (defined($comment) && $comment ne '') { + $comment = '-- Added comment by '.$user_name. ' ['. localtime(time).'] --
'.$comment.'
'; + } + print_log "[INFO] Adding event '$event' for agent '$agent_name' \n\n"; + + # Base64 encode custom data + $custom_data = encode_base64 ($custom_data); + + pandora_event ($conf, $event, $id_group, $id_agent, $severity, + $id_alert_agent_module, $id_agentmodule, $event_type, $event_status, $dbh, $source, $user_name, $comment, $id_extra, $tags, $c_instructions, $w_instructions, $u_instructions, $custom_data); } + } else { + if (! $agent_name) { + $id_agent = 0; + } + else { + $id_agent = get_agent_id($dbh,$agent_name); + # exist_check($id_agent,'agent',$agent_name); + if($id_agent == -1){ + if($force_create_agent == 1){ + pandora_create_agent ($conf, '', $agent_name, '', '', '', '', 'Created by cli_create_event', '', $dbh); + print_log "[INFO] Adding agent '$agent_name' \n\n"; + $id_agent = get_agent_id($dbh,$agent_name); + } + else{ + exist_check($id_agent,'agent',$agent_name); + } + } + + } + + my $id_agentmodule; + + if (! $module_name) { + $id_agentmodule = 0; + } + else { + $id_agentmodule = get_agent_module_id($dbh,$module_name,$id_agent); + exist_check($id_agentmodule,'module',$module_name); + } + + my $id_alert_agent_module; + + if(defined($template_name) && $template_name ne '') { + my $id_template = get_template_id($dbh,$template_name); + exist_check($id_template,'template',$template_name); + $id_alert_agent_module = get_template_module_id($dbh,$id_agentmodule,$id_template); + exist_check($id_alert_agent_module,'alert template module',$template_name); + } + else { + $id_alert_agent_module = 0; + } + + if (defined($comment) && $comment ne '') { + $comment = '-- Added comment by '.$user_name. ' ['. localtime(time).'] --
'.$comment.'
'; + } + print_log "[INFO] Adding event '$event' for agent '$agent_name' \n\n"; + + # Base64 encode custom data + $custom_data = encode_base64 ($custom_data); + + pandora_event ($conf, $event, $id_group, $id_agent, $severity, + $id_alert_agent_module, $id_agentmodule, $event_type, $event_status, $dbh, $source, $user_name, $comment, $id_extra, $tags, $c_instructions, $w_instructions, $u_instructions, $custom_data); } - - my $id_agentmodule; - - if (! $module_name) { - $id_agentmodule = 0; - } - else { - $id_agentmodule = get_agent_module_id($dbh,$module_name,$id_agent); - exist_check($id_agentmodule,'module',$module_name); - } - - my $id_alert_agent_module; - - if(defined($template_name) && $template_name ne '') { - my $id_template = get_template_id($dbh,$template_name); - exist_check($id_template,'template',$template_name); - $id_alert_agent_module = get_template_module_id($dbh,$id_agentmodule,$id_template); - exist_check($id_alert_agent_module,'alert template module',$template_name); - } - else { - $id_alert_agent_module = 0; - } - - if (defined($comment) && $comment ne '') { - $comment = '-- Added comment by '.$user_name. ' ['. localtime(time).'] --
'.$comment.'
'; - } - print_log "[INFO] Adding event '$event' for agent '$agent_name' \n\n"; - - # Base64 encode custom data - $custom_data = encode_base64 ($custom_data); - - pandora_event ($conf, $event, $id_group, $id_agent, $severity, - $id_alert_agent_module, $id_agentmodule, $event_type, $event_status, $dbh, $source, $user_name, $comment, $id_extra, $tags, $c_instructions, $w_instructions, $u_instructions, $custom_data); } ############################################################################## @@ -3487,20 +4154,10 @@ sub cli_create_event() { ############################################################################## sub cli_validate_event() { - my ($agent_name, $module_name, $datetime_min, $datetime_max, $user_name, $criticity, $template_name) = @ARGV[2..8]; + my ($agent_name, $module_name, $datetime_min, $datetime_max, $user_name, $criticity, $template_name, $use_alias) = @ARGV[2..9]; my $id_agent = ''; my $id_agentmodule = ''; - if(defined($agent_name) && $agent_name ne '') { - $id_agent = get_agent_id($dbh,$agent_name); - exist_check($id_agent,'agent',$agent_name); - - if($module_name ne '') { - $id_agentmodule = get_agent_module_id($dbh, $module_name, $id_agent); - exist_check($id_agentmodule,'module',$module_name); - } - } - if(defined($datetime_min) && $datetime_min ne '') { if ($datetime_min !~ /([0-9]{2,4})\-([0-1][0-9])\-([0-3][0-9]) +([0-2][0-9]):([0-5][0-9])/) { print_log "[ERROR] Invalid datetime_min format. (Correct format: YYYY-MM-DD HH:mm)\n"; @@ -3519,17 +4176,57 @@ sub cli_validate_event() { $datetime_max .= ":00"; } - my $id_alert_agent_module = ''; - - if(defined($template_name) && $template_name ne '') { - my $id_template = get_template_id($dbh,$template_name); - exist_check($id_template,'template',$template_name); - $id_alert_agent_module = get_template_module_id($dbh,$id_agentmodule,$id_template); - exist_check($id_alert_agent_module,'template module',$template_name); - } + if (defined $use_alias and $use_alias eq 'use_alias') { + my @id_agents = get_agent_ids_from_alias($dbh,$agent_name); + + foreach my $id (@id_agents) { + if(defined($agent_name) && $agent_name ne '') { + $id_agent = $id->{'id_agente'}; + exist_check($id_agent,'agent',$agent_name); - pandora_validate_event_filter ($conf, $id_agentmodule, $id_agent, $datetime_min, $datetime_max, $user_name, $id_alert_agent_module, $criticity, $dbh); - print_log "[INFO] Validating event for agent '$agent_name'\n\n"; + if($module_name ne '') { + $id_agentmodule = get_agent_module_id($dbh, $module_name, $id_agent); + if ($id_agentmodule eq -1) { + next; + } + } + } + + my $id_alert_agent_module = ''; + + if(defined($template_name) && $template_name ne '') { + my $id_template = get_template_id($dbh,$template_name); + exist_check($id_template,'template',$template_name); + $id_alert_agent_module = get_template_module_id($dbh,$id_agentmodule,$id_template); + exist_check($id_alert_agent_module,'template module',$template_name); + } + + pandora_validate_event_filter ($conf, $id_agentmodule, $id_agent, $datetime_min, $datetime_max, $user_name, $id_alert_agent_module, $criticity, $dbh); + print_log "[INFO] Validating event for agent '$id->{'nombre'}'\n\n"; + } + } else { + if(defined($agent_name) && $agent_name ne '') { + $id_agent = get_agent_id($dbh,$agent_name); + exist_check($id_agent,'agent',$agent_name); + + if($module_name ne '') { + $id_agentmodule = get_agent_module_id($dbh, $module_name, $id_agent); + exist_check($id_agentmodule,'module',$module_name); + } + } + + my $id_alert_agent_module = ''; + + if(defined($template_name) && $template_name ne '') { + my $id_template = get_template_id($dbh,$template_name); + exist_check($id_template,'template',$template_name); + $id_alert_agent_module = get_template_module_id($dbh,$id_agentmodule,$id_template); + exist_check($id_alert_agent_module,'template module',$template_name); + } + + pandora_validate_event_filter ($conf, $id_agentmodule, $id_agent, $datetime_min, $datetime_max, $user_name, $id_alert_agent_module, $criticity, $dbh); + print_log "[INFO] Validating event for agent '$agent_name'\n\n"; + } } ############################################################################## @@ -3675,38 +4372,84 @@ sub cli_create_incident() { sub cli_delete_data($) { my $ltotal = shift; - my ($opt, $name, $name2) = @ARGV[2..4]; + my ($opt, $name, $name2, $use_alias) = @ARGV[2..5]; + if($opt eq '-m' || $opt eq '--m') { - # Delete module data - param_check($ltotal, 3) unless ($name2 ne ''); - my $id_agent = get_agent_id($dbh,$name2); - exist_check($id_agent,'agent',$name2); + if (defined $use_alias and $use_alias eq 'use_alias') { + my @id_agents = get_agent_ids_from_alias($dbh,$name2); + + foreach my $id (@id_agents) { + # Delete module data + param_check($ltotal, 3) unless ($name2 ne ''); + my $id_agent = $id->{'id_agente'}; + exist_check($id_agent,'agent',$name2); + + my $id_module = get_agent_module_id($dbh,$name,$id_agent); + exist_check($id_module,'module',$name); + + print_log "DELETING THE DATA OF THE MODULE $name OF THE AGENT $name2\n\n"; + + pandora_delete_data($dbh, 'module', $id_module); + } + } else { + # Delete module data + param_check($ltotal, 3) unless ($name2 ne ''); + my $id_agent = get_agent_id($dbh,$name2); + exist_check($id_agent,'agent',$name2); + + my $id_module = get_agent_module_id($dbh,$name,$id_agent); + exist_check($id_module,'module',$name); - my $id_module = get_agent_module_id($dbh,$name,$id_agent); - exist_check($id_module,'module',$name); - - print_log "DELETING THE DATA OF THE MODULE $name OF THE AGENT $name2\n\n"; - - pandora_delete_data($dbh, 'module', $id_module); + print_log "DELETING THE DATA OF THE MODULE $name OF THE AGENT $name2\n\n"; + + pandora_delete_data($dbh, 'module', $id_module); + } + } elsif($opt eq '-a' || $opt eq '--a') { - # Delete agent's modules data - my $id_agent = get_agent_id($dbh,$name); - exist_check($id_agent,'agent',$name); + if (defined $use_alias and $use_alias eq 'use_alias') { + my @id_agents = get_agent_ids_from_alias($dbh,$name); + foreach my $id (@id_agents) { + # Delete agent's modules data + my $id_agent = $id->{'id_agente'}; + exist_check($id_agent,'agent',$name); - print_log "DELETING THE DATA OF THE AGENT $name\n\n"; + print_log "DELETING THE DATA OF THE AGENT $name\n\n"; - pandora_delete_data($dbh, 'module', $id_agent); + pandora_delete_data($dbh, 'module', $id_agent); + } + } else { + my $id_agent = get_agent_id($dbh,$name); + exist_check($id_agent,'agent',$name); + + print_log "DELETING THE DATA OF THE AGENT $name\n\n"; + + pandora_delete_data($dbh, 'module', $id_agent); + } } elsif($opt eq '-g' || $opt eq '--g') { - # Delete group's modules data - my $id_group = get_group_id($dbh,$name); - exist_check($id_group,'group',$name); - - print_log "DELETING THE DATA OF THE GROUP $name\n\n"; - - pandora_delete_data($dbh, 'group', $id_group); + if (defined $use_alias and $use_alias eq 'use_alias') { + my @id_agents = get_agent_ids_from_alias($dbh,$name); + + foreach my $id (@id_agents) { + # Delete group's modules data + my $id_group = $id->{'id_agente'}; + exist_check($id_group,'group',$name); + + print_log "DELETING THE DATA OF THE GROUP $name\n\n"; + + pandora_delete_data($dbh, 'group', $id_group); + } + } else { + # Delete group's modules data + my $id_group = get_group_id($dbh,$name); + exist_check($id_group,'group',$name); + + print_log "DELETING THE DATA OF THE GROUP $name\n\n"; + + pandora_delete_data($dbh, 'group', $id_group); + } } else { print_log "[ERROR] Invalid parameter '$opt'.\n\n"; @@ -3863,21 +4606,20 @@ sub cli_get_module_id() { ############################################################################## -# Show the group name where is a given agent +# Show the group name where a given agent is # Related option: --get_agent_group ############################################################################## sub cli_get_agent_group() { - my $agent_name = @ARGV[2]; - + my ($agent_name,$use_alias) = @ARGV[2..3]; + if (is_metaconsole($conf) == 1) { my $agents_groups = enterprise_hook('get_metaconsole_agent',[$dbh, $agent_name]); - if (scalar(@{$agents_groups}) != 0) { + if (not defined $use_alias and scalar(@{$agents_groups}) != 0) { foreach my $agent (@{$agents_groups}) { my @test = $agent; - print Dumper $test[0]; my $group_name = get_group_name ($dbh, $agent->{'id_grupo'}); print "Server: $agent->{'server_name'} Agent: $agent->{'nombre'} Name Group: $group_name \n\n"; } @@ -3890,28 +4632,68 @@ sub cli_get_agent_group() { foreach my $server (@servers_id) { my $dbh_metaconsole = enterprise_hook('get_node_dbh',[$conf, $server, $dbh]); - my $id_agent = get_agent_id($dbh_metaconsole,$agent_name); - - if ($id_agent == -1) { - next; - } - else { - my $id_group = get_agent_group ($dbh_metaconsole, $id_agent); - my $group_name = get_group_name ($dbh_metaconsole, $id_group); - $agent_name = safe_output($agent_name); - print "[INFO] Agent: $agent_name Name Group: $group_name\n\n"; + my @id_agents; + my $id_agent; + + if (defined $use_alias and $use_alias eq 'use_alias') { + @id_agents = get_agent_ids_from_alias($dbh_metaconsole,$agent_name); + + foreach my $id (@id_agents) { + + if ($id->{'id_agente'} == -1) { + next; + } + else { + my $id_group = get_agent_group ($dbh_metaconsole, $id->{'id_agente'}); + my $group_name = get_group_name ($dbh_metaconsole, $id_group); + $agent_name = safe_output($agent_name); + print "[INFO] Agent: $id->{'nombre'} Name Group: $group_name\n\n"; + } + } + } else { + $id_agent = get_agent_id($dbh_metaconsole,$agent_name); + + if ($id_agent == -1) { + next; + } + else { + my $id_group = get_agent_group ($dbh_metaconsole, $id_agent); + my $group_name = get_group_name ($dbh_metaconsole, $id_group); + $agent_name = safe_output($agent_name); + print "[INFO] Agent: $agent_name Name Group: $group_name\n\n"; + } } } } } else { - my $id_agent = get_agent_id($dbh,$agent_name); - exist_check($id_agent,'agent',$agent_name); + my @id_agents; + my $id_agent; + my $id_group; + my $group_name; + + if (defined $use_alias and $use_alias eq 'use_alias') { + @id_agents = get_agent_ids_from_alias($dbh,$agent_name); + + foreach my $id (@id_agents) { + exist_check($id->{'id_agente'},'agent',$agent_name); + + $id_group = get_agent_group ($dbh, $id->{'id_agente'}); - my $id_group = get_agent_group ($dbh, $id_agent); + $group_name = get_group_name ($dbh, $id_group); + print $group_name."\n"; + } + } else { + $id_agent = get_agent_id($dbh,$agent_name); + exist_check($id_agent,'agent',$agent_name); + + $id_group = get_agent_group ($dbh, $id_agent); - my $group_name = get_group_name ($dbh, $id_group); - print $group_name; + $group_name = get_group_name ($dbh, $id_group); + print $group_name; + } + + } } @@ -3920,12 +4702,13 @@ sub cli_get_agent_group() { # Related option: --get_agent_group_id ############################################################################## sub cli_get_agent_group_id() { - my $agent_name = @ARGV[2]; + my ($agent_name,$use_alias) = @ARGV[2..3]; if (is_metaconsole($conf) == 1) { + my $agents_groups = enterprise_hook('get_metaconsole_agent',[$dbh, $agent_name]); - if (scalar(@{$agents_groups}) != 0) { + if (not defined $use_alias and scalar(@{$agents_groups}) != 0) { foreach my $agent (@{$agents_groups}) { print "Server: $agent->{'server_name'} Agent: $agent->{'nombre'} ID Group: $agent->{'id_grupo'}\n\n"; @@ -3939,26 +4722,62 @@ sub cli_get_agent_group_id() { foreach my $server (@servers_id) { my $dbh_metaconsole = enterprise_hook('get_node_dbh',[$conf, $server, $dbh]); - my $id_agent = get_agent_id($dbh_metaconsole,$agent_name); - - if ($id_agent == -1) { - next; - } - else { - my $id_group = get_agent_group ($dbh_metaconsole, $id_agent); - $agent_name = safe_output($agent_name); - print "Agent: $agent_name ID Group: $id_group\n\n"; + my @id_agents; + my $id_agent; + + if (defined $use_alias and $use_alias eq 'use_alias') { + @id_agents = get_agent_ids_from_alias($dbh_metaconsole,$agent_name); + + foreach my $id (@id_agents) { + + if ($id->{'id_agente'} == -1) { + next; + } + else { + my $id_group = get_agent_group ($dbh_metaconsole, $id->{'id_agente'}); + $agent_name = safe_output($agent_name); + print "Agent: $id->{'nombre'} ID Group: $id_group\n\n"; + } + } + } else { + $id_agent = get_agent_id($dbh_metaconsole,$agent_name); + + if ($id_agent == -1) { + next; + } + else { + my $id_group = get_agent_group ($dbh_metaconsole, $id_agent); + $agent_name = safe_output($agent_name); + print "Agent: $agent_name ID Group: $id_group\n\n"; + } } } } } else { - my $id_agent = get_agent_id($dbh,$agent_name); - exist_check($id_agent,'agent',$agent_name); - - my $id_group = get_agent_group ($dbh, $id_agent); + my @id_agents; + my $id_agent; + my $id_group; + my $group_name; - print $id_group; + if (defined $use_alias and $use_alias eq 'use_alias') { + @id_agents = get_agent_ids_from_alias($dbh,$agent_name); + + foreach my $id (@id_agents) { + exist_check($id->{'id_agente'},'agent',$agent_name); + + $id_group = get_agent_group ($dbh, $id->{'id_agente'}); + + print $id_group."\n"; + } + } else { + $id_agent = get_agent_id($dbh,$agent_name); + exist_check($id_agent,'agent',$agent_name); + + $id_group = get_agent_group ($dbh, $id_agent); + + print $id_group; + } } } @@ -3986,20 +4805,43 @@ sub cli_get_agents_module_current_data() { ############################################################################## sub cli_get_agent_modules() { - my $agent_name = @ARGV[2]; + my ($agent_name,$use_alias) = @ARGV[2..3]; - my $id_agent = get_agent_id($dbh,$agent_name); - exist_check($id_agent,'agent',$agent_name); + my @id_agents; + my $id_agent; + + if (defined $use_alias and $use_alias eq 'use_alias') { + @id_agents = get_agent_ids_from_alias($dbh,$agent_name); + + foreach my $id (@id_agents) { + exist_check($id->{'id_agente'},'agent',$agent_name); + + my $modules = pandora_get_agent_modules ($dbh, $id->{'id_agente'}); + + if(scalar(@{$modules}) == 0) { + print_log "[INFO] The agent '$agent_name' have no modules\n\n"; + } + + print "\n".$id->{'nombre'}."\n"; + print "id_module, module_name\n"; + foreach my $module (@{$modules}) { + print $module->{'id_agente_modulo'}.",".safe_output($module->{'nombre'})."\n"; + } + } + } else { + $id_agent = get_agent_id($dbh,$agent_name); + exist_check($id_agent,'agent',$agent_name); - my $modules = pandora_get_agent_modules ($dbh, $id_agent); + my $modules = pandora_get_agent_modules ($dbh, $id_agent); + + if(scalar(@{$modules}) == 0) { + print_log "[INFO] The agent '$agent_name' have no modules\n\n"; + } - if(scalar(@{$modules}) == 0) { - print_log "[INFO] The agent '$agent_name' have not modules\n\n"; - } - - print "id_module, module_name\n"; - foreach my $module (@{$modules}) { - print $module->{'id_agente_modulo'}.",".safe_output($module->{'nombre'})."\n"; + print "id_module, module_name\n"; + foreach my $module (@{$modules}) { + print $module->{'id_agente_modulo'}.",".safe_output($module->{'nombre'})."\n"; + } } } @@ -4008,7 +4850,15 @@ sub cli_create_synthetic() { my $synthetic_type = @ARGV[3]; my $agent_name = @ARGV[4]; - my @module_data = @ARGV[5..$#ARGV]; + + my @module_data; + + if (@ARGV[$#ARGV] == "use_alias") { + @module_data = @ARGV[5..$#ARGV-1]; + } else { + @module_data = @ARGV[5..$#ARGV]; + } + my $module; my (@filterdata,@data_module); @@ -4030,86 +4880,181 @@ sub cli_create_synthetic() { $module->{'prediction_module'} = 3; # Synthetic code is 3 $module->{'flag'} = 1; - my $id_agent = int(get_agent_id($dbh,$agent_name)); - - if ($id_agent > 0) { - foreach my $i (0 .. $#module_data) { - my @split_data = split(',',$module_data[$i]); - if (@split_data[0] =~ m/(x|\/|\+|\*|\-)/ && length(@split_data[0]) == 1 ) { - if ( @split_data[0] =~ m/(\/|\+|\*|\-)/ && $synthetic_type eq 'average' ) { - print("[ERROR] With this type: $synthetic_type only be allow use this operator: 'x' \n\n"); - exit 1; + my @id_agents; + my $id_agent; + + if (@ARGV[$#ARGV] eq 'use_alias') { + @id_agents = get_agent_ids_from_alias($dbh,$agent_name); + + foreach my $id (@id_agents) { + @filterdata = (); + $id_agent = $id->{'id_agente'}; + + if ($id_agent > 0) { + + foreach my $i (0 .. $#module_data) { + my @split_data = split(',',$module_data[$i]); + if (@split_data[0] =~ m/(x|\/|\+|\*|\-)/ && length(@split_data[0]) == 1 ) { + if ( @split_data[0] =~ m/(\/|\+|\*|\-)/ && $synthetic_type eq 'average' ) { + print("[ERROR] With this type: $synthetic_type only be allow use this operator: 'x' \n\n"); + exit 1; + } + if (is_numeric(@split_data[1]) == 0) { + next; + } + @data_module = ("",@split_data[0],@split_data[1]); + my $text_data = join(',',@data_module); + push (@filterdata,$text_data); + } + else { + if (scalar(@split_data) == 2) { + @data_module = (safe_output(@split_data[0]),'',safe_output(@split_data[1])); + my $text_data = join(',',@data_module); + push (@filterdata,$text_data); + } + else { + if (length(@split_data[1]) > 1 ) { + print("[ERROR] You can only use +, -, *, / or x, and you use this: @split_data[1] \n\n"); + exit 1; + } + if ( @split_data[1] =~ m/(\/|\+|\*|\-)/ && $synthetic_type eq 'average' ) { + print("[ERROR] With this type: $synthetic_type only be allow use this operator: 'x' \n\n"); + exit 1; + } + if ( $synthetic_type eq 'arithmetic' && $i == 0) { + @data_module = (safe_output(@split_data[0]),'',safe_output(@split_data[2])); + } + else { + @data_module = (safe_output(@split_data[0]),@split_data[1],safe_output(@split_data[2])); + } + my $text_data = join(',',@data_module); + push (@filterdata,$text_data); + } + } } - if (is_numeric(@split_data[1]) == 0) { - next; + + my $module_exists = get_agent_module_id($dbh, $name_module, $id_agent); + non_exist_check($module_exists, 'module name', $name_module); + + $module->{'id_agente'} = $id_agent; + $module->{'nombre'} = safe_input($name_module); + my $id_tipo_modulo = get_db_value ($dbh, "SELECT id_tipo FROM ttipo_modulo WHERE nombre = ?", "generic_data"); + $module->{'id_modulo'} = 5; + $module->{'id_tipo_modulo'} = $id_tipo_modulo; + + my $id_module = db_process_insert($dbh, 'id_agente_modulo', 'tagente_modulo', $module); + + if ($id_module) { + my $result = enterprise_hook('create_synthetic_operations_by_alias', + [$dbh,int($id_module), @filterdata]); + + if ($result) { + + db_do ($dbh, 'INSERT INTO tagente_estado (id_agente_modulo, id_agente, estado, + known_status, last_status, last_known_status, last_try, datos) + VALUES (?, ?, ?, ?, ?, ?, \'1970-01-01 00:00:00\', \'\')', $id_module, $id_agent, 4, 4, 4, 4); + # Update the module status count. When the module is created disabled dont do it + pandora_mark_agent_for_module_update ($dbh, $id_agent); + print("[OK] Created module ID: $id_module \n\n"); + } + else { + #db_do ($dbh, 'DELETE FROM tagente_modulo WHERE id_agente_modulo = ?', $id_module); + print("[ERROR] Problems with creating data module. \n\n"); + } + } + else { + db_do ($dbh, 'DELETE FROM tagente_modulo WHERE nombre = ? AND id_agente = ?', $name_module, $id_agent); + print("[INFO] Problems with creating module \n\n"); } - @data_module = ("",@split_data[0],@split_data[1]); - my $text_data = join(',',@data_module); - push (@filterdata,$text_data); } else { - if (scalar(@split_data) == 2) { - @data_module = (safe_output(@split_data[0]),'',safe_output(@split_data[1])); + print( "[INFO] The agent '$id->{'nombre'}' doesn't exist\n\n"); + } + } + } else { + my $id_agent = int(get_agent_id($dbh,$agent_name)); + + if ($id_agent > 0) { + foreach my $i (0 .. $#module_data) { + my @split_data = split(',',$module_data[$i]); + if (@split_data[0] =~ m/(x|\/|\+|\*|\-)/ && length(@split_data[0]) == 1 ) { + if ( @split_data[0] =~ m/(\/|\+|\*|\-)/ && $synthetic_type eq 'average' ) { + print("[ERROR] With this type: $synthetic_type only be allow use this operator: 'x' \n\n"); + exit 1; + } + if (is_numeric(@split_data[1]) == 0) { + next; + } + @data_module = ("",@split_data[0],@split_data[1]); my $text_data = join(',',@data_module); push (@filterdata,$text_data); } else { - if (length(@split_data[1]) > 1 ) { - print("[ERROR] You can only use +, -, *, / or x, and you use this: @split_data[1] \n\n"); - exit 1; - } - if ( @split_data[1] =~ m/(\/|\+|\*|\-)/ && $synthetic_type eq 'average' ) { - print("[ERROR] With this type: $synthetic_type only be allow use this operator: 'x' \n\n"); - exit 1; - } - if ( $synthetic_type eq 'arithmetic' && $i == 0) { - @data_module = (safe_output(@split_data[0]),'',safe_output(@split_data[2])); + if (scalar(@split_data) == 2) { + @data_module = (safe_output(@split_data[0]),'',safe_output(@split_data[1])); + my $text_data = join(',',@data_module); + push (@filterdata,$text_data); } else { - @data_module = (safe_output(@split_data[0]),@split_data[1],safe_output(@split_data[2])); + if (length(@split_data[1]) > 1 ) { + print("[ERROR] You can only use +, -, *, / or x, and you use this: @split_data[1] \n\n"); + exit 1; + } + if ( @split_data[1] =~ m/(\/|\+|\*|\-)/ && $synthetic_type eq 'average' ) { + print("[ERROR] With this type: $synthetic_type only be allow use this operator: 'x' \n\n"); + exit 1; + } + if ( $synthetic_type eq 'arithmetic' && $i == 0) { + @data_module = (safe_output(@split_data[0]),'',safe_output(@split_data[2])); + } + else { + @data_module = (safe_output(@split_data[0]),@split_data[1],safe_output(@split_data[2])); + } + + my $text_data = join(',',@data_module); + push (@filterdata,$text_data); } - - my $text_data = join(',',@data_module); - push (@filterdata,$text_data); } } - } - my $module_exists = get_agent_module_id($dbh, $name_module, $id_agent); - non_exist_check($module_exists, 'module name', $name_module); - - $module->{'id_agente'} = $id_agent; - $module->{'nombre'} = safe_input($name_module); - my $id_tipo_modulo = get_db_value ($dbh, "SELECT id_tipo FROM ttipo_modulo WHERE nombre = ?", "generic_data"); - $module->{'id_modulo'} = 5; - $module->{'id_tipo_modulo'} = $id_tipo_modulo; - - my $id_module = db_process_insert($dbh, 'id_agente_modulo', 'tagente_modulo', $module); - - if ($id_module) { - my $result = enterprise_hook('create_synthetic_operations', - [$dbh,int($id_module), @filterdata]); - if ($result) { - db_do ($dbh, 'INSERT INTO tagente_estado (id_agente_modulo, id_agente, estado, - known_status, last_status, last_known_status, last_try, datos) - VALUES (?, ?, ?, ?, ?, ?, \'1970-01-01 00:00:00\', \'\')', $id_module, $id_agent, 4, 4, 4, 4); - # Update the module status count. When the module is created disabled dont do it - pandora_mark_agent_for_module_update ($dbh, $id_agent); - print("[OK] The modules are creating ID: $id_module \n\n"); + my $module_exists = get_agent_module_id($dbh, $name_module, $id_agent); + non_exist_check($module_exists, 'module name', $name_module); + + $module->{'id_agente'} = $id_agent; + $module->{'nombre'} = safe_input($name_module); + my $id_tipo_modulo = get_db_value ($dbh, "SELECT id_tipo FROM ttipo_modulo WHERE nombre = ?", "generic_data"); + $module->{'id_modulo'} = 5; + $module->{'id_tipo_modulo'} = $id_tipo_modulo; + + my $id_module = db_process_insert($dbh, 'id_agente_modulo', 'tagente_modulo', $module); + + if ($id_module) { + my $result = enterprise_hook('create_synthetic_operations', + [$dbh,int($id_module), @filterdata]); + if ($result) { + db_do ($dbh, 'INSERT INTO tagente_estado (id_agente_modulo, id_agente, estado, + known_status, last_status, last_known_status, last_try, datos) + VALUES (?, ?, ?, ?, ?, ?, \'1970-01-01 00:00:00\', \'\')', $id_module, $id_agent, 4, 4, 4, 4); + # Update the module status count. When the module is created disabled dont do it + pandora_mark_agent_for_module_update ($dbh, $id_agent); + print("[OK] Created module ID: $id_module \n\n"); + } + else { + db_do ($dbh, 'DELETE FROM tagente_modulo WHERE id_agente_modulo = ?', $id_module); + print("[ERROR] Problems with creating data module. \n\n"); + } } else { - db_do ($dbh, 'DELETE FROM tagente_modulo WHERE id_agente_modulo = ?', $id_module); - print("[ERROR] Problems with creating data module. \n\n"); + db_do ($dbh, 'DELETE FROM tagente_modulo WHERE nombre = ? AND id_agente = ?', $name_module, $id_agent); + print("[INFO] Problems with creating module \n\n"); } } - else { - db_do ($dbh, 'DELETE FROM tagente_modulo WHERE nombre = ? AND id_agente = ?', $name_module, $id_agent); - print("[INFO] Problems with creating module \n\n"); + else { + print( "[INFO] The agent '$agent_name' doesn't exist\n\n"); } } - else { - print( "[INFO] The agent '$agent_name' doesn't exists\n\n"); - } + + } @@ -4146,32 +5091,63 @@ sub cli_get_policy_modules() { ######################################################################## sub cli_get_policies() { - my $agent_name = @ARGV[2]; + my ($agent_name, $use_alias) = @ARGV[2..3]; my $policies; - if (defined($agent_name)) { - my $id_agent = get_agent_id($dbh,$agent_name); - exist_check($id_agent,'agent',$agent_name); + if (defined $use_alias and $use_alias eq 'use_alias') { + my @id_agents = get_agent_ids_from_alias($dbh,$agent_name); + + foreach my $id (@id_agents) { + if (defined($agent_name)) { + my $id_agent = $id->{'id_agente'}; + exist_check($id_agent,'agent',$agent_name); + + $policies = enterprise_hook('get_agent_policies', [$dbh,$id_agent]); + + if (scalar(@{$policies}) == 0) { + print_log "[INFO] No policies found on agent $id->{'nombre'}\n\n"; + exit; + } + } + else { + $policies = enterprise_hook('get_policies', [$dbh]); + if (scalar(@{$policies}) == 0) { + print_log "[INFO] No policies found\n\n"; + exit; + } + } + + print "agent_name, id_policy, policy_name\n"; + foreach my $module (@{$policies}) { + print $id->{'nombre'}.",".$module->{'id'}.",".safe_output($module->{'name'})."\n"; + } + } + } else { + if (defined($agent_name)) { + my $id_agent = get_agent_id($dbh,$agent_name); + exist_check($id_agent,'agent',$agent_name); + + $policies = enterprise_hook('get_agent_policies', [$dbh,$id_agent]); + + if (scalar(@{$policies}) == 0) { + print_log "[INFO] No policies found on agent '$agent_name'\n\n"; + exit; + } + } + else { + $policies = enterprise_hook('get_policies', [$dbh]); + if (scalar(@{$policies}) == 0) { + print_log "[INFO] No policies found\n\n"; + exit; + } + } - $policies = enterprise_hook('get_agent_policies', [$dbh,$id_agent]); - - if (scalar(@{$policies}) == 0) { - print_log "[INFO] No policies found on agent '$agent_name'\n\n"; - exit; + print "id_policy, policy_name\n"; + foreach my $module (@{$policies}) { + print $module->{'id'}.",".safe_output($module->{'name'})."\n"; } } - else { - $policies = enterprise_hook('get_policies', [$dbh]); - if (scalar(@{$policies}) == 0) { - print_log "[INFO] No policies found\n\n"; - exit; - } - } - - print "id_policy, policy_name\n"; - foreach my $module (@{$policies}) { - print $module->{'id'}.",".safe_output($module->{'name'})."\n"; - } + } ############################################################################## @@ -4180,7 +5156,7 @@ sub cli_get_policies() { ############################################################################## sub cli_get_agents() { - my ($group_name, $os_name, $status, $max_modules, $filter_substring, $policy_name) = @ARGV[2..7]; + my ($group_name, $os_name, $status, $max_modules, $filter_substring, $policy_name, $use_alias) = @ARGV[2..8]; my $condition = ' disabled=0'; @@ -4191,7 +5167,7 @@ sub cli_get_agents() { if($group_name ne '') { $id_group = get_group_id($dbh, $group_name); exist_check($id_group,'group',$group_name); - + $condition .= " AND id_grupo = $id_group "; } @@ -4216,7 +5192,11 @@ sub cli_get_agents() { } if($filter_substring ne '') { - $condition .= " AND nombre LIKE '%".safe_input($filter_substring)."%'"; + if (defined $use_alias and $use_alias eq 'use_alias') { + $condition .= " AND alias LIKE '%".safe_input($filter_substring)."%'"; + } else { + $condition .= " AND nombre LIKE '%".safe_input($filter_substring)."%'"; + } } my @agents = get_db_rows ($dbh, "SELECT * FROM tagente WHERE $condition"); @@ -4229,6 +5209,10 @@ sub cli_get_agents() { my $agent_status; my $head_print = 0; + + use Data::Dumper; + + foreach my $agent (@agents) { if($status ne '') { $agent_status = pandora_get_agent_status($dbh,$agent->{'id_agente'}); @@ -4240,6 +5224,7 @@ sub cli_get_agents() { $head_print = 1; print "id_agent, agent_name\n"; } + print $agent->{'id_agente'}.",".safe_output($agent->{'nombre'})."\n"; } @@ -4254,26 +5239,51 @@ sub cli_get_agents() { ############################################################################## sub cli_delete_conf_file() { - my $agent_name = @ARGV[2]; - + my ($agent_name,$use_alias) = @ARGV[2..3]; + my $conf_deleted = 0; my $md5_deleted = 0; - - if (-e $conf->{incomingdir}.'/conf/'.md5($agent_name).'.conf') { - unlink($conf->{incomingdir}.'/conf/'.md5($agent_name).'.conf'); - $conf_deleted = 1; - } - if (-e $conf->{incomingdir}.'/md5/'.md5($agent_name).'.md5') { - unlink($conf->{incomingdir}.'/md5/'.md5($agent_name).'.md5'); - $md5_deleted = 1; - } - - if($conf_deleted == 1 || $md5_deleted == 1) { - print_log "[INFO] Local conf files of the agent '$agent_name' has been deleted succesfully\n\n"; - } - else { - print_log "[ERROR] Local conf file of the agent '$agent_name' didn't found\n\n"; - exit; + + if (defined $use_alias and $use_alias eq 'use_alias') { + my @id_agents = get_agent_ids_from_alias($dbh,$agent_name); + + foreach my $id (@id_agents) { + $agent_name = $id->{'nombre'}; + + if (-e $conf->{incomingdir}.'/conf/'.md5($agent_name).'.conf') { + unlink($conf->{incomingdir}.'/conf/'.md5($agent_name).'.conf'); + $conf_deleted = 1; + } + if (-e $conf->{incomingdir}.'/md5/'.md5($agent_name).'.md5') { + unlink($conf->{incomingdir}.'/md5/'.md5($agent_name).'.md5'); + $md5_deleted = 1; + } + + if($conf_deleted == 1 || $md5_deleted == 1) { + print_log "[INFO] Local conf files of the agent '$agent_name' has been deleted succesfully\n\n"; + } + else { + print_log "[ERROR] Local conf file of the agent '$agent_name' was not found\n\n"; + exit; + } + } + } else { + if (-e $conf->{incomingdir}.'/conf/'.md5($agent_name).'.conf') { + unlink($conf->{incomingdir}.'/conf/'.md5($agent_name).'.conf'); + $conf_deleted = 1; + } + if (-e $conf->{incomingdir}.'/md5/'.md5($agent_name).'.md5') { + unlink($conf->{incomingdir}.'/md5/'.md5($agent_name).'.md5'); + $md5_deleted = 1; + } + + if($conf_deleted == 1 || $md5_deleted == 1) { + print_log "[INFO] Local conf files of the agent '$agent_name' has been deleted succesfully\n\n"; + } + else { + print_log "[ERROR] Local conf file of the agent '$agent_name' was not found\n\n"; + exit; + } } } @@ -4283,14 +5293,29 @@ sub cli_delete_conf_file() { ############################################################################## sub cli_clean_conf_file() { - my $agent_name = @ARGV[2]; + my ($agent_name,$use_alias) = @ARGV[2..3]; my $result; if(defined($agent_name)) { - if (-e $conf->{incomingdir}.'/conf/'.md5($agent_name).'.conf') { - $result = enterprise_hook('pandora_clean_conf_file',[$conf, md5($agent_name)]); - if($result != -1) { - print_log "[INFO] Conf file '".$conf->{incomingdir}.'/conf/'.md5($agent_name).".conf has been cleaned'\n\n"; + if (defined $use_alias and $use_alias eq 'use_alias') { + my @id_agents = get_agent_ids_from_alias($dbh,$agent_name); + + foreach my $id (@id_agents) { + $agent_name = $id->{'nombre'}; + + if (-e $conf->{incomingdir}.'/conf/'.md5($agent_name).'.conf') { + $result = enterprise_hook('pandora_clean_conf_file',[$conf, md5($agent_name)]); + if($result != -1) { + print_log "[INFO] Conf file '".$conf->{incomingdir}.'/conf/'.md5($agent_name).".conf has been cleaned'\n\n"; + } + } + } + } else { + if (-e $conf->{incomingdir}.'/conf/'.md5($agent_name).'.conf') { + $result = enterprise_hook('pandora_clean_conf_file',[$conf, md5($agent_name)]); + if($result != -1) { + print_log "[INFO] Conf file '".$conf->{incomingdir}.'/conf/'.md5($agent_name).".conf has been cleaned'\n\n"; + } } } } @@ -4367,23 +5392,46 @@ sub cli_disable_policy_alerts() { ############################################################################## sub cli_policy_add_agent() { - my ($agent_name, $policy_name) = @ARGV[2..3]; + my ($agent_name, $policy_name, $use_alias) = @ARGV[2..4]; - my $agent_id = get_agent_id($dbh,$agent_name); - exist_check($agent_id,'agent',$agent_name); - - my $policy_id = enterprise_hook('get_policy_id',[$dbh, safe_input($policy_name)]); - exist_check($policy_id,'policy',$policy_name); + if (defined $use_alias and $use_alias eq 'use_alias') { + my @id_agents = get_agent_ids_from_alias($dbh,$agent_name); + + foreach my $id (@id_agents) { + my $agent_id = $id->{'id_agente'}; + exist_check($agent_id,'agent',$agent_name); + + my $policy_id = enterprise_hook('get_policy_id',[$dbh, safe_input($policy_name)]); + exist_check($policy_id,'policy',$policy_name); + + # Add the agent to policy + my $policy_agent_id = enterprise_hook('pandora_policy_add_agent',[$policy_id, $agent_id, $dbh]); + + if($policy_agent_id == -1) { + print_log "[ERROR] A problem has been ocurred adding agent $id->{'nombre'} to policy '$policy_name'\n\n"; + } + else { + print_log "[INFO] Added agent $id->{'nombre'} to policy $policy_name. Is necessary to apply the policy in order to changes take effect.\n\n"; + } + } + } else { + my $agent_id = get_agent_id($dbh,$agent_name); + exist_check($agent_id,'agent',$agent_name); - # Add the agent to policy - my $policy_agent_id = enterprise_hook('pandora_policy_add_agent',[$policy_id, $agent_id, $dbh]); - - if($policy_agent_id == -1) { - print_log "[ERROR] A problem has been ocurred adding agent '$agent_name' to policy '$policy_name'\n\n"; - } - else { - print_log "[INFO] Added agent '$agent_name' to policy '$policy_name'. Is necessary to apply the policy in order to changes take effect.\n\n"; + my $policy_id = enterprise_hook('get_policy_id',[$dbh, safe_input($policy_name)]); + exist_check($policy_id,'policy',$policy_name); + + # Add the agent to policy + my $policy_agent_id = enterprise_hook('pandora_policy_add_agent',[$policy_id, $agent_id, $dbh]); + + if($policy_agent_id == -1) { + print_log "[ERROR] A problem has been ocurred adding agent '$agent_name' to policy '$policy_name'\n\n"; + } + else { + print_log "[INFO] Added agent '$agent_name' to policy '$policy_name'. Is necessary to apply the policy in order to changes take effect.\n\n"; + } } + } ############################################################################## @@ -4609,11 +5657,6 @@ sub cli_update_group() { print_log "[INFO] Updated group '$group_id'\n\n"; } } - - - - - } @@ -4622,47 +5665,79 @@ sub cli_update_group() { # Related option: --locate_agent ############################################################################### sub cli_locate_agent () { - my ($agent_name) = @ARGV[2]; + my ($agent_name, $use_alias) = @ARGV[2..3]; if (is_metaconsole($conf) == 1) { - my $agents_server = enterprise_hook('get_metaconsole_agent',[$dbh, $agent_name]); - - if (scalar(@{$agents_server}) != 0) { - foreach my $agent (@{$agents_server}) { - #my $server = enterprise_hook('get_metaconsole_setup_server_id',[$dbh, $agent->{'server_name'}]); - print $agent->{'id_tmetaconsole_setup'} . "\n"; - } - } - else { + if (defined $use_alias and $use_alias eq 'use_alias') { my $servers = enterprise_hook('get_metaconsole_setup_servers',[$dbh]); my @servers_id = split(',',$servers); my @list_servers; my $list_names_servers; + my @id_agents; foreach my $server (@servers_id) { my $dbh_metaconsole = enterprise_hook('get_node_dbh',[$conf, $server, $dbh]); - my $agent_id = get_agent_id($dbh_metaconsole,$agent_name); + @id_agents = get_agent_ids_from_alias($dbh_metaconsole,$agent_name); - if ($agent_id == -1) { - next; - } - else { - push @list_servers,$server; + foreach my $id (@id_agents) { + if ($id->{'id_agente'} == -1) { + next; + } + else { + push @list_servers,$server; + last; + } } } if (scalar(@list_servers) > 0) { $list_names_servers = join(',',@list_servers); - print_log "[INFO] The agent: $agent_name find in server with IDS: $list_names_servers\n\n"; + print_log "[INFO] One or more agents with the alias '$agent_name' were found in server with IDS: $list_names_servers\n\n"; } else { - print_log "[ERROR] This agent: $agent_name not find in any node\n\n"; + print_log "[ERROR] No agent with alias '$agent_name' found in any node\n\n"; + } + } else { + my $agents_server = enterprise_hook('get_metaconsole_agent',[$dbh, $agent_name]); + + if (scalar(@{$agents_server}) != 0) { + foreach my $agent (@{$agents_server}) { + #my $server = enterprise_hook('get_metaconsole_setup_server_id',[$dbh, $agent->{'server_name'}]); + print $agent->{'id_tmetaconsole_setup'} . "\n"; + } + } + else { + my $servers = enterprise_hook('get_metaconsole_setup_servers',[$dbh]); + my @servers_id = split(',',$servers); + my @list_servers; + my $list_names_servers; + foreach my $server (@servers_id) { + my $dbh_metaconsole = enterprise_hook('get_node_dbh',[$conf, $server, $dbh]); + + my $agent_id = get_agent_id($dbh_metaconsole,$agent_name); + + if ($agent_id == -1) { + next; + } + else { + push @list_servers,$server; + } + } + + if (scalar(@list_servers) > 0) { + $list_names_servers = join(',',@list_servers); + print_log "[INFO] Agent '$agent_name' found in server with IDS: $list_names_servers\n\n"; + } + else { + print_log "[ERROR] Agent '$agent_name' not found in any node\n\n"; + } } } + } else { - print_log "[ERROR] This functions only working in metaconsole system\n\n"; + print_log "[ERROR] This function can only be used in metaconsole\n\n"; } } @@ -4835,7 +5910,7 @@ sub cli_stop_downtime () { # Related option: --get_module_data ############################################################################### sub cli_module_get_data () { - my ($agent_name, $module_name, $interval, $csv_separator) = @ARGV[2..5]; + my ($agent_name, $module_name, $interval, $csv_separator, $use_alias) = @ARGV[2..6]; $csv_separator = '|' unless defined($csv_separator); @@ -4843,57 +5918,112 @@ sub cli_module_get_data () { print_log "[ERROR] Interval must be a possitive value\n\n"; exit; } + + my @id_agents; - - - my $agent_id = get_agent_id($dbh,$agent_name); - exist_check($agent_id, 'agent name', $agent_name); - - my $module_id = get_agent_module_id($dbh, $module_name, $agent_id); - exist_check($module_id, 'module name', $module_name); - - my $id_agent_module = get_agent_module_id ($dbh, $module_name, $agent_id); - - my $module_type_id = get_db_value($dbh, - "SELECT id_tipo_modulo FROM tagente_modulo WHERE id_agente_modulo = ?", - $id_agent_module); - - my $module_type = get_db_value($dbh, - "SELECT nombre FROM ttipo_modulo WHERE id_tipo = ?", - $module_type_id); - - my @data = NULL; - if ($module_type eq "log4x") { - @data = get_db_rows ($dbh, "SELECT utimestamp, datos - FROM tagente_datos_log4x - WHERE id_agente_modulo = $id_agent_module - AND utimestamp > (UNIX_TIMESTAMP(NOW()) - $interval) - ORDER BY utimestamp DESC"); - } - elsif ($module_type =~ m/_string/) { - print("aaaa\n"); - @data = get_db_rows ($dbh, "SELECT utimestamp, datos - FROM tagente_datos_string - WHERE id_agente_modulo = $id_agent_module - AND utimestamp > (UNIX_TIMESTAMP(NOW()) - $interval) - ORDER BY utimestamp DESC"); - } - else { - @data = get_db_rows ($dbh, "SELECT utimestamp, datos - FROM tagente_datos - WHERE id_agente_modulo = $id_agent_module - AND utimestamp > (UNIX_TIMESTAMP(NOW()) - $interval) - ORDER BY utimestamp DESC"); - } - - - - foreach my $data_timestamp (@data) { - print $data_timestamp->{'utimestamp'}; - print $csv_separator; - print $data_timestamp->{'datos'}; - print "\n"; + if (defined $use_alias and $use_alias eq 'use_alias') { + @id_agents = get_agent_ids_from_alias($dbh,$agent_name); + + my $agent_id; + + foreach my $id (@id_agents) { + $agent_id = $id->{'id_agente'}; # se hace para cada agente + exist_check($agent_id, 'agent name', $agent_name); + + my $module_id = get_agent_module_id($dbh, $module_name, $agent_id); # se hace para ada agente + if ($module_id == -1) { + next; + } + + my $id_agent_module = get_agent_module_id ($dbh, $module_name, $agent_id); # 6 + + my $module_type_id = get_db_value($dbh, + "SELECT id_tipo_modulo FROM tagente_modulo WHERE id_agente_modulo = ?", + $id_agent_module); # se hace para cada agente + + my $module_type = get_db_value($dbh, + "SELECT nombre FROM ttipo_modulo WHERE id_tipo = ?", + $module_type_id); # se hace para cada agente + + my @data = NULL; + if ($module_type eq "log4x") { + @data = get_db_rows ($dbh, "SELECT utimestamp, datos + FROM tagente_datos_log4x + WHERE id_agente_modulo = $id_agent_module + AND utimestamp > (UNIX_TIMESTAMP(NOW()) - $interval) + ORDER BY utimestamp DESC"); + } + elsif ($module_type =~ m/_string/) { + @data = get_db_rows ($dbh, "SELECT utimestamp, datos + FROM tagente_datos_string + WHERE id_agente_modulo = $id_agent_module + AND utimestamp > (UNIX_TIMESTAMP(NOW()) - $interval) + ORDER BY utimestamp DESC"); + } + else { + @data = get_db_rows ($dbh, "SELECT utimestamp, datos + FROM tagente_datos + WHERE id_agente_modulo = $id_agent_module + AND utimestamp > (UNIX_TIMESTAMP(NOW()) - $interval) + ORDER BY utimestamp DESC"); + } + + foreach my $data_timestamp (@data) { + print $data_timestamp->{'utimestamp'}; + print $csv_separator; + print $data_timestamp->{'datos'}; + print "\n"; + } + } + + } else { + my $agent_id = get_agent_id($dbh,$agent_name); # se hace para cada agente + exist_check($agent_id, 'agent name', $agent_name); + + my $module_id = get_agent_module_id($dbh, $module_name, $agent_id); # se hace para ada agente + exist_check($module_id, 'module name', $module_name); + + my $id_agent_module = get_agent_module_id ($dbh, $module_name, $agent_id); # 6 + + my $module_type_id = get_db_value($dbh, + "SELECT id_tipo_modulo FROM tagente_modulo WHERE id_agente_modulo = ?", + $id_agent_module); # se hace para cada agente + + my $module_type = get_db_value($dbh, + "SELECT nombre FROM ttipo_modulo WHERE id_tipo = ?", + $module_type_id); # se hace para cada agente + + my @data = NULL; + if ($module_type eq "log4x") { + @data = get_db_rows ($dbh, "SELECT utimestamp, datos + FROM tagente_datos_log4x + WHERE id_agente_modulo = $id_agent_module + AND utimestamp > (UNIX_TIMESTAMP(NOW()) - $interval) + ORDER BY utimestamp DESC"); + } + elsif ($module_type =~ m/_string/) { + @data = get_db_rows ($dbh, "SELECT utimestamp, datos + FROM tagente_datos_string + WHERE id_agente_modulo = $id_agent_module + AND utimestamp > (UNIX_TIMESTAMP(NOW()) - $interval) + ORDER BY utimestamp DESC"); + } + else { + @data = get_db_rows ($dbh, "SELECT utimestamp, datos + FROM tagente_datos + WHERE id_agente_modulo = $id_agent_module + AND utimestamp > (UNIX_TIMESTAMP(NOW()) - $interval) + ORDER BY utimestamp DESC"); + } + + foreach my $data_timestamp (@data) { + print $data_timestamp->{'utimestamp'}; + print $csv_separator; + print $data_timestamp->{'datos'}; + print "\n"; + } } + exit; } @@ -6023,7 +7153,7 @@ sub pandora_manage_main ($$$) { cli_create_agent(); } elsif ($param eq '--delete_agent') { - param_check($ltotal, 1); + param_check($ltotal, 2, 1); cli_delete_agent(); } elsif ($param eq '--create_data_module') { @@ -6031,7 +7161,7 @@ sub pandora_manage_main ($$$) { cli_create_data_module(0); } elsif ($param eq '--create_web_module') { - param_check($ltotal, 38, 35); + param_check($ltotal, 39, 36); cli_create_web_module(0); } @@ -6048,19 +7178,19 @@ sub pandora_manage_main ($$$) { cli_module_group_synch(); } elsif ($param eq '--create_network_module') { - param_check($ltotal, 32, 20); + param_check($ltotal, 33, 21); cli_create_network_module(0); } elsif ($param eq '--create_snmp_module') { - param_check($ltotal, 40, 28); + param_check($ltotal, 41, 29); cli_create_snmp_module(0); } elsif ($param eq '--create_plugin_module') { - param_check($ltotal, 34, 19); + param_check($ltotal, 35, 20); cli_create_plugin_module(0); } elsif ($param eq '--delete_module') { - param_check($ltotal, 2); + param_check($ltotal, 3, 1); cli_delete_module(); } elsif ($param eq '--delete_not_policy_modules') { @@ -6068,23 +7198,23 @@ sub pandora_manage_main ($$$) { cli_delete_not_policy_modules(); } elsif ($param eq '--create_template_module') { - param_check($ltotal, 3); + param_check($ltotal, 4, 1); cli_create_template_module(); } elsif ($param eq '--delete_template_module') { - param_check($ltotal, 3); + param_check($ltotal, 4, 1); cli_delete_template_module(); } elsif ($param eq '--create_template_action') { - param_check($ltotal, 6, 2); + param_check($ltotal, 7, 3); cli_create_template_action(); } elsif ($param eq '--delete_template_action') { - param_check($ltotal, 4); + param_check($ltotal,5, 1); cli_delete_template_action(); } elsif ($param eq '--data_module') { - param_check($ltotal, 6, 1); + param_check($ltotal, 7, 2); cli_data_module(); } elsif ($param eq '--create_user') { @@ -6108,11 +7238,11 @@ sub pandora_manage_main ($$$) { cli_delete_profile(); } elsif ($param eq '--create_event') { - param_check($ltotal, 18, 15); + param_check($ltotal, 19, 16); cli_create_event(); } elsif ($param eq '--validate_event') { - param_check($ltotal, 7, 6); + param_check($ltotal, 8, 7); cli_validate_event(); } elsif ($param eq '--validate_event_id') { @@ -6132,7 +7262,7 @@ sub pandora_manage_main ($$$) { cli_create_incident(); } elsif ($param eq '--delete_data') { - param_check($ltotal, 3, 1); + param_check($ltotal, 4, 2); cli_delete_data($ltotal); } elsif ($param eq '--apply_policy') { @@ -6156,7 +7286,7 @@ sub pandora_manage_main ($$$) { cli_update_group(); } elsif ($param eq '--add_agent_to_policy') { - param_check($ltotal, 2); + param_check($ltotal, 3, 1); cli_policy_add_agent(); } elsif ($param eq '--remove_agent_from_policy') { @@ -6180,7 +7310,7 @@ sub pandora_manage_main ($$$) { cli_user_add_profile(); } elsif ($param eq '--get_module_data') { - param_check($ltotal, 4, 1); + param_check($ltotal, 5, 2); cli_module_get_data(); } elsif ($param eq '--add_collection_to_policy') { @@ -6196,7 +7326,7 @@ sub pandora_manage_main ($$$) { cli_create_policy(); } elsif ($param eq '--create_policy_data_module') { - param_check($ltotal, 28, 20); + param_check($ltotal, 29, 21); cli_create_data_module(1); } elsif ($param eq '--create_policy_web_module') { @@ -6244,7 +7374,7 @@ sub pandora_manage_main ($$$) { cli_alert_template_update(); } elsif ($param eq '--update_module') { - param_check($ltotal, 4); + param_check($ltotal, 6, 2); cli_module_update(); } elsif ($param eq '--exec_from_file') { @@ -6270,11 +7400,11 @@ sub pandora_manage_main ($$$) { cli_get_module_id(); } elsif ($param eq '--get_agent_group') { - param_check($ltotal, 1); + param_check($ltotal, 2, 1); cli_get_agent_group(); } elsif ($param eq '--get_agent_group_id') { - param_check($ltotal, 1); + param_check($ltotal, 2, 1); cli_get_agent_group_id(); } elsif ($param eq '--get_agents_module_current_data') { @@ -6282,7 +7412,7 @@ sub pandora_manage_main ($$$) { cli_get_agents_module_current_data(); } elsif ($param eq '--get_agent_modules') { - param_check($ltotal, 1); + param_check($ltotal, 2, 1); cli_get_agent_modules(); } elsif ($param eq '--get_policy_modules') { @@ -6290,23 +7420,23 @@ sub pandora_manage_main ($$$) { cli_get_policy_modules(); } elsif ($param eq '--get_policies') { - param_check($ltotal, 1, 1); + param_check($ltotal, 2, 2); cli_get_policies(); } elsif ($param eq '--get_agents') { - param_check($ltotal, 6, 6); + param_check($ltotal, 7, 7); cli_get_agents(); } elsif ($param eq '--delete_conf_file') { - param_check($ltotal, 1); + param_check($ltotal, 2, 1); cli_delete_conf_file(); } elsif ($param eq '--clean_conf_file') { - param_check($ltotal, 1, 1); + param_check($ltotal, 2, 1); cli_clean_conf_file(); } elsif ($param eq '--update_agent') { - param_check($ltotal, 3); + param_check($ltotal, 4, 1); cli_agent_update(); } elsif ($param eq '--get_bad_conf_files') { @@ -6314,7 +7444,7 @@ sub pandora_manage_main ($$$) { cli_get_bad_conf_files(); } elsif ($param eq '--create_network_module_from_component') { - param_check($ltotal, 2); + param_check($ltotal, 3, 1); cli_create_network_module_from_component(); } elsif ($param eq '--create_network_component') { @@ -6366,7 +7496,7 @@ sub pandora_manage_main ($$$) { cli_delete_special_day(); } elsif ($param eq '--create_data_module_from_local_component') { - param_check($ltotal, 2); + param_check($ltotal, 3, 1); cli_create_data_module_from_local_component(); } elsif ($param eq '--create_local_component') { @@ -6414,7 +7544,7 @@ sub pandora_manage_main ($$$) { cli_set_delete_planned_downtime(); } elsif ($param eq '--locate_agent') { - param_check($ltotal, 1); + param_check($ltotal, 2, 1); cli_locate_agent(); } elsif ($param eq '--create_visual_console') { @@ -6638,22 +7768,53 @@ sub cli_add_collection_to_policy () { ############################################################################## sub cli_create_data_module_from_local_component() { - my ($agent_name, $component_name) = @ARGV[2..3]; - - my $agent_id = get_agent_id($dbh,$agent_name); - exist_check($agent_id,'agent',$agent_name); + my ($agent_name, $component_name, $use_alias) = @ARGV[2..4]; + + my @id_agents; + + if (defined $use_alias and $use_alias eq 'use_alias') { + @id_agents = get_agent_ids_from_alias($dbh,$agent_name); + + my $agent_id; + + foreach my $id (@id_agents) { + $agent_id = $id->{'id_agente'}; + exist_check($agent_id,'agent',$agent_name); + + my $lc_id = pandora_get_local_component_id($dbh, $component_name); + exist_check($lc_id,'local component',$component_name); + + my $module_exists = get_agent_module_id($dbh, $component_name, $agent_id); + if ($module_exists ne -1) { + next; + } + + # Get local component data + my $component = get_db_single_row ($dbh, 'SELECT * FROM tlocal_component WHERE id = ?', $lc_id); + + print_log "[INFO] Creating module from local component '$component_name'\n\n"; + + #~ pandora_create_module_from_local_component ($conf, $component, $agent_id, $dbh); + enterprise_hook('pandora_create_module_from_local_component',[$conf, $component, $agent_id, $dbh]); + } + } else { + my $agent_id = get_agent_id($dbh,$agent_name); + exist_check($agent_id,'agent',$agent_name); + + my $lc_id = pandora_get_local_component_id($dbh, $component_name); + exist_check($lc_id,'local component',$component_name); - my $lc_id = pandora_get_local_component_id($dbh, $component_name); - exist_check($lc_id,'local component',$component_name); - - my $module_exists = get_agent_module_id($dbh, $component_name, $agent_id); - non_exist_check($module_exists, 'module name', $component_name); - - # Get local component data - my $component = get_db_single_row ($dbh, 'SELECT * FROM tlocal_component WHERE id = ?', $lc_id); - - #~ pandora_create_module_from_local_component ($conf, $component, $agent_id, $dbh); - enterprise_hook('pandora_create_module_from_local_component',[$conf, $component, $agent_id, $dbh]); + my $module_exists = get_agent_module_id($dbh, $component_name, $agent_id); + non_exist_check($module_exists, 'module name', $component_name); + + # Get local component data + my $component = get_db_single_row ($dbh, 'SELECT * FROM tlocal_component WHERE id = ?', $lc_id); + + print_log "[INFO] Creating module from local component '$component_name'\n\n"; + + #~ pandora_create_module_from_local_component ($conf, $component, $agent_id, $dbh); + enterprise_hook('pandora_create_module_from_local_component',[$conf, $component, $agent_id, $dbh]); + } } ############################################################################## # Create policy data module from local component.