diff --git a/pandora_agents/pc/AIX/pandora_agent.conf b/pandora_agents/pc/AIX/pandora_agent.conf index 4fff860931..f3657d3a7a 100644 --- a/pandora_agents/pc/AIX/pandora_agent.conf +++ b/pandora_agents/pc/AIX/pandora_agent.conf @@ -1,5 +1,5 @@ # Base config file for Pandora FMS agents -# Version 7.0NG.739, AIX version +# Version 7.0NG.740, AIX version # Licensed under GPL license v2, # Copyright (c) 2003-2010 Artica Soluciones Tecnologicas # http://www.pandorafms.com diff --git a/pandora_agents/pc/FreeBSD/pandora_agent.conf b/pandora_agents/pc/FreeBSD/pandora_agent.conf index f33b304a7c..95fd1090ad 100644 --- a/pandora_agents/pc/FreeBSD/pandora_agent.conf +++ b/pandora_agents/pc/FreeBSD/pandora_agent.conf @@ -1,5 +1,5 @@ # Base config file for Pandora FMS agents -# Version 7.0NG.739, FreeBSD Version +# Version 7.0NG.740, FreeBSD Version # Licensed under GPL license v2, # Copyright (c) 2003-2010 Artica Soluciones Tecnologicas # http://www.pandorafms.com diff --git a/pandora_agents/pc/HP-UX/pandora_agent.conf b/pandora_agents/pc/HP-UX/pandora_agent.conf index 1d627fc222..04122feb30 100644 --- a/pandora_agents/pc/HP-UX/pandora_agent.conf +++ b/pandora_agents/pc/HP-UX/pandora_agent.conf @@ -1,5 +1,5 @@ # Base config file for Pandora FMS agents -# Version 7.0NG.739, HP-UX Version +# Version 7.0NG.740, HP-UX Version # Licensed under GPL license v2, # Copyright (c) 2003-2009 Artica Soluciones Tecnologicas # http://www.pandorafms.com diff --git a/pandora_agents/pc/Linux/pandora_agent.conf b/pandora_agents/pc/Linux/pandora_agent.conf index 9501fa5222..b083e7f3aa 100644 --- a/pandora_agents/pc/Linux/pandora_agent.conf +++ b/pandora_agents/pc/Linux/pandora_agent.conf @@ -1,5 +1,5 @@ # Base config file for Pandora FMS agents -# Version 7.0NG.739, GNU/Linux +# Version 7.0NG.740, GNU/Linux # Licensed under GPL license v2, # Copyright (c) 2003-2009 Artica Soluciones Tecnologicas # http://www.pandorafms.com diff --git a/pandora_agents/pc/NT4/pandora_agent.conf b/pandora_agents/pc/NT4/pandora_agent.conf index bdd4bcf425..4bb88c5b4e 100644 --- a/pandora_agents/pc/NT4/pandora_agent.conf +++ b/pandora_agents/pc/NT4/pandora_agent.conf @@ -1,5 +1,5 @@ # Base config file for Pandora FMS agents -# Version 7.0NG.739, GNU/Linux +# Version 7.0NG.740, GNU/Linux # Licensed under GPL license v2, # Copyright (c) 2003-2009 Artica Soluciones Tecnologicas # http://www.pandorafms.com diff --git a/pandora_agents/pc/SunOS/pandora_agent.conf b/pandora_agents/pc/SunOS/pandora_agent.conf index f80f403dd8..a90b7227ca 100644 --- a/pandora_agents/pc/SunOS/pandora_agent.conf +++ b/pandora_agents/pc/SunOS/pandora_agent.conf @@ -1,5 +1,5 @@ # Base config file for Pandora FMS agents -# Version 7.0NG.739, Solaris Version +# Version 7.0NG.740, Solaris Version # Licensed under GPL license v2, # Copyright (c) 2003-2009 Artica Soluciones Tecnologicas # http://www.pandorafms.com diff --git a/pandora_agents/pc/Win32/pandora_agent.conf b/pandora_agents/pc/Win32/pandora_agent.conf index 86bfede9d7..d750898b70 100644 --- a/pandora_agents/pc/Win32/pandora_agent.conf +++ b/pandora_agents/pc/Win32/pandora_agent.conf @@ -1,6 +1,6 @@ # Base config file for Pandora FMS Windows Agent # (c) 2006-2010 Artica Soluciones Tecnologicas -# Version 7.0NG.739 +# Version 7.0NG.740 # This program is Free Software, you can redistribute it and/or modify it # under the terms of the GNU General Public Licence as published by the Free Software diff --git a/pandora_agents/shellscript/aix/pandora_agent.conf b/pandora_agents/shellscript/aix/pandora_agent.conf index aa65eb24d9..6a663d6331 100644 --- a/pandora_agents/shellscript/aix/pandora_agent.conf +++ b/pandora_agents/shellscript/aix/pandora_agent.conf @@ -1,6 +1,6 @@ # Fichero de configuracion base de agentes de Pandora # Base config file for Pandora agents -# Version 7.0NG.739, AIX version +# Version 7.0NG.740, AIX version # General Parameters # ================== diff --git a/pandora_agents/shellscript/bsd-ipso/pandora_agent.conf b/pandora_agents/shellscript/bsd-ipso/pandora_agent.conf index 7c225fea1c..99e4c62462 100644 --- a/pandora_agents/shellscript/bsd-ipso/pandora_agent.conf +++ b/pandora_agents/shellscript/bsd-ipso/pandora_agent.conf @@ -1,6 +1,6 @@ # Fichero de configuracion base de agentes de Pandora # Base config file for Pandora agents -# Version 7.0NG.739 +# Version 7.0NG.740 # FreeBSD/IPSO version # Licenced under GPL licence, 2003-2007 Sancho Lerena diff --git a/pandora_agents/shellscript/hp-ux/pandora_agent.conf b/pandora_agents/shellscript/hp-ux/pandora_agent.conf index 06bd17ed45..7a9d2c4347 100644 --- a/pandora_agents/shellscript/hp-ux/pandora_agent.conf +++ b/pandora_agents/shellscript/hp-ux/pandora_agent.conf @@ -1,6 +1,6 @@ # Fichero de configuracion base de agentes de Pandora # Base config file for Pandora agents -# Version 7.0NG.739, HPUX Version +# Version 7.0NG.740, HPUX Version # General Parameters # ================== diff --git a/pandora_agents/shellscript/linux/pandora_agent.conf b/pandora_agents/shellscript/linux/pandora_agent.conf index 54b0e06dbf..a55d96a952 100644 --- a/pandora_agents/shellscript/linux/pandora_agent.conf +++ b/pandora_agents/shellscript/linux/pandora_agent.conf @@ -1,5 +1,5 @@ # Base config file for Pandora FMS agents -# Version 7.0NG.739 +# Version 7.0NG.740 # Licensed under GPL license v2, # (c) 2003-2010 Artica Soluciones Tecnologicas # please visit http://pandora.sourceforge.net diff --git a/pandora_agents/shellscript/mac_osx/pandora_agent.conf b/pandora_agents/shellscript/mac_osx/pandora_agent.conf index f5205f4d16..a24a24d16e 100644 --- a/pandora_agents/shellscript/mac_osx/pandora_agent.conf +++ b/pandora_agents/shellscript/mac_osx/pandora_agent.conf @@ -1,5 +1,5 @@ # Base config file for Pandora FMS agents -# Version 7.0NG.739 +# Version 7.0NG.740 # Licensed under GPL license v2, # (c) 2003-2009 Artica Soluciones Tecnologicas # please visit http://pandora.sourceforge.net diff --git a/pandora_agents/shellscript/openWRT/pandora_agent.conf b/pandora_agents/shellscript/openWRT/pandora_agent.conf index 7d6a12d62b..74d515cb4e 100644 --- a/pandora_agents/shellscript/openWRT/pandora_agent.conf +++ b/pandora_agents/shellscript/openWRT/pandora_agent.conf @@ -1,5 +1,5 @@ # Base config file for Pandora FMS agents -# Version 7.0NG.739 +# Version 7.0NG.740 # Licensed under GPL license v2, # please visit http://pandora.sourceforge.net diff --git a/pandora_agents/shellscript/solaris/pandora_agent.conf b/pandora_agents/shellscript/solaris/pandora_agent.conf index cbae89b0b5..dc915ef18e 100644 --- a/pandora_agents/shellscript/solaris/pandora_agent.conf +++ b/pandora_agents/shellscript/solaris/pandora_agent.conf @@ -1,6 +1,6 @@ # Fichero de configuracion base de agentes de Pandora # Base config file for Pandora agents -# Version 7.0NG.739, Solaris version +# Version 7.0NG.740, Solaris version # General Parameters # ================== diff --git a/pandora_agents/unix/AIX/pandora_agent.conf b/pandora_agents/unix/AIX/pandora_agent.conf index 7f2791b05f..de7e97911b 100644 --- a/pandora_agents/unix/AIX/pandora_agent.conf +++ b/pandora_agents/unix/AIX/pandora_agent.conf @@ -1,5 +1,5 @@ # Base config file for Pandora FMS agents -# Version 7.0NG.739, AIX version +# Version 7.0NG.740, AIX version # Licensed under GPL license v2, # Copyright (c) 2003-2010 Artica Soluciones Tecnologicas # http://www.pandorafms.com diff --git a/pandora_agents/unix/DEBIAN/control b/pandora_agents/unix/DEBIAN/control index 9c47b6ec9a..ce165fde19 100644 --- a/pandora_agents/unix/DEBIAN/control +++ b/pandora_agents/unix/DEBIAN/control @@ -1,5 +1,5 @@ package: pandorafms-agent-unix -Version: 7.0NG.739-191028 +Version: 7.0NG.740-191029 Architecture: all Priority: optional Section: admin diff --git a/pandora_agents/unix/DEBIAN/make_deb_package.sh b/pandora_agents/unix/DEBIAN/make_deb_package.sh index ace99c417d..a4a8dce448 100644 --- a/pandora_agents/unix/DEBIAN/make_deb_package.sh +++ b/pandora_agents/unix/DEBIAN/make_deb_package.sh @@ -14,7 +14,7 @@ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. -pandora_version="7.0NG.739-191028" +pandora_version="7.0NG.740-191029" echo "Test if you has the tools for to make the packages." whereis dpkg-deb | cut -d":" -f2 | grep dpkg-deb > /dev/null diff --git a/pandora_agents/unix/Darwin/pandora_agent.conf b/pandora_agents/unix/Darwin/pandora_agent.conf index 7f403ae687..34dd1bf4fc 100644 --- a/pandora_agents/unix/Darwin/pandora_agent.conf +++ b/pandora_agents/unix/Darwin/pandora_agent.conf @@ -1,5 +1,5 @@ # Base config file for Pandora FMS agents -# Version 7.0NG.739, GNU/Linux +# Version 7.0NG.740, GNU/Linux # Licensed under GPL license v2, # Copyright (c) 2003-2012 Artica Soluciones Tecnologicas # http://www.pandorafms.com diff --git a/pandora_agents/unix/FreeBSD/pandora_agent.conf b/pandora_agents/unix/FreeBSD/pandora_agent.conf index 2871669f88..e4ad74ffe7 100644 --- a/pandora_agents/unix/FreeBSD/pandora_agent.conf +++ b/pandora_agents/unix/FreeBSD/pandora_agent.conf @@ -1,5 +1,5 @@ # Base config file for Pandora FMS agents -# Version 7.0NG.739, FreeBSD Version +# Version 7.0NG.740, FreeBSD Version # Licensed under GPL license v2, # Copyright (c) 2003-2016 Artica Soluciones Tecnologicas # http://www.pandorafms.com diff --git a/pandora_agents/unix/HP-UX/pandora_agent.conf b/pandora_agents/unix/HP-UX/pandora_agent.conf index 1fcbc17977..e55445fe13 100644 --- a/pandora_agents/unix/HP-UX/pandora_agent.conf +++ b/pandora_agents/unix/HP-UX/pandora_agent.conf @@ -1,5 +1,5 @@ # Base config file for Pandora FMS agents -# Version 7.0NG.739, HP-UX Version +# Version 7.0NG.740, HP-UX Version # Licensed under GPL license v2, # Copyright (c) 2003-2009 Artica Soluciones Tecnologicas # http://www.pandorafms.com diff --git a/pandora_agents/unix/Linux/pandora_agent.conf b/pandora_agents/unix/Linux/pandora_agent.conf index 938565241d..54ace6a811 100644 --- a/pandora_agents/unix/Linux/pandora_agent.conf +++ b/pandora_agents/unix/Linux/pandora_agent.conf @@ -1,5 +1,5 @@ # Base config file for Pandora FMS agents -# Version 7.0NG.739, GNU/Linux +# Version 7.0NG.740, GNU/Linux # Licensed under GPL license v2, # Copyright (c) 2003-2014 Artica Soluciones Tecnologicas # http://www.pandorafms.com diff --git a/pandora_agents/unix/NT4/pandora_agent.conf b/pandora_agents/unix/NT4/pandora_agent.conf index f9a2b10b93..94e32e8056 100644 --- a/pandora_agents/unix/NT4/pandora_agent.conf +++ b/pandora_agents/unix/NT4/pandora_agent.conf @@ -1,5 +1,5 @@ # Base config file for Pandora FMS agents -# Version 7.0NG.739, GNU/Linux +# Version 7.0NG.740, GNU/Linux # Licensed under GPL license v2, # Copyright (c) 2003-2009 Artica Soluciones Tecnologicas # http://www.pandorafms.com diff --git a/pandora_agents/unix/NetBSD/pandora_agent.conf b/pandora_agents/unix/NetBSD/pandora_agent.conf index 2f108c2011..71e7d14192 100644 --- a/pandora_agents/unix/NetBSD/pandora_agent.conf +++ b/pandora_agents/unix/NetBSD/pandora_agent.conf @@ -1,5 +1,5 @@ # Base config file for Pandora FMS agents -# Version 7.0NG.739, NetBSD Version +# Version 7.0NG.740, NetBSD Version # Licensed under GPL license v2, # Copyright (c) 2003-2010 Artica Soluciones Tecnologicas # http://www.pandorafms.com diff --git a/pandora_agents/unix/SunOS/pandora_agent.conf b/pandora_agents/unix/SunOS/pandora_agent.conf index 47f17aae0f..cc89dee501 100644 --- a/pandora_agents/unix/SunOS/pandora_agent.conf +++ b/pandora_agents/unix/SunOS/pandora_agent.conf @@ -1,5 +1,5 @@ # Base config file for Pandora FMS agents -# Version 7.0NG.739, Solaris Version +# Version 7.0NG.740, Solaris Version # Licensed under GPL license v2, # Copyright (c) 2003-2009 Artica Soluciones Tecnologicas # http://www.pandorafms.com diff --git a/pandora_agents/unix/pandora_agent b/pandora_agents/unix/pandora_agent index a011385d9d..4bbac522b1 100755 --- a/pandora_agents/unix/pandora_agent +++ b/pandora_agents/unix/pandora_agent @@ -41,8 +41,8 @@ my $Sem = undef; # Semaphore used to control the number of threads my $ThreadSem = undef; -use constant AGENT_VERSION => '7.0NG.739'; -use constant AGENT_BUILD => '191028'; +use constant AGENT_VERSION => '7.0NG.740'; +use constant AGENT_BUILD => '191029'; # Agent log default file size maximum and instances use constant DEFAULT_MAX_LOG_SIZE => 600000; diff --git a/pandora_agents/unix/pandora_agent.redhat.spec b/pandora_agents/unix/pandora_agent.redhat.spec index ded364b98a..79f2310c11 100644 --- a/pandora_agents/unix/pandora_agent.redhat.spec +++ b/pandora_agents/unix/pandora_agent.redhat.spec @@ -2,8 +2,8 @@ #Pandora FMS Linux Agent # %define name pandorafms_agent_unix -%define version 7.0NG.739 -%define release 191028 +%define version 7.0NG.740 +%define release 191029 Summary: Pandora FMS Linux agent, PERL version Name: %{name} diff --git a/pandora_agents/unix/pandora_agent.spec b/pandora_agents/unix/pandora_agent.spec index f503af722d..392fa174ce 100644 --- a/pandora_agents/unix/pandora_agent.spec +++ b/pandora_agents/unix/pandora_agent.spec @@ -2,8 +2,8 @@ #Pandora FMS Linux Agent # %define name pandorafms_agent_unix -%define version 7.0NG.739 -%define release 191028 +%define version 7.0NG.740 +%define release 191029 Summary: Pandora FMS Linux agent, PERL version Name: %{name} diff --git a/pandora_agents/unix/pandora_agent_installer b/pandora_agents/unix/pandora_agent_installer index e1ef018c0e..cc4b78b51c 100755 --- a/pandora_agents/unix/pandora_agent_installer +++ b/pandora_agents/unix/pandora_agent_installer @@ -9,8 +9,8 @@ # Please see http://www.pandorafms.org. This code is licensed under GPL 2.0 license. # ********************************************************************** -PI_VERSION="7.0NG.739" -PI_BUILD="191028" +PI_VERSION="7.0NG.740" +PI_BUILD="191029" OS_NAME=`uname -s` FORCE=0 diff --git a/pandora_agents/win32/bin/pandora_agent.conf b/pandora_agents/win32/bin/pandora_agent.conf index d6bf2f9005..79660e5b5d 100644 --- a/pandora_agents/win32/bin/pandora_agent.conf +++ b/pandora_agents/win32/bin/pandora_agent.conf @@ -1,6 +1,6 @@ # Base config file for Pandora FMS Windows Agent # (c) 2006-2017 Artica Soluciones Tecnologicas -# Version 7.0NG.739 +# Version 7.0NG.740 # This program is Free Software, you can redistribute it and/or modify it # under the terms of the GNU General Public Licence as published by the Free Software diff --git a/pandora_agents/win32/installer/pandora.mpi b/pandora_agents/win32/installer/pandora.mpi index fd4c5af3e4..730edbe5de 100644 --- a/pandora_agents/win32/installer/pandora.mpi +++ b/pandora_agents/win32/installer/pandora.mpi @@ -3,7 +3,7 @@ AllowLanguageSelection {Yes} AppName -{Pandora FMS Windows Agent v7.0NG.739} +{Pandora FMS Windows Agent v7.0NG.740} ApplicationID {17E3D2CF-CA02-406B-8A80-9D31C17BD08F} @@ -186,7 +186,7 @@ UpgradeApplicationID {} Version -{191028} +{191029} ViewReadme {Yes} diff --git a/pandora_agents/win32/pandora.cc b/pandora_agents/win32/pandora.cc index 2687ff4b4f..823b486b0e 100644 --- a/pandora_agents/win32/pandora.cc +++ b/pandora_agents/win32/pandora.cc @@ -30,7 +30,7 @@ using namespace Pandora; using namespace Pandora_Strutils; #define PATH_SIZE _MAX_PATH+1 -#define PANDORA_VERSION ("7.0NG.739(Build 191028)") +#define PANDORA_VERSION ("7.0NG.740(Build 191029)") string pandora_path; string pandora_dir; diff --git a/pandora_agents/win32/versioninfo.rc b/pandora_agents/win32/versioninfo.rc index fe4067fa72..d714543482 100644 --- a/pandora_agents/win32/versioninfo.rc +++ b/pandora_agents/win32/versioninfo.rc @@ -11,7 +11,7 @@ BEGIN VALUE "LegalCopyright", "Artica ST" VALUE "OriginalFilename", "PandoraAgent.exe" VALUE "ProductName", "Pandora FMS Windows Agent" - VALUE "ProductVersion", "(7.0NG.739(Build 191028))" + VALUE "ProductVersion", "(7.0NG.740(Build 191029))" VALUE "FileVersion", "1.0.0.0" END END diff --git a/pandora_console/DEBIAN/control b/pandora_console/DEBIAN/control index a2bc785644..cab514f37e 100644 --- a/pandora_console/DEBIAN/control +++ b/pandora_console/DEBIAN/control @@ -1,5 +1,5 @@ package: pandorafms-console -Version: 7.0NG.739-191028 +Version: 7.0NG.740-191029 Architecture: all Priority: optional Section: admin diff --git a/pandora_console/DEBIAN/make_deb_package.sh b/pandora_console/DEBIAN/make_deb_package.sh index d995e7f837..64140d02ad 100644 --- a/pandora_console/DEBIAN/make_deb_package.sh +++ b/pandora_console/DEBIAN/make_deb_package.sh @@ -14,7 +14,7 @@ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. -pandora_version="7.0NG.739-191028" +pandora_version="7.0NG.740-191029" package_pear=0 package_pandora=1 diff --git a/pandora_console/include/chart_generator.php b/pandora_console/include/chart_generator.php index cdd1b88460..0d1d138ae6 100644 --- a/pandora_console/include/chart_generator.php +++ b/pandora_console/include/chart_generator.php @@ -1,16 +1,32 @@ + +

Access is not granted

- + @@ -76,11 +105,18 @@ if ($config['metaconsole'] && !empty($server_id)) { $server = metaconsole_get_connection_by_id($server_id); // Error connecting. if (metaconsole_connect($server) !== NOERR) { - echo ''; - echo ''; - ui_print_error_message(__('There was a problem connecting with the node')); - echo ''; - echo ''; + ?> + + + + + + '; switch ($type_graph_pdf) { @@ -253,20 +291,7 @@ if (file_exists('languages/'.$user_language.'.mo') === true) { } echo ''; - + echoPhantomCallback(); ?> - - - diff --git a/pandora_console/include/config_process.php b/pandora_console/include/config_process.php index b0b018df59..484f8dd138 100644 --- a/pandora_console/include/config_process.php +++ b/pandora_console/include/config_process.php @@ -20,8 +20,8 @@ /** * Pandora build version and version */ -$build_version = 'PC191028'; -$pandora_version = 'v7.0NG.739'; +$build_version = 'PC191029'; +$pandora_version = 'v7.0NG.740'; // Do not overwrite default timezone set if defined. $script_tz = @date_default_timezone_get(); diff --git a/pandora_console/include/functions_api.php b/pandora_console/include/functions_api.php index ca52209101..5aa1020214 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) { @@ -1521,7 +1522,7 @@ function api_set_new_agent($thrash1, $thrash2, $other, $thrash3) if (($id_parent != 0) && (db_get_value_sql( 'SELECT id_agente_modulo FROM tagente_modulo - WHERE id_agente = '.$id_parent.' AND id_agente_modulo = '.$cascade_protection_module + WHERE id_agente = '.$id_parent.' AND id_agente_modulo = '.$cascade_protection_module ) === false) ) { returnError('parent_agent_not_exist', 'Is not a parent module to do cascade protection.'); @@ -1594,7 +1595,7 @@ function api_set_new_agent($thrash1, $thrash2, $other, $thrash3) $tpolicy_group_old = db_get_all_rows_sql( 'SELECT id_policy FROM tpolicy_groups - WHERE id_group = '.$grupo + WHERE id_group = '.$grupo ); if ($tpolicy_group_old) { @@ -1610,18 +1611,18 @@ function api_set_new_agent($thrash1, $thrash2, $other, $thrash3) } $info = '{"Name":"'.$nombre_agente.'", - "IP":"'.$direccion_agente.'", - "Group":"'.$grupo.'", - "Interval":"'.$intervalo.'", - "Comments":"'.$comentarios.'", - "Mode":"'.$modo.'", - "ID_parent:":"'.$id_parent.'", - "Server":"'.$server_name.'", - "ID os":"'.$id_os.'", - "Disabled":"'.$disabled.'", - "Custom ID":"'.$custom_id.'", - "Cascade protection":"'.$cascade_protection.'", - "Cascade protection module":"'.$cascade_protection_module.'"}'; + "IP":"'.$direccion_agente.'", + "Group":"'.$grupo.'", + "Interval":"'.$intervalo.'", + "Comments":"'.$comentarios.'", + "Mode":"'.$modo.'", + "ID_parent:":"'.$id_parent.'", + "Server":"'.$server_name.'", + "ID os":"'.$id_os.'", + "Disabled":"'.$disabled.'", + "Custom ID":"'.$custom_id.'", + "Cascade protection":"'.$cascade_protection.'", + "Cascade protection module":"'.$cascade_protection_module.'"}'; $unsafe_alias = io_safe_output($alias); db_pandora_audit( @@ -1813,17 +1814,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'); @@ -1832,8 +1839,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) { @@ -1842,19 +1849,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) { @@ -1941,20 +1976,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); @@ -2078,9 +2113,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] ); @@ -2427,9 +2462,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'] ); @@ -2469,10 +2504,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] ); @@ -2511,8 +2546,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) { @@ -2526,9 +2561,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); @@ -2551,9 +2586,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); @@ -2603,8 +2638,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) { @@ -2619,9 +2654,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); @@ -2647,9 +2682,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); @@ -2692,8 +2727,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) { @@ -2750,8 +2785,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) { @@ -2765,9 +2800,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); @@ -2791,9 +2826,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); @@ -2843,8 +2878,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) { @@ -2859,9 +2894,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); @@ -2887,9 +2922,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); @@ -2947,8 +2982,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 ); @@ -3046,12 +3081,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) { @@ -3069,7 +3114,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], @@ -3112,7 +3156,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 @@ -3280,17 +3344,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 = []; @@ -3300,7 +3374,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], @@ -3348,7 +3421,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 @@ -3509,17 +3602,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]; @@ -3529,7 +3632,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], @@ -3568,7 +3670,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 @@ -3593,7 +3715,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; @@ -3601,8 +3723,6 @@ function api_set_create_synthetic_module($id, $thrash1, $other, $thrash3) global $config; - $agentName = $id; - io_safe_input_array($other); if ($other['data'][0] == '') { @@ -3610,15 +3730,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]); @@ -3626,7 +3765,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, @@ -3639,151 +3777,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)]); + } } } } @@ -3933,8 +4232,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; @@ -3945,10 +4242,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]; @@ -3975,7 +4284,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], @@ -4013,7 +4321,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], @@ -4050,7 +4357,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 @@ -4753,9 +5080,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 ); @@ -5382,20 +5709,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); @@ -5416,14 +5771,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; + } } @@ -5562,7 +5932,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 @@ -5583,11 +5953,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') ) { @@ -5595,10 +5960,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]); @@ -5611,25 +5992,50 @@ 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); + + if ($result != 0) { + $delete_count++; + } + } + + returnError('error_delete_module_template_by_name', __('Module template has 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.')]); + } } } @@ -5672,15 +6078,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 ); @@ -5768,9 +6174,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 ); @@ -7654,8 +8060,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) { @@ -7944,10 +8350,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 @@ -7956,17 +8362,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) ); @@ -8285,7 +8691,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 = []; @@ -8294,17 +8700,27 @@ function otherParameter2Filter($other, $return_as_array=false) } if (isset($other['data'][2]) && $other['data'][2] != '') { - $idAgents = agents_get_agent_id_by_alias($other['data'][2]); + if ($use_agent_name === false) { + $idAgents = agents_get_agent_id_by_alias($other['data'][2]); - if (!empty($idAgents)) { - $idAgent = []; - foreach ($idAgents as $key => $value) { - $idAgent[] .= $value['id_agente']; + if (!empty($idAgents)) { + $idAgent = []; + foreach ($idAgents as $key => $value) { + $idAgent[] .= $value['id_agente']; + } + + $filter[] = 'id_agente IN ('.implode(',', $idAgent).')'; + } else { + $filter['sql'] = '1=0'; } - - $filter[] = 'id_agente IN ('.implode(',', $idAgent).')'; } else { - $filter['sql'] = '1=0'; + $idAgent = agents_get_agent_id($other['data'][2]); + + if (!empty($idAgent)) { + $filter[] = 'id_agente = '.$idAgent; + } else { + $filter['sql'] = '1=0'; + } } } @@ -8450,7 +8866,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.'%"'; } @@ -8486,48 +8902,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; } } @@ -8538,18 +8997,61 @@ function api_set_delete_module($id, $id2, $other, $trash1) return; } - if ($other['type'] == 'string') { - $simulate = false; - if ($other['data'] == 'simulate') { - $simulate = true; - } + $simulate = false; + if ($other['data'][0] == 'simulate') { + $simulate = true; + } + $agent_by_alias = false; + + 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) { @@ -8572,9 +9074,6 @@ function api_set_delete_module($id, $id2, $other, $trash1) returnData('string', $data); return; - } else { - returnError('error_parameter', 'Error in the parameters.'); - return; } } @@ -8605,14 +9104,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, @@ -8654,9 +9153,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; @@ -8736,11 +9255,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) { @@ -8776,66 +9314,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; } } @@ -9164,11 +9769,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) { @@ -9322,7 +9927,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( @@ -9405,9 +10012,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) { @@ -9639,7 +10246,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'][16] === '1') ? true : false; + + $filter = otherParameter2Filter($other, true, $use_agent_name); if (isset($filter['criticity'])) { $severity = $filter['criticity']; @@ -9774,7 +10383,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."'"; } @@ -9833,48 +10442,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; @@ -9882,25 +10491,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': @@ -9910,26 +10519,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; } @@ -9939,26 +10548,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': @@ -9967,21 +10576,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; } @@ -10112,7 +10721,10 @@ function api_get_events($trash1, $trash2, $other, $returnType, $user_in_db=null) } else if ($other['type'] == 'array') { $separator = $other['data'][0]; - $filterString = otherParameter2Filter($other); + // By default it uses agent alias. + $use_agent_name = ($other['data'][16] === '1') ? true : false; + + $filterString = otherParameter2Filter($other, false, $use_agent_name); } if (is_metaconsole()) { @@ -10596,25 +11208,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')]); + } } } @@ -10629,25 +11274,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')]); + } } } @@ -10687,8 +11365,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) { @@ -10736,8 +11414,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) { @@ -10787,8 +11465,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) { @@ -10836,8 +11514,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) { @@ -10864,7 +11542,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; @@ -10877,20 +11555,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']); + } } @@ -10906,7 +11623,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; @@ -10919,20 +11636,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']); + } } @@ -11004,11 +11760,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); @@ -11069,8 +11825,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); @@ -11146,8 +11902,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); @@ -11179,8 +11935,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 ); @@ -11221,15 +11977,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); @@ -11259,8 +12015,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 @@ -13788,8 +14544,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'] ); @@ -13832,8 +14588,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) { @@ -14002,8 +14758,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'] ); @@ -14045,12 +14801,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; @@ -14075,8 +14829,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) { @@ -14109,7 +14863,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; } @@ -14498,7 +15251,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); @@ -14524,7 +15277,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); @@ -14550,7 +15303,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); @@ -14576,7 +15329,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_console/include/load_session.php b/pandora_console/include/load_session.php index 80fb8643ef..7d06cea88d 100644 --- a/pandora_console/include/load_session.php +++ b/pandora_console/include/load_session.php @@ -161,7 +161,7 @@ function pandora_session_gc($max_lifetime=300) if (isset($config['session_timeout'])) { $session_timeout = $config['session_timeout']; } else { - // if $config doesn`t work ... + // If $config doesn`t work ... $session_timeout = db_get_value( 'value', 'tconfig', @@ -170,9 +170,9 @@ function pandora_session_gc($max_lifetime=300) ); } - if (!empty($session_timeout)) { + if (empty($session_timeout) === false) { if ($session_timeout == -1) { - // The session expires in 10 years + // The session expires in 10 years. $session_timeout = 315576000; } else { $session_timeout *= 60; @@ -191,15 +191,14 @@ function pandora_session_gc($max_lifetime=300) ); // Deleting cron and empty sessions. - $sql = "DELETE FROM tsessions_php WHERE - data IS NULL OR id_session REGEXP '^cron-'"; + $sql = 'DELETE FROM tsessions_php WHERE data IS NULL'; db_process_sql($sql); return $retval; } -// FIXME: SAML should work with pandora session handlers +// TODO: SAML should work with pandora session handlers. if (db_get_value('value', 'tconfig', 'token', 'auth') != 'saml') { $result_handler = session_set_save_handler( 'pandora_session_open', diff --git a/pandora_console/include/web2image.js b/pandora_console/include/web2image.js index 29bac30e76..8e2887aa26 100644 --- a/pandora_console/include/web2image.js +++ b/pandora_console/include/web2image.js @@ -50,6 +50,29 @@ if (type_graph_pdf == "combined") { var page = require("webpage").create(); +page.onResourceError = function(resourceError) { + console.log( + "Unable to load resource (#" + + resourceError.id + + "URL:" + + resourceError.url + + ")" + ); + console.log( + "Error code: " + + resourceError.errorCode + + ". Description: " + + resourceError.errorString + ); + phantom.exit(1); +}; + +// Not supposed to be prompted messages. +page.onPrompt = function() { + console.log("Prompt message detected."); + phantom.exit(1); +}; + page.viewportSize = { width: viewport_width, height: viewport_height @@ -84,3 +107,21 @@ page.open(url, "POST", post_data, function(status) { phantom.exit(); } }); + +phantom.onError = function(msg, trace) { + var msgStack = ["PHANTOM ERROR: " + msg]; + if (trace && trace.length) { + msgStack.push("TRACE:"); + trace.forEach(function(t) { + msgStack.push( + " -> " + + (t.file || t.sourceURL) + + ": " + + t.line + + (t.function ? " (in function " + t.function + ")" : "") + ); + }); + } + console.log(msgStack.join("\n")); + phantom.exit(1); +}; diff --git a/pandora_console/install.php b/pandora_console/install.php index 5eb0a6cc56..4f668fcd81 100644 --- a/pandora_console/install.php +++ b/pandora_console/install.php @@ -128,8 +128,8 @@
$custom_id, 'url_address' => $url_address, 'timezone_offset' => $timezone_offset, - 'alias' => $alias, + 'alias' => safe_input($alias), 'update_module_count' => 1, # Force to replicate in metaconsole }); 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/PluginTools.pm b/pandora_server/lib/PandoraFMS/PluginTools.pm index c34ba6a656..287b11e900 100644 --- a/pandora_server/lib/PandoraFMS/PluginTools.pm +++ b/pandora_server/lib/PandoraFMS/PluginTools.pm @@ -31,8 +31,8 @@ use base 'Exporter'; our @ISA = qw(Exporter); # version: Defines actual version of Pandora Server for this module only -my $pandora_version = "7.0NG.739"; -my $pandora_build = "191028"; +my $pandora_version = "7.0NG.740"; +my $pandora_build = "191029"; our $VERSION = $pandora_version." ".$pandora_build; our %EXPORT_TAGS = ( 'all' => [ qw() ] ); diff --git a/pandora_server/lib/PandoraFMS/Tools.pm b/pandora_server/lib/PandoraFMS/Tools.pm index 114718ca6e..ba15f03d24 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/pandora_server.redhat.spec b/pandora_server/pandora_server.redhat.spec index 54c35970fc..805efb4c41 100644 --- a/pandora_server/pandora_server.redhat.spec +++ b/pandora_server/pandora_server.redhat.spec @@ -2,8 +2,8 @@ # Pandora FMS Server # %define name pandorafms_server -%define version 7.0NG.739 -%define release 191028 +%define version 7.0NG.740 +%define release 191029 Summary: Pandora FMS Server Name: %{name} diff --git a/pandora_server/pandora_server.spec b/pandora_server/pandora_server.spec index 75afd6f3df..e2eb323b24 100644 --- a/pandora_server/pandora_server.spec +++ b/pandora_server/pandora_server.spec @@ -2,8 +2,8 @@ # Pandora FMS Server # %define name pandorafms_server -%define version 7.0NG.739 -%define release 191028 +%define version 7.0NG.740 +%define release 191029 Summary: Pandora FMS Server Name: %{name} diff --git a/pandora_server/pandora_server_installer b/pandora_server/pandora_server_installer index e8f543cb14..6b1870fadf 100755 --- a/pandora_server/pandora_server_installer +++ b/pandora_server/pandora_server_installer @@ -8,8 +8,8 @@ # This code is licensed under GPL 2.0 license. # ********************************************************************** -PI_VERSION="7.0NG.739" -PI_BUILD="191028" +PI_VERSION="7.0NG.740" +PI_BUILD="191029" MODE=$1 if [ $# -gt 1 ]; then diff --git a/pandora_server/util/pandora_db.pl b/pandora_server/util/pandora_db.pl index 8359c6050e..a9cc09579b 100644 --- a/pandora_server/util/pandora_db.pl +++ b/pandora_server/util/pandora_db.pl @@ -34,7 +34,7 @@ use PandoraFMS::Config; use PandoraFMS::DB; # version: define current version -my $version = "7.0NG.739 PS191028"; +my $version = "7.0NG.740 PS191029"; # Pandora server configuration my %conf; diff --git a/pandora_server/util/pandora_manage.pl b/pandora_server/util/pandora_manage.pl index c0b407ecc4..3b72f91063 100755 --- a/pandora_server/util/pandora_manage.pl +++ b/pandora_server/util/pandora_manage.pl @@ -36,7 +36,7 @@ use Encode::Locale; Encode::Locale::decode_argv; # version: define current version -my $version = "7.0NG.739 PS191028"; +my $version = "7.0NG.740 PS191029"; # save program name for logging my $progname = basename($0); @@ -104,8 +104,8 @@ sub help_screen{ print "Available options for $param:\n\n" unless $param eq ''; print "AGENTS:\n\n" unless $param ne ''; help_screen_line('--create_agent', " \n\t [
]", 'Create agent'); - help_screen_line('--update_agent', ' ', "Update an agent field. The fields can be \n\t the following: agent_name, address, description, group_name, interval, os_name, disabled (0-1), \n\t parent_name, cascade_protection (0-1), icon_path, update_gis_data (0-1), custom_id"); - help_screen_line('--delete_agent', '', 'Delete agent'); + help_screen_line('--update_agent', ' []', "Update an agent field. The fields can be \n\t the following: agent_name, address, description, group_name, interval, os_name, disabled (0-1), \n\t parent_name, cascade_protection (0-1), icon_path, update_gis_data (0-1), custom_id"); + help_screen_line('--delete_agent', ' []', 'Delete agent'); help_screen_line('--disable_group', '', 'Disable agents from an entire group'); help_screen_line('--enable_group', '', 'Enable agents from an entire group'); help_screen_line('--create_group', ' [ ]', 'Create an agent group'); @@ -118,14 +118,14 @@ sub help_screen{ help_screen_line('--get_planned_downtimes_items', ' [ ]', 'Get all items of planned downtimes'); help_screen_line('--set_planned_downtimes_deleted', ' ', 'Deleted a planned downtime'); help_screen_line('--get_module_id', ' ', 'Get the id of an module'); - help_screen_line('--get_agent_group', '', 'Get the group name of an agent'); - help_screen_line('--get_agent_group_id', '', 'Get the group ID of an agent'); - help_screen_line('--get_agent_modules', '', 'Get the modules of an agent'); - help_screen_line('--get_agents', '[ ]', "Get \n\t list of agents with optative filter parameters"); - help_screen_line('--delete_conf_file', '', 'Delete a local conf of a given agent'); - help_screen_line('--clean_conf_file', '', "Clean a local conf of a given agent deleting all modules, \n\t policies, file collections and comments"); + help_screen_line('--get_agent_group', ' []', 'Get the group name of an agent'); + help_screen_line('--get_agent_group_id', ' []', 'Get the group ID of an agent'); + help_screen_line('--get_agent_modules', ' []', 'Get the modules of an agent'); + help_screen_line('--get_agents', '[ ]', "Get \n\t list of agents with optative filter parameters"); + help_screen_line('--delete_conf_file', ' []', 'Delete a local conf of a given agent'); + help_screen_line('--clean_conf_file', ' []', "Clean a local conf of a given agent deleting all modules, \n\t policies, file collections and comments"); help_screen_line('--get_bad_conf_files', '', 'Get the files bad configured (without essential tokens)'); - help_screen_line('--locate_agent', '', 'Search a agent into of nodes of metaconsole. Only Enterprise.'); + help_screen_line('--locate_agent', ' []', 'Search a agent into of nodes of metaconsole. Only Enterprise.'); help_screen_line('--migration_agent_queue', ' []', 'Migrate agent only metaconsole'); help_screen_line('--migration_agent', ' ', 'Is migrating the agent only metaconsole'); help_screen_line('--apply_module_template', ' ', 'Apply module template to agent'); @@ -139,29 +139,29 @@ sub help_screen{ help_screen_line('--set_disabled_and_standby', ' ', 'Overwrite and disable and standby status'); help_screen_line('--reset_agent_counts', '', 'Resets module counts and alert counts in the agents'); print "\nMODULES:\n\n" unless $param ne ''; - help_screen_line('--create_data_module', " [ \n\t \n\t \n\t \n\t \n\t ]", 'Add data server module to agent'); - help_screen_line('--create_web_module', " [ \n\t \n\t \n\t \n\t \n\t \n\t ].\n\t The valid data types are web_data, web_proc, web_content_data or web_content_string", 'Add web server module to agent'); - help_screen_line('--create_network_module', " \n\t [ \n\t \n\t \n\t \n\t \n\t ]", 'Add not snmp network module to agent'); - help_screen_line('--create_snmp_module', " \n\t [ \n\t \n\t \n\t \n\t \n\t + help_screen_line('--create_data_module', " [ \n\t \n\t \n\t \n\t \n\t ]", 'Add data server module to agent'); + help_screen_line('--create_web_module', " [ \n\t \n\t \n\t \n\t \n\t \n\t ].\n\t The valid data types are web_data, web_proc, web_content_data or web_content_string", 'Add web server module to agent'); + help_screen_line('--create_network_module', " \n\t [ \n\t \n\t \n\t \n\t \n\t ]", 'Add not snmp network module to agent'); + help_screen_line('--create_snmp_module', " \n\t [ \n\t \n\t \n\t \n\t \n\t ] \n\t \n\t ]", 'Add snmp network module to agent'); - help_screen_line('--create_plugin_module', " \n\t [ \n\t \n\t \n\t \n\t \n\t \n\t ]", 'Add plug-in module to agent'); + help_screen_line('--create_plugin_module', " \n\t [ \n\t \n\t \n\t \n\t \n\t \n\t ]", 'Add plug-in module to agent'); help_screen_line('--get_module_group', '[]', 'Dysplay all module groups'); help_screen_line('--create_module_group', ''); help_screen_line('--module_group_synch', " []", 'Synchronize metaconsole module groups'); - help_screen_line('--delete_module', 'Delete module from agent', ' '); - help_screen_line('--data_module', " \n\t []", 'Insert data to module'); - help_screen_line('--get_module_data', " []", "\n\t Show the data of a module in the last X seconds (interval) in CSV format"); - help_screen_line('--delete_data', '-m | -a | -g ', "Delete historic \n\t data of a module, the modules of an agent or the modules of the agents of a group"); - help_screen_line('--update_module', ' ', 'Update a module field'); + help_screen_line('--delete_module', 'Delete module from agent', ' []'); + help_screen_line('--data_module', " \n\t [ ]", 'Insert data to module'); + help_screen_line('--get_module_data', " [ ]", "\n\t Show the data of a module in the last X seconds (interval) in CSV format"); + help_screen_line('--delete_data', '-m | -a | -g []', "Delete historic \n\t data of a module, the modules of an agent or the modules of the agents of a group"); + help_screen_line('--update_module', ' []', 'Update a module field'); help_screen_line('--get_agents_module_current_data', '', "Get the agent and current data \n\t of all the modules with a given name"); - help_screen_line('--create_network_module_from_component', ' ', "Create a new network \n\t module from a network component"); + help_screen_line('--create_network_module_from_component', ' []', "Create a new network \n\t module from a network component"); help_screen_line('--create_network_component', " \n\t [ \n\t \n\t \n\t \n\t \n\t \n\t ]", "Create a new network component"); - help_screen_line('--create_synthetic', " ,,|, \n\t [ , | ,, ]", "Create a new Synthetic module"); + help_screen_line('--create_synthetic', " ,,|, \n\t [ , | ,, ]", "Create a new Synthetic module"); print "\nALERTS:\n\n" unless $param ne ''; - help_screen_line('--create_template_module', ' ', 'Add alert template to module'); - help_screen_line('--delete_template_module', ' ', 'Delete alert template from module'); - help_screen_line('--create_template_action', " \n\t [ ]', 'Add alert action to module-template"); - help_screen_line('--delete_template_action', " \n\t ", 'Delete alert action from module-template'); + help_screen_line('--create_template_module', ' []', 'Add alert template to module'); + help_screen_line('--delete_template_module', ' []', 'Delete alert template from module'); + help_screen_line('--create_template_action', " \n\t [ ]', 'Add alert action to module-template"); + help_screen_line('--delete_template_action', " \n\t []", 'Delete alert action from module-template'); help_screen_line('--disable_alerts', '', 'Disable alerts in all groups (system wide)'); help_screen_line('--enable_alerts', '', 'Enable alerts in all groups (system wide)'); help_screen_line('--create_alert_template', " \n\t [ \n\t \n\t \n\t ]", 'Create alert template'); @@ -175,7 +175,7 @@ sub help_screen{ help_screen_line('--create_special_day', " ", 'Create special day'); help_screen_line('--delete_special_day', '', 'Delete special day'); help_screen_line('--update_special_day', " ", 'Update a field of a special day'); - help_screen_line('--create_data_module_from_local_component', ' ', "Create a new data \n\t module from a local component"); + help_screen_line('--create_data_module_from_local_component', ' []', "Create a new data \n\t module from a local component"); help_screen_line('--create_local_component', " [ \n\t \n\t \n\t \n\t \n\t \n\t \n\t \n\t ]", 'Create local component'); print "\nUSERS:\n\n" unless $param ne ''; @@ -193,17 +193,17 @@ sub help_screen{ help_screen_line('--disable_double_auth', '', 'Disable the double authentication for the specified user'); help_screen_line('--meta_synch_user', " [ \n\t ]", 'Synchronize metaconsole users'); print "\nEVENTS:\n\n" unless $param ne ''; - help_screen_line('--create_event', " [ \n\t \n\t \n\t ]", 'Add event'); - help_screen_line('--validate_event', " \n\t ", 'Validate events'); - help_screen_line('--validate_event_id', '', 'Validate event given a event id'); - help_screen_line('--get_event_info', '[]', 'Show info about a event given a event id'); - help_screen_line('--add_event_comment', ' ', 'Add event\'s comment'); + help_screen_line('--create_event', " [ \n\t \n\t \n\t ]", 'Add event'); + help_screen_line('--validate_event', " \n\t []", 'Validate events'); + help_screen_line('--validate_event_id', '', 'Validate event given a event id'); + help_screen_line('--get_event_info', '[]', 'Show info about a event given a event id'); + help_screen_line('--add_event_comment', ' ', 'Add event\'s comment'); print "\nINCIDENTS:\n\n" unless $param ne ''; help_screen_line('--create_incident', " <description> <origin> <status> <priority 0 for Informative, \n\t 1 for Low, 2 for Medium, 3 for Serious, 4 for Very serious or 5 for Maintenance>\n\t <group> [<owner>]", 'Create incidents'); print "\nPOLICIES:\n\n" unless $param ne ''; help_screen_line('--apply_policy', '<id_policy> [<id_agent> <name(boolean)> <id_server>]', 'Force apply a policy in an agent'); help_screen_line('--apply_all_policies', '', 'Force apply to all the policies'); - help_screen_line('--add_agent_to_policy', '<agent_name> <policy_name>', 'Add an agent to a policy'); + help_screen_line('--add_agent_to_policy', '<agent_name> <policy_name> [<use_alias>]', 'Add an agent to a policy'); help_screen_line('--remove_agent_from_policy', '<policy_id> <agent_id>', 'Delete an agent to a policy'); help_screen_line('--delete_not_policy_modules', '', 'Delete all modules without policy from configuration file'); help_screen_line('--disable_policy_alerts', '<policy_name>', 'Disable all the alerts of a policy'); @@ -218,7 +218,7 @@ sub help_screen{ help_screen_line('--add_collection_to_policy', "<policy_name> <collection_name>"); help_screen_line('--validate_policy_alerts', '<policy_name>', 'Validate the alerts of a given policy'); help_screen_line('--get_policy_modules', '<policy_name>', 'Get the modules of a policy'); - help_screen_line('--get_policies', '[<agent_name>]', "Get all the policies (without parameters) or \n\tthe policies of a given agent (agent name as parameter)"); + help_screen_line('--get_policies', '[<agent_name> <use_alias>]', "Get all the policies (without parameters) or \n\tthe policies of a given agent (agent name as parameter)"); help_screen_line('--recreate_collection', '<collection_id>', 'Recreate the files of a collection'); print "\nNETFLOW:\n\n" unless $param ne ''; @@ -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 = <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 (<FILE>) { - 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 = <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 (<FILE>) { + 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 = <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 = <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 = <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 (<FILE>) { + 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 = <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 = <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 (<FILE>) { - 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 = <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 (<FILE>) { + 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 = <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 = <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 = <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 (<FILE>) { + 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 = <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,41 @@ 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) { + print_log "[ERROR] Error: The module '$module_name' does not exist. \n\n"; + 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 +2713,46 @@ 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) { + print_log "[ERROR] Error: The module '$module_name' does not exist. \n\n"; + 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 +2761,68 @@ 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) { + print_log "[ERROR] Error: The module '$module_name' does not exist. \n\n"; + 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 +2831,46 @@ 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) { + print_log "[ERROR] Error: The module '$module_name' does not exist. \n\n"; + 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 +2879,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 +2900,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 +3017,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 +3078,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 +3092,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 +3366,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 +4022,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 +4048,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 = '<b>-- Added comment by '.$user_name. ' ['. localtime(time).'] --</b><br>'.$comment.'<br>'; + } + 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 = '<b>-- Added comment by '.$user_name. ' ['. localtime(time).'] --</b><br>'.$comment.'<br>'; + } + 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 = '<b>-- Added comment by '.$user_name. ' ['. localtime(time).'] --</b><br>'.$comment.'<br>'; - } - 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 +4158,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 +4180,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 +4376,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 +4610,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 +4636,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 +4706,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 +4726,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 +4809,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 +4854,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 +4884,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 +5095,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 +5160,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 +5171,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 +5196,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 +5213,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 +5228,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 +5243,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 +5297,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 +5396,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 +5661,6 @@ sub cli_update_group() { print_log "[INFO] Updated group '$group_id'\n\n"; } } - - - - - } @@ -4622,47 +5669,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 +5914,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 +5922,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 +7157,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 +7165,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 +7182,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 +7202,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 +7242,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 +7266,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 +7290,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 +7314,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 +7330,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 +7378,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 +7404,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 +7416,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 +7424,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 +7448,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 +7500,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 +7548,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 +7772,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.