diff --git a/pandora_agents/pc/AIX/pandora_agent.conf b/pandora_agents/pc/AIX/pandora_agent.conf index fbea6eb404..4fff860931 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.738, AIX version +# Version 7.0NG.739, 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 ded3ec4b9c..f33b304a7c 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.738, FreeBSD Version +# Version 7.0NG.739, 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 0d3e20d05e..1d627fc222 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.738, HP-UX Version +# Version 7.0NG.739, 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 5d951d984d..9501fa5222 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.738, GNU/Linux +# Version 7.0NG.739, 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 1c73d12fc9..bdd4bcf425 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.738, GNU/Linux +# Version 7.0NG.739, 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 fc59e308e4..f80f403dd8 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.738, Solaris Version +# Version 7.0NG.739, 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 f12cb5089c..86bfede9d7 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.738 +# Version 7.0NG.739 # 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 863ab92907..aa65eb24d9 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.738, AIX version +# Version 7.0NG.739, 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 f56402fbcc..7c225fea1c 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.738 +# Version 7.0NG.739 # 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 55576e58fd..06bd17ed45 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.738, HPUX Version +# Version 7.0NG.739, HPUX Version # General Parameters # ================== diff --git a/pandora_agents/shellscript/linux/pandora_agent.conf b/pandora_agents/shellscript/linux/pandora_agent.conf index a584a597cb..54b0e06dbf 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.738 +# Version 7.0NG.739 # 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 18db7a174f..f5205f4d16 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.738 +# Version 7.0NG.739 # 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 d3266f091d..7d6a12d62b 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.738 +# Version 7.0NG.739 # 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 aa59a262c4..cbae89b0b5 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.738, Solaris version +# Version 7.0NG.739, Solaris version # General Parameters # ================== diff --git a/pandora_agents/unix/AIX/pandora_agent.conf b/pandora_agents/unix/AIX/pandora_agent.conf index dedf2c366f..7f2791b05f 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.738, AIX version +# Version 7.0NG.739, 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 5e5dabfb7f..110d62c3d9 100644 --- a/pandora_agents/unix/DEBIAN/control +++ b/pandora_agents/unix/DEBIAN/control @@ -1,5 +1,5 @@ package: pandorafms-agent-unix -Version: 7.0NG.738-190920 +Version: 7.0NG.739-191003 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 cb275c2e53..e5f3872708 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.738-190920" +pandora_version="7.0NG.739-191003" 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 4624a5ceff..7f403ae687 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.738, GNU/Linux +# Version 7.0NG.739, 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 0895974d7b..2871669f88 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.738, FreeBSD Version +# Version 7.0NG.739, 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 bfd0612672..1fcbc17977 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.738, HP-UX Version +# Version 7.0NG.739, 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 e1a83ed6e5..938565241d 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.738, GNU/Linux +# Version 7.0NG.739, 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 b0841bd0f7..f9a2b10b93 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.738, GNU/Linux +# Version 7.0NG.739, 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 4d96ba223d..2f108c2011 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.738, NetBSD Version +# Version 7.0NG.739, 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 c4a4c578d9..47f17aae0f 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.738, Solaris Version +# Version 7.0NG.739, 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 190ace7b38..2607d184a2 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.738'; -use constant AGENT_BUILD => '190920'; +use constant AGENT_VERSION => '7.0NG.739'; +use constant AGENT_BUILD => '191003'; # Agent log default file size maximum and instances use constant DEFAULT_MAX_LOG_SIZE => 600000; @@ -239,6 +239,15 @@ my $tentacle_pid = undef; # PID of udp_server my $udp_server_pid = undef; +# BrokerFlag +my $BrokerFlag = 0; + +# Global loop counter. +my $LoopCounter = 0; + +# Define a max value for loopCounter to avoid overflow. +use constant MAX_LOOP_COUNTER => 1000000000; + ################################################################################ # Print usage information and exit. ################################################################################ @@ -1385,6 +1394,7 @@ sub sleep_agent { exit (0); } + $LoopCounter = ($LoopCounter + 1) % MAX_LOOP_COUNTER; return $iter_base_time; } @@ -1699,6 +1709,14 @@ sub exec_module { } # Check module interval + if ($BrokerFlag > 0) { + if ($LoopCounter == 0) { + $module->{'counter'} = $module->{'intensive_interval'}; + } else { + $module->{'counter'} = (($LoopCounter -1 ) % $module->{'intensive_interval'}); + } + } + if (++($module->{'counter'}) < $module->{'intensive_interval'}) { $ThreadSem->up () if (defined ($ThreadSem) && $Conf{'agent_threads'} > 1); return; @@ -2985,7 +3003,6 @@ while (1) { @BrokerPid = (); my @broker_agents = read_config ('broker_agent'); foreach my $broker_agent (@broker_agents) { - # Create broker conf file if it does not exist if (! -e "$ConfDir/${broker_agent}.conf") { write_broker_conf($broker_agent); @@ -2995,7 +3012,9 @@ while (1) { # Broker agent if ($main_agent == 0) { - + # Mark broker flag. + $BrokerFlag = 1; + # Set the configuration file $ConfFile = "${broker_agent}.conf"; diff --git a/pandora_agents/unix/pandora_agent.redhat.spec b/pandora_agents/unix/pandora_agent.redhat.spec index 21eb1a01b2..84af8cdc01 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.738 -%define release 190920 +%define version 7.0NG.739 +%define release 191003 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 07e61aa6b8..58ac4decde 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.738 -%define release 190920 +%define version 7.0NG.739 +%define release 191003 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 6950b20fde..28905ce92b 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.738" -PI_BUILD="190920" +PI_VERSION="7.0NG.739" +PI_BUILD="191003" OS_NAME=`uname -s` FORCE=0 @@ -162,7 +162,15 @@ uninstall () { rm -Rf $PANDORA_BASE$PANDORA_EXEC_BIN 2> /dev/null rm -Rf $PANDORA_BASE$PANDORA_REVENT_BIN 2> /dev/null rm -f $DESTDIR/etc/logrotate.d/pandora_agent - + + # Remove systemd service if exists + if [ $(systemctl --v | grep systemd | wc -l) != 0 ] + then + PANDORA_AGENT_SERVICE="/etc/systemd/system/pandora_agent_daemon.service" + rm -f $PANDORA_AGENT_SERVICE + systemctl reset-failed + fi + #Test if exist Pandora Server in this machine if [ -d $PANDORA_BASE$PANDORA_TEMP/data_in ] then @@ -465,6 +473,22 @@ install () { else RCDIRS="/etc/rc2.d /etc/rc3.d" fi + + # Create systemd service + if [ $(systemctl --v | grep systemd | wc -l) != 0 ] + then + echo "Creating systemd service for pandora_agent_daemon" + + PANDORA_AGENT_SERVICE="/etc/systemd/system/pandora_agent_daemon.service" + EXEC_START='ExecStart='$PANDORA_BASE$PANDORA_BIN' '$PANDORA_BASE$PANDORA_CFG + + rm -f $PANDORA_AGENT_SERVICE + cp pandora_agent_daemon.service $PANDORA_AGENT_SERVICE + + sed -i "s|^ExecStart=.*$|$EXEC_START|g" $PANDORA_AGENT_SERVICE + + systemctl daemon-reload + fi fi [ "$RCDIRS" ] && for RCDIR in $RCDIRS do diff --git a/pandora_agents/win32/bin/pandora_agent.conf b/pandora_agents/win32/bin/pandora_agent.conf index c74dadfff6..d6bf2f9005 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.738 +# Version 7.0NG.739 # 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 b7e0b3c260..ecfc01e4b4 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.738} +{Pandora FMS Windows Agent v7.0NG.739} ApplicationID {17E3D2CF-CA02-406B-8A80-9D31C17BD08F} @@ -186,7 +186,7 @@ UpgradeApplicationID {} Version -{190920} +{191003} ViewReadme {Yes} diff --git a/pandora_agents/win32/modules/pandora_module.cc b/pandora_agents/win32/modules/pandora_module.cc index e7c751d367..7cbde21ca7 100644 --- a/pandora_agents/win32/modules/pandora_module.cc +++ b/pandora_agents/win32/modules/pandora_module.cc @@ -1422,48 +1422,48 @@ Pandora_Module::evaluatePreconditions () { buffer[read] = '\0'; output += (char *) buffer; } - - try { - double_output = Pandora_Strutils::strtodouble (output); - } catch (Pandora_Strutils::Invalid_Conversion e) { - double_output = 0; + + try { + double_output = Pandora_Strutils::strtodouble (output); + } catch (Pandora_Strutils::Invalid_Conversion e) { + double_output = 0; + } + + if (dwRet == WAIT_OBJECT_0) { + break; + } else if(this->getTimeout() < GetTickCount() - tickbase) { + /* STILL_ACTIVE */ + TerminateProcess(pi.hThread, STILL_ACTIVE); + pandoraLog ("evaluatePreconditions: %s timed out (retcode: %d)", this->module_name.c_str (), STILL_ACTIVE); + break; + } } - if (dwRet == WAIT_OBJECT_0) { - break; - } else if(this->getTimeout() < GetTickCount() - tickbase) { - /* STILL_ACTIVE */ - TerminateProcess(pi.hThread, STILL_ACTIVE); - pandoraLog ("evaluatePreconditions: %s timed out (retcode: %d)", this->module_name.c_str (), STILL_ACTIVE); - break; + GetExitCodeProcess (pi.hProcess, &retval); + + if (retval != 0) { + if (! TerminateJobObject (job, 0)) { + pandoraLog ("evaluatePreconditions: TerminateJobObject failed. (error %d)", + GetLastError ()); + } + if (retval != STILL_ACTIVE) { + pandoraLog ("evaluatePreconditions: %s did not executed well (retcode: %d)", + this->module_name.c_str (), retval); + } + /* Close job, process and thread handles. */ + CloseHandle (job); + CloseHandle (pi.hProcess); + CloseHandle (pi.hThread); + CloseHandle (new_stdout); + CloseHandle (out_read); + return 0; } - } - - GetExitCodeProcess (pi.hProcess, &retval); - - if (retval != 0) { - if (! TerminateJobObject (job, 0)) { - pandoraLog ("evaluatePreconditions: TerminateJobObject failed. (error %d)", - GetLastError ()); - } - if (retval != STILL_ACTIVE) { - pandoraLog ("evaluatePreconditions: %s did not executed well (retcode: %d)", - this->module_name.c_str (), retval); - } + /* Close job, process and thread handles. */ CloseHandle (job); CloseHandle (pi.hProcess); CloseHandle (pi.hThread); - CloseHandle (new_stdout); - CloseHandle (out_read); - return 0; } - - /* Close job, process and thread handles. */ - CloseHandle (job); - CloseHandle (pi.hProcess); - CloseHandle (pi.hThread); - } CloseHandle (new_stdout); CloseHandle (out_read); @@ -1693,4 +1693,18 @@ Pandora_Module::getAsync () { return this->async; } +/** + * Get current exections + */ +long +Pandora_Module::getExecutions () { + return this->executions; +} +/** + * Set current execution (global) used for brokers. + */ +void +Pandora_Module::setExecutions (long executions) { + this->executions = executions; +} diff --git a/pandora_agents/win32/modules/pandora_module.h b/pandora_agents/win32/modules/pandora_module.h index c766766950..3b9d6c15a4 100644 --- a/pandora_agents/win32/modules/pandora_module.h +++ b/pandora_agents/win32/modules/pandora_module.h @@ -234,6 +234,8 @@ namespace Pandora_Modules { int getTimeout (); string getSave (); bool getAsync (); + void setExecutions(long executions=0); + long getExecutions(); virtual string getXml (); diff --git a/pandora_agents/win32/pandora.cc b/pandora_agents/win32/pandora.cc index 66e5852583..8f35e47e02 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.738(Build 190920)") +#define PANDORA_VERSION ("7.0NG.739(Build 191003)") string pandora_path; string pandora_dir; diff --git a/pandora_agents/win32/pandora_windows_service.cc b/pandora_agents/win32/pandora_windows_service.cc index 275ee60724..b62fc1f70d 100644 --- a/pandora_agents/win32/pandora_windows_service.cc +++ b/pandora_agents/win32/pandora_windows_service.cc @@ -1849,7 +1849,7 @@ Pandora_Windows_Service::sendBufferedXml (string path) { } void -Pandora_Windows_Service::pandora_run_broker (string config) { +Pandora_Windows_Service::pandora_run_broker (string config, long executions) { Pandora_Agent_Conf *conf = NULL; string server_addr; unsigned char data_flag = 0; @@ -1876,7 +1876,10 @@ Pandora_Windows_Service::pandora_run_broker (string config) { Pandora_Module *module; module = this->broker_modules->getCurrentValue (); - + + /* Keep executions matching main agent */ + module->setExecutions(executions); + /* Check preconditions */ if (module->evaluatePreconditions () == 0) { pandoraDebug ("Preconditions not matched for module %s", module->getName ().c_str ()); @@ -2074,7 +2077,7 @@ Pandora_Windows_Service::pandora_run (int forced_run) { check_broker_agents(all_conf); for (i=0;i 'noaccess']; + echo json_encode(['error' => '[CredentialStore]'.$e->getMessage() ]); + exit; + } else { + echo '[CredentialStore]'.$e->getMessage(); } - include 'general/noaccess.php'; + // Stop this execution, but continue 'globally'. return; } -// Required files. -ui_require_css_file('credential_store'); -require_once $config['homedir'].'/include/functions_credential_store.php'; -require_once $config['homedir'].'/include/functions_io.php'; - +// AJAX controller. if (is_ajax()) { - $draw = get_parameter('draw', 0); - $filter = get_parameter('filter', []); - $get_key = get_parameter('get_key', 0); - $new_form = get_parameter('new_form', 0); - $new_key = get_parameter('new_key', 0); - $update_key = get_parameter('update_key', 0); - $delete_key = get_parameter('delete_key', 0); + $method = get_parameter('method'); - if ($new_form) { - echo print_inputs(); - exit; - } - - if ($delete_key) { - $identifier = get_parameter('identifier', null); - - if (empty($identifier)) { - ajax_msg('error', __('identifier cannot be empty')); - } - - if (db_process_sql_delete( - 'tcredential_store', - ['identifier' => $identifier] - ) === false - ) { - ajax_msg('error', $config['dbconnection']->error, true); + if (method_exists($cs, $method) === true) { + if ($cs->ajaxMethod($method) === true) { + $cs->{$method}(); } else { - ajax_msg('result', $identifier, true); + $cs->error('Unavailable method.'); } + } else { + $cs->error('Method not found. ['.$method.']'); } - if ($update_key) { - $data = get_parameter('values', null); - - if ($data === null || !is_array($data)) { - echo json_encode(['error' => __('Invalid parameters, please retry')]); - exit; - } - - $values = []; - foreach ($data as $key => $value) { - if ($key == 'identifier') { - $identifier = base64_decode($value); - } else if ($key == 'product') { - $product = base64_decode($value); - } else { - $values[$key] = base64_decode($value); - } - } - - if (empty($identifier)) { - ajax_msg('error', __('identifier cannot be empty')); - } - - if (empty($product)) { - ajax_msg('error', __('product cannot be empty')); - } - - if (db_process_sql_update( - 'tcredential_store', - $values, - ['identifier' => $identifier] - ) === false - ) { - ajax_msg('error', $config['dbconnection']->error); - } else { - ajax_msg('result', $identifier); - } - - exit; - } - - if ($new_key) { - $data = get_parameter('values', null); - - if ($data === null || !is_array($data)) { - echo json_encode(['error' => __('Invalid parameters, please retry')]); - exit; - } - - $values = []; - foreach ($data as $key => $value) { - $values[$key] = base64_decode($value); - if ($key == 'identifier') { - $values[$key] = preg_replace('/\s+/', '-', trim($values[$key])); - } - } - - $identifier = $values['identifier']; - - if (empty($identifier)) { - ajax_msg('error', __('identifier cannot be empty')); - } - - if (empty($values['product'])) { - ajax_msg('error', __('product cannot be empty')); - } - - if (db_process_sql_insert('tcredential_store', $values) === false) { - ajax_msg('error', $config['dbconnection']->error); - } else { - ajax_msg('result', $identifier); - } - - exit; - } - - if ($get_key) { - $identifier = get_parameter('identifier', null); - - $key = get_key($identifier); - echo print_inputs($key); - - exit; - } - - if ($draw) { - // Datatables offset, limit and order. - $start = get_parameter('start', 0); - $length = get_parameter('length', $config['block_size']); - $order = get_datatable_order(true); - try { - ob_start(); - - $fields = [ - 'cs.*', - 'tg.nombre as `group`', - ]; - - // Retrieve data. - $data = credentials_get_all( - // Fields. - $fields, - // Filter. - $filter, - // Offset. - $start, - // Limit. - $length, - // Order. - $order['direction'], - // Sort field. - $order['field'] - ); - - // Retrieve counter. - $count = credentials_get_all( - 'count', - $filter - ); - - if ($data) { - $data = array_reduce( - $data, - function ($carry, $item) { - // Transforms array of arrays $data into an array - // of objects, making a post-process of certain fields. - $tmp = (object) $item; - $tmp->username = io_safe_output($tmp->username); - - if (empty($tmp->group)) { - $tmp->group = __('All'); - } else { - $tmp->group = io_safe_output($tmp->group); - } - - $carry[] = $tmp; - return $carry; - } - ); - } - - // Datatables format: RecordsTotal && recordsfiltered. - echo json_encode( - [ - 'data' => $data, - 'recordsTotal' => $count, - 'recordsFiltered' => $count, - ] - ); - // Capture output. - $response = ob_get_clean(); - } catch (Exception $e) { - return json_encode(['error' => $e->getMessage()]); - } - - // If not valid, show error with issue. - json_decode($response); - if (json_last_error() == JSON_ERROR_NONE) { - // If valid dump. - echo $response; - } else { - echo json_encode( - ['error' => $response] - ); - } - - - exit; - } - + // Stop any execution. exit; +} else { + // Run. + $cs->run(); } - -// Datatables list. -try { - $columns = [ - 'group', - 'identifier', - 'product', - 'username', - 'options', - ]; - - $column_names = [ - __('Group'), - __('Identifier'), - __('Product'), - __('User'), - [ - 'text' => __('Options'), - 'class' => 'action_buttons', - ], - ]; - - $table_id = 'keystore'; - // Load datatables user interface. - ui_print_datatable( - [ - 'id' => $table_id, - 'class' => 'info_table', - 'style' => 'width: 100%', - 'columns' => $columns, - 'column_names' => $column_names, - 'ajax_url' => 'godmode/groups/credential_store', - 'ajax_postprocess' => 'process_datatables_item(item)', - 'no_sortable_columns' => [-1], - 'order' => [ - 'field' => 'identifier', - 'direction' => 'asc', - ], - 'search_button_class' => 'sub filter float-right', - 'form' => [ - 'inputs' => [ - [ - 'label' => __('Group'), - 'type' => 'select', - 'id' => 'filter_id_group', - 'name' => 'filter_id_group', - 'options' => users_get_groups_for_select( - $config['id_user'], - 'AR', - true, - true, - false - ), - ], - [ - 'label' => __('Free search'), - 'type' => 'text', - 'class' => 'mw250px', - 'id' => 'free_search', - 'name' => 'free_search', - ], - ], - ], - ] - ); -} catch (Exception $e) { - echo $e->getMessage(); -} - -// Auxiliar div. -$new = ''; -$details = ''; -$aux = ''; - - -echo $new.$details.$aux; - -// Create button. -echo '
'; -html_print_submit_button( - __('Add key'), - 'create', - false, - 'class="sub next"' -); -echo '
'; - -?> - - diff --git a/pandora_console/godmode/menu.php b/pandora_console/godmode/menu.php index 000a105257..e57ee47409 100644 --- a/pandora_console/godmode/menu.php +++ b/pandora_console/godmode/menu.php @@ -295,6 +295,9 @@ if (check_acl($config['id_user'], 0, 'PM')) { $sub2['godmode/setup/setup&section=ehorus']['text'] = __('eHorus'); $sub2['godmode/setup/setup&section=ehorus']['refr'] = 0; + $sub2['godmode/setup/setup&section=integria']['text'] = __('Integria IMS'); + $sub2['godmode/setup/setup&section=integria']['refr'] = 0; + $sub2['godmode/setup/setup&section=notifications']['text'] = __('Notifications'); $sub2['godmode/setup/setup&section=notifications']['refr'] = 0; diff --git a/pandora_console/godmode/reporting/graphs.php b/pandora_console/godmode/reporting/graphs.php index cb52af5e04..fd8d3cff84 100644 --- a/pandora_console/godmode/reporting/graphs.php +++ b/pandora_console/godmode/reporting/graphs.php @@ -257,11 +257,6 @@ $table_aux = new stdClass(); $table->cellspacing = 0; $table->align = []; $table->head = []; - if ($report_w || $report_m) { - $table->align[5] = 'left'; - $table->head[5] = html_print_checkbox('all_delete', 0, false, true, false); - $table->size[5] = '20px'; - } $table->head[0] = __('Graph name'); $table->head[1] = __('Description'); @@ -280,6 +275,12 @@ $table_aux = new stdClass(); $table->size[4] = '90px'; } + if ($report_w || $report_m) { + $table->align[5] = 'left'; + $table->head[5] = html_print_checkbox('all_delete', 0, false, true, false); + $table->size[5] = '20px'; + } + $table->data = []; $result_graphs = array_slice($graphs, $offset, $config['block_size']); @@ -287,10 +288,6 @@ $table_aux = new stdClass(); foreach ($result_graphs as $graph) { $data = []; - if ($report_m) { - $data[5] .= html_print_checkbox_extended('delete_multiple[]', $graph['id_graph'], false, false, '', 'class="check_delete" style="margin-left:2px;"', true); - } - $data[0] = ''.ui_print_truncate_text($graph['name'], 70).''; $data[1] = ui_print_truncate_text($graph['description'], 70); @@ -309,6 +306,10 @@ $table_aux = new stdClass(); return false;">'.html_print_image('images/cross.png', true, ['alt' => __('Delete'), 'title' => __('Delete')]).''; } + if ($report_m) { + $data[5] .= html_print_checkbox_extended('delete_multiple[]', $graph['id_graph'], false, false, '', 'class="check_delete" style="margin-left:2px;"', true); + } + array_push($table->data, $data); } diff --git a/pandora_console/godmode/setup/setup.php b/pandora_console/godmode/setup/setup.php index c32f128ddd..a46d7bc832 100644 --- a/pandora_console/godmode/setup/setup.php +++ b/pandora_console/godmode/setup/setup.php @@ -117,6 +117,11 @@ if (check_acl($config['id_user'], 0, 'AW')) { } } +$buttons['integria'] = [ + 'active' => false, + 'text' => ''.html_print_image('images/integria.png', true, ['title' => __('Integria IMS')]).'', +]; + $buttons['ehorus'] = [ 'active' => false, 'text' => ''.html_print_image('images/ehorus/ehorus.png', true, ['title' => __('eHorus')]).'', @@ -167,6 +172,12 @@ switch ($section) { $help_header = 'setup_ehorus_tab'; break; + case 'integria': + $buttons['integria']['active'] = true; + $subpage = ' » '.__('Integria IMS'); + $help_header = 'setup_integria_tab'; + break; + case 'notifications': $buttons['notifications']['active'] = true; $subpage = ' » '.__('Notifications'); @@ -228,6 +239,10 @@ switch ($section) { include_once $config['homedir'].'/godmode/setup/setup_ehorus.php'; break; + case 'integria': + include_once $config['homedir'].'/godmode/setup/setup_integria.php'; + break; + case 'notifications': include_once $config['homedir'].'/godmode/setup/setup_notifications.php'; break; diff --git a/pandora_console/godmode/setup/setup_integria.php b/pandora_console/godmode/setup/setup_integria.php new file mode 100644 index 0000000000..3ce6cf334c --- /dev/null +++ b/pandora_console/godmode/setup/setup_integria.php @@ -0,0 +1,702 @@ + 1]); + } else { + echo json_encode(['login' => 0]); + } + + return; +} + +$has_connection = integria_api_call($config['integria_hostname'], $config['integria_user'], $config['integria_pass'], $config['integria_api_pass'], 'get_login', []); + +if ($has_connection === false && $config['integria_enabled']) { + ui_print_error_message(__('Integria IMS API is not reachable')); +} + +if (get_parameter('update_config', 0) == 1) { + // Try to retrieve event response 'Create incident in IntegriaIMS from event' to check if it exists. + $event_response_exists = db_get_row_filter('tevent_response', ['name' => io_safe_input('Create ticket in IntegriaIMS from event')]); + + // Try to retrieve command 'Integia IMS Ticket' to check if it exists. + $command_exists = db_get_row_filter('talert_commands', ['name' => io_safe_input('Integria IMS Ticket')]); + + if ($config['integria_enabled'] == 1) { + if ($event_response_exists === false) { + // Create 'Create incident in IntegriaIMS from event' event response only when user enables IntegriaIMS integration and it does not exist in database. + db_process_sql_insert( + 'tevent_response', + [ + 'name' => io_safe_input('Create ticket in IntegriaIMS from event'), + 'description' => io_safe_input('Create a ticket in Integria IMS from an event'), + 'target' => io_safe_input('index.php?sec=incident&sec2=operation/incidents/configure_integriaims_incident&from_event=_event_id_'), + 'type' => 'url', + 'id_group' => '0', + 'modal_width' => '0', + 'modal_height' => '0', + 'new_window' => '1', + 'params' => '', + 'server_to_exec' => '0', + ] + ); + } + + if ($command_exists === false) { + // Create 'Integria IMS Ticket' command only when user enables IntegriaIMS integration and it does not exist in database. + $id_command_inserted = db_process_sql_insert( + 'talert_commands', + [ + 'name' => io_safe_input('Integria IMS Ticket'), + 'command' => io_safe_input('Internal type'), + 'internal' => 1, + 'description' => io_safe_input('Create a ticket in Integria IMS'), + 'fields_descriptions' => '["'.io_safe_input('Ticket title').'","'.io_safe_input('Ticket group ID').'","'.io_safe_input('Ticket priority').'","'.io_safe_input('Ticket owner').'","'.io_safe_input('Ticket type').'","'.io_safe_input('Ticket status').'","'.io_safe_input('Ticket description').'"]', + ] + ); + + // Create 'Create Integria IMS Ticket' action only when user enables IntegriaIMS integration and command exists in database. + $action_values = [ + 'field1' => io_safe_input($config['incident_title']), + 'field1_recovery' => io_safe_input($config['incident_title']), + 'field2' => io_safe_input($config['default_group']), + 'field2_recovery' => io_safe_input($config['default_group']), + 'field3' => io_safe_input($config['default_criticity']), + 'field3_recovery' => io_safe_input($config['default_criticity']), + 'field4' => io_safe_input($config['default_owner']), + 'field4_recovery' => io_safe_input($config['default_owner']), + 'field5' => io_safe_input($config['incident_type']), + 'field5_recovery' => io_safe_input($config['incident_type']), + 'field6' => io_safe_input($config['incident_status']), + 'field6_recovery' => io_safe_input($config['incident_status']), + 'field7' => io_safe_input($config['incident_content']), + 'field7_recovery' => io_safe_input($config['incident_content']), + 'id_group' => 0, + 'action_threshold' => 0, + ]; + + alerts_create_alert_action(io_safe_input('Create Integria IMS ticket'), $id_command_inserted, $action_values); + } else { + // Update 'Create Integria IMS Ticket' action when setup data is updated, user enables IntegriaIMS integration and command does exist in database. + db_process_sql_update( + 'talert_actions', + [ + 'field1' => io_safe_input($config['incident_title']), + 'field1_recovery' => io_safe_input($config['incident_title']), + 'field2' => io_safe_input($config['default_group']), + 'field2_recovery' => io_safe_input($config['default_group']), + 'field3' => io_safe_input($config['default_criticity']), + 'field3_recovery' => io_safe_input($config['default_criticity']), + 'field4' => io_safe_input($config['default_owner']), + 'field4_recovery' => io_safe_input($config['default_owner']), + 'field5' => io_safe_input($config['incident_type']), + 'field5_recovery' => io_safe_input($config['incident_type']), + 'field6' => io_safe_input($config['incident_status']), + 'field6_recovery' => io_safe_input($config['incident_status']), + 'field7' => io_safe_input($config['incident_content']), + 'field7_recovery' => io_safe_input($config['incident_content']), + ], + ['name' => io_safe_input('Create Integria IMS ticket')] + ); + } + } else { + if ($event_response_exists != false) { + // Delete 'Create incident in IntegriaIMS from event' event response if it does exist and IntegriaIMS integration is disabled. + db_process_sql_delete('tevent_response', ['name' => io_safe_input('Create ticket in IntegriaIMS from event')]); + } + + if ($command_exists != false) { + // Delete 'Integria IMS Ticket' command if it does exist and IntegriaIMS integration is disabled. + db_process_sql_delete('talert_commands', ['name' => io_safe_input('Integria IMS Ticket')]); + + // Delete 'Create Integria IMS Ticket' action if command exists and IntegriaIMS integration is disabled. + db_process_sql_delete('talert_actions', ['name' => io_safe_input('Create Integria IMS ticket')]); + } + } +} + +// Get parameters from Integria IMS API. +$integria_group_values = []; +$integria_criticity_values = []; +$integria_users_values = []; +$integria_types_values = []; +$integria_status_values = []; + +$integria_groups_csv = integria_api_call($config['integria_hostname'], $config['integria_user'], $config['integria_pass'], $config['integria_api_pass'], 'get_groups', []); + +get_array_from_csv_data_pair($integria_groups_csv, $integria_group_values); + +$integria_status_csv = integria_api_call($config['integria_hostname'], $config['integria_user'], $config['integria_pass'], $config['integria_api_pass'], 'get_incidents_status', []); + +get_array_from_csv_data_pair($integria_status_csv, $integria_status_values); + +$integria_criticity_levels_csv = integria_api_call($config['integria_hostname'], $config['integria_user'], $config['integria_pass'], $config['integria_api_pass'], 'get_incident_priorities', []); + +get_array_from_csv_data_pair($integria_criticity_levels_csv, $integria_criticity_values); + +$integria_users_csv = integria_api_call($config['integria_hostname'], $config['integria_user'], $config['integria_pass'], $config['integria_api_pass'], 'get_users', []); + +$csv_array = explode("\n", $integria_users_csv); + +foreach ($csv_array as $csv_line) { + if (!empty($csv_line)) { + $integria_users_values[$csv_line] = $csv_line; + } +} + +$integria_types_csv = integria_api_call($config['integria_hostname'], $config['integria_user'], $config['integria_pass'], $config['integria_api_pass'], 'get_types', []); + +get_array_from_csv_data_pair($integria_types_csv, $integria_types_values); + +// Enable table. +$table_enable = new StdClass(); +$table_enable->data = []; +$table_enable->width = '100%'; +$table_enable->id = 'integria-enable-setup'; +$table_enable->class = 'databox filters'; +$table_enable->size['name'] = '30%'; +$table_enable->style['name'] = 'font-weight: bold'; + +// Enable Integria. +$row = []; +$row['name'] = __('Enable Integria IMS'); +$row['control'] = html_print_checkbox_switch('integria_enabled', 1, $config['integria_enabled'], true); +$table_enable->data['integria_enabled'] = $row; + +// Remote config table. +$table_remote = new StdClass(); +$table_remote->data = []; +$table_remote->width = '100%'; +$table_remote->styleTable = 'margin-bottom: 10px;'; +$table_remote->id = 'integria-remote-setup'; +$table_remote->class = 'databox filters'; +$table_remote->size['name'] = '30%'; +$table_remote->style['name'] = 'font-weight: bold'; + +// Integria user. +$row = []; +$row['name'] = __('User'); +$row['control'] = html_print_input_text('integria_user', $config['integria_user'], '', 30, 100, true); +$table_remote->data['integria_user'] = $row; + +// Integria password. +$row = []; +$row['name'] = __('Password'); +$row['control'] = html_print_input_password('integria_pass', io_output_password($config['integria_pass']), '', 30, 100, true); +$table_remote->data['integria_pass'] = $row; + +// Integria hostname. +$row = []; +$row['name'] = __('API Hostname'); +$row['control'] = html_print_input_text('integria_hostname', $config['integria_hostname'], '', 30, 100, true); +$row['control'] .= ui_print_help_tip(__('Hostname of Integria IMS\' API (scheme must be specified. Example: http://192.168.0.0)'), true); +$table_remote->data['integria_hostname'] = $row; + +// API password. +$row = []; +$row['name'] = __('API Password'); +$row['control'] = html_print_input_password('integria_api_pass', io_output_password($config['integria_api_pass']), '', 30, 100, true); +$row['control'] .= ui_print_help_tip(__('Password of Integria IMS\' API'), true); +$table_remote->data['integria_api_pass'] = $row; + +// Request timeout. +$row = []; +$row['name'] = __('Request timeout'); +$row['control'] = html_print_input_text('integria_req_timeout', $config['integria_req_timeout'], '', 3, 10, true); +$row['control'] .= ui_print_help_tip(__('Time in seconds to set the maximum time of the requests to the Integria API').'. '.__('0 to disable'), true); +$table_remote->data['integria_req_timeout'] = $row; + +// Alert settings. +$table_alert_settings = new StdClass(); +$table_alert_settings->data = []; +$table_alert_settings->width = '100%'; +$table_alert_settings->styleTable = 'margin-bottom: 10px;'; +$table_alert_settings->id = 'integria-cr-settings-setup'; +$table_alert_settings->class = 'databox filters'; +$table_alert_settings->size['name'] = '30%'; +$table_alert_settings->style['name'] = 'font-weight: bold'; + +// Alert incident title. +$row = []; +$row['name'] = __('Title'); +$row['control'] = html_print_input_text( + 'incident_title', + $config['incident_title'], + __('Name'), + 50, + 100, + true, + false, + false +).ui_print_help_icon('alert_macros', true); +$table_alert_settings->data['custom_response_incident_title'] = $row; + +// Alert incident description. +$row = []; +$row['name'] = __('Description'); +$row['control'] = html_print_input_text( + 'incident_content', + $config['incident_content'], + '', + 50, + 100, + true, + false, + false +).ui_print_help_icon('alert_macros', true); +$table_alert_settings->data['custom_response_incident_content'] = $row; + +// Alert default group. +$row = []; +$row['name'] = __('Group'); +$row['control'] = html_print_select( + $integria_group_values, + 'default_group', + $config['default_group'], + '', + __('Select'), + 0, + true, + false, + true, + '', + false +); +$table_alert_settings->data['custom_response_def_group'] = $row; + +// Alert default criticity. +$row = []; +$row['name'] = __('Priority'); +$row['control'] = html_print_select( + $integria_criticity_values, + 'default_criticity', + $config['default_criticity'], + '', + __('Select'), + 0, + true, + false, + true, + '', + false +); +$table_alert_settings->data['custom_response_def_criticity'] = $row; + +// Alert default owner. +$row = []; +$row['name'] = __('Owner'); +$row['control'] = html_print_autocomplete_users_from_integria( + 'default_owner', + $config['default_owner'], + true +); + +$table_alert_settings->data['custom_response_def_owner'] = $row; + +// Alert default incident type. +$row = []; +$row['name'] = __('Type'); +$row['control'] = html_print_select( + $integria_types_values, + 'incident_type', + $config['incident_type'], + '', + __('Select'), + 0, + true, + false, + true, + '', + false +); +$table_alert_settings->data['custom_response_incident_type'] = $row; + +// Alert default incident status. +$row = []; +$row['name'] = __('Status'); +$row['control'] = html_print_select( + $integria_status_values, + 'incident_status', + $config['incident_status'], + '', + __('Select'), + 0, + true, + false, + true, + '', + false +); +$table_alert_settings->data['custom_response_incident_status'] = $row; + +// Custom response settings. +$table_cr_settings = new StdClass(); +$table_cr_settings->data = []; +$table_cr_settings->width = '100%'; +$table_cr_settings->styleTable = 'margin-bottom: 10px;'; +$table_cr_settings->id = 'integria-cr-settings-setup'; +$table_cr_settings->class = 'databox filters'; +$table_cr_settings->size['name'] = '30%'; +$table_cr_settings->style['name'] = 'font-weight: bold'; + +// Custom response incident title. +$row = []; +$row['name'] = __('Title'); +$row['control'] = html_print_input_text( + 'cr_incident_title', + $config['cr_incident_title'], + __('Name'), + 50, + 100, + true, + false, + false +).ui_print_help_icon('response_macros', true); +$table_cr_settings->data['custom_response_incident_title'] = $row; + +// Custom response incident description. +$row = []; +$row['name'] = __('Description'); +$row['control'] = html_print_input_text( + 'cr_incident_content', + $config['cr_incident_content'], + '', + 50, + 100, + true, + false, + false +).ui_print_help_icon('response_macros', true); +$table_cr_settings->data['custom_response_incident_content'] = $row; + +// Custom response default group. +$row = []; +$row['name'] = __('Group'); +$row['control'] = html_print_select( + $integria_group_values, + 'cr_default_group', + $config['cr_default_group'], + '', + __('Select'), + 0, + true, + false, + true, + '', + false +); +$table_cr_settings->data['custom_response_def_group'] = $row; + +// Custom response default criticity. +$row = []; +$row['name'] = __('Priority'); +$row['control'] = html_print_select( + $integria_criticity_values, + 'cr_default_criticity', + $config['cr_default_criticity'], + '', + __('Select'), + 0, + true, + false, + true, + '', + false +); +$table_cr_settings->data['custom_response_def_criticity'] = $row; + +// Custom response default owner. +$row = []; +$row['name'] = __('Owner'); +$row['control'] = html_print_autocomplete_users_from_integria( + 'cr_default_owner', + $config['cr_default_owner'], + true +); + +$table_cr_settings->data['custom_response_def_owner'] = $row; + +// Custom response default incident type. +$row = []; +$row['name'] = __('Type'); +$row['control'] = html_print_select( + $integria_types_values, + 'cr_incident_type', + $config['cr_incident_type'], + '', + __('Select'), + 0, + true, + false, + true, + '', + false +); +$table_cr_settings->data['custom_response_incident_type'] = $row; + +// Custom response default incident status. +$row = []; +$row['name'] = __('Status'); +$row['control'] = html_print_select( + $integria_status_values, + 'cr_incident_status', + $config['cr_incident_status'], + '', + __('Select'), + 0, + true, + false, + true, + '', + false +); +$table_cr_settings->data['custom_response_incident_status'] = $row; + +// Test. +$row = []; +$row['name'] = __('Test'); +$row['control'] = html_print_button(__('Start'), 'test-integria', false, '', 'class="sub next"', true); +$row['control'] .= ''; +$row['control'] .= ''; +$row['control'] .= ''; +$row['control'] .= ' '; +$table_remote->data['integria_test'] = $row; + +// Print. +echo '
'; +echo ''; +html_print_image('images/integria_logo.png'); +echo ''; +echo '
'; +echo '
'; +echo __('Integria IMS'); +echo '
'; +echo ''; +echo 'https://integriaims.com'; +echo ''; +echo '
'; + +echo "
"; +html_print_input_hidden('update_config', 1); + +// Form enable. +echo '
'; +html_print_table($table_enable); +echo '
'; + +// Form remote. +echo '
'; +echo '
'; +echo ''.__('Integria API settings').''; + +html_print_table($table_remote); + +echo '
'; +echo '
'; + +if ($has_connection != false) { + // Form alert default settings. + echo '
'; + echo '
'; + echo ''.__('Alert default values').''; + + html_print_table($table_alert_settings); + + echo '
'; + echo '
'; + + // Form custom response default settings. + echo '
'; + echo '
'; + echo ''.__('Event custom response default values').''; + + html_print_table($table_cr_settings); + + echo '
'; + echo '
'; + + echo '
'; + html_print_submit_button(__('Update'), 'update_button', false, 'class="sub upd"'); + echo '
'; +} else { + echo '
'; + html_print_submit_button(__('Update and continue'), 'update_button', false, 'class="sub next"'); + echo '
'; +} + + +echo '
'; + +?> + + diff --git a/pandora_console/godmode/users/configure_user.php b/pandora_console/godmode/users/configure_user.php index 07e9942880..602a536f5a 100644 --- a/pandora_console/godmode/users/configure_user.php +++ b/pandora_console/godmode/users/configure_user.php @@ -828,7 +828,7 @@ $values = [ 0 => __('No'), ]; -$table->data[12][0] = __('Home screen').ui_print_help_tip(__('User can customize the home page. By default, will display \'Agent Detail\'. Example: Select \'Other\' and type sec=estado&sec2=operation/agentes/estado_agente to show agent detail view'), true); +$table->data[12][0] = __('Home screen').ui_print_help_tip(__('User can customize the home page. By default, will display \'Agent Detail\'. Example: Select \'Other\' and type index.php?sec=estado&sec2=operation/agentes/ver_agente&id_agente=1 to show agent detail view'), true); $values = [ 'Default' => __('Default'), 'Visual console' => __('Visual console'), diff --git a/pandora_console/images/integria.png b/pandora_console/images/integria.png new file mode 100644 index 0000000000..9dcad27e3b Binary files /dev/null and b/pandora_console/images/integria.png differ diff --git a/pandora_console/images/integria_logo.png b/pandora_console/images/integria_logo.png new file mode 100644 index 0000000000..f56ecfdab5 Binary files /dev/null and b/pandora_console/images/integria_logo.png differ diff --git a/pandora_console/images/integria_logo_gray.png b/pandora_console/images/integria_logo_gray.png new file mode 100644 index 0000000000..d901e72493 Binary files /dev/null and b/pandora_console/images/integria_logo_gray.png differ diff --git a/pandora_console/include/ajax/integria_incidents.ajax.php b/pandora_console/include/ajax/integria_incidents.ajax.php new file mode 100644 index 0000000000..3991ecf51c --- /dev/null +++ b/pandora_console/include/ajax/integria_incidents.ajax.php @@ -0,0 +1,60 @@ +notify( [ 'type' => 'NOTIF.PERMISSIONS.REMOTE_CONFIG', 'title' => __('Remote configuration directory is not readable'), 'message' => __( 'Remote configuration directory %s is not readable. Please, adjust configuration.', - $config['remote_config'] + $remote_config_dir ), 'url' => ui_get_full_url('index.php?sec=general&sec2=godmode/setup/setup§ion=general'), ] @@ -896,14 +898,14 @@ class ConsoleSupervisor ); } - if (is_writable($config['remote_config'].'/conf') !== true) { + if (is_writable($remote_config_dir.'/conf') !== true) { $this->notify( [ 'type' => 'NOTIF.PERMISSIONS.REMOTE_CONFIG.CONF', 'title' => __('Remote configuration directory is not writable'), 'message' => __( 'Remote configuration directory %s is not writable. Please, adjust configuration.', - $config['remote_config'].'/conf' + $remote_config_dir.'/conf' ), 'url' => ui_get_full_url('index.php?sec=general&sec2=godmode/setup/setup§ion=general'), ] @@ -914,14 +916,14 @@ class ConsoleSupervisor ); } - if (is_writable($config['remote_config'].'/collections') !== true) { + if (is_writable($remote_config_dir.'/collections') !== true) { $this->notify( [ 'type' => 'NOTIF.PERMISSIONS.REMOTE_CONFIG.COLLECTIONS', 'title' => __('Remote collections directory is not writable'), 'message' => __( 'Collections directory %s is not writable. Please, adjust configuration.', - $config['remote_config'].'/collections' + $remote_config_dir.'/collections' ), 'url' => ui_get_full_url('index.php?sec=general&sec2=godmode/setup/setup§ion=general'), ] @@ -932,14 +934,14 @@ class ConsoleSupervisor ); } - if (is_writable($config['remote_config'].'/md5') !== true) { + if (is_writable($remote_config_dir.'/md5') !== true) { $this->notify( [ 'type' => 'NOTIF.PERMISSIONS.REMOTE_CONFIG.MD5', 'title' => __('Remote md5 directory is not writable'), 'message' => __( 'MD5 directory %s is not writable. Please, adjust configuration.', - $config['remote_config'].'/md5' + $remote_config_dir.'/md5' ), 'url' => ui_get_full_url('index.php?sec=general&sec2=godmode/setup/setup§ion=general'), ] @@ -957,7 +959,7 @@ class ConsoleSupervisor $MAX_BADXML_FILES_DATA_IN = 150; $filecount = $this->countFiles( - $config['remote_config'], + $remote_config_dir, '', $MAX_FILES_DATA_IN ); @@ -970,7 +972,7 @@ class ConsoleSupervisor 'message' => __( 'There are more than %d files in %s. Consider checking DataServer performance', $MAX_FILES_DATA_IN, - $config['remote_config'] + $remote_config_dir ), 'url' => ui_get_full_url('index.php?sec=general&sec2=godmode/setup/setup§ion=perf'), ] @@ -980,7 +982,7 @@ class ConsoleSupervisor } $filecount = $this->countFiles( - $config['remote_config'], + $remote_config_dir, '/^.*BADXML$/', $MAX_BADXML_FILES_DATA_IN ); @@ -993,7 +995,7 @@ class ConsoleSupervisor 'message' => __( 'There are more than %d files in %s. Consider checking software agents.', $MAX_BADXML_FILES_DATA_IN, - $config['remote_config'] + $remote_config_dir ), 'url' => ui_get_full_url('index.php?sec=general&sec2=godmode/setup/setup§ion=perf'), ] @@ -1266,7 +1268,8 @@ class ConsoleSupervisor $PHPSerialize_precision = ini_get('serialize_precision'); // PhantomJS status. - $result_ejecution = exec($config['phantomjs_bin'].'/phantomjs --version'); + $phantomjs_dir = io_safe_output($config['phantomjs_bin']); + $result_ejecution = exec($phantomjs_dir.'/phantomjs --version'); // PHP version checks. $php_version = phpversion(); @@ -1451,8 +1454,9 @@ class ConsoleSupervisor 'type' => 'NOTIF.PHP.SERIALIZE_PRECISION', 'title' => sprintf( __("Not recommended '%s' value in PHP configuration"), - 'serialze_precision' - ), 'message' => sprintf( + 'serialize_precision' + ), + 'message' => sprintf( __('Recommended value is: %s'), sprintf('-1') ).'

'.__('Please, change it on your PHP configuration file (php.ini) or contact with administrator'), @@ -2061,8 +2065,10 @@ class ConsoleSupervisor { global $config; - if (($config['fontpath'] == '') - || (file_exists($config['fontpath']) === false) + $fontpath = io_safe_output($config['fontpath']); + + if (($fontpath == '') + || (file_exists($fontpath) === false) ) { $this->notify( [ diff --git a/pandora_console/include/class/CredentialStore.class.php b/pandora_console/include/class/CredentialStore.class.php new file mode 100644 index 0000000000..188950d92a --- /dev/null +++ b/pandora_console/include/class/CredentialStore.class.php @@ -0,0 +1,1203 @@ +AJAXMethods); + } + + + /** + * Generates a JSON error. + * + * @param string $msg Error message. + * + * @return void + */ + public function error($msg) + { + echo json_encode( + ['error' => $msg] + ); + } + + + /** + * Minor function to dump json message as ajax response. + * + * @param string $type Type: result || error. + * @param string $msg Message. + * @param boolean $delete Deletion messages. + * + * @return void + */ + private function ajaxMsg($type, $msg, $delete=false) + { + $msg_err = 'Failed while saving: %s'; + $msg_ok = 'Successfully saved into keystore '; + + if ($delete) { + $msg_err = 'Failed while removing: %s'; + $msg_ok = 'Successfully deleted '; + } + + if ($type == 'error') { + echo json_encode( + [ + $type => ui_print_error_message( + __( + $msg_err, + $msg + ), + '', + true + ), + ] + ); + } else { + echo json_encode( + [ + $type => ui_print_success_message( + __( + $msg_ok, + $msg + ), + '', + true + ), + ] + ); + } + + exit; + } + + + /** + * Initializes object and validates user access. + * + * @param string $ajax_controller Path of ajaxController, is the 'page' + * variable sent in ajax calls. + * + * @return Object + */ + public function __construct($ajax_controller) + { + global $config; + + // Check access. + check_login(); + + if (! check_acl($config['id_user'], 0, 'AR')) { + db_pandora_audit( + 'ACL Violation', + 'Trying to access event viewer' + ); + + if (is_ajax()) { + echo json_encode(['error' => 'noaccess']); + } + + include 'general/noaccess.php'; + exit; + } + + $this->ajaxController = $ajax_controller; + + return $this; + } + + + /** + * Returns an array with all the credentials matching filter and ACL. + * + * @param array $fields Fields array or 'count' keyword to retrieve count. + * @param array $filter Filters to be applied. + * @param integer $offset Offset (pagination). + * @param integer $limit Limit (pagination). + * @param string $order Sort order. + * @param string $sort_field Sort field. + * + * @return array With all results or false if error. + * @throws Exception On error. + */ + public static function getAll( + $fields, + $filter, + $offset=null, + $limit=null, + $order=null, + $sort_field=null + ) { + $sql_filters = []; + $order_by = ''; + $pagination = ''; + + global $config; + + if (!is_array($filter)) { + error_log('[credential_get_all] Filter must be an array.'); + throw new Exception('[credential_get_all] Filter must be an array.'); + } + + $count = false; + if (!is_array($fields) && $fields == 'count') { + $fields = ['cs.*']; + $count = true; + } else if (!is_array($fields)) { + error_log('[credential_get_all] Fields must be an array or "count".'); + throw new Exception('[credential_get_all] Fields must be an array or "count".'); + } + + if (isset($filter['product']) && !empty($filter['product'])) { + $sql_filters[] = sprintf(' AND cs.product = "%s"', $filter['product']); + } + + if (isset($filter['free_search']) && !empty($filter['free_search'])) { + $sql_filters[] = vsprintf( + ' AND (lower(cs.username) like lower("%%%s%%") + OR cs.identifier like "%%%s%%" + OR lower(cs.product) like lower("%%%s%%"))', + array_fill(0, 3, $filter['free_search']) + ); + } + + if (isset($filter['filter_id_group']) && $filter['filter_id_group'] > 0) { + $propagate = db_get_value( + 'propagate', + 'tgrupo', + 'id_grupo', + $filter['filter_id_group'] + ); + + if (!$propagate) { + $sql_filters[] = sprintf( + ' AND cs.id_group = %d ', + $filter['filter_id_group'] + ); + } else { + $groups = [ $filter['filter_id_group'] ]; + $childrens = groups_get_childrens($id_group, null, true); + if (!empty($childrens)) { + foreach ($childrens as $child) { + $groups[] = (int) $child['id_grupo']; + } + } + + $filter['filter_id_group'] = $groups; + $sql_filters[] = sprintf( + ' AND cs.id_group IN (%s) ', + join(',', $filter['filter_id_group']) + ); + } + } + + if (isset($filter['group_list']) && is_array($filter['group_list'])) { + $sql_filters[] = sprintf( + ' AND cs.id_group IN (%s) ', + join(',', $filter['group_list']) + ); + } else if (users_is_admin() !== true) { + $user_groups = users_get_groups( + $config['id_user'], + 'AR' + ); + + // Always add group 'ALL' because 'ALL' group credentials + // must be available for all users. + if (is_array($user_groups) === true) { + $user_groups = ([0] + array_keys($user_groups)); + } else { + $user_groups = [0]; + } + + $sql_filters[] = sprintf( + ' AND cs.id_group IN (%s) ', + join(',', $user_groups) + ); + } + + if (isset($filter['identifier'])) { + $sql_filters[] = sprintf( + ' AND cs.identifier = "%s" ', + $filter['identifier'] + ); + } + + if (isset($order)) { + $dir = 'asc'; + if ($order == 'desc') { + $dir = 'desc'; + }; + + if (in_array( + $sort_field, + [ + 'group', + 'identifier', + 'product', + 'username', + 'options', + ] + ) + ) { + $order_by = sprintf( + 'ORDER BY `%s` %s', + $sort_field, + $dir + ); + } + } + + if (isset($limit) && $limit > 0 + && isset($offset) && $offset >= 0 + ) { + $pagination = sprintf( + ' LIMIT %d OFFSET %d ', + $limit, + $offset + ); + } + + $sql = sprintf( + 'SELECT %s + FROM tcredential_store cs + LEFT JOIN tgrupo tg + ON tg.id_grupo = cs.id_group + WHERE 1=1 + %s + %s + %s', + join(',', $fields), + join(' ', $sql_filters), + $order_by, + $pagination + ); + + if ($count) { + $sql = sprintf('SELECT count(*) as n FROM ( %s ) tt', $sql); + + return db_get_value_sql($sql); + } + + return db_get_all_rows_sql($sql); + } + + + /** + * Retrieves target key from keystore or false in case of error. + * + * @param string $identifier Key identifier. + * + * @return array Key or false if error. + */ + public static function getKey($identifier) + { + global $config; + + if (empty($identifier)) { + return false; + } + + $keys = self::getAll( + [ + 'cs.*', + 'tg.nombre as `group`', + ], + ['identifier' => $identifier] + ); + + if (is_array($keys) === true) { + // Only 1 must exist. + $key = $keys[0]; + + // Decrypt content. + $key['username'] = io_output_password($key['username']); + $key['password'] = io_output_password($key['password']); + + return $key; + } + + return false; + } + + + /** + * Return all keys avaliable for current user. + * + * @param string $product Filter by product. + * + * @return array Keys or false if error. + */ + public static function getKeys($product=false) + { + global $config; + + if ($product !== false) { + $filter['product'] = $product; + } + + $keys = self::getAll( + [ + 'cs.*', + 'tg.nombre as `group`', + ], + $filter + ); + + if (is_array($keys) === true) { + // Improve usage and decode output. + $return = array_reduce( + $keys, + function ($carry, $item) { + $item['username'] = io_output_password($item['username']); + $item['password'] = io_output_password($item['password']); + $carry[$item['identifier']] = $item['identifier']; + return $carry; + } + ); + + return $return; + } + + return false; + } + + + /** + * Ajax method invoked by datatables to draw content. + * + * @return void + */ + public function draw() + { + // Datatables offset, limit and order. + $filter = get_parameter('filter', []); + $start = get_parameter('start', 0); + $length = get_parameter('length', $config['block_size']); + $order = get_datatable_order(true); + try { + ob_start(); + + $fields = [ + 'cs.*', + 'tg.nombre as `group`', + ]; + + // Retrieve data. + $data = $this->getAll( + // Fields. + $fields, + // Filter. + $filter, + // Offset. + $start, + // Limit. + $length, + // Order. + $order['direction'], + // Sort field. + $order['field'] + ); + + // Retrieve counter. + $count = $this->getAll( + 'count', + $filter + ); + + if ($data) { + $data = array_reduce( + $data, + function ($carry, $item) { + // Transforms array of arrays $data into an array + // of objects, making a post-process of certain fields. + $tmp = (object) $item; + $tmp->username = io_output_password($tmp->username); + + if (empty($tmp->group)) { + $tmp->group = __('All'); + } else { + $tmp->group = io_safe_output($tmp->group); + } + + $carry[] = $tmp; + return $carry; + } + ); + } + + // Datatables format: RecordsTotal && recordsfiltered. + echo json_encode( + [ + 'data' => $data, + 'recordsTotal' => $count, + 'recordsFiltered' => $count, + ] + ); + // Capture output. + $response = ob_get_clean(); + } catch (Exception $e) { + echo json_encode(['error' => $e->getMessage()]); + exit; + } + + // If not valid, show error with issue. + json_decode($response); + if (json_last_error() == JSON_ERROR_NONE) { + // If valid dump. + echo $response; + } else { + echo json_encode( + ['error' => $response] + ); + } + + exit; + } + + + /** + * Prints inputs for modal "Add key". + * + * @return void + */ + public function loadModal() + { + $identifier = get_parameter('identifier', null); + $key = self::getKey($identifier); + + echo $this->printInputs($key); + } + + + /** + * Prepare variables received using form. AJAX environment only. + * + * @return array of values processed or false in case of error. + */ + private function prepareKeyValues() + { + $identifier = get_parameter('identifier', null); + $id_group = get_parameter('id_group', null); + $product = get_parameter('product', null); + $username = get_parameter('username', null); + $password = get_parameter('password', null); + $extra_1 = get_parameter('extra_1', null); + $extra_2 = get_parameter('extra_2', null); + + if (empty($identifier)) { + $error = __('Key identifier is required'); + } else if ($id_group === null) { + $error = __('You must select a group where store this key!'); + } else if (empty($product)) { + $error = __('You must specify a product type'); + } else if (empty($username) && (empty($password))) { + $error = __('You must specify a username and/or password'); + } + + // Encrypt content (if needed). + $values = [ + 'identifier' => $identifier, + 'id_group' => $id_group, + 'product' => $product, + 'username' => io_input_password($username), + 'password' => io_input_password($password), + 'extra_1' => $extra_1, + 'extra_2' => $extra_2, + ]; + + // Spaces are not allowed. + $values['identifier'] = preg_replace('/\s+/', '-', trim($identifier)); + + return $values; + } + + + /** + * Stores a key into credential store. + * + * @param array $values Key definition. + * @param string $identifier Update or create. + * + * @return boolean True if ok, false if not ok. + */ + private function storeKey($values, $identifier=false) + { + if ($identifier === false) { + // New. + return db_process_sql_insert('tcredential_store', $values); + } else { + // Update. + return db_process_sql_update( + 'tcredential_store', + $values, + ['identifier' => $identifier] + ); + } + + } + + + /** + * Add a new key into Credential Store + * + * @return void + */ + public function addKey() + { + global $config; + + $values = $this->prepareKeyValues(); + + if ($this->storeKey($values) === false) { + $this->ajaxMsg('error', $config['dbconnection']->error); + } else { + $this->ajaxMsg('result', $values['identifier']); + } + + exit; + } + + + /** + * Add a new key into Credential Store + * + * @return void + */ + public function updateKey() + { + global $config; + + $values = $this->prepareKeyValues(); + $identifier = $values['identifier']; + + if ($this->storeKey($values, $identifier) === false) { + $this->ajaxMsg('error', $config['dbconnection']->error); + } else { + $this->ajaxMsg('result', $identifier); + } + + exit; + } + + + /** + * AJAX method. Delete key from keystore. + * + * @return void + */ + public function deleteKey() + { + global $config; + + $identifier = get_parameter('identifier', null); + + if (empty($identifier)) { + $this->ajaxMsg('error', __('identifier cannot be empty'), true); + } + + if (self::getKey($identifier) === false) { + // User has no grants to delete target key. + $this->ajaxMsg('error', __('Not allowed'), true); + } + + if (db_process_sql_delete( + 'tcredential_store', + ['identifier' => $identifier] + ) === false + ) { + $this->ajaxMsg('error', $config['dbconnection']->error, true); + } else { + $this->ajaxMsg('result', $identifier, true); + } + + } + + + /** + * Run CredentialStore (main page). + * + * @return void + */ + public function run() + { + global $config; + + // Require specific CSS and JS. + ui_require_css_file('wizard'); + ui_require_css_file('discovery'); + ui_require_css_file('credential_store'); + + if (!isset($config['encryption_passphrase'])) { + $url = 'https://pandorafms.com/docs/index.php?title=Pandora:Documentation_en:Password_Encryption'; + if ($config['language'] == 'es') { + $url = 'https://pandorafms.com/docs/index.php?title=Pandora:Documentation_es:Cifrado_Contrase%C3%B1as'; + } + + ui_print_warning_message( + __( + 'Database encryption is not enabled. Credentials will be stored in plaintext. %s', + ''.__('How to configure encryption.').'' + ) + ); + } + + // Datatables list. + try { + $columns = [ + 'group', + 'identifier', + 'product', + 'username', + 'options', + ]; + + $column_names = [ + __('Group'), + __('Identifier'), + __('Product'), + __('User'), + [ + 'text' => __('Options'), + 'class' => 'action_buttons', + ], + ]; + + $this->tableId = 'keystore'; + // Load datatables user interface. + ui_print_datatable( + [ + 'id' => $this->tableId, + 'class' => 'info_table', + 'style' => 'width: 100%', + 'columns' => $columns, + 'column_names' => $column_names, + 'ajax_url' => $this->ajaxController, + 'ajax_data' => ['method' => 'draw'], + 'ajax_postprocess' => 'process_datatables_item(item)', + 'no_sortable_columns' => [-1], + 'order' => [ + 'field' => 'identifier', + 'direction' => 'asc', + ], + 'search_button_class' => 'sub filter float-right', + 'form' => [ + 'inputs' => [ + [ + 'label' => __('Group'), + 'type' => 'select', + 'id' => 'filter_id_group', + 'name' => 'filter_id_group', + 'options' => users_get_groups_for_select( + $config['id_user'], + 'AR', + true, + true, + false + ), + ], + [ + 'label' => __('Free search'), + 'type' => 'text', + 'class' => 'mw250px', + 'id' => 'free_search', + 'name' => 'free_search', + ], + ], + ], + ] + ); + } catch (Exception $e) { + echo $e->getMessage(); + } + + // Auxiliar div. + $modal = ''; + $msg = ''; + $aux = ''; + + echo $modal.$msg.$aux; + + // Create button. + echo '
'; + html_print_submit_button( + __('Add key'), + 'create', + false, + 'class="sub next"' + ); + echo '
'; + + echo $this->loadJS(); + + } + + + /** + * Generates inputs for new/update forms. + * + * @param array $values Values or null. + * + * @return string Inputs. + */ + public function printInputs($values=null) + { + if (!is_array($values)) { + $values = []; + } + + $form = [ + 'action' => '#', + 'id' => 'modal_form', + 'onsubmit' => 'return false;', + 'class' => 'modal', + 'extra' => 'autocomplete="new-password"', + ]; + + $inputs = []; + + $inputs[] = [ + 'label' => __('Identifier'), + 'id' => 'div-identifier', + 'arguments' => [ + 'name' => 'identifier', + 'type' => 'text', + 'value' => $values['identifier'], + 'disabled' => (bool) $values['identifier'], + 'return' => true, + ], + ]; + + $inputs[] = [ + 'label' => __('Group'), + 'arguments' => [ + 'name' => 'id_group', + 'id' => 'id_group', + 'input_class' => 'flex-row', + 'type' => 'select_groups', + 'selected' => $values['id_group'], + 'return' => true, + 'class' => 'w50p', + ], + ]; + + $inputs[] = [ + 'label' => __('Product'), + 'id' => 'div-product', + 'arguments' => [ + 'name' => 'product', + 'input_class' => 'flex-row', + 'type' => 'select', + 'script' => 'calculate_inputs()', + 'fields' => [ + 'CUSTOM' => __('Custom'), + 'AWS' => __('Aws'), + 'AZURE' => __('Azure'), + // 'GOOGLE' => __('Google'), + ], + 'selected' => (isset($values['product']) ? $values['product'] : 'CUSTOM'), + 'disabled' => (bool) $values['product'], + 'return' => true, + ], + ]; + + $user_label = __('Username'); + $pass_label = __('Password'); + $extra_1_label = __('Extra'); + $extra_2_label = __('Extra (2)'); + $extra1 = true; + $extra2 = true; + + // Remember to update credential_store.php also. + switch ($values['product']) { + case 'AWS': + $user_label = __('Access key ID'); + $pass_label = __('Secret access key'); + $extra1 = false; + $extra2 = false; + break; + + case 'AZURE': + $user_label = __('Account ID'); + $pass_label = __('Application secret'); + $extra_1_label = __('Tenant or domain name'); + $extra_2_label = __('Subscription id'); + break; + + case 'GOOGLE': + // Need further investigation. + case 'CUSTOM': + $user_label = __('Account ID'); + $pass_label = __('Password'); + $extra1 = false; + $extra2 = false; + default: + // Use defaults. + break; + } + + $inputs[] = [ + 'label' => $user_label, + 'id' => 'div-username', + 'arguments' => [ + 'name' => 'username', + 'input_class' => 'flex-row', + 'type' => 'text', + 'value' => $values['username'], + 'return' => true, + ], + ]; + + $inputs[] = [ + 'label' => $pass_label, + 'id' => 'div-password', + 'arguments' => [ + 'name' => 'password', + 'input_class' => 'flex-row', + 'type' => 'password', + 'value' => $values['password'], + 'return' => true, + ], + ]; + + if ($extra1) { + $inputs[] = [ + 'label' => $extra_1_label, + 'id' => 'div-extra_1', + 'arguments' => [ + 'name' => 'extra_1', + 'input_class' => 'flex-row', + 'type' => 'text', + 'value' => $values['extra_1'], + 'return' => true, + ], + ]; + } + + if ($extra2) { + $inputs[] = [ + 'label' => $extra_2_label, + 'id' => 'div-extra_2', + 'arguments' => [ + 'name' => 'extra_2', + 'input_class' => 'flex-row', + 'type' => 'text', + 'value' => $values['extra_2'], + 'return' => true, + 'display' => $extra2, + ], + + ]; + } + + return $this->printForm( + [ + 'form' => $form, + 'inputs' => $inputs, + ], + true + ); + } + + + /** + * Loads JS content. + * + * @return string JS content. + */ + public function loadJS() + { + ob_start(); + + // Javascript content. + ?> + + 0) { - $propagate = db_get_value( - 'propagate', - 'tgrupo', - 'id_grupo', - $filter['filter_id_group'] - ); - - if (!$propagate) { - $sql_filters[] = sprintf( - ' AND cs.id_group = %d ', - $filter['filter_id_group'] - ); - } else { - $groups = [ $filter['filter_id_group'] ]; - $childrens = groups_get_childrens($id_group, null, true); - if (!empty($childrens)) { - foreach ($childrens as $child) { - $groups[] = (int) $child['id_grupo']; - } - } - - $filter['filter_id_group'] = $groups; - $sql_filters[] = sprintf( - ' AND cs.id_group IN (%s) ', - join(',', $filter['filter_id_group']) - ); - } - } - - if (isset($filter['group_list']) && is_array($filter['group_list'])) { - $sql_filters[] = sprintf( - ' AND cs.id_group IN (%s) ', - join(',', $filter['group_list']) - ); - } - - if (isset($order)) { - $dir = 'asc'; - if ($order == 'desc') { - $dir = 'desc'; - }; - - if (in_array( - $sort_field, - [ - 'group', - 'identifier', - 'product', - 'username', - 'options', - ] - ) - ) { - $order_by = sprintf( - 'ORDER BY `%s` %s', - $sort_field, - $dir - ); - } - } - - if (isset($limit) && $limit > 0 - && isset($offset) && $offset >= 0 - ) { - $pagination = sprintf( - ' LIMIT %d OFFSET %d ', - $limit, - $offset - ); - } - - $sql = sprintf( - 'SELECT %s - FROM tcredential_store cs - LEFT JOIN tgrupo tg - ON tg.id_grupo = cs.id_group - WHERE 1=1 - %s - %s - %s', - join(',', $fields), - join(' ', $sql_filters), - $order_by, - $pagination - ); - - if ($count) { - $sql = sprintf('SELECT count(*) as n FROM ( %s ) tt', $sql); - - return db_get_value_sql($sql); - } - - return db_get_all_rows_sql($sql); -} - - -/** - * Retrieves target key from keystore or false in case of error. - * - * @param string $identifier Key identifier. - * - * @return array Key or false if error. - */ -function get_key($identifier) -{ - return db_get_row_filter( - 'tcredential_store', - [ 'identifier' => $identifier ] - ); -} - - -/** - * Minor function to dump json message as ajax response. - * - * @param string $type Type: result || error. - * @param string $msg Message. - * @param boolean $delete Deletion messages. - * - * @return void - */ -function ajax_msg($type, $msg, $delete=false) -{ - $msg_err = 'Failed while saving: %s'; - $msg_ok = 'Successfully saved into keystore '; - - if ($delete) { - $msg_err = 'Failed while removing: %s'; - $msg_ok = 'Successfully deleted '; - } - - if ($type == 'error') { - echo json_encode( - [ - $type => ui_print_error_message( - __( - $msg_err, - $msg - ), - '', - true - ), - ] - ); - } else { - echo json_encode( - [ - $type => ui_print_success_message( - __( - $msg_ok, - $msg - ), - '', - true - ), - ] - ); - } - - exit; -} - - -/** - * Generates inputs for new/update forms. - * - * @param array $values Values or null. - * - * @return string Inputs. - */ -function print_inputs($values=null) -{ - if (!is_array($values)) { - $values = []; - } - - $return = ''; - $return .= html_print_input( - [ - 'label' => __('Identifier'), - 'name' => 'identifier', - 'input_class' => 'flex-row', - 'type' => 'text', - 'value' => $values['identifier'], - 'disabled' => (bool) $values['identifier'], - 'return' => true, - 'script' => 'alert(\'puta\')', - ] - ); - $return .= html_print_input( - [ - 'label' => __('Group'), - 'name' => 'id_group', - 'id' => 'id_group', - 'input_class' => 'flex-row', - 'type' => 'select_groups', - 'selected' => $values['id_group'], - 'return' => true, - 'class' => 'w50p', - ] - ); - $return .= html_print_input( - [ - 'label' => __('Product'), - 'name' => 'product', - 'input_class' => 'flex-row', - 'type' => 'select', - 'script' => 'calculate_inputs()', - 'fields' => [ - 'CUSTOM' => __('Custom'), - 'AWS' => __('Aws'), - 'AZURE' => __('Azure'), - // 'GOOGLE' => __('Google'), - ], - 'selected' => $values['product'], - 'disabled' => (bool) $values['product'], - 'return' => true, - ] - ); - $user_label = __('Username'); - $pass_label = __('Password'); - $extra_1_label = __('Extra'); - $extra_2_label = __('Extra (2)'); - $extra1 = true; - $extra2 = true; - - // Remember to update credential_store.php also. - switch ($values['product']) { - case 'AWS': - $user_label = __('Access key ID'); - $pass_label = __('Secret access key'); - $extra1 = false; - $extra2 = false; - break; - - case 'AZURE': - $user_label = __('Account ID'); - $pass_label = __('Application secret'); - $extra_1_label = __('Tenant or domain name'); - $extra_2_label = __('Subscription id'); - break; - - case 'GOOGLE': - // Need further investigation. - case 'CUSTOM': - $user_label = __('Account ID'); - $pass_label = __('Password'); - $extra1 = false; - $extra2 = false; - default: - // Use defaults. - break; - } - - $return .= html_print_input( - [ - 'label' => $user_label, - 'name' => 'username', - 'input_class' => 'flex-row', - 'type' => 'text', - 'value' => $values['username'], - 'return' => true, - ] - ); - $return .= html_print_input( - [ - 'label' => $pass_label, - 'name' => 'password', - 'input_class' => 'flex-row', - 'type' => 'password', - 'value' => $values['password'], - 'return' => true, - ] - ); - if ($extra1) { - $return .= html_print_input( - [ - 'label' => $extra_1_label, - 'name' => 'extra_1', - 'input_class' => 'flex-row', - 'type' => 'text', - 'value' => $values['extra_1'], - 'return' => true, - ] - ); - } - - if ($extra2) { - $return .= html_print_input( - [ - 'label' => $extra_2_label, - 'name' => 'extra_2', - 'input_class' => 'flex-row', - 'type' => 'text', - 'value' => $values['extra_2'], - 'return' => true, - 'display' => $extra2, - ] - ); - } - - return $return; -} - - -/** - * Retrieve all identifiers available for current user. - * - * @param string $product Target product. - * - * @return array Of account identifiers. - */ -function credentials_list_accounts($product) -{ - global $config; - - check_login(); - - include_once $config['homedir'].'/include/functions_users.php'; - - static $user_groups; - - if (!isset($user_groups)) { - $user_groups = users_get_groups( - $config['id_user'], - 'AR' - ); - - // Always add group 'ALL' because 'ALL' group credentials - // must be available for all users. - if (is_array($user_groups)) { - $user_groups = ([0] + array_keys($user_groups)); - } else { - $user_groups = [0]; - } - } - - $creds = credentials_get_all( - ['identifier'], - [ - 'product' => $product, - 'group_list' => $user_groups, - ] - ); - - if ($creds === false) { - return []; - } - - $ret = array_reduce( - $creds, - function ($carry, $item) { - $carry[$item['identifier']] = $item['identifier']; - return $carry; - } - ); - - return $ret; -} +// Deprecated. diff --git a/pandora_console/include/functions_events.php b/pandora_console/include/functions_events.php index 24cfd6f10c..c3a25238f4 100644 --- a/pandora_console/include/functions_events.php +++ b/pandora_console/include/functions_events.php @@ -2739,6 +2739,10 @@ function events_get_agent( $date = time_w_fixed_tz($date); } + if (is_metaconsole() && $events_group === false) { + $id_server = true; + } + if (empty($date)) { $date = get_system_time(); } @@ -6636,6 +6640,247 @@ function events_get_secondary_groups_left_join($table) } +/** + * Replace macros in any string given an event id. + * If server_id > 0, it's a metaconsole query. + * + * @param integer $event_id Event identifier. + * @param integer $value String value in which we want to apply macros. + * + * @return string The response text with the macros applied. + */ +function events_get_field_value_by_event_id( + int $event_id, + $value +) { + global $config; + + $meta = false; + $event = db_get_row('tevento', 'id_evento', $event_id); + + // Replace each macro. + if (strpos($value, '_agent_address_') !== false) { + if ($meta) { + $agente_table_name = 'tmetaconsole_agent'; + $filter = [ + 'id_tagente' => $event['id_agente'], + 'id_tmetaconsole_setup' => $server_id, + ]; + } else { + $agente_table_name = 'tagente'; + $filter = ['id_agente' => $event['id_agente']]; + } + + $ip = db_get_value_filter('direccion', $agente_table_name, $filter); + // If agent does not have an IP, display N/A. + if ($ip === false) { + $ip = __('N/A'); + } + + $value = str_replace('_agent_address_', $ip, $value); + } + + if (strpos($value, '_agent_id_') !== false) { + $value = str_replace('_agent_id_', $event['id_agente'], $value); + } + + if (strpos($value, '_module_address_') !== false) { + if ($event['id_agentmodule'] != 0) { + if ($meta) { + $server = metaconsole_get_connection_by_id($server_id); + metaconsole_connect($server); + } + + $module = db_get_row('tagente_modulo', 'id_agente_modulo', $event['id_agentmodule']); + if (empty($module['ip_target'])) { + $module['ip_target'] = __('N/A'); + } + + $value = str_replace('_module_address_', $module['ip_target'], $value); + if (empty($module['nombre'])) { + $module['nombre'] = __('N/A'); + } + + if ($meta) { + metaconsole_restore_db(); + } + } else { + $value = str_replace('_module_address_', __('N/A'), $value); + } + } + + if (strpos($value, '_module_name_') !== false) { + if ($event['id_agentmodule'] != 0) { + if ($meta) { + $server = metaconsole_get_connection_by_id($server_id); + metaconsole_connect($server); + } + + $module = db_get_row('tagente_modulo', 'id_agente_modulo', $event['id_agentmodule']); + if (empty($module['ip_target'])) { + $module['ip_target'] = __('N/A'); + } + + $value = str_replace( + '_module_name_', + io_safe_output($module['nombre']), + $value + ); + + if ($meta) { + metaconsole_restore_db(); + } + } else { + $value = str_replace('_module_name_', __('N/A'), $value); + } + } + + if (strpos($value, '_event_id_') !== false) { + $value = str_replace('_event_id_', $event['id_evento'], $value); + } + + if (strpos($value, '_user_id_') !== false) { + if (!empty($event['id_usuario'])) { + $value = str_replace('_user_id_', $event['id_usuario'], $value); + } else { + $value = str_replace('_user_id_', __('N/A'), $value); + } + } + + if (strpos($value, '_group_id_') !== false) { + $value = str_replace('_group_id_', $event['id_grupo'], $value); + } + + if (strpos($value, '_group_name_') !== false) { + $value = str_replace( + '_group_name_', + groups_get_name($event['id_grupo'], true), + $value + ); + } + + if (strpos($value, '_event_utimestamp_') !== false) { + $value = str_replace( + '_event_utimestamp_', + $event['utimestamp'], + $value + ); + } + + if (strpos($value, '_event_date_') !== false) { + $value = str_replace( + '_event_date_', + date($config['date_format'], $event['utimestamp']), + $value + ); + } + + if (strpos($value, '_event_text_') !== false) { + $value = str_replace( + '_event_text_', + events_display_name($event['evento']), + $value + ); + } + + if (strpos($value, '_event_type_') !== false) { + $value = str_replace( + '_event_type_', + events_print_type_description($event['event_type'], true), + $value + ); + } + + if (strpos($value, '_alert_id_') !== false) { + $value = str_replace( + '_alert_id_', + empty($event['is_alert_am']) ? __('N/A') : $event['is_alert_am'], + $value + ); + } + + if (strpos($value, '_event_severity_id_') !== false) { + $value = str_replace('_event_severity_id_', $event['criticity'], $value); + } + + if (strpos($value, '_event_severity_text_') !== false) { + $value = str_replace( + '_event_severity_text_', + get_priority_name($event['criticity']), + $value + ); + } + + if (strpos($value, '_module_id_') !== false) { + $value = str_replace('_module_id_', $event['id_agentmodule'], $value); + } + + if (strpos($value, '_event_tags_') !== false) { + $value = str_replace('_event_tags_', $event['tags'], $value); + } + + if (strpos($value, '_event_extra_id_') !== false) { + if (empty($event['id_extra'])) { + $value = str_replace('_event_extra_id_', __('N/A'), $value); + } else { + $value = str_replace('_event_extra_id_', $event['id_extra'], $value); + } + } + + if (strpos($value, '_event_source_') !== false) { + $value = str_replace('_event_source_', $event['source'], $value); + } + + if (strpos($value, '_event_instruction_') !== false) { + $value = str_replace( + '_event_instruction_', + events_display_instructions($event['event_type'], $event, false), + $value + ); + } + + if (strpos($value, '_owner_user_') !== false) { + if (empty($event['owner_user'])) { + $value = str_replace('_owner_user_', __('N/A'), $value); + } else { + $value = str_replace('_owner_user_', $event['owner_user'], $value); + } + } + + if (strpos($value, '_event_status_') !== false) { + $event_st = events_display_status($event['estado']); + $value = str_replace('_event_status_', $event_st['title'], $value); + } + + if (strpos($value, '_group_custom_id_') !== false) { + $group_custom_id = db_get_value_sql( + sprintf( + 'SELECT custom_id FROM tgrupo WHERE id_grupo=%s', + $event['id_grupo'] + ) + ); + $event_st = events_display_status($event['estado']); + $value = str_replace('_group_custom_id_', $group_custom_id, $value); + } + + // Parse the event custom data. + if (!empty($event['custom_data'])) { + $custom_data = json_decode(base64_decode($event['custom_data'])); + foreach ($custom_data as $key => $val) { + $value = str_replace('_customdata_'.$key.'_', $val, $value); + } + } + + // This will replace the macro with the current logged user. + if (strpos($value, '_current_user_') !== false) { + $value = str_replace('_current_user_', $config['id_user'], $value); + } + + return $value; + +} + + function events_get_instructions($event) { if (!is_array($event)) { diff --git a/pandora_console/include/functions_events.php.orig b/pandora_console/include/functions_events.php.orig new file mode 100644 index 0000000000..6ec9d42dfe --- /dev/null +++ b/pandora_console/include/functions_events.php.orig @@ -0,0 +1,6934 @@ + 0]; + } + + $table = events_get_events_table(is_metaconsole(), $history); + + switch ($filter['group_rep']) { + case '0': + case '2': + default: + // No groups option direct update. + $delete_sql = sprintf( + 'DELETE FROM %s + WHERE id_evento = %d', + $table, + $id_evento + ); + break; + + case '1': + // Group by events. + $sql = events_get_all( + ['te.*'], + $filter, + // Offset. + null, + // Limit. + null, + // Order. + null, + // Sort_field. + null, + // Historical table. + $history, + // Return_sql. + true + ); + + $target_ids = db_get_all_rows_sql( + sprintf( + 'SELECT tu.id_evento FROM %s tu INNER JOIN ( %s ) tf + ON tu.estado = tf.estado + AND tu.evento = tf.evento + AND tu.id_agente = tf.id_agente + AND tu.id_agentmodule = tf.id_agentmodule + AND tf.max_id_evento = %d', + $table, + $sql, + $id_evento + ) + ); + + // Try to avoid deadlock while updating full set. + if ($target_ids !== false && count($target_ids) > 0) { + $target_ids = array_reduce( + $target_ids, + function ($carry, $item) { + $carry[] = $item['id_evento']; + return $carry; + } + ); + + $delete_sql = sprintf( + 'DELETE FROM %s WHERE id_evento IN (%s)', + $table, + join(', ', $target_ids) + ); + } + break; + } + + return db_process_sql($delete_sql); +} + + +/** + * Retrieves all events related to matching one. + * + * @param integer $id_evento Master event (max_id_evento). + * @param array $filter Filters. + * @param boolean $count Count results or get results. + * @param boolean $history Apply on historical table. + * + * @return array Events or false in case of error. + */ +function events_get_related_events( + $id_evento, + $filter=null, + $count=false, + $history=false +) { + global $config; + + if (!isset($id_evento) || $id_evento <= 0) { + return false; + } + + if (!isset($filter) || !is_array($filter)) { + $filter = ['group_rep' => 0]; + } + + $table = events_get_events_table(is_metaconsole(), $history); + $select = '*'; + if ($count === true) { + $select = 'count(*) as n'; + }; + + switch ($filter['group_rep']) { + case '0': + case '2': + default: + // No groups option direct update. + $related_sql = sprintf( + 'SELECT %s FROM %s + WHERE id_evento = %d', + $select, + $table, + $id_evento + ); + break; + + case '1': + // Group by events. + $sql = events_get_all( + ['te.*'], + $filter, + // Offset. + null, + // Limit. + null, + // Order. + null, + // Sort_field. + null, + // Historical table. + $history, + // Return_sql. + true + ); + $related_sql = sprintf( + 'SELECT %s FROM %s tu INNER JOIN ( %s ) tf + WHERE tu.estado = tf.estado + AND tu.evento = tf.evento + AND tu.id_agente = tf.id_agente + AND tu.id_agentmodule = tf.id_agentmodule + AND tf.max_id_evento = %d', + $select, + $table, + $sql, + $id_evento + ); + break; + } + + if ($count === true) { + $r = db_get_all_rows_sql($related_sql); + + return $r[0]['n']; + } + + return db_get_all_rows_sql($related_sql); + +} + + +/** + * Validates all events matching target filter. + * + * @param integer $id_evento Master event. + * @param integer $status Target status. + * @param array $filter Optional. Filter options. + * @param boolean $history Apply on historical table. + * + * @return integer Events validated or false if error. + */ +function events_update_status($id_evento, $status, $filter=null, $history=false) +{ + global $config; + + if (!$status) { + error_log('No hay estado'); + return false; + } + + if (!isset($id_evento) || $id_evento <= 0) { + error_log('No hay id_evento'); + return false; + } + + if (!isset($filter) || !is_array($filter)) { + $filter = ['group_rep' => 0]; + } + + $table = events_get_events_table(is_metaconsole(), $history); + + switch ($filter['group_rep']) { + case '0': + case '2': + default: + // No groups option direct update. + $update_sql = sprintf( + 'UPDATE %s + SET estado = %d + WHERE id_evento = %d', + $table, + $status, + $id_evento + ); + break; + + case '1': + // Group by events. + $sql = events_get_all( + ['te.*'], + $filter, + // Offset. + null, + // Limit. + null, + // Order. + null, + // Sort_field. + null, + // Historical table. + $history, + // Return_sql. + true + ); + + $target_ids = db_get_all_rows_sql( + sprintf( + 'SELECT tu.id_evento FROM %s tu INNER JOIN ( %s ) tf + ON tu.estado = tf.estado + AND tu.evento = tf.evento + AND tu.id_agente = tf.id_agente + AND tu.id_agentmodule = tf.id_agentmodule + AND tf.max_id_evento = %d', + $table, + $sql, + $id_evento + ) + ); + + // Try to avoid deadlock while updating full set. + if ($target_ids !== false && count($target_ids) > 0) { + $target_ids = array_reduce( + $target_ids, + function ($carry, $item) { + $carry[] = $item['id_evento']; + return $carry; + } + ); + + $update_sql = sprintf( + 'UPDATE %s + SET estado = %d, + ack_utimestamp = %d, + id_usuario = "%s" + WHERE id_evento IN (%s)', + $table, + $status, + time(), + $config['id_user'], + join(',', $target_ids) + ); + } + break; + } + + return db_process_sql($update_sql); +} + + +/** + * Retrieve all events filtered. + * + * @param array $fields Fields to retrieve. + * @param array $filter Filters to be applied. + * @param integer $offset Offset (pagination). + * @param integer $limit Limit (pagination). + * @param string $order Sort order. + * @param string $sort_field Sort field. + * @param boolean $history Apply on historical table. + * @param boolean $return_sql Return SQL (true) or execute it (false). + * @param string $having Having filter. + * + * @return array Events. + * @throws Exception On error. + */ +function events_get_all( + $fields, + array $filter, + $offset=null, + $limit=null, + $order=null, + $sort_field=null, + $history=false, + $return_sql=false, + $having='' +) { + global $config; + + $user_is_admin = users_is_admin(); + + if (!is_array($filter)) { + error_log('[events_get_all] Filter must be an array.'); + throw new Exception('[events_get_all] Filter must be an array.'); + } + + $count = false; + if (!is_array($fields) && $fields == 'count') { + $fields = ['te.*']; + $count = true; + } else if (!is_array($fields)) { + error_log('[events_get_all] Fields must be an array or "count".'); + throw new Exception('[events_get_all] Fields must be an array or "count".'); + } + + if (isset($filter['date_from']) + && !empty($filter['date_from']) + && $filter['date_from'] != '0000-00-00' + ) { + $date_from = $filter['date_from']; + } + + if (isset($filter['time_from'])) { + $time_from = $filter['time_from']; + } + + if (isset($date_from)) { + if (!isset($time_from)) { + $time_from = '00:00:00'; + } + + $from = $date_from.' '.$time_from; + $sql_filters[] = sprintf( + ' AND te.utimestamp >= %d', + strtotime($from) + ); + } + + if (isset($filter['date_to']) + && !empty($filter['date_to']) + && $filter['date_to'] != '0000-00-00' + ) { + $date_to = $filter['date_to']; + } + + if (isset($filter['time_to'])) { + $time_to = $filter['time_to']; + } + + if (isset($date_to)) { + if (!isset($time_to)) { + $time_to = '23:59:59'; + } + + $to = $date_to.' '.$time_to; + $sql_filters[] = sprintf( + ' AND te.utimestamp <= %d', + strtotime($to) + ); + } + + if (!isset($from)) { + if (isset($filter['event_view_hr']) && ($filter['event_view_hr'] > 0)) { + $sql_filters[] = sprintf( + ' AND utimestamp > UNIX_TIMESTAMP(now() - INTERVAL %d HOUR) ', + $filter['event_view_hr'] + ); + } + } + + if (isset($filter['id_agent']) && $filter['id_agent'] > 0) { + $sql_filters[] = sprintf( + ' AND te.id_agente = %d ', + $filter['id_agent'] + ); + } + + if (!empty($filter['event_type']) && $filter['event_type'] != 'all') { + if ($filter['event_type'] == 'warning' + || $filter['event_type'] == 'critical' + || $filter['event_type'] == 'normal' + ) { + $sql_filters[] = ' AND event_type LIKE "%'.$filter['event_type'].'%"'; + } else if ($filter['event_type'] == 'not_normal') { + $sql_filters[] = ' AND (event_type LIKE "%warning%" + OR event_type LIKE "%critical%" + OR event_type LIKE "%unknown%")'; + } else { + $sql_filters[] = ' AND event_type = "'.$filter['event_type'].'"'; + } + } + + if (isset($filter['severity']) && $filter['severity'] > 0) { + switch ($filter['severity']) { + case EVENT_CRIT_MAINTENANCE: + case EVENT_CRIT_INFORMATIONAL: + case EVENT_CRIT_NORMAL: + case EVENT_CRIT_MINOR: + case EVENT_CRIT_WARNING: + case EVENT_CRIT_MAJOR: + case EVENT_CRIT_CRITICAL: + default: + $sql_filters[] = sprintf( + ' AND criticity = %d ', + $filter['severity'] + ); + break; + + case EVENT_CRIT_WARNING_OR_CRITICAL: + $sql_filters[] = sprintf( + ' AND (criticity = %d OR criticity = %d)', + EVENT_CRIT_WARNING, + EVENT_CRIT_CRITICAL + ); + break; + + case EVENT_CRIT_NOT_NORMAL: + $sql_filters[] = sprintf( + ' AND criticity != %d', + EVENT_CRIT_NORMAL + ); + break; + + case EVENT_CRIT_OR_NORMAL: + $sql_filters[] = sprintf( + ' AND (criticity = %d OR criticity = %d)', + EVENT_CRIT_NORMAL, + EVENT_CRIT_CRITICAL + ); + break; + } + } + + $groups = $filter['id_group_filter']; + if (isset($groups) && $groups > 0) { + $propagate = db_get_value( + 'propagate', + 'tgrupo', + 'id_grupo', + $groups + ); + + if (!$propagate) { + $sql_filters[] = sprintf( + ' AND (te.id_grupo = %d OR tasg.id_group = %d)', + $groups + ); + } else { + $children = groups_get_children($groups); + $_groups = [ $groups ]; + if (!empty($children)) { + foreach ($children as $child) { + $_groups[] = (int) $child['id_grupo']; + } + } + + $groups = $_groups; + + $sql_filters[] = sprintf( + ' AND (te.id_grupo IN (%s) OR tasg.id_group IN (%s))', + join(',', $groups), + join(',', $groups) + ); + } + } + + // Skip system messages if user is not PM. + if (!check_acl($config['id_user'], 0, 'PM')) { + $sql_filters[] = ' AND te.id_grupo != 0 '; + } + + if (isset($filter['status'])) { + switch ($filter['status']) { + case EVENT_ALL: + default: + // Do not filter. + break; + + case EVENT_NEW: + case EVENT_VALIDATE: + case EVENT_PROCESS: + $sql_filters[] = sprintf( + ' AND estado = %d', + $filter['status'] + ); + break; + + case EVENT_NO_VALIDATED: + $sql_filters[] = sprintf( + ' AND (estado = %d OR estado = %d)', + EVENT_NEW, + EVENT_PROCESS + ); + break; + } + } + + if (!$user_is_admin) { + $ER_groups = users_get_groups($config['id_user'], 'ER', false); + $EM_groups = users_get_groups($config['id_user'], 'EM', false, true); + $EW_groups = users_get_groups($config['id_user'], 'EW', false, true); + } + + if (!$user_is_admin && !users_can_manage_group_all('ER')) { + // Get groups where user have ER grants. + $sql_filters[] = sprintf( + ' AND (te.id_grupo IN ( %s ) OR tasg.id_group IN (%s))', + join(', ', array_keys($ER_groups)), + join(', ', array_keys($ER_groups)) + ); + } + + $table = events_get_events_table(is_metaconsole(), $history); + $tevento = sprintf( + ' %s te', + $table + ); + + // Prepare agent join sql filters. + $agent_join_filters = []; + $tagente_table = 'tagente'; + $tagente_field = 'id_agente'; + $conditionMetaconsole = ''; + if (is_metaconsole()) { + $tagente_table = 'tmetaconsole_agent'; + $tagente_field = 'id_tagente'; + $conditionMetaconsole = ' AND ta.id_tmetaconsole_setup = te.server_id '; + } + + // Agent alias. + if (!empty($filter['agent_alias'])) { + $agent_join_filters[] = sprintf( + ' AND ta.alias = "%s" ', + $filter['agent_alias'] + ); + } + + // Free search. + if (!empty($filter['search'])) { + if (isset($config['dbconnection']->server_version) + && $config['dbconnection']->server_version > 50600 + ) { + // Use "from_base64" requires mysql 5.6 or greater. + $custom_data_search = 'from_base64(te.custom_data)'; + } else { + // Custom data is JSON encoded base64, if 5.6 or lower, + // user is condemned to use plain search. + $custom_data_search = 'te.custom_data'; + } + + $sql_filters[] = vsprintf( + ' AND (lower(ta.alias) like lower("%%%s%%") + OR te.id_evento like "%%%s%%" + OR lower(te.evento) like lower("%%%s%%") + OR lower(te.user_comment) like lower("%%%s%%") + OR lower(te.id_extra) like lower("%%%s%%") + OR lower(te.source) like lower("%%%s%%") + OR lower('.$custom_data_search.') like lower("%%%s%%") )', + array_fill(0, 7, $filter['search']) + ); + } + + // Id extra. + if (!empty($filter['id_extra'])) { + $sql_filters[] = sprintf( + ' AND lower(te.id_extra) like lower("%%%s%%") ', + $filter['id_extra'] + ); + } + + // User comment. + if (!empty($filter['user_comment'])) { + $sql_filters[] = sprintf( + ' AND lower(te.user_comment) like lower("%%%s%%") ', + $filter['user_comment'] + ); + } + + // Source. + if (!empty($filter['source'])) { + $sql_filters[] = sprintf( + ' AND lower(te.source) like lower("%%%s%%") ', + $filter['source'] + ); + } + + // Validated or in process by. + if (!empty($filter['id_user_ack'])) { + $sql_filters[] = sprintf( + ' AND te.id_usuario like lower("%%%s%%") ', + $filter['id_user_ack'] + ); + } + + $tag_names = []; + // With following tags. + if (!empty($filter['tag_with'])) { + $tag_with = base64_decode($filter['tag_with']); + $tags = json_decode($tag_with, true); + if (is_array($tags) && !in_array('0', $tags)) { + if (!$user_is_admin) { + $user_tags = array_flip(tags_get_tags_for_module_search()); + if ($user_tags != null) { + foreach ($tags as $id_tag) { + // User cannot filter with those tags. + if (!array_search($id_tag, $user_tags)) { + return false; + } + } + } + } + + foreach ($tags as $id_tag) { + if (!isset($tags_names[$id_tag])) { + $tags_names[$id_tag] = tags_get_name($id_tag); + } + + $_tmp .= ' AND ( '; + $_tmp .= sprintf( + ' tags LIKE "%s" OR', + $tags_names[$id_tag] + ); + + $_tmp .= sprintf( + ' tags LIKE "%s,%%" OR', + $tags_names[$id_tag] + ); + + $_tmp .= sprintf( + ' tags LIKE "%%,%s" OR', + $tags_names[$id_tag] + ); + + $_tmp .= sprintf( + ' tags LIKE "%%,%s,%%" ', + $tags_names[$id_tag] + ); + + $_tmp .= ') '; + } + + $sql_filters[] = $_tmp; + } + } + + // Without following tags. + if (!empty($filter['tag_without'])) { + $tag_without = base64_decode($filter['tag_without']); + $tags = json_decode($tag_without, true); + if (is_array($tags) && !in_array('0', $tags)) { + foreach ($tags as $id_tag) { + if (!isset($tags_names[$id_tag])) { + $tags_names[$id_tag] = tags_get_name($id_tag); + } + + $_tmp .= sprintf( + ' AND tags NOT LIKE "%s" ', + $tags_names[$id_tag] + ); + $_tmp .= sprintf( + ' AND tags NOT LIKE "%s,%%" ', + $tags_names[$id_tag] + ); + $_tmp .= sprintf( + ' AND tags NOT LIKE "%%,%s" ', + $tags_names[$id_tag] + ); + $_tmp .= sprintf( + ' AND tags NOT LIKE "%%,%s,%%" ', + $tags_names[$id_tag] + ); + } + + $sql_filters[] = $_tmp; + } + } + + // Filter/ Only alerts. + if (isset($filter['filter_only_alert'])) { + if ($filter['filter_only_alert'] == 0) { + $sql_filters[] = ' AND event_type NOT LIKE "%alert%"'; + } else if ($filter['filter_only_alert'] == 1) { + $sql_filters[] = ' AND event_type LIKE "%alert%"'; + } + } + + // TAgs ACLS. + if (check_acl($config['id_user'], 0, 'ER')) { + $tags_acls_condition = tags_get_acl_tags( + // Id_user. + $config['id_user'], + // Id_group. + $ER_groups, + // Access. + 'ER', + // Return_mode. + 'event_condition', + // Query_prefix. + 'AND', + // Query_table. + '', + // Meta. + is_metaconsole(), + // Childrens_ids. + [], + // Force_group_and_tag. + true, + // Table tag for id_grupo. + 'te.', + // Alt table tag for id_grupo. + 'tasg.' + ); + // FORCE CHECK SQL "(TAG = tag1 AND id_grupo = 1)". + } else if (check_acl($config['id_user'], 0, 'EW')) { + $tags_acls_condition = tags_get_acl_tags( + // Id_user. + $config['id_user'], + // Id_group. + $EW_groups, + // Access. + 'EW', + // Return_mode. + 'event_condition', + // Query_prefix. + 'AND', + // Query_table. + '', + // Meta. + is_metaconsole(), + // Childrens_ids. + [], + // Force_group_and_tag. + true, + // Table tag for id_grupo. + 'te.', + // Alt table tag for id_grupo. + 'tasg.' + ); + // FORCE CHECK SQL "(TAG = tag1 AND id_grupo = 1)". + } else if (check_acl($config['id_user'], 0, 'EM')) { + $tags_acls_condition = tags_get_acl_tags( + // Id_user. + $config['id_user'], + // Id_group. + $EM_groups, + // Access. + 'EM', + // Return_mode. + 'event_condition', + // Query_prefix. + 'AND', + // Query_table. + '', + // Meta. + is_metaconsole(), + // Childrens_ids. + [], + // Force_group_and_tag. + true, + // Table tag for id_grupo. + 'te.', + // Alt table tag for id_grupo. + 'tasg.' + ); + // FORCE CHECK SQL "(TAG = tag1 AND id_grupo = 1)". + } + + if (($tags_acls_condition != ERR_WRONG_PARAMETERS) + && ($tags_acls_condition != ERR_ACL) + ) { + $sql_filters[] = $tags_acls_condition; + } + + // Module search. + $agentmodule_join = 'LEFT JOIN tagente_modulo am ON te.id_agentmodule = am.id_agente_modulo'; + if (is_metaconsole()) { + $agentmodule_join = ''; + } else if (!empty($filter['module_search'])) { + $agentmodule_join = 'INNER JOIN tagente_modulo am ON te.id_agentmodule = am.id_agente_modulo'; + $sql_filters[] = sprintf( + ' AND am.nombre = "%s" ', + $filter['module_search'] + ); + } + + // Order. + $order_by = ''; + if (isset($order, $sort_field)) { + $order_by = events_get_sql_order($sort_field, $order); + } + + // Pagination. + $pagination = ''; + if (isset($limit, $offset) && $limit > 0) { + $pagination = sprintf(' LIMIT %d OFFSET %d', $limit, $offset); + } + + $extra = ''; + if (is_metaconsole()) { + $extra = ', server_id'; + } + + // Group by. + $group_by = 'GROUP BY '; + $tagente_join = 'LEFT'; + switch ($filter['group_rep']) { + case '0': + default: + // All events. + $group_by = ''; + break; + + case '1': + // Group by events. + $group_by .= 'te.estado, te.evento, te.id_agente, te.id_agentmodule'; + $group_by .= $extra; + break; + + case '2': + // Group by agents. + $tagente_join = 'INNER'; + // $group_by .= 'te.id_agente, te.event_type'; + // $group_by .= $extra; + $group_by = ''; + $order_by = events_get_sql_order('id_agente', 'asc'); + if (isset($order, $sort_field)) { + $order_by .= ','.events_get_sql_order( + $sort_field, + $order, + 0, + true + ); + } + break; + } + + $tgrupo_join = 'LEFT'; + $tgrupo_join_filters = []; + if (isset($groups) + && (is_array($groups) + || $groups > 0) + ) { + $tgrupo_join = 'INNER'; + if (is_array($groups)) { + $tgrupo_join_filters[] = sprintf( + ' AND (tg.id_grupo IN (%s) OR tasg.id_group IN (%s))', + join(', ', $groups), + join(', ', $groups) + ); + } else { + $tgrupo_join_filters[] = sprintf( + ' AND (tg.id_grupo = %s OR tasg.id_group = %s)', + $groups, + $groups + ); + } + } + + $server_join = ''; + if (is_metaconsole()) { + $server_join = ' LEFT JOIN tmetaconsole_setup ts + ON ts.id = te.server_id'; + if (!empty($filter['server_id'])) { + $server_join = sprintf( + ' INNER JOIN tmetaconsole_setup ts + ON ts.id = te.server_id AND ts.id= %d', + $filter['server_id'] + ); + } + } + + // Secondary groups. + db_process_sql('SET group_concat_max_len = 9999999'); + $event_lj = events_get_secondary_groups_left_join($table); + + $group_selects = ''; + if ($group_by != '') { + $group_selects = ',COUNT(id_evento) AS event_rep + ,GROUP_CONCAT(DISTINCT user_comment SEPARATOR "
") AS comments, + MAX(utimestamp) as timestamp_last, + MIN(utimestamp) as timestamp_first, + MAX(id_evento) as max_id_evento'; + + if ($count === false) { + $idx = array_search('te.user_comment', $fields); + if ($idx !== false) { + unset($fields[$idx]); + } + } + } + + $sql = sprintf( + 'SELECT %s + %s + FROM %s + %s + %s + %s JOIN %s ta + ON ta.%s = te.id_agente + %s + %s + %s JOIN tgrupo tg + ON te.id_grupo = tg.id_grupo + %s + %s + WHERE 1=1 + %s + %s + %s + %s + %s + ', + join(',', $fields), + $group_selects, + $tevento, + $event_lj, + $agentmodule_join, + $tagente_join, + $tagente_table, + $tagente_field, + $conditionMetaconsole, + join(' ', $agent_join_filters), + $tgrupo_join, + join(' ', $tgrupo_join_filters), + $server_join, + join(' ', $sql_filters), + $group_by, + $order_by, + $pagination, + $having + ); + + if (!$user_is_admin) { + // XXX: Confirm there's no extra grants unhandled!. + $can_manage = '0 as user_can_manage'; + if (!empty($EM_groups)) { + $can_manage = sprintf( + '(tbase.id_grupo IN (%s)) as user_can_manage', + join(', ', array_keys($EM_groups)) + ); + } + + $can_write = '0 as user_can_write'; + if (!empty($EW_groups)) { + $can_write = sprintf( + '(tbase.id_grupo IN (%s)) as user_can_write', + join(', ', array_keys($EW_groups)) + ); + } + + $sql = sprintf( + 'SELECT + tbase.*, + %s, + %s + FROM + (', + $can_manage, + $can_write + ).$sql.') tbase'; + } else { + $sql = 'SELECT + tbase.*, + 1 as user_can_manage, + 1 as user_can_write + FROM + ('.$sql.') tbase'; + } + + if ($count) { + $sql = 'SELECT count(*) as nitems FROM ('.$sql.') tt'; + } + + if ($return_sql) { + return $sql; + } + + return db_get_all_rows_sql($sql); +} + + +/** + * Get all rows of events from the database, that + * pass the filter, and can get only some fields. + * + * @param mixed $filter Filters elements. It can be an indexed array + * (keys would be the field name and value the expected + * value, and would be joined with an AND operator) or a + * string, including any SQL clause (without the WHERE + * keyword). Example: + * + * Both are similars: + * db_get_all_rows_filter ('table', ['disabled', 0]); + * db_get_all_rows_filter ('table', 'disabled = 0'); + * Both are similars: + * db_get_all_rows_filter ( + * 'table', + * [ + * 'disabled' => 0, + * 'history_data' => 0 + * ], + * 'name', + * 'OR' + * ); + * db_get_all_rows_filter ( + * 'table', + * 'disabled = 0 OR history_data = 0', 'name' + * ); + * . + * @param mixed $fields Fields of the table to retrieve. Can be an array or a + * coma separated string. All fields are retrieved by + * default. + * + * @return mixed False in case of error or invalid values passed. + * Affected rows otherwise + */ +function events_get_events($filter=false, $fields=false) +{ + if ($filter['criticity'] == EVENT_CRIT_WARNING_OR_CRITICAL) { + $filter['criticity'] = [ + EVENT_CRIT_WARNING, + EVENT_CRIT_CRITICAL, + ]; + } + + return db_get_all_rows_filter('tevento', $filter, $fields); +} + + +/** + * Get the event with the id pass as parameter. + * + * @param integer $id Event id. + * @param mixed $fields The fields to show or by default all with false. + * @param boolean $meta Metaconsole environment or not. + * @param boolean $history Retrieve also historical data. + * + * @return mixed False in case of error or invalid values passed. + * Event row otherwise. + */ +function events_get_event($id, $fields=false, $meta=false, $history=false) +{ + if (empty($id)) { + return false; + } + + global $config; + + if (is_array($fields)) { + if (! in_array('id_grupo', $fields)) { + $fields[] = 'id_grupo'; + } + } + + $table = events_get_events_table($meta, $history); + + $event = db_get_row($table, 'id_evento', $id, $fields); + if (! check_acl($config['id_user'], $event['id_grupo'], 'ER')) { + return false; + } + + return $event; +} + + +/** + * Retrieve all events ungrouped. + * + * @param string $sql_post Sql_post. + * @param integer $offset Offset. + * @param integer $pagination Pagination. + * @param boolean $meta Meta. + * @param boolean $history History. + * @param boolean $total Total. + * @param boolean $history_db History_db. + * @param string $order Order. + * + * @return mixed Array of events or false. + */ +function events_get_events_no_grouped( + $sql_post, + $offset=0, + $pagination=1, + $meta=false, + $history=false, + $total=false, + $history_db=false, + $order='ASC' +) { + global $config; + + $table = events_get_events_table($meta, $history); + + $sql = 'SELECT * FROM '.$table.' te WHERE 1=1 '.$sql_post; + + $events = db_get_all_rows_sql($sql, $history_db); + + return $events; +} + + +/** + * Return all events matching sql_post grouped. + * + * @param string $sql_post Sql_post. + * @param integer $offset Offset. + * @param integer $pagination Pagination. + * @param boolean $meta Meta. + * @param boolean $history History. + * @param boolean $total Total. + * @param boolean $history_db History_db. + * @param string $order Order. + * @param string $sort_field Sort_field. + * + * @return mixed Array of events or false. + */ +function events_get_events_grouped( + $sql_post, + $offset=0, + $pagination=1, + $meta=false, + $history=false, + $total=false, + $history_db=false, + $order='down', + $sort_field='utimestamp' +) { + global $config; + + $table = events_get_events_table($meta, $history); + + if ($meta) { + $groupby_extra = ', server_id'; + } else { + $groupby_extra = ''; + } + + db_process_sql('SET group_concat_max_len = 9999999'); + $event_lj = events_get_secondary_groups_left_join($table); + if ($total) { + $sql = "SELECT COUNT(*) FROM (SELECT id_evento + FROM $table te $event_lj + WHERE 1=1 ".$sql_post.' + GROUP BY estado, evento, id_agente, id_agentmodule'.$groupby_extra.') AS t'; + } else { + $sql = "SELECT *, MAX(id_evento) AS id_evento, + GROUP_CONCAT(DISTINCT user_comment SEPARATOR '
') AS user_comment, + GROUP_CONCAT(DISTINCT id_evento SEPARATOR ',') AS similar_ids, + COUNT(id_evento) AS event_rep, MAX(utimestamp) AS timestamp_rep, + MIN(utimestamp) AS timestamp_rep_min, + (SELECT owner_user FROM $table WHERE id_evento = MAX(te.id_evento)) owner_user, + (SELECT id_usuario FROM $table WHERE id_evento = MAX(te.id_evento)) id_usuario, + (SELECT id_agente FROM $table WHERE id_evento = MAX(te.id_evento)) id_agente, + (SELECT criticity FROM $table WHERE id_evento = MAX(te.id_evento)) AS criticity, + (SELECT ack_utimestamp FROM $table WHERE id_evento = MAX(te.id_evento)) AS ack_utimestamp, + (SELECT nombre FROM tagente_modulo WHERE id_agente_modulo = te.id_agentmodule) AS module_name + FROM $table te $event_lj + WHERE 1=1 ".$sql_post.' + GROUP BY estado, evento, id_agente, id_agentmodule'.$groupby_extra; + $sql .= ' '.events_get_sql_order($sort_field, $order, 2); + $sql .= ' LIMIT '.$offset.','.$pagination; + } + + // Extract the events by filter (or not) from db + $events = db_get_all_rows_sql($sql, $history_db); + + if ($total) { + return reset($events[0]); + } else { + return $events; + } +} + + +/** + * Return count of events grouped. + * + * @param string $sql_post Sql_post. + * @param boolean $meta Meta. + * @param boolean $history History. + * + * @return integer Number of events or false if failed. + */ +function events_get_total_events_grouped($sql_post, $meta=false, $history=false) +{ + return events_get_events_grouped($sql_post, 0, 0, $meta, $history, true); +} + + +/** + * Get all the events ids similar to a given event id. + * + * An event is similar then the event text (evento) and the id_agentmodule are + * the same. + * + * @param integer $id Event id to get similar events. + * @param boolean $meta Metaconsole mode flag. + * @param boolean $history History mode flag. + * + * @return array A list of events ids. + */ +function events_get_similar_ids($id, $meta=false, $history=false) +{ + $events_table = events_get_events_table($meta, $history); + + $ids = []; + if ($meta) { + $event = events_meta_get_event( + $id, + [ + 'evento', + 'id_agentmodule', + ], + $history + ); + } else { + $event = events_get_event($id, ['evento', 'id_agentmodule']); + } + + if ($event === false) { + return $ids; + } + + $events = db_get_all_rows_filter( + $events_table, + [ + 'evento' => $event['evento'], + 'id_agentmodule' => $event['id_agentmodule'], + ], + ['id_evento'] + ); + if ($events === false) { + return $ids; + } + + foreach ($events as $event) { + $ids[] = $event['id_evento']; + } + + return $ids; +} + + +/** + * Delete events in a transresponse + * + * @param mixed $id_event Event ID or array of events. + * @param boolean $similar Whether to delete similar events too. + * @param boolean $meta Metaconsole mode flag. + * @param boolean $history History mode flag. + * + * @return boolean Whether or not it was successful + */ +function events_delete_event( + $id_event, + $similar=true, + $meta=false, + $history=false +) { + global $config; + + $table_event = events_get_events_table($meta, $history); + + // Cleans up the selection for all unwanted values also casts any single values as an array. + $id_event = (array) safe_int($id_event, 1); + + // We must delete all events like the selected. + if ($similar) { + foreach ($id_event as $id) { + $id_event = array_merge( + $id_event, + events_get_similar_ids($id, $meta, $history) + ); + } + + $id_event = array_unique($id_event); + } + + $errors = 0; + + foreach ($id_event as $event) { + if ($meta) { + $event_group = events_meta_get_group($event, $history); + } else { + $event_group = events_get_group($event); + } + + if (check_acl($config['id_user'], $event_group, 'EM') == 0) { + // Check ACL. + db_pandora_audit('ACL Violation', 'Attempted deleting event #'.$event); + $errors++; + } else { + $ret = db_process_sql_delete($table_event, ['id_evento' => $event]); + + if (!$ret) { + $errors++; + } else { + db_pandora_audit('Event deleted', 'Deleted event #'.$event); + // ACL didn't fail nor did return. + continue; + } + } + + break; + } + + if ($errors > 0) { + return false; + } else { + return true; + } +} + + +/** + * Change the status of one or multiple events. + * + * @param mixed $id_event Event ID or array of events. + * @param integer $new_status New status of the event. + * @param boolean $meta Metaconsole mode flag. + * @param boolean $history History mode flag. + * + * @return boolean Whether or not it was successful + */ +function events_change_status( + $id_event, + $new_status, + $meta=false, + $history=false +) { + global $config; + + $event_table = events_get_events_table($meta, $history); + + // Cleans up the selection for all unwanted values also casts any single values as an array. + $id_event = (array) safe_int($id_event, 1); + + // Update ack info if the new status is validated. + if ($new_status == EVENT_STATUS_VALIDATED) { + $ack_utimestamp = time(); + $ack_user = $config['id_user']; + } else { + $acl_utimestamp = 0; + $ack_user = $config['id_user']; + } + + switch ($new_status) { + case EVENT_STATUS_NEW: + $status_string = 'New'; + break; + + case EVENT_STATUS_VALIDATED: + $status_string = 'Validated'; + break; + + case EVENT_STATUS_INPROCESS: + $status_string = 'In process'; + break; + + default: + $status_string = ''; + break; + } + + $alerts = []; + + foreach ($id_event as $k => $id) { + if ($meta) { + $event_group = events_meta_get_group($id, $history); + $event = events_meta_get_event($id, false, $history); + $server_id = $event['server_id']; + } else { + $event_group = events_get_group($id); + $event = events_get_event($id); + } + + if ($event['id_alert_am'] > 0 && !in_array($event['id_alert_am'], $alerts)) { + $alerts[] = $event['id_alert_am']; + } + + if (check_acl($config['id_user'], $event_group, 'EW') == 0) { + db_pandora_audit('ACL Violation', 'Attempted updating event #'.$id); + + unset($id_event[$k]); + } + } + + if (empty($id_event)) { + return false; + } + + $values = [ + 'estado' => $new_status, + 'id_usuario' => $ack_user, + 'ack_utimestamp' => $ack_utimestamp, + ]; + + $ret = db_process_sql_update( + $event_table, + $values, + ['id_evento' => $id_event] + ); + + if (($ret === false) || ($ret === 0)) { + return false; + } + + events_comment( + $id_event, + '', + 'Change status to '.$status_string, + $meta, + $history + ); + + if ($meta && !empty($alerts)) { + $server = metaconsole_get_connection_by_id($server_id); + metaconsole_connect($server); + } + + // Put the alerts in standby or not depends the new status. + foreach ($alerts as $alert) { + switch ($new_status) { + case EVENT_NEW: + case EVENT_VALIDATE: + alerts_agent_module_standby($alert, 0); + break; + + case EVENT_PROCESS: + alerts_agent_module_standby($alert, 1); + break; + + default: + // Ignore. + break; + } + } + + if ($meta && !empty($alerts)) { + metaconsole_restore_db(); + } + + return true; +} + + +/** + * Change the owner of an event if the event hasn't owner. + * + * @param mixed $id_event Event ID or array of events. + * @param string $new_owner Id_user of the new owner. If is false, the current + * owner will be setted. + * @param boolean $force Flag to force the change or not (not force is + * change only when it hasn't owner). + * @param boolean $meta Metaconsole mode flag. + * @param boolean $history History mode flag. + * + * @return boolean Whether or not it was successful. + */ +function events_change_owner( + $id_event, + $new_owner=false, + $force=false, + $meta=false, + $history=false +) { + global $config; + + $event_table = events_get_events_table($meta, $history); + + // Cleans up the selection for all unwanted values also casts any single + // values as an array. + $id_event = (array) safe_int($id_event, 1); + + foreach ($id_event as $k => $id) { + if ($meta) { + $event_group = events_meta_get_group($id, $history); + } else { + $event_group = events_get_group($id); + } + + if (check_acl($config['id_user'], $event_group, 'EW') == 0) { + db_pandora_audit('ACL Violation', 'Attempted updating event #'.$id); + unset($id_event[$k]); + } + } + + if (empty($id_event)) { + return false; + } + + // If no new_owner is provided, the current user will be the owner + // * #2250: Comment this lines because if possible selected None owner. + // if (empty($new_owner)) { + // $new_owner = $config['id_user']; + // } + // Only generate comment when is forced (sometimes is owner changes when + // comment). + if ($force) { + events_comment( + $id_event, + '', + 'Change owner to '.$new_owner, + $meta, + $history + ); + } + + $values = ['owner_user' => $new_owner]; + + $where = ['id_evento' => $id_event]; + + // If not force, add to where if owner_user = ''. + if (!$force) { + $where['owner_user'] = ''; + } + + $ret = db_process_sql_update( + $event_table, + $values, + $where, + 'AND', + false + ); + + if (($ret === false) || ($ret === 0)) { + return false; + } + + return true; +} + + +/** + * Returns proper event table based on environment. + * + * @param boolean $meta Metaconsole environment or not. + * @param boolean $history Historical data or not. + * + * @return string Table name. + */ +function events_get_events_table($meta, $history) +{ + if ($meta) { + if ($history) { + $event_table = 'tmetaconsole_event_history'; + } else { + $event_table = 'tmetaconsole_event'; + } + } else { + $event_table = 'tevento'; + } + + return $event_table; +} + + +/** + * Comment events in a transresponse + * + * @param mixed $id_event Event ID or array of events. + * @param string $comment Comment to be registered. + * @param string $action Action performed with comment. By default just add + * a comment. + * @param boolean $meta Flag of metaconsole mode. + * @param boolean $history Flag of history mode. + * @param boolean $similars Similars. + * + * @return boolean Whether or not it was successful + */ +function events_comment( + $id_event, + $comment='', + $action='Added comment', + $meta=false, + $history=false, + $similars=true +) { + global $config; + + $event_table = events_get_events_table($meta, $history); + + // Cleans up the selection for all unwanted values also casts any single + // values as an array. + $id_event = (array) safe_int($id_event, 1); + + foreach ($id_event as $k => $id) { + if ($meta) { + $event_group = events_meta_get_group($id, $history); + } else { + $event_group = events_get_group($id); + } + + if (check_acl($config['id_user'], $event_group, 'EW') == 0) { + db_pandora_audit('ACL Violation', 'Attempted updating event #'.$id); + + unset($id_event[$k]); + } + } + + if (empty($id_event)) { + return false; + } + + // If the event hasn't owner, assign the user as owner. + events_change_owner($id_event); + + // Get the current event comments. + $first_event = $id_event; + if (is_array($id_event)) { + $first_event = reset($id_event); + } + + $event_comments = mysql_db_process_sql( + 'SELECT user_comment FROM '.$event_table.' WHERE id_evento = '.$first_event, + 'affected_rows', + '', + false + ); + + $event_comments_array = []; + + if ($event_comments[0]['user_comment'] == '') { + $comments_format = 'new'; + } else { + // If comments are not stored in json, the format is old. + $event_comments_array = json_decode($event_comments[0]['user_comment']); + + if (empty($event_comments_array)) { + $comments_format = 'old'; + } else { + $comments_format = 'new'; + } + } + + switch ($comments_format) { + case 'new': + $comment_for_json['comment'] = $comment; + $comment_for_json['action'] = $action; + $comment_for_json['id_user'] = $config['id_user']; + $comment_for_json['utimestamp'] = time(); + + $event_comments_array[] = $comment_for_json; + + $event_comments = io_json_mb_encode($event_comments_array); + + // Update comment. + $ret = db_process_sql_update( + $event_table, + ['user_comment' => $event_comments], + ['id_evento' => implode(',', $id_event)] + ); + break; + + case 'old': + // Give old ugly format to comment. TODO: Change this method for + // aux table or json. + $comment = str_replace(["\r\n", "\r", "\n"], '
', $comment); + + if ($comment != '') { + $commentbox = '
'.$comment.'
'; + } else { + $commentbox = ''; + } + + // Don't translate 'by' word because if multiple users with + // different languages make comments in the same console + // will be a mess. + $comment = '-- '.$action.' by '.$config['id_user'].' ['.date($config['date_format']).'] --
'.$commentbox.'
'; + + // Update comment. + $sql_validation = sprintf( + 'UPDATE %s + SET user_comment = concat("%s", user_comment) + WHERE id_evento in (%s)', + $event_table, + $comment, + implode(',', $id_event) + ); + + $ret = db_process_sql($sql_validation); + break; + + default: + // Ignore. + break; + } + + if (($ret === false) || ($ret === 0)) { + return false; + } + + return true; +} + + +/** + * Get group id of an event. + * + * @param integer $id_event Event id. + * + * @return integer Group id of the given event. + */ +function events_get_group($id_event) +{ + return (int) db_get_value( + 'id_grupo', + 'tevento', + 'id_evento', + (int) $id_event + ); +} + + +/** + * Get description of an event. + * + * @param integer $id_event Event id. + * + * @return string Description of the given event. + */ +function events_get_description($id_event) +{ + return (string) db_get_value( + 'evento', + 'tevento', + 'id_evento', + (int) $id_event + ); +} + + +/** + * Insert a event in the event log system. + * + * @param integer $event Event. + * @param integer $id_group Id_group. + * @param integer $id_agent Id_agent. + * @param integer $status Status. + * @param string $id_user Id_user. + * @param string $event_type Event_type. + * @param integer $priority Priority. + * @param integer $id_agent_module Id_agent_module. + * @param integer $id_aam Id_aam. + * @param string $critical_instructions Critical_instructions. + * @param string $warning_instructions Warning_instructions. + * @param string $unknown_instructions Unknown_instructions. + * @param boolean $source Source. + * @param string $tags Tags. + * @param string $custom_data Custom_data. + * @param integer $server_id Server_id. + * @param string $id_extra Id_extra. + * + * @return integer Event id. + */ +function events_create_event( + $event, + $id_group, + $id_agent, + $status=0, + $id_user='', + $event_type='unknown', + $priority=0, + $id_agent_module=0, + $id_aam=0, + $critical_instructions='', + $warning_instructions='', + $unknown_instructions='', + $source=false, + $tags='', + $custom_data='', + $server_id=0, + $id_extra='' +) { + global $config; + + if ($source === false) { + $source = get_product_name(); + } + + $table_events = 'tevento'; + if (defined('METACONSOLE')) { + $table_events = 'tmetaconsole_event'; + + $sql = sprintf( + 'INSERT INTO '.$table_events.' (id_agente, id_grupo, evento, + timestamp, estado, utimestamp, id_usuario, + event_type, criticity, id_agentmodule, id_alert_am, + critical_instructions, warning_instructions, + unknown_instructions, source, tags, custom_data, + server_id, id_extra, data, module_status) + VALUES (%d, %d, "%s", NOW(), %d, UNIX_TIMESTAMP(NOW()), + "%s", "%s", %d, %d, %d, "%s", "%s", "%s", "%s", + "%s", "%s", %d, "%s", %d, %d)', + $id_agent, + $id_group, + $event, + $status, + $id_user, + $event_type, + $priority, + $id_agent_module, + $id_aam, + $critical_instructions, + $warning_instructions, + $unknown_instructions, + $source, + $tags, + $custom_data, + $server_id, + $id_extra, + $data, + $module_status + ); + } else { + $sql = sprintf( + 'INSERT INTO '.$table_events.' (id_agente, id_grupo, evento, + timestamp, estado, utimestamp, id_usuario, + event_type, criticity, id_agentmodule, id_alert_am, + critical_instructions, warning_instructions, + unknown_instructions, source, tags, custom_data, id_extra, data, module_status) + VALUES (%d, %d, "%s", NOW(), %d, UNIX_TIMESTAMP(NOW()), + "%s", "%s", %d, %d, %d, "%s", "%s", "%s", "%s", "%s", "%s", "%s", %d, %d)', + $id_agent, + $id_group, + $event, + $status, + $id_user, + $event_type, + $priority, + $id_agent_module, + $id_aam, + $critical_instructions, + $warning_instructions, + $unknown_instructions, + $source, + $tags, + $custom_data, + $id_extra, + $data, + $module_status + ); + } + + return (int) db_process_sql($sql, 'insert_id'); +} + + +/** + * Prints a small event table. + * + * @param string $filter SQL WHERE clause. + * @param integer $limit How many events to show. + * @param integer $width How wide the table should be. + * @param boolean $return Prints out HTML if false. + * @param integer $agent_id Agent id if is the table of one agent. + * 0 otherwise. + * @param boolean $tactical_view Be shown in tactical view or not. + * + * @return string HTML with table element. + */ +function events_print_event_table( + $filter='', + $limit=10, + $width=440, + $return=false, + $agent_id=0, + $tactical_view=false +) { + global $config; + + ui_require_css_file('events'); + + if ($agent_id == 0) { + $agent_condition = ''; + } else { + $agent_condition = ' id_agente = '.$agent_id.' AND '; + } + + if ($filter == '') { + $filter = '1 = 1'; + } + + $secondary_join = 'LEFT JOIN tagent_secondary_group tasg ON tevento.id_agente = tasg.id_agent'; + + $sql = sprintf( + 'SELECT DISTINCT tevento.* + FROM tevento %s + WHERE %s %s + ORDER BY utimestamp DESC LIMIT %d', + $secondary_join, + $agent_condition, + $filter, + $limit + ); + + $result = db_get_all_rows_sql($sql); + + if ($result === false) { + if ($return) { + $returned = ui_print_info_message(__('No events'), '', true); + return $returned; + } else { + echo ui_print_info_message(__('No events')); + } + } else { + $table = new stdClass(); + $table->id = 'latest_events_table'; + $table->cellpadding = 0; + $table->cellspacing = 0; + $table->width = $width; + $table->class = 'info_table no-td-padding'; + if (!$tactical_view) { + $table->title = __('Latest events'); + } + + $table->titleclass = 'tabletitle'; + $table->titlestyle = 'text-transform:uppercase;'; + $table->headclass = []; + $table->head = []; + $table->rowclass = []; + $table->cellclass = []; + $table->data = []; + $table->align = []; + $table->style = []; + + $i = 0; + $table->head[$i] = "".__('S.').''; + $table->headstyle[$i] = 'width: 1%;text-align: center;'; + $table->style[$i++] = 'text-align: center;'; + + $table->head[$i] = __('Type'); + $table->headstyle[$i] = 'width: 3%;text-align: center;'; + $table->style[$i++] = 'text-align: center;'; + + $table->head[$i] = __('Event name'); + $table->headstyle[$i] = ''; + $table->style[$i++] = 'word-break: break-word;'; + + if ($agent_id == 0) { + $table->head[$i] = __('Agent name'); + $table->headstyle[$i] = ''; + $table->style[$i++] = 'word-break: break-all;'; + } + + $table->head[$i] = __('Timestamp'); + $table->headstyle[$i] = 'width: 150px;'; + $table->style[$i++] = 'word-break: break-word;'; + + $table->head[$i] = __('Status'); + $table->headstyle[$i] = 'width: 150px;text-align: center;'; + $table->style[$i++] = 'text-align: center;'; + + $table->head[$i] = "".__('V.').''; + $table->headstyle[$i] = 'width: 1%;text-align: center;'; + $table->style[$i++] = 'text-align: center;'; + + $all_groups = []; + if ($agent_id != 0) { + $all_groups = agents_get_all_groups_agent($agent_id); + } + + foreach ($result as $event) { + // Copy all groups of the agent and append the event group. + $check_events = $all_groups; + $check_events[] = $event['id_grupo']; + if (! check_acl_one_of_groups($config['id_user'], $check_events, 'ER')) { + continue; + } + + $data = []; + + // Colored box. + switch ($event['estado']) { + case 0: + $img = 'images/star.png'; + $title = __('New event'); + break; + + case 1: + $img = 'images/tick.png'; + $title = __('Event validated'); + break; + + case 2: + $img = 'images/hourglass.png'; + $title = __('Event in process'); + break; + + default: + // Ignore. + break; + } + + $i = 0; + // Criticity. + $data[$i++] = ui_print_event_priority($event['criticity'], true, true); + + // Event type. + $data[$i++] = events_print_type_img($event['event_type'], true); + + // Event text. + $data[$i++] = ui_print_string_substr( + strip_tags(io_safe_output($event['evento'])), + 75, + true, + '7.5' + ); + + if ($agent_id == 0) { + if ($event['id_agente'] > 0) { + // Agent name. + // Get class name, for the link color, etc. + $data[$i] = "".agents_get_alias($event['id_agente']).''; + // For System or SNMP generated alerts. + } else if ($event['event_type'] == 'system') { + $data[$i] = __('System'); + } else { + $data[$i] = __('Alert').'SNMP'; + } + + $i++; + } + + // Timestamp. + $data[$i++] = ui_print_timestamp($event['timestamp'], true, ['style' => 'font-size: 7.5pt; letter-spacing: 0.3pt;']); + + // Status. + $data[$i++] = ui_print_event_type($event['event_type'], true); + + $data[$i++] = html_print_image( + $img, + true, + [ + 'class' => 'image_status', + 'title' => $title, + ] + ); + $table->data[] = $data; + } + + $events_table = html_print_table($table, true); + $out = $events_table; + + if (!$tactical_view) { + $out .= '
'; + if ($agent_id != 0) { + $out .= ''; + $out .= '
'; + $out .= '
+ '.__('Events -by module-').''.graph_event_module(180, 100, $event['id_agente']).'
'; + $out .= '
'; + } else { + $out .= '
'; + $out .= '
'; + $out .= '
+ '.__('Event graph').''.grafico_eventos_total('', 180, 60).'
'; + $out .= '
+ '.__('Event graph by agent').''.grafico_eventos_grupo(180, 60).'
'; + $out .= '
'; + } + + $out .= '
'; + } + + unset($table); + + if ($return) { + return $out; + } else { + echo $out; + } + } +} + + +/** + * Prints the event type image. + * + * @param string $type Event type from SQL. + * @param boolean $return Whether to return or print. + * @param boolean $only_url Flag to return only url of image, by default false. + * + * @return string HTML with img. + */ +function events_print_type_img( + $type, + $return=false, + $only_url=false +) { + global $config; + + $output = ''; + + $urlImage = ui_get_full_url(false); + + switch ($type) { + case 'alert_recovered': + $icon = 'bell.png'; + break; + + case 'alert_manual_validation': + $icon = 'ok.png'; + break; + + case 'going_down_critical': + case 'going_up_critical': + // This is to be backwards compatible. + $icon = 'module_critical.png'; + break; + + case 'going_up_normal': + case 'going_down_normal': + // This is to be backwards compatible. + $icon = 'module_ok.png'; + break; + + case 'going_up_warning': + case 'going_down_warning': + $icon = 'module_warning.png'; + break; + + case 'going_unknown': + $icon = 'module_unknown.png'; + break; + + case 'alert_fired': + $icon = 'bell_error.png'; + break; + + case 'system': + $icon = 'cog.png'; + break; + + case 'recon_host_detected': + $icon = 'recon.png'; + break; + + case 'new_agent': + $icon = 'agent.png'; + break; + + case 'configuration_change': + $icon = 'config.png'; + break; + + case 'unknown': + default: + $icon = 'lightning_go.png'; + break; + } + + if ($only_url) { + $output = $urlImage.'/images/'.$icon; + } else { + $output .= html_print_image( + 'images/'.$icon, + true, + ['title' => events_print_type_description($type, true)] + ); + } + + if ($return) { + return $output; + } + + echo $output; +} + + +/** + * Prints the event type description + * + * @param string $type Event type from SQL. + * @param boolean $return Whether to return or print. + * + * @return string HTML with img + */ +function events_print_type_description($type, $return=false) +{ + $output = ''; + + switch ($type) { + case 'going_unknown': + $output .= __('Going to unknown'); + break; + + case 'alert_recovered': + $output .= __('Alert recovered'); + break; + + case 'alert_manual_validation': + $output .= __('Alert manually validated'); + break; + + case 'going_up_warning': + $output .= __('Going from critical to warning'); + break; + + case 'going_down_critical': + case 'going_up_critical': + // This is to be backwards compatible. + $output .= __('Going up to critical state'); + break; + + case 'going_up_normal': + case 'going_down_normal': + // This is to be backwards compatible. + $output .= __('Going up to normal state'); + break; + + case 'going_down_warning': + $output .= __('Going down from normal to warning'); + break; + + case 'alert_fired': + $output .= __('Alert fired'); + break; + + case 'system'; + $output .= __('SYSTEM'); + break; + + case 'recon_host_detected'; + $output .= __('Discovery server detected a new host'); + break; + + case 'new_agent'; + $output .= __('New agent created'); + break; + + case 'configuration_change'; + $output .= __('Configuration change'); + break; + + case 'alert_ceased'; + $output .= __('Alert ceased'); + break; + + case 'error'; + $output .= __('Error'); + break; + + case 'unknown': + default: + $output .= __('Unknown type:').': '.$type; + break; + } + + if ($return) { + return $output; + } + + echo $output; +} + + +/** + * Get all the events happened in a group during a period of time. + * + * The returned events will be in the time interval ($date - $period, $date] + * + * @param mixed $begin Begin. + * @param mixed $result Result. + * @param mixed $id_group Group id to get events for. + * @param integer $period Period in seconds to get events. + * @param integer $date Beginning date to get events. + * @param boolean $filter_event_validated Filter_event_validated. + * @param boolean $filter_event_critical Filter_event_critical. + * @param boolean $filter_event_warning Filter_event_warning. + * @param boolean $filter_event_no_validated Filter_event_no_validated. + * + * @return array An array with all the events happened. + */ +function events_get_group_events_steps( + $begin, + &$result, + $id_group, + $period, + $date, + $filter_event_validated=false, + $filter_event_critical=false, + $filter_event_warning=false, + $filter_event_no_validated=false +) { + global $config; + + $id_group = groups_safe_acl($config['id_user'], $id_group, 'ER'); + + if (empty($id_group)) { + // An empty array means the user doesn't have access. + return false; + } + + $datelimit = ($date - $period); + + $sql_where = ' AND 1 = 1 '; + $criticities = []; + if ($filter_event_critical) { + $criticities[] = 4; + } + + if ($filter_event_warning) { + $criticities[] = 3; + } + + if (!empty($criticities)) { + $sql_where .= ' AND criticity IN ('.implode(', ', $criticities).')'; + } + + if ($filter_event_validated) { + $sql_where .= ' AND estado = 1 '; + } + + if ($filter_event_no_validated) { + $sql_where .= ' AND estado = 0 '; + } + + $sql = sprintf( + 'SELECT *, + (SELECT t2.nombre + FROM tagente t2 + WHERE t2.id_agente = t3.id_agente) AS agent_name, + (SELECT t2.fullname + FROM tusuario t2 + WHERE t2.id_user = t3.id_usuario) AS user_name + FROM tevento t3 + WHERE utimestamp > %d AND utimestamp <= %d + AND id_grupo IN (%s) '.$sql_where.' + ORDER BY utimestamp ASC', + $datelimit, + $date, + implode(',', $id_group) + ); + + return db_get_all_row_by_steps_sql($begin, $result, $sql); +} + + +/** + * Get all the events happened in an Agent during a period of time. + * + * The returned events will be in the time interval ($date - $period, $date] + * + * @param integer $id_agent Agent id to get events. + * @param integer $period Period in seconds to get events. + * @param integer $date Beginning date to get events. + * @param boolean $history History. + * @param boolean $show_summary_group Show_summary_group. + * @param boolean $filter_event_severity Filter_event_severity. + * @param boolean $filter_event_type Filter_event_type. + * @param boolean $filter_event_status Filter_event_status. + * @param boolean $filter_event_filter_search Filter_event_filter_search. + * @param boolean $id_group Id_group. + * @param boolean $events_group Events_group. + * @param boolean $id_agent_module Id_agent_module. + * @param boolean $events_module Events_module. + * @param boolean $id_server Id_server. + * + * @return array An array with all the events happened. + */ +function events_get_agent( + $id_agent, + $period, + $date=0, + $history=false, + $show_summary_group=false, + $filter_event_severity=false, + $filter_event_type=false, + $filter_event_status=false, + $filter_event_filter_search=false, + $id_group=false, + $events_group=false, + $id_agent_module=false, + $events_module=false, + $id_server=false +) { + global $config; + + if (!is_numeric($date)) { + $date = time_w_fixed_tz($date); + } + + if (is_metaconsole() && $events_group === false) { + $id_server = true; + } + + if (empty($date)) { + $date = get_system_time(); + } + + if ($events_group) { + $id_group = groups_safe_acl($config['id_user'], $id_group, 'ER'); + + if (empty($id_group)) { + // An empty array means the user doesn't have access. + return false; + } + } + + $datelimit = ($date - $period); + + $sql_where = ''; + $severity_all = 0; + if (!empty($filter_event_severity)) { + foreach ($filter_event_severity as $key => $value) { + switch ($value) { + case -1: + $severity_all = 1; + break; + + case 34: + $filter_event_severity[$key] = '3, 4'; + break; + + case 20: + $filter_event_severity[$key] = '0, 1, 3, 4, 5, 6'; + break; + + case 21: + $filter_event_severity[$key] = '4, 2'; + break; + + default: + // Ignore. + break; + } + } + + if (!$severity_all) { + $sql_where .= ' AND criticity IN ('.implode(', ', $filter_event_severity).')'; + } + } + + $status_all = 0; + if (!empty($filter_event_status)) { + foreach ($filter_event_status as $key => $value) { + switch ($value) { + case -1: + $status_all = 1; + break; + + case 3: + $filter_event_status[$key] = ('0, 2'); + default: + // Ignore. + break; + } + } + + if (!$status_all) { + $sql_where .= ' AND estado IN ('.implode( + ', ', + $filter_event_status + ).')'; + } + } + + if (!empty($filter_event_type) && $filter_event_type[0] != 'all') { + $sql_where .= ' AND ('; + $type = []; + foreach ($filter_event_type as $event_type) { + if ($event_type != '') { + // If normal, warning, could be several (going_up_warning, going_down_warning... too complex. + // Shown to user only "warning, critical and normal". + if ($event_type == 'warning' || $event_type == 'critical' || $event_type == 'normal') { + $type[] = " event_type LIKE '%".$event_type."%' "; + } else if ($event_type == 'not_normal') { + $type[] = " (event_type LIKE '%warning%' OR event_type LIKE '%critical%' OR event_type LIKE '%unknown%') "; + } else if ($event_type != 'all') { + $type[] = " event_type = '".$event_type."'"; + } + } + } + + $sql_where .= implode(' OR ', $type).')'; + } + + if (!empty($filter_event_filter_search)) { + $sql_where .= ' AND (evento LIKE "%'.io_safe_input($filter_event_filter_search).'%" OR id_evento LIKE "%'.io_safe_input($filter_event_filter_search).'%")'; + } + + if ($events_group) { + $sql_where .= sprintf( + ' AND id_grupo IN (%s) AND utimestamp > %d + AND utimestamp <= %d ', + implode(',', $id_group), + $datelimit, + $date + ); + } else if ($events_module) { + $sql_where .= sprintf( + ' AND id_agentmodule = %d AND utimestamp > %d + AND utimestamp <= %d ', + $id_agent_module, + $datelimit, + $date + ); + } else { + $sql_where .= sprintf( + ' AND id_agente = %d AND utimestamp > %d + AND utimestamp <= %d ', + $id_agent, + $datelimit, + $date + ); + } + + if (is_metaconsole() && $id_server) { + $sql_where .= ' AND server_id = '.$id_server; + } + + if ($show_summary_group) { + return events_get_events_grouped( + $sql_where, + 0, + 1000, + is_metaconsole(), + false, + false, + $history + ); + } else { + return events_get_events_no_grouped( + $sql_where, + 0, + 1000, + (is_metaconsole() && $id_server) ? true : false, + false, + false, + $history + ); + } +} + + +/** + * Decode a numeric type into type description. + * + * @param integer $type_id Numeric type. + * + * @return string Type description. + */ +function events_get_event_types($type_id) +{ + $diferent_types = get_event_types(); + + $type_desc = ''; + switch ($type_id) { + case 'unknown': + $type_desc = __('Unknown'); + break; + + case 'critical': + $type_desc = __('Monitor Critical'); + break; + + case 'warning': + $type_desc = __('Monitor Warning'); + break; + + case 'normal': + $type_desc = __('Monitor Normal'); + break; + + case 'alert_fired': + $type_desc = __('Alert fired'); + break; + + case 'alert_recovered': + $type_desc = __('Alert recovered'); + break; + + case 'alert_ceased': + $type_desc = __('Alert ceased'); + break; + + case 'alert_manual_validation': + $type_desc = __('Alert manual validation'); + break; + + case 'recon_host_detected': + $type_desc = __('Recon host detected'); + break; + + case 'system': + $type_desc = __('System'); + break; + + case 'error': + $type_desc = __('Error'); + break; + + case 'configuration_change': + $type_desc = __('Configuration change'); + break; + + case 'not_normal': + $type_desc = __('Not normal'); + break; + + default: + if (isset($config['text_char_long'])) { + foreach ($diferent_types as $key => $type) { + if ($key == $type_id) { + $type_desc = ui_print_truncate_text($type, $config['text_char_long'], false, true, false); + } + } + } + break; + } + + return $type_desc; +} + + +/** + * Decode a numeric severity into severity description. + * + * @param integer $severity_id Numeric severity. + * + * @return string Severity description. + */ +function events_get_severity_types($severity_id) +{ + $diferent_types = get_priorities(); + + $severity_desc = ''; + switch ($severity_id) { + case EVENT_CRIT_MAINTENANCE: + $severity_desc = __('Maintenance'); + break; + + case EVENT_CRIT_INFORMATIONAL: + $severity_desc = __('Informational'); + break; + + case EVENT_CRIT_NORMAL: + $severity_desc = __('Normal'); + break; + + case EVENT_CRIT_WARNING: + $severity_desc = __('Warning'); + break; + + case EVENT_CRIT_CRITICAL: + $severity_desc = __('Critical'); + break; + + default: + if (isset($config['text_char_long'])) { + foreach ($diferent_types as $key => $type) { + if ($key == $severity_id) { + $severity_desc = ui_print_truncate_text( + $type, + $config['text_char_long'], + false, + true, + false + ); + } + } + } + break; + } + + return $severity_desc; +} + + +/** + * Return all descriptions of event status. + * + * @param boolean $report Show in report or not. + * + * @return array Status description array. + */ +function events_get_all_status($report=false) +{ + $fields = []; + if (!$report) { + $fields[-1] = __('All event'); + $fields[0] = __('Only new'); + $fields[1] = __('Only validated'); + $fields[2] = __('Only in process'); + $fields[3] = __('Only not validated'); + } else { + $fields[-1] = __('All event'); + $fields[0] = __('New'); + $fields[1] = __('Validated'); + $fields[2] = __('In process'); + $fields[3] = __('Not Validated'); + } + + return $fields; +} + + +/** + * Decode a numeric status into status description. + * + * @param integer $status_id Numeric status. + * + * @return string Status description. + */ +function events_get_status($status_id) +{ + switch ($status_id) { + case -1: + $status_desc = __('All event'); + break; + + case 0: + $status_desc = __('Only new'); + break; + + case 1: + $status_desc = __('Only validated'); + break; + + case 2: + $status_desc = __('Only in process'); + break; + + case 3: + $status_desc = __('Only not validated'); + break; + + default: + // Ignore. + break; + } + + return $status_desc; +} + + +/** + * Checks if a user has permissions to see an event filter. + * + * @param integer $id_filter Id of the event filter. + * + * @return boolean True if the user has permissions or false otherwise. + */ +function events_check_event_filter_group($id_filter) +{ + global $config; + + $id_group = db_get_value('id_group_filter', 'tevent_filter', 'id_filter', $id_filter); + $own_info = get_user_info($config['id_user']); + // Get group list that user has access. + $groups_user = users_get_groups($config['id_user'], 'EW', $own_info['is_admin'], true); + + // Permissions in any group allow to edit "All group" filters. + if ($id_group == 0 && !empty($groups_user)) { + return true; + } + + $groups_id = []; + $has_permission = false; + + foreach ($groups_user as $key => $groups) { + if ($groups['id_grupo'] == $id_group) { + return true; + } + } + + return false; +} + + +/** + * Get a event filter. + * + * @param integer $id_filter Filter id to be fetched. + * @param array $filter Extra filter. + * @param array $fields Fields to be fetched. + * + * @return array A event filter matching id and filter or false. + */ +function events_get_event_filter($id_filter, $filter=false, $fields=false) +{ + if (empty($id_filter)) { + return false; + } + + if (! is_array($filter)) { + $filter = []; + $filter['id_filter'] = (int) $id_filter; + } + + return db_get_row_filter('tevent_filter', $filter, $fields); +} + + +/** + * Get a event filters in select format. + * + * @param boolean $manage If event filters are used for manage/view operations + * (non admin users can see group ALL for manage) # Fix. + * + * @return array A event filter matching id and filter or false. + */ +function events_get_event_filter_select($manage=true) +{ + global $config; + + $strict_acl = db_get_value('strict_acl', 'tusuario', 'id_user', $config['id_user']); + + if ($strict_acl) { + $user_groups = users_get_strict_mode_groups( + $config['id_user'], + users_can_manage_group_all() + ); + } else { + $user_groups = users_get_groups( + $config['id_user'], + 'ER', + users_can_manage_group_all(), + true + ); + } + + if (empty($user_groups)) { + return []; + } + + $sql = ' + SELECT id_filter, id_name + FROM tevent_filter + WHERE id_group_filter IN (0, '.implode(',', array_keys($user_groups)).')'; + + $event_filters = db_get_all_rows_sql($sql); + + if ($event_filters === false) { + return []; + } else { + $result = []; + foreach ($event_filters as $event_filter) { + $result[$event_filter['id_filter']] = $event_filter['id_name']; + } + } + + return $result; +} + + +/** + * Events pages functions to load modal window with advanced view of an event. + * Called from include/ajax/events.php. + * + * @param mixed $event Event. + * @param array $childrens_ids Children_ids. + * + * @return string HTML. + */ +function events_page_responses($event, $childrens_ids=[]) +{ + global $config; + // + // Responses. + // + $table_responses = new StdClass(); + $table_responses->cellspacing = 2; + $table_responses->cellpadding = 2; + $table_responses->id = 'responses_table'; + $table_responses->width = '100%'; + $table_responses->data = []; + $table_responses->head = []; + $table_responses->style[0] = 'height:30px'; + $table_responses->style[2] = 'text-align:right;'; + $table_responses->class = 'table_modal_alternate'; + + if (tags_checks_event_acl($config['id_user'], $event['id_grupo'], 'EM', $event['clean_tags'], $childrens_ids)) { + // Owner. + $data = []; + $data[0] = __('Change owner'); + // Owner change can be done to users that belong to the event group + // with ER permission. + $profiles_view_events = db_get_all_rows_filter('tperfil', ['event_view' => '1'], 'id_perfil'); + foreach ($profiles_view_events as $k => $v) { + $profiles_view_events[$k] = reset($v); + } + + // Juanma (05/05/2014) Fix : Propagate ACL. + $_user_groups = array_keys( + users_get_groups($config['id_user'], 'ER', users_can_manage_group_all()) + ); + $strict_user = db_get_value( + 'strict_acl', + 'tusuario', + 'id_user', + $config['id_user'] + ); + if ($strict_user) { + $user_name = db_get_value( + 'fullname', + 'tusuario', + 'id_user', + $config['id_user'] + ); + + $users = []; + $users[0]['id_user'] = $config['id_user']; + $users[0]['fullname'] = $user_name; + } else { + $users = groups_get_users( + $_user_groups, + ['id_perfil' => $profiles_view_events], + true + ); + } + + foreach ($users as $u) { + $owners[$u['id_user']] = $u['fullname']; + } + + if ($event['owner_user'] == '') { + $owner_name = __('None'); + } else { + $owner_name = db_get_value( + 'fullname', + 'tusuario', + 'id_user', + $event['owner_user'] + ); + $owners[$event['owner_user']] = $owner_name; + } + + $data[1] = html_print_select( + $owners, + 'id_owner', + $event['owner_user'], + '', + __('None'), + -1, + true + ); + $data[2] .= html_print_button( + __('Update'), + 'owner_button', + false, + 'event_change_owner();', + 'class="sub next"', + true + ); + + $table_responses->data[] = $data; + } + + // Status. + $data = []; + $data[0] = __('Change status'); + + $status_blocked = false; + + if (tags_checks_event_acl( + $config['id_user'], + $event['id_grupo'], + 'EM', + $event['clean_tags'], + $childrens_ids + ) + ) { + // If the user has manager acls, the status can be changed to all + // possibilities always. + $status = [ + 0 => __('New'), + 2 => __('In process'), + 1 => __('Validated'), + ]; + } else { + switch ($event['estado']) { + case 0: + // If the user hasnt manager acls and the event is new. + // The status can be changed. + $status = [ + 2 => __('In process'), + 1 => __('Validated'), + ]; + break; + + case 1: + // If the user hasnt manager acls and the event is validated. + // The status cannot be changed. + $status = [1 => __('Validated')]; + $status_blocked = true; + break; + + case 2: + // If the user hasnt manager acls and the event is in process. + // The status only can be changed to validated. + $status = [1 => __('Validated')]; + break; + + default: + // Ignored. + break; + } + } + + // The change status option will be enabled only when is possible change + // the status. + $data[1] = html_print_select( + $status, + 'estado', + $event['estado'], + '', + '', + 0, + true, + false, + false, + '', + $status_blocked + ); + + if (!$status_blocked) { + $data[2] .= html_print_button( + __('Update'), + 'status_button', + false, + 'event_change_status(\''.$event['similar_ids'].'\');', + 'class="sub next"', + true + ); + } + + $table_responses->data[] = $data; + + // Comments. + $data = []; + $data[0] = __('Comment'); + $data[1] = ''; + $data[2] = html_print_button( + __('Add comment'), + 'comment_button', + false, + '$(\'#link_comments\').trigger(\'click\');', + 'class="sub next"', + true + ); + + $table_responses->data[] = $data; + + if (tags_checks_event_acl( + $config['id_user'], + $event['id_grupo'], + 'EM', + $event['clean_tags'], + $childrens_ids + ) + ) { + // Delete. + $data = []; + $data[0] = __('Delete event'); + $data[1] = ''; + $data[2] = '
'; + $data[2] .= html_print_button( + __('Delete event'), + 'delete_button', + false, + 'if(!confirm(\''.__('Are you sure?').'\')) { return false; } this.form.submit();', + 'class="sub cancel"', + true + ); + $data[2] .= html_print_input_hidden('delete', 1, true); + $data[2] .= html_print_input_hidden( + 'validate_ids', + $event['id_evento'], + true + ); + $data[2] .= '
'; + + $table_responses->data[] = $data; + } + + // Custom responses. + $data = []; + $data[0] = __('Custom responses'); + + $id_groups = array_keys(users_get_groups(false, 'EW')); + $event_responses = db_get_all_rows_filter( + 'tevent_response', + ['id_group' => $id_groups] + ); + + if (empty($event_responses)) { + $data[1] = ''.__('N/A').''; + } else { + $responses = []; + foreach ($event_responses as $v) { + $responses[$v['id']] = $v['name']; + } + + $data[1] = html_print_select( + $responses, + 'select_custom_response', + '', + '', + '', + '', + true, + false, + false + ); + + if (isset($event['server_id'])) { + $server_id = $event['server_id']; + } else { + $server_id = 0; + } + + $data[2] .= html_print_button( + __('Execute'), + 'custom_response_button', + false, + 'execute_response('.$event['id_evento'].','.$server_id.')', + "class='sub next'", + true + ); + } + + $table_responses->data[] = $data; + + $responses_js = ""; + + $responses = '
'.html_print_table($table_responses, true).$responses_js.'
'; + + return $responses; +} + + +/** + * Replace macros in the target of a response and return it. + * If server_id > 0, it's a metaconsole query. + * + * @param integer $event_id Event identifier. + * @param integer $response_id Event response identifier. + * @param integer $server_id Node identifier (for metaconsole). + * @param boolean $history Use the history database or not. + * + * @return string The response text with the macros applied. + */ +function events_get_response_target( + int $event_id, + int $response_id, + int $server_id=0, + bool $history=false +) { + global $config; + + // If server_id > 0, it's a metaconsole query. + $meta = $server_id > 0 || is_metaconsole(); + $event_table = events_get_events_table($meta, $history); + $event = db_get_row($event_table, 'id_evento', $event_id); + + $event_response = db_get_row('tevent_response', 'id', $response_id); + $target = io_safe_output($event_response['target']); + + // Substitute each macro. + if (strpos($target, '_agent_address_') !== false) { + if ($meta) { + $agente_table_name = 'tmetaconsole_agent'; + $filter = [ + 'id_tagente' => $event['id_agente'], + 'id_tmetaconsole_setup' => $server_id, + ]; + } else { + $agente_table_name = 'tagente'; + $filter = ['id_agente' => $event['id_agente']]; + } + + $ip = db_get_value_filter('direccion', $agente_table_name, $filter); + // If agent has not an IP, display N/A. + if ($ip === false) { + $ip = __('N/A'); + } + + $target = str_replace('_agent_address_', $ip, $target); + } + + if (strpos($target, '_agent_id_') !== false) { + $target = str_replace('_agent_id_', $event['id_agente'], $target); + } + + if ((strpos($target, '_module_address_') !== false) + || (strpos($target, '_module_name_') !== false) + ) { + if ($event['id_agentmodule'] !== 0) { + if ($meta) { + $server = metaconsole_get_connection_by_id($server_id); + metaconsole_connect($server); + } + + $module = db_get_row('tagente_modulo', 'id_agente_modulo', $event['id_agentmodule']); + if (empty($module['ip_target'])) { + $module['ip_target'] = __('N/A'); + } + + $target = str_replace('_module_address_', $module['ip_target'], $target); + if (empty($module['nombre'])) { + $module['nombre'] = __('N/A'); + } + + $target = str_replace( + '_module_name_', + io_safe_output($module['nombre']), + $target + ); + + if ($meta) { + metaconsole_restore_db(); + } + } else { + $target = str_replace('_module_address_', __('N/A'), $target); + $target = str_replace('_module_name_', __('N/A'), $target); + } + } + + if (strpos($target, '_event_id_') !== false) { + $target = str_replace('_event_id_', $event['id_evento'], $target); + } + + if (strpos($target, '_user_id_') !== false) { + if (!empty($event['id_usuario'])) { + $target = str_replace('_user_id_', $event['id_usuario'], $target); + } else { + $target = str_replace('_user_id_', __('N/A'), $target); + } + } + + if (strpos($target, '_group_id_') !== false) { + $target = str_replace('_group_id_', $event['id_grupo'], $target); + } + + if (strpos($target, '_group_name_') !== false) { + $target = str_replace( + '_group_name_', + groups_get_name($event['id_grupo'], true), + $target + ); + } + + if (strpos($target, '_event_utimestamp_') !== false) { + $target = str_replace( + '_event_utimestamp_', + $event['utimestamp'], + $target + ); + } + + if (strpos($target, '_event_date_') !== false) { + $target = str_replace( + '_event_date_', + date($config['date_format'], $event['utimestamp']), + $target + ); + } + + if (strpos($target, '_event_text_') !== false) { + $target = str_replace( + '_event_text_', + events_display_name($event['evento']), + $target + ); + } + + if (strpos($target, '_event_type_') !== false) { + $target = str_replace( + '_event_type_', + events_print_type_description($event['event_type'], true), + $target + ); + } + + if (strpos($target, '_alert_id_') !== false) { + $target = str_replace( + '_alert_id_', + empty($event['is_alert_am']) ? __('N/A') : $event['is_alert_am'], + $target + ); + } + + if (strpos($target, '_event_severity_id_') !== false) { + $target = str_replace('_event_severity_id_', $event['criticity'], $target); + } + + if (strpos($target, '_event_severity_text_') !== false) { + $target = str_replace( + '_event_severity_text_', + get_priority_name($event['criticity']), + $target + ); + } + + if (strpos($target, '_module_id_') !== false) { + $target = str_replace('_module_id_', $event['id_agentmodule'], $target); + } + + if (strpos($target, '_event_tags_') !== false) { + $target = str_replace('_event_tags_', $event['tags'], $target); + } + + if (strpos($target, '_event_extra_id_') !== false) { + if (empty($event['id_extra'])) { + $target = str_replace('_event_extra_id_', __('N/A'), $target); + } else { + $target = str_replace('_event_extra_id_', $event['id_extra'], $target); + } + } + + if (strpos($target, '_event_source_') !== false) { + $target = str_replace('_event_source_', $event['source'], $target); + } + + if (strpos($target, '_event_instruction_') !== false) { + $target = str_replace( + '_event_instruction_', + events_display_instructions($event['event_type'], $event, false), + $target + ); + } + + if (strpos($target, '_owner_user_') !== false) { + if (empty($event['owner_user'])) { + $target = str_replace('_owner_user_', __('N/A'), $target); + } else { + $target = str_replace('_owner_user_', $event['owner_user'], $target); + } + } + + if (strpos($target, '_event_status_') !== false) { + $event_st = events_display_status($event['estado']); + $target = str_replace('_event_status_', $event_st['title'], $target); + } + + if (strpos($target, '_group_custom_id_') !== false) { + $group_custom_id = db_get_value_sql( + sprintf( + 'SELECT custom_id FROM tgrupo WHERE id_grupo=%s', + $event['id_grupo'] + ) + ); + $event_st = events_display_status($event['estado']); + $target = str_replace('_group_custom_id_', $group_custom_id, $target); + } + + // Parse the event custom data. + if (!empty($event['custom_data'])) { + $custom_data = json_decode(base64_decode($event['custom_data'])); + foreach ($custom_data as $key => $value) { + $target = str_replace('_customdata_'.$key.'_', $value, $target); + } + } + + // This will replace the macro with the current logged user. + if (strpos($target, '_current_user_') !== false) { + $target = str_replace('_current_user_', $config['id_user'], $target); + } + + return $target; +} + + +/** + * Generates 'custom field' page for event viewer. + * + * @param array $event Event to be displayed. + * + * @return string HTML. + */ +function events_page_custom_fields($event) +{ + global $config; + + // Custom fields. + $table = new stdClass; + $table->cellspacing = 2; + $table->cellpadding = 2; + $table->width = '100%'; + $table->data = []; + $table->head = []; + $table->class = 'table_modal_alternate'; + + $all_customs_fields = (bool) check_acl( + $config['id_user'], + $event['id_grupo'], + 'AW' + ); + + if ($all_customs_fields) { + $fields = db_get_all_rows_filter('tagent_custom_fields'); + } else { + $fields = db_get_all_rows_filter( + 'tagent_custom_fields', + ['display_on_front' => 1] + ); + } + + if ($event['id_agente'] == 0) { + $fields_data = []; + } else { + $fields_data = db_get_all_rows_filter('tagent_custom_data', ['id_agent' => $event['id_agente']]); + if (is_array($fields_data)) { + $fields_data_aux = []; + foreach ($fields_data as $fd) { + $fields_data_aux[$fd['id_field']] = $fd['description']; + } + + $fields_data = $fields_data_aux; + } + } + + foreach ($fields as $field) { + // Owner. + $data = []; + $data[0] = $field['name']; + + if (empty($fields_data[$field['id_field']])) { + $data[1] = ''.__('N/A').''; + } else { + if ($field['is_password_type']) { + $data[1] = '••••••••'; + } else { + $data[1] = ui_bbcode_to_html($fields_data[$field['id_field']]); + } + } + + $field['id_field']; + + $table->data[] = $data; + } + + $custom_fields = '
'.html_print_table($table, true).'
'; + + return $custom_fields; +} + + +/** + * Retrieves extended information of given event. + * + * @param integer $id_evento Target event. + * + * @return mixed array Of extended events or false if error. + */ +function events_get_extended_events(int $id_evento) +{ + return db_get_all_rows_sql( + sprintf( + 'SELECT * FROM tevent_extended + WHERE id_evento=%d ORDER BY utimestamp DESC', + $id_evento + ) + ); + +} + + +/** + * Return if event has extended info or not. + * + * @param integer $id_event Target event. + * + * @return boolean Has extended info or not + */ +function events_has_extended_info(int $id_event) +{ + return (bool) db_get_value_sql( + sprintf( + 'SELECT count(*) as "n" FROM ( + SELECT * + FROM tevent_extended WHERE id_evento=%d LIMIT 1) t', + $id_event + ) + ); +} + + +/** + * Generates the 'related' page in event view. + * + * @param array $event To be displayed. + * @param string $server Server (if in metaconsole environment). + * + * @return string HTML to be displayed. + */ +function events_page_related($event, $server='') +{ + $html = ''; + + return $html; +} + + +/** + * Generates the 'details' page in event view. + * + * @param array $event To be displayed. + * @param string $server Server (if in metaconsole environment). + * + * @return string HTML to be displayed. + */ +function events_page_details($event, $server='') +{ + global $img_sev; + global $config; + + // If server is provided, get the hash parameters. + if (!empty($server) && is_metaconsole()) { + $hashdata = metaconsole_get_server_hashdata($server); + $hashstring = '&loginhash=auto&loginhash_data='.$hashdata.'&loginhash_user='.str_rot13($config['id_user']); + $serverstring = $server['server_url'].'/'; + + if (metaconsole_connect($server) !== NOERR) { + return ui_print_error_message(__('There was an error connecting to the node'), '', true); + } + } else { + $hashstring = ''; + $serverstring = ''; + } + + // Details. + $table_details = new stdClass; + $table_details->width = '100%'; + $table_details->data = []; + $table_details->head = []; + $table_details->cellspacing = 0; + $table_details->cellpadding = 0; + $table_details->class = 'table_modal_alternate'; + + /* + * Useless switch. + + switch ($event['event_type']) { + case 'going_unknown': + case 'going_up_warning': + case 'going_down_warning': + case 'going_up_critical': + case 'going_down_critical': + default: + // Ignore. + break; + } + */ + + if ($event['id_agente'] != 0) { + $agent = db_get_row('tagente', 'id_agente', $event['id_agente']); + } else { + $agent = []; + } + + $data[0] = __('Agent details'); + $data[1] = empty($agent) ? ''.__('N/A').'' : ''; + $table_details->data[] = $data; + + if (!empty($agent)) { + $data = []; + $data[0] = '
'.__('Name').'
'; + if (can_user_access_node()) { + $data[1] = ui_print_agent_name( + $event['id_agente'], + true, + 'agent_medium', + '', + false, + $serverstring, + $hashstring, + $agent['alias'] + ); + } else { + $data[1] = ui_print_truncate_text( + $agent['alias'], + 'agent_medium', + true, + true, + true + ); + } + + $table_details->data[] = $data; + + $data = []; + $data[0] = '
'.__('IP Address').'
'; + $data[1] = empty($agent['direccion']) ? ''.__('N/A').'' : $agent['direccion']; + $table_details->data[] = $data; + + $data = []; + $data[0] = '
'.__('OS').'
'; + $data[1] = ui_print_os_icon($agent['id_os'], true, true); + if (!empty($agent['os_version'])) { + $data[1] .= ' ('.$agent['os_version'].')'; + } + + $table_details->data[] = $data; + + $data = []; + $data[0] = '
'.__('Last contact').'
'; + $data[1] = ($agent['ultimo_contacto'] == '1970-01-01 00:00:00') ? ''.__('N/A').'' : date_w_fixed_tz($agent['ultimo_contacto']); + $table_details->data[] = $data; + + $data = []; + $data[0] = '
'.__('Last remote contact').'
'; + $data[1] = ($agent['ultimo_contacto_remoto'] == '1970-01-01 00:00:00') ? ''.__('N/A').'' : date_w_fixed_tz($agent['ultimo_contacto_remoto']); + $table_details->data[] = $data; + + $data = []; + $data[0] = '
'.__('Custom fields').'
'; + $data[1] = html_print_button( + __('View custom fields'), + 'custom_button', + false, + '$(\'#link_custom_fields\').trigger(\'click\');', + 'class="sub next"', + true + ); + $table_details->data[] = $data; + } + + if ($event['id_agentmodule'] != 0) { + $module = db_get_row_filter( + 'tagente_modulo', + [ + 'id_agente_modulo' => $event['id_agentmodule'], + 'delete_pending' => 0, + ] + ); + } else { + $module = []; + } + + $data = []; + $data[0] = __('Module details'); + $data[1] = empty($module) ? ''.__('N/A').'' : ''; + $table_details->data[] = $data; + + if (!empty($module)) { + // Module name. + $data = []; + $data[0] = '
'.__('Name').'
'; + $data[1] = $module['nombre']; + $table_details->data[] = $data; + + // Module group. + $data = []; + $data[0] = '
'.__('Module group').'
'; + $id_module_group = $module['id_module_group']; + if ($id_module_group == 0) { + $data[1] = __('No assigned'); + } else { + $module_group = db_get_value( + 'name', + 'tmodule_group', + 'id_mg', + $id_module_group + ); + $data[1] = ''; + $data[1] .= $module_group; + $data[1] .= ''; + } + + $table_details->data[] = $data; + + // ACL. + $acl_graph = false; + $strict_user = (bool) db_get_value( + 'strict_acl', + 'tusuario', + 'id_user', + $config['id_user'] + ); + + if (!empty($agent['id_grupo'])) { + $acl_graph = check_acl( + $config['id_user'], + $agent['id_grupo'], + 'RR' + ); + } + + if ($acl_graph) { + $data = []; + $data[0] = '
'.__('Graph').'
'; + + $module_type = -1; + if (isset($module['module_type'])) { + $module_type = $module['module_type']; + } + + $graph_type = return_graphtype($module_type); + $url = ui_get_full_url( + 'operation/agentes/stat_win.php', + false, + false, + false + ); + $handle = dechex( + crc32($module['id_agente_modulo'].$module['nombre']) + ); + $win_handle = 'day_'.$handle; + + $graph_params = [ + 'type' => $graph_type, + 'period' => SECONDS_1DAY, + 'id' => $module['id_agente_modulo'], + 'label' => base64_encode($module['nombre']), + 'refresh' => SECONDS_10MINUTES, + ]; + + if (defined('METACONSOLE')) { + // Set the server id. + $graph_params['server'] = $server['id']; + } + + $graph_params_str = http_build_query($graph_params); + + $link = "winopeng('".$url.'?'.$graph_params_str."','".$win_handle."')"; + + $data[1] = ''; + $data[1] .= html_print_image('images/chart_curve.png', true); + $data[1] .= ''; + $table_details->data[] = $data; + } + } + + $data = []; + $data[0] = __('Alert details'); + $data[1] = ($event['id_alert_am'] == 0) ? ''.__('N/A').'' : ''; + $table_details->data[] = $data; + + if ($event['id_alert_am'] != 0) { + $data = []; + $data[0] = '
'.__('Source').'
'; + $data[1] = ''; + $standby = db_get_value('standby', 'talert_template_modules', 'id', $event['id_alert_am']); + if (!$standby) { + $data[1] .= html_print_image( + 'images/bell.png', + true, + ['title' => __('Go to data overview')] + ); + } else { + $data[1] .= html_print_image( + 'images/bell_pause.png', + true, + ['title' => __('Go to data overview')] + ); + } + + $sql = 'SELECT name + FROM talert_templates + WHERE id IN (SELECT id_alert_template + FROM talert_template_modules + WHERE id = '.$event['id_alert_am'].');'; + + $templateName = db_get_sql($sql); + + $data[1] .= $templateName; + + $data[1] .= ''; + + $table_details->data[] = $data; + + $data = []; + $data[0] = '
'.__('Priority').'
'; + + $priority_code = db_get_value('priority', 'talert_template_modules', 'id', $event['id_alert_am']); + $alert_priority = get_priority_name($priority_code); + $data[1] = html_print_image( + $img_sev, + true, + [ + 'class' => 'image_status', + 'width' => 61, + 'height' => 28, + 'title' => $alert_priority, + 'style' => 'vertical-align:text-bottom', + ] + ); + $data[1] .= ' '.$alert_priority; + + $table_details->data[] = $data; + } + + $data = []; + $data[0] = __('Instructions'); + $data[1] = html_entity_decode(events_display_instructions($event['event_type'], $event, true)); + $table_details->data[] = $data; + + $data = []; + $data[0] = __('Extra id'); + if ($event['id_extra'] != '') { + $data[1] = $event['id_extra']; + } else { + $data[1] = ''.__('N/A').''; + } + + $table_details->data[] = $data; + + $data = []; + $data[0] = __('Source'); + if ($event['source'] != '') { + $data[1] = $event['source']; + } else { + $data[1] = ''.__('N/A').''; + } + + $table_details->data[] = $data; + + $details = '
'.html_print_table($table_details, true).'
'; + + return $details; +} + + +/** + * Generates content for 'custom data' page in event viewer. + * + * @param array $event Event. + * + * @return string HTML. + */ +function events_page_custom_data($event) +{ + global $config; + + // + // Custom data. + // + if ($event['custom_data'] == '') { + return ''; + } + + $table->width = '100%'; + $table->data = []; + $table->head = []; + $table->class = 'table_modal_alternate'; + + $json_custom_data = base64_decode($event['custom_data']); + $custom_data = json_decode($json_custom_data); + if ($custom_data === null) { + return '
'.__('Invalid custom data: %s', $json_custom_data).'
'; + } + + $i = 0; + foreach ($custom_data as $field => $value) { + $table->data[$i][0] = io_safe_output($field); + $table->data[$i][1] = io_safe_output($value); + $i++; + } + + $custom_data = '
'.html_print_table($table, true).'
'; + + return $custom_data; +} + + +/** + * Get the event name from tevento and display it in console. + * + * @param string $db_name Target event name. + * + * @return string Event name. + */ +function events_display_name($db_name='') +{ + return io_safe_output(str_replace(' ', '
', $db_name)); +} + + +/** + * Get the image and status value of event. + * + * @param integer $status Status. + * + * @return string Image path. + */ +function events_display_status($status) +{ + switch ($status) { + case 0: + return [ + 'img' => 'images/star.png', + 'title' => __('New event'), + ]; + + case 1: + return [ + 'img' => 'images/tick.png', + 'title' => __('Event validated'), + ]; + + case 2: + return [ + 'img' => 'images/hourglass.png', + 'title' => __('Event in process'), + ]; + + default: + // Ignore. + break; + } +} + + +/** + * Get the instruction of an event. + * + * @param string $event_type Type of event. + * @param array $inst Array with unknown warning and critical + * instructions. + * @param boolean $italic Display N/A between italic html marks if + * instruction is not found. + * + * @return string Safe output. + */ +function events_display_instructions($event_type='', $inst=[], $italic=true) +{ + switch ($event_type) { + case 'going_unknown': + if ($inst['unknown_instructions'] != '') { + return str_replace("\n", '
', io_safe_output($inst['unknown_instructions'])); + } + break; + + case 'going_up_warning': + case 'going_down_warning': + if ($inst['warning_instructions'] != '') { + return str_replace("\n", '
', io_safe_output($inst['warning_instructions'])); + } + break; + + case 'going_up_critical': + case 'going_down_critical': + if ($inst['critical_instructions'] != '') { + return str_replace("\n", '
', io_safe_output($inst['critical_instructions'])); + } + break; + + case 'system': + $data = []; + if ($inst['critical_instructions'] != '') { + return str_replace("\n", '
', io_safe_output($inst['critical_instructions'])); + } + + if ($inst['warning_instructions'] != '') { + return str_replace("\n", '
', io_safe_output($inst['warning_instructions'])); + } + + if ($inst['unknown_instructions'] != '') { + return str_replace("\n", '
', io_safe_output($inst['unknown_instructions'])); + } + break; + + default: + // Ignore. + break; + } + + $na_return = ($italic === true) ? ''.__('N/A').'' : __('N/A'); + + return $na_return; +} + + +/** + * Generates 'general' page for events viewer. + * + * @param array $event Event. + * + * @return string HTML. + */ +function events_page_general($event) +{ + global $img_sev; + global $config; + + /* + Commented out (old) + // $group_rep = $event['similar_ids'] == -1 ? 1 : count(explode(',',$event['similar_ids'])); + */ + + global $group_rep; + + // General. + $table_general = new stdClass; + $table_general->cellspacing = 0; + $table_general->cellpadding = 0; + $table_general->width = '100%'; + $table_general->data = []; + $table_general->head = []; + $table_general->class = 'table_modal_alternate'; + + $data = []; + $data[0] = __('Event ID'); + $data[1] = '#'.$event['id_evento']; + $table_general->data[] = $data; + + $data = []; + $data[0] = __('Event name'); + $data[1] = events_display_name($event['evento']); + $table_general->data[] = $data; + + $data = []; + $data[0] = __('Timestamp'); + + if ($group_rep == 1 && $event['event_rep'] > 1) { + $data[1] = __('First event').': '.date($config['date_format'], $event['timestamp_first']).'
'.__('Last event').': '.date($config['date_format'], $event['timestamp_last']); + } else { + $data[1] = date($config['date_format'], $event['utimestamp']); + } + + $table_general->data[] = $data; + + // $event['owner_user'] = $event['id_usuario']; + $data = []; + $data[0] = __('Owner'); + if (empty($event['owner_user'])) { + $data[1] = ''.__('N/A').''; + } else { + $user_owner = db_get_value('fullname', 'tusuario', 'id_user', $event['owner_user']); + if (empty($user_owner)) { + $user_owner = $event['owner_user']; + } + + $data[1] = $user_owner; + } + + $table_general->cellclass[3][1] = 'general_owner'; + + $table_general->data[] = $data; + + $data = []; + $data[0] = __('Type'); + $data[1] = events_print_type_description($event['event_type'], true); + $data[2] = events_print_type_img( + $event['event_type'], + true + ); + + $table_general->data[] = $data; + + $data = []; + $data[0] = __('Repeated'); + if ($group_rep != 0) { + if ($event['event_rep'] <= 1) { + $data[1] = ''.__('No').''; + } else { + $data[1] = sprintf('%d Times', $event['event_rep']); + } + } else { + $data[1] = ''.__('No').''; + } + + $table_general->data[] = $data; + + $data = []; + $data[0] = __('Severity'); + $event_criticity = get_priority_name($event['criticity']); + $data[1] = $event_criticity; + $data[2] = html_print_image( + $img_sev, + true, + [ + 'class' => 'image_status', + 'width' => 61, + 'height' => 28, + 'title' => $event_criticity, + ] + ); + $table_general->data[] = $data; + + // Get Status. + $event_st = events_display_status($event['estado']); + + $data = []; + $data[0] = __('Status'); + $data[1] = $event_st['title']; + $data[2] = html_print_image($event_st['img'], true); + $table_general->data[] = $data; + + // If event is validated, show who and when acknowleded it. + $data = []; + $data[0] = __('Acknowledged by'); + + if ($event['estado'] == 1) { + $user_ack = db_get_value('fullname', 'tusuario', 'id_user', $event['id_usuario']); + if (empty($user_ack)) { + $user_ack = $event['id_usuario']; + } + + $date_ack = date($config['date_format'], $event['ack_utimestamp']); + $data[1] = $user_ack.' ('.$date_ack.')'; + } else { + $data[1] = ''.__('N/A').''; + } + + $table_general->cellclass[7][1] = 'general_status'; + + $table_general->data[] = $data; + + $data = []; + $data[0] = __('Group'); + $data[1] = groups_get_name($event['id_grupo']); + if (!$config['show_group_name']) { + $data[2] = ui_print_group_icon($event['id_grupo'], true); + } + + $table_general->data[] = $data; + + $data = []; + $data[0] = __('Contact'); + $data[1] = ''; + $contact = db_get_value('contact', 'tgrupo', 'id_grupo', $event['id_grupo']); + if (empty($contact)) { + $data[1] = ''.__('N/A').''; + } else { + $data[1] = $contact; + } + + $table_general->data[] = $data; + + $data = []; + $data[0] = __('Tags'); + + if ($event['tags'] != '') { + $tags = tags_get_tags_formatted($event['tags']); + + $data[1] = $tags; + } else { + $data[1] = ''.__('N/A').''; + } + + $table_general->data[] = $data; + + $data = []; + $data[0] = __('ID extra'); + if ($event['id_extra'] != '') { + $data[1] = $event['id_extra']; + } else { + $data[1] = ''.__('N/A').''; + } + + $table_general->data[] = $data; + + $table_data = $table_general->data; + if (is_array($table_data)) { + $table_data_total = count($table_data); + } else { + $table_data_total = -1; + } + + for ($i = 0; $i <= $table_data_total; $i++) { + if (is_array($table_data[$i]) && count($table_data[$i]) == 2) { + $table_general->colspan[$i][1] = 2; + $table_general->style[2] = 'text-align:center; width:10%;'; + } + } + + $general = '
'.html_print_table($table_general, true).'
'; + + return $general; +} + + +/** + * Generate 'comments' page for event viewer. + * + * @param array $event Event. + * + * @return string HTML. + */ +function events_page_comments($event, $ajax=false) +{ + // Comments. + global $config; + + $comments = ''; + + $comments = $event['user_comment']; + if (isset($event['comments'])) { + $comments = explode('
', $event['comments']); + } + + $table_comments = new stdClass; + $table_comments->width = '100%'; + $table_comments->data = []; + $table_comments->head = []; + $table_comments->class = 'table_modal_alternate'; + + $comments = str_replace(["\n", ' '], '
', $comments); + + if (is_array($comments)) { + foreach ($comments as $comm) { + if (empty($comm)) { + continue; + } + + $comments_array[] = json_decode(io_safe_output($comm), true); + } + } else { + // If comments are not stored in json, the format is old. + $comments_array = json_decode(io_safe_output($comments), true); + } + + foreach ($comments_array as $comm) { + // Show the comments more recent first. + if (is_array($comm)) { + $comm = array_reverse($comm); + } + + if (empty($comm)) { + $comments_format = 'old'; + } else { + $comments_format = 'new'; + } + + switch ($comments_format) { + case 'new': + if (empty($comm)) { + $table_comments->style[0] = 'text-align:center;'; + $table_comments->colspan[0][0] = 2; + $data = []; + $data[0] = __('There are no comments'); + $table_comments->data[] = $data; + } + + if (isset($comm) === true + && is_array($comm) === true + ) { + foreach ($comm as $c) { + $data[0] = ''.$c['action'].' by '.$c['id_user'].''; + $data[0] .= '

'.date($config['date_format'], $c['utimestamp']).''; + $data[1] = $c['comment']; + $table_comments->data[] = $data; + } + } + break; + + case 'old': + $comm = explode('
', $comments); + + // Split comments and put in table. + $col = 0; + $data = []; + + foreach ($comm as $c) { + switch ($col) { + case 0: + $row_text = preg_replace('/\s*--\s*/', '', $c); + $row_text = preg_replace('/\<\/b\>/', '', $row_text); + $row_text = preg_replace('/\[/', '

[', $row_text); + $row_text = preg_replace('/[\[|\]]/', '', $row_text); + break; + + case 1: + $row_text = preg_replace("/[\r\n|\r|\n]/", '
', io_safe_output(strip_tags($c))); + break; + + default: + // Ignore. + break; + } + + $data[$col] = $row_text; + + $col++; + + if ($col == 2) { + $col = 0; + $table_comments->data[] = $data; + $data = []; + } + } + + if (count($comm) == 1 && $comm[0] == '') { + $table_comments->style[0] = 'text-align:center;'; + $table_comments->colspan[0][0] = 2; + $data = []; + $data[0] = __('There are no comments'); + $table_comments->data[] = $data; + } + break; + + default: + // Ignore. + break; + } + } + + if (((tags_checks_event_acl( + $config['id_user'], + $event['id_grupo'], + 'EM', + $event['clean_tags'], + $childrens_ids + )) || (tags_checks_event_acl( + $config['id_user'], + $event['id_grupo'], + 'EW', + $event['clean_tags'], + $childrens_ids + ))) && $config['show_events_in_local'] == false || $config['event_replication'] == false + ) { + $comments_form = '
'; + $comments_form .= html_print_textarea( + 'comment', + 3, + 10, + '', + 'style="min-height: 15px; padding:0; width: 100%; disabled"', + true + ); + + $comments_form .= '
'; + $comments_form .= html_print_button( + __('Add comment'), + 'comment_button', + false, + 'event_comment();', + 'class="sub next"', + true + ); + $comments_form .= '

'; + } else { + $comments_form = ui_print_message( + __('If event replication is ongoing, it won\'t be possible to enter comments here. This option is only to allow local pandora users to see comments, but not to operate with them. The operation, when event replication is enabled, must be done only in the Metaconsole.') + ); + } + + if ($ajax) { + return $comments_form.html_print_table($table_comments, true); + } + + return '
'.$comments_form.html_print_table($table_comments, true).'
'; +} + + +/** + * Retrieve event tags (cleaned). + * + * @param string $tags Tags. + * + * @return array of Tags. + */ +function events_clean_tags($tags) +{ + if (empty($tags)) { + return []; + } + + $event_tags = tags_get_tags_formatted($tags, false); + return explode(',', str_replace(' ', '', $event_tags)); +} + + +/** + * Get all the events happened in a group during a period of time. + * + * The returned events will be in the time interval ($date - $period, $date] + * + * @param mixed $id_group Group id to get events for. + * @param integer $period Period in seconds to get events. + * @param integer $date Beginning date to get events. + * @param boolean $filter_event_severity Filter_event_severity. + * @param boolean $filter_event_type Filter_event_type. + * @param boolean $filter_event_status Filter_event_status. + * @param boolean $filter_event_filter_search Filter_event_filter_search. + * @param boolean $dbmeta Dbmeta. + * + * @return array An array with all the events happened. + */ +function events_get_count_events_by_agent( + $id_group, + $period, + $date, + $filter_event_severity=false, + $filter_event_type=false, + $filter_event_status=false, + $filter_event_filter_search=false, + $dbmeta=false +) { + global $config; + + // Date. + if (!is_numeric($date)) { + $date = time_w_fixed_tz($date); + } + + if (empty($date)) { + $date = get_system_time(); + } + + // Group. + $id_group = groups_safe_acl($config['id_user'], $id_group, 'AR'); + + if (empty($id_group)) { + // An empty array means the user doesn't have access. + return false; + } + + $datelimit = ($date - $period); + + $sql_where = ''; + $severity_all = 0; + if (!empty($filter_event_severity)) { + foreach ($filter_event_severity as $key => $value) { + switch ($value) { + case -1: + $severity_all = 1; + break; + + case 34: + $filter_event_severity[$key] = '3, 4'; + break; + + case 20: + $filter_event_severity[$key] = '0, 1, 3, 4, 5, 6'; + break; + + case 21: + $filter_event_severity[$key] = '4, 2'; + break; + + default: + // Ignore. + break; + } + } + + if (!$severity_all) { + $sql_where .= ' AND criticity IN ('.implode(', ', $filter_event_severity).')'; + } + } + + $status_all = 0; + if (!empty($filter_event_status)) { + foreach ($filter_event_status as $key => $value) { + switch ($value) { + case -1: + $status_all = 1; + break; + + case 3: + $filter_event_status[$key] = ('0, 2'); + default: + // Ignore. + break; + } + } + + if (!$status_all) { + $sql_where .= ' AND estado IN ('.implode(', ', $filter_event_status).')'; + } + } + + if (!empty($filter_event_type) && $filter_event_type[0] != 'all') { + $sql_where .= ' AND ('; + $type = []; + foreach ($filter_event_type as $event_type) { + if ($event_type != '') { + // If normal, warning, could be several (going_up_warning, going_down_warning... too complex. + // Shown to user only "warning, critical and normal". + if ($event_type == 'warning' || $event_type == 'critical' || $event_type == 'normal') { + $type[] = " event_type LIKE '%".$event_type."%' "; + } else if ($event_type == 'not_normal') { + $type[] = " (event_type LIKE '%warning%' OR event_type LIKE '%critical%' OR event_type LIKE '%unknown%') "; + } else if ($event_type != 'all') { + $type[] = " event_type = '".$event_type."'"; + } + } + } + + $sql_where .= implode(' OR ', $type).')'; + } + + if (!empty($filter_event_filter_search)) { + $sql_where .= ' AND (evento LIKE "%'.io_safe_input($filter_event_filter_search).'%" OR id_evento LIKE "%'.io_safe_input($filter_event_filter_search).'%")'; + } + + $tagente = 'tagente'; + $tevento = 'tevento'; + + $sql = sprintf( + 'SELECT id_agente, + (SELECT t2.alias + FROM %s t2 + WHERE t2.id_agente = t3.id_agente) AS agent_name, + COUNT(*) AS count + FROM %s t3 + WHERE utimestamp > %d AND utimestamp <= %d + AND id_grupo IN (%s) + GROUP BY id_agente', + $tagente, + $tevento, + $datelimit, + $date, + implode(',', $id_group), + $sql_where + ); + + $rows = db_get_all_rows_sql($sql); + + if ($rows == false) { + $rows = []; + } + + $return = []; + foreach ($rows as $row) { + $agent_name = $row['agent_name']; + if (empty($row['agent_name'])) { + $agent_name = __('Pandora System'); + } + + $return[$agent_name] = $row['count']; + } + + return $return; +} + + +/** + * Get all the events happened in a group during a period of time. + * + * The returned events will be in the time interval ($date - $period, $date] + * + * @param array $filter Use target filter. + * @param integer $period Period in seconds to get events. + * @param integer $date Beginning date to get events. + * @param boolean $filter_event_severity Filter_event_severity. + * @param boolean $filter_event_type Filter_event_type. + * @param boolean $filter_event_status Filter_event_status. + * @param boolean $filter_event_filter_search Filter_event_filter_search. + * @param boolean $dbmeta Dbmeta. + * + * @return array An array with all the events happened. + */ +function events_get_count_events_validated_by_user( + $filter, + $period, + $date, + $filter_event_severity=false, + $filter_event_type=false, + $filter_event_status=false, + $filter_event_filter_search=false, + $dbmeta=false +) { + global $config; + // Group. + $sql_filter = ' AND 1=1 '; + if (isset($filter['id_group'])) { + $id_group = groups_safe_acl($config['id_user'], $filter['id_group'], 'AR'); + + if (empty($id_group)) { + // An empty array means the user doesn't have access. + return false; + } + + $sql_filter .= sprintf(' AND id_grupo IN (%s) ', implode(',', $id_group)); + } + + if (!empty($filter['id_agent'])) { + $sql_filter .= sprintf(' AND id_agente = %d ', $filter['id_agent']); + } + + if (!empty($filter['id_agentmodule'])) { + $sql_filter .= sprintf(' AND id_agentmodule = %d ', $filter['id_agentmodule']); + } + + // Date. + if (!is_numeric($date)) { + $date = time_w_fixed_tz($date); + } + + if (empty($date)) { + $date = get_system_time(); + } + + $datelimit = ($date - $period); + + $sql_where = ''; + $severity_all = 0; + if (!empty($filter_event_severity)) { + foreach ($filter_event_severity as $key => $value) { + switch ($value) { + case -1: + $severity_all = 1; + break; + + case 34: + $filter_event_severity[$key] = '3, 4'; + break; + + case 20: + $filter_event_severity[$key] = '0, 1, 3, 4, 5, 6'; + break; + + case 21: + $filter_event_severity[$key] = '4, 2'; + break; + + default: + // Ignore. + break; + } + } + + if (!$severity_all) { + $sql_where .= ' AND criticity IN ('.implode(', ', $filter_event_severity).')'; + } + } + + $status_all = 0; + if (!empty($filter_event_status)) { + foreach ($filter_event_status as $key => $value) { + switch ($value) { + case -1: + $status_all = 1; + break; + + case 3: + $filter_event_status[$key] = ('0, 2'); + default: + // Ignore. + break; + } + } + + if (!$status_all) { + $sql_where .= ' AND estado IN ('.implode(', ', $filter_event_status).')'; + } + } + + if (!empty($filter_event_type) && $filter_event_type[0] != 'all') { + $sql_where .= ' AND ('; + $type = []; + foreach ($filter_event_type as $event_type) { + if ($event_type != '') { + // If normal, warning, could be several (going_up_warning, going_down_warning... too complex. + // Shown to user only "warning, critical and normal". + if ($event_type == 'warning' || $event_type == 'critical' || $event_type == 'normal') { + $type[] = " event_type LIKE '%".$event_type."%' "; + } else if ($event_type == 'not_normal') { + $type[] = " (event_type LIKE '%warning%' OR event_type LIKE '%critical%' OR event_type LIKE '%unknown%') "; + } else if ($event_type != 'all') { + $type[] = " event_type = '".$event_type."'"; + } + } + } + + $sql_where .= implode(' OR ', $type).')'; + } + + if (!empty($filter_event_filter_search)) { + $sql_where .= ' AND (evento LIKE "%'.io_safe_input($filter_event_filter_search).'%" OR id_evento LIKE "%'.io_safe_input($filter_event_filter_search).'%")'; + } + + $tevento = 'tevento'; + + $sql = sprintf( + 'SELECT id_usuario, + (SELECT t2.fullname + FROM tusuario t2 + WHERE t2.id_user = t3.id_usuario) AS user_name, + COUNT(*) AS count + FROM %s t3 + WHERE utimestamp > %d AND utimestamp <= %d + %s %s + GROUP BY id_usuario', + $tevento, + $datelimit, + $date, + $sql_filter, + $sql_where + ); + $rows = db_get_all_rows_sql($sql); + + if ($rows == false) { + $rows = []; + } + + $return = []; + foreach ($rows as $row) { + $user_name = $row['user_name']; + if (empty($row['user_name'])) { + $user_name = __('Unknown'); + } + + $return[$user_name] = $row['count']; + } + + return $return; +} + + +/** + * Get all the events happened in a group during a period of time. + * + * The returned events will be in the time interval ($date - $period, $date] + * + * @param mixed $filter Target filter. + * @param integer $period Period in seconds to get events. + * @param integer $date Beginning date to get events. + * @param boolean $filter_event_severity Filter_event_severity. + * @param boolean $filter_event_type Filter_event_type. + * @param boolean $filter_event_status Filter_event_status. + * @param boolean $filter_event_filter_search Filter_event_filter_search. + * @param boolean $dbmeta Dbmeta. + * + * @return array An array with all the events happened. + */ +function events_get_count_events_by_criticity( + $filter, + $period, + $date, + $filter_event_severity=false, + $filter_event_type=false, + $filter_event_status=false, + $filter_event_filter_search=false, + $dbmeta=false +) { + global $config; + + $sql_filter = ' AND 1=1 '; + if (isset($filter['id_group'])) { + $id_group = groups_safe_acl($config['id_user'], $filter['id_group'], 'AR'); + + if (empty($id_group)) { + // An empty array means the user doesn't have access. + return false; + } + + $sql_filter .= sprintf(' AND id_grupo IN (%s) ', implode(',', $id_group)); + } + + if (!empty($filter['id_agent'])) { + $sql_filter .= sprintf(' AND id_agente = %d ', $filter['id_agent']); + } + + if (!empty($filter['id_agentmodule'])) { + $sql_filter .= sprintf(' AND id_agentmodule = %d ', $filter['id_agentmodule']); + } + + if (!is_numeric($date)) { + $date = time_w_fixed_tz($date); + } + + if (empty($date)) { + $date = get_system_time(); + } + + $datelimit = ($date - $period); + + $sql_where = ''; + $severity_all = 0; + if (!empty($filter_event_severity)) { + foreach ($filter_event_severity as $key => $value) { + switch ($value) { + case -1: + $severity_all = 1; + break; + + case 34: + $filter_event_severity[$key] = '3, 4'; + break; + + case 20: + $filter_event_severity[$key] = '0, 1, 3, 4, 5, 6'; + break; + + case 21: + $filter_event_severity[$key] = '4, 2'; + break; + + default: + // Ignore. + break; + } + } + + if (!$severity_all) { + $sql_where .= ' AND criticity IN ('.implode(', ', $filter_event_severity).')'; + } + } + + $status_all = 0; + if (!empty($filter_event_status)) { + foreach ($filter_event_status as $key => $value) { + switch ($value) { + case -1: + $status_all = 1; + break; + + case 3: + $filter_event_status[$key] = ('0, 2'); + break; + + default: + // Ignored. + break; + } + } + + if (!$status_all) { + $sql_where .= ' AND estado IN ('.implode(', ', $filter_event_status).')'; + } + } + + if (!empty($filter_event_type) && $filter_event_type[0] != 'all') { + $sql_where .= ' AND ('; + $type = []; + foreach ($filter_event_type as $event_type) { + if ($event_type != '') { + // If normal, warning, could be several (going_up_warning, going_down_warning... too complex. + // Shown to user only "warning, critical and normal". + if ($event_type == 'warning' || $event_type == 'critical' || $event_type == 'normal') { + $type[] = " event_type LIKE '%".$event_type."%' "; + } else if ($event_type == 'not_normal') { + $type[] = " (event_type LIKE '%warning%' OR event_type LIKE '%critical%' OR event_type LIKE '%unknown%') "; + } else if ($event_type != 'all') { + $type[] = " event_type = '".$event_type."'"; + } + } + } + + $sql_where .= implode(' OR ', $type).')'; + } + + if (!empty($filter_event_filter_search)) { + $sql_where .= ' AND (evento LIKE "%'.io_safe_input($filter_event_filter_search).'%" OR id_evento LIKE "%'.io_safe_input($filter_event_filter_search).'%")'; + } + + $tevento = 'tevento'; + + $sql = sprintf( + 'SELECT criticity, + COUNT(*) AS count + FROM %s + WHERE utimestamp > %d AND utimestamp <= %d + %s %s + GROUP BY criticity', + $tevento, + $datelimit, + $date, + $sql_filter, + $sql_where + ); + + $rows = db_get_all_rows_sql($sql); + + if ($rows == false) { + $rows = []; + } + + $return = []; + foreach ($rows as $row) { + $return[get_priority_name($row['criticity'])] = $row['count']; + } + + return $return; +} + + +/** + * Get all the events happened in a group during a period of time. + * + * The returned events will be in the time interval ($date - $period, $date] + * + * @param mixed $filter Target filter. + * @param integer $period Period in seconds to get events. + * @param integer $date Beginning date to get events. + * @param boolean $filter_event_severity Filter_event_severity. + * @param boolean $filter_event_type Filter_event_type. + * @param boolean $filter_event_status Filter_event_status. + * @param boolean $filter_event_filter_search Filter_event_filter_search. + * @param boolean $dbmeta Dbmeta. + * + * @return array An array with all the events happened. + */ +function events_get_count_events_validated( + $filter, + $period=null, + $date=null, + $filter_event_severity=false, + $filter_event_type=false, + $filter_event_status=false, + $filter_event_filter_search=false, + $dbmeta=false +) { + global $config; + + // Group. + $sql_filter = ' 1=1 '; + if (isset($filter['id_group'])) { + $id_group = groups_safe_acl( + $config['id_user'], + $filter['id_group'], + 'AR' + ); + + if (empty($id_group)) { + // An empty array means the user doesn't have access. + return false; + } + + $sql_filter .= sprintf( + ' AND id_grupo IN (%s) ', + implode(',', $id_group) + ); + } + + // Agent. + if (!empty($filter['id_agent'])) { + $sql_filter .= sprintf( + ' AND id_agente = %d ', + $filter['id_agent'] + ); + } + + // Module. + if (!empty($filter['id_agentmodule'])) { + $sql_filter .= sprintf( + ' AND id_agentmodule = %d ', + $filter['id_agentmodule'] + ); + } + + // Date. + if (!is_numeric($date)) { + $date = time_w_fixed_tz($date); + } + + if (empty($date)) { + $date = get_system_time(); + } + + $date_filter = ''; + if (!empty($date) && !empty($period)) { + $datelimit = ($date - $period); + + $date_filter .= sprintf( + ' AND utimestamp > %d AND utimestamp <= %d ', + $datelimit, + $date + ); + } else if (!empty($period)) { + $date = time(); + $datelimit = ($date - $period); + + $date_filter .= sprintf( + ' AND utimestamp > %d AND utimestamp <= %d ', + $datelimit, + $date + ); + } else if (!empty($date)) { + $date_filter .= sprintf(' AND utimestamp <= %d ', $date); + } + + $sql_where = ''; + $severity_all = 0; + if (!empty($filter_event_severity)) { + foreach ($filter_event_severity as $key => $value) { + switch ($value) { + case -1: + $severity_all = 1; + break; + + case 34: + $filter_event_severity[$key] = '3, 4'; + break; + + case 20: + $filter_event_severity[$key] = '0, 1, 3, 4, 5, 6'; + break; + + case 21: + $filter_event_severity[$key] = '4, 2'; + break; + + default: + // Ingore. + break; + } + } + + if (!$severity_all) { + $sql_where .= ' AND criticity IN ('.implode(', ', $filter_event_severity).')'; + } + } + + $status_all = 0; + if (!empty($filter_event_status)) { + foreach ($filter_event_status as $key => $value) { + switch ($value) { + case -1: + $status_all = 1; + break; + + case 3: + $filter_event_status[$key] = ('0, 2'); + break; + + default: + // Ignore. + break; + } + } + + if (!$status_all) { + $sql_where .= ' AND estado IN ('.implode(', ', $filter_event_status).')'; + } + } + + if (!empty($filter_event_type) && $filter_event_type[0] != 'all') { + $sql_where .= ' AND ('; + $type = []; + foreach ($filter_event_type as $event_type) { + if ($event_type != '') { + // If normal, warning, could be several (going_up_warning, going_down_warning... too complex. + // Shown to user only "warning, critical and normal". + if ($event_type == 'warning' || $event_type == 'critical' || $event_type == 'normal') { + $type[] = " event_type LIKE '%".$event_type."%' "; + } else if ($event_type == 'not_normal') { + $type[] = " (event_type LIKE '%warning%' OR event_type LIKE '%critical%' OR event_type LIKE '%unknown%') "; + } else if ($event_type != 'all') { + $type[] = " event_type = '".$event_type."'"; + } + } + } + + $sql_where .= implode(' OR ', $type).')'; + } + + if (!empty($filter_event_filter_search)) { + $sql_where .= ' AND (evento LIKE "%'.io_safe_input($filter_event_filter_search).'%" OR id_evento LIKE "%'.io_safe_input($filter_event_filter_search).'%")'; + } + + $tevento = 'tevento'; + + $sql = sprintf('SELECT estado, COUNT(*) AS count FROM %s WHERE %s %s GROUP BY estado', $tevento, $sql_filter, $sql_where); + + $rows = db_get_all_rows_sql($sql); + + if ($rows == false) { + $rows = []; + } + + $return = array_reduce( + $rows, + function ($carry, $item) { + $status = (int) $item['estado']; + $count = (int) $item['count']; + + if ($status === 1) { + $carry[__('Validated')] += $count; + } else if ($status === 0) { + $carry[__('Not validated')] += $count; + } + + return $carry; + }, + [ + __('Validated') => 0, + __('Not validated') => 0, + ] + ); + + return $return; +} + + +/** + * Check event tags. + * + * @param array $event_data Event. + * @param array $acltags Acl tags. + * + * @return boolean True or false. + */ +function events_checks_event_tags($event_data, $acltags) +{ + global $config; + + if (empty($acltags[$event_data['id_grupo']])) { + return true; + } else { + $tags_arr_acl = explode(',', $acltags[$event_data['id_grupo']]); + $tags_arr_event = explode(',', $event_data['tags']); + + foreach ($tags_arr_acl as $tag) { + $tag_name = tags_get_name($tag); + if (in_array($tag_name, $tags_arr_event)) { + return true; + } else { + $has_tag = false; + } + } + + if (!$has_tag) { + return false; + } + } + + return false; +} + + +/** + * Retrieves events grouped by agent. + * + * @param string $sql_post Sql_post. + * @param integer $offset Offset. + * @param integer $pagination Pagination. + * @param boolean $meta Meta. + * @param boolean $history History. + * @param boolean $total Total. + * + * @return array Data. + */ +function events_get_events_grouped_by_agent( + $sql_post, + $offset=0, + $pagination=1, + $meta=false, + $history=false, + $total=false +) { + global $config; + + $table = events_get_events_table($meta, $history); + + if ($meta) { + $fields_extra = ', agent_name, server_id'; + $groupby_extra = ', server_id'; + } else { + $groupby_extra = ''; + $fields_extra = ''; + } + + $event_lj = events_get_secondary_groups_left_join($table); + if ($total) { + $sql = 'SELECT COUNT(*) FROM (select id_agente from '.$table.' '.$event_lj.' WHERE 1=1 + '.$sql_post.' GROUP BY id_agente, event_type'.$groupby_extra.' ORDER BY id_agente ) AS t'; + } else { + $sql = 'select id_agente, count(*) as total'.$fields_extra.' from '.$table.' te '.$event_lj.' + WHERE id_agente > 0 '.$sql_post.' GROUP BY id_agente'.$groupby_extra.' ORDER BY id_agente LIMIT '.$offset.','.$pagination; + } + + $result = []; + // Extract the events by filter (or not) from db. + $events = db_get_all_rows_sql($sql); + $result = []; + + if ($events) { + foreach ($events as $event) { + if ($meta) { + $sql = 'SELECT event_type FROM '.$table.' te '.$event_lj." + WHERE agent_name = '".$event['agent_name']."' ".$sql_post.' ORDER BY utimestamp DESC '; + $resultado = db_get_row_sql($sql); + + $id_agente = $event['agent_name']; + $result[] = [ + 'total' => $event['total'], + 'id_server' => $event['server_id'], + 'id_agent' => $id_agente, + 'event_type' => $resultado['event_type'], + ]; + } else { + $sql = 'SELECT event_type FROM '.$table.' te '.$event_lj.' + WHERE id_agente = '.$event['id_agente'].' '.$sql_post.' ORDER BY utimestamp DESC '; + $resultado = db_get_row_sql($sql); + + $id_agente = $event['id_agente']; + $result[] = [ + 'total' => $event['total'], + 'id_agent' => $id_agente, + 'event_type' => $resultado['event_type'], + ]; + } + } + } + + return $result; +} + + +/** + * Return SQL query to group events by agents. + * + * @param mixed $id_agent Id_agent. + * @param integer $server_id Server_id. + * @param string $event_type Event_type. + * @param integer $severity Severity. + * @param integer $status Status. + * @param string $search Search. + * @param integer $id_agent_module Id_agent_module. + * @param integer $event_view_hr Event_view_hr. + * @param boolean $id_user_ack Id_user_ack. + * @param array $tag_with Tag_with. + * @param array $tag_without Tag_without. + * @param boolean $filter_only_alert Filter_only_alert. + * @param string $date_from Date_from. + * @param string $date_to Date_to. + * @param boolean $id_user Id_user. + * @param boolean $server_id_search Server_id_search. + * + * @return string SQL. + */ +function events_sql_events_grouped_agents( + $id_agent, + $server_id=-1, + $event_type='', + $severity=-1, + $status=3, + $search='', + $id_agent_module=0, + $event_view_hr=8, + $id_user_ack=false, + $tag_with=[], + $tag_without=[], + $filter_only_alert=false, + $date_from='', + $date_to='', + $id_user=false, + $server_id_search=false +) { + global $config; + + $sql_post = ' 1 = 1 '; + + $meta = false; + if (is_metaconsole()) { + $meta = true; + } + + switch ($status) { + case 0: + case 1: + case 2: + $sql_post .= ' AND estado = '.$status; + break; + + case 3: + $sql_post .= ' AND (estado = 0 OR estado = 2)'; + break; + + default: + // Ignore. + break; + } + + if ($search != '') { + $sql_post .= " AND (evento LIKE '%".io_safe_input($search)."%' OR id_evento LIKE '%".$search."%')"; + } + + if ($event_type != '') { + // If normal, warning, could be several (going_up_warning, going_down_warning... too complex + // Shown to user only "warning, critical and normal". + if ($event_type == 'warning' || $event_type == 'critical' || $event_type == 'normal') { + $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%') "; + } else if ($event_type != 'all') { + $sql_post .= " AND event_type = '".$event_type."'"; + } + } + + if ($severity != -1) { + switch ($severity) { + case EVENT_CRIT_WARNING_OR_CRITICAL: + $sql_post .= ' + AND (criticity = '.EVENT_CRIT_WARNING.' OR + criticity = '.EVENT_CRIT_CRITICAL.')'; + break; + + case EVENT_CRIT_OR_NORMAL: + $sql_post .= ' + AND (criticity = '.EVENT_CRIT_NORMAL.' OR + criticity = '.EVENT_CRIT_CRITICAL.')'; + break; + + case EVENT_CRIT_NOT_NORMAL: + $sql_post .= ' AND criticity != '.EVENT_CRIT_NORMAL; + break; + + default: + $sql_post .= ' AND criticity = '.$severity; + break; + } + } + + // In metaconsole mode the agent search is performed by name. + if ($meta) { + if ($id_agent != __('All')) { + $sql_post .= " AND agent_name LIKE '%".$id_agent."%'"; + } + } else { + switch ($id_agent) { + case 0: + // Ignore. + $__invalid_value = 1; + break; + + case -1: + // Agent doesnt exist. No results will returned. + $sql_post .= ' AND 1 = 0'; + break; + + default: + $sql_post .= ' AND id_agente = '.$id_agent; + break; + } + } + + // There is another filter for if ($meta). + if (!$meta) { + if (!empty($text_module)) { + $sql_post .= " AND id_agentmodule IN ( + SELECT id_agente_modulo + FROM tagente_modulo + WHERE nombre = '".$text_module."' + )"; + } + } + + if ($id_user_ack != '0') { + $sql_post .= " AND id_usuario = '".$id_user_ack."'"; + } + + if (!isset($date_from)) { + $date_from = ''; + } + + if (!isset($date_to)) { + $date_to = ''; + } + + if (($date_from == '') && ($date_to == '')) { + if ($event_view_hr > 0) { + $unixtime = (get_system_time() - ($event_view_hr * SECONDS_1HOUR)); + $sql_post .= ' AND (utimestamp > '.$unixtime.')'; + } + } else { + if ($date_from != '') { + $udate_from = strtotime($date_from.' 00:00:00'); + $sql_post .= ' AND (utimestamp >= '.$udate_from.')'; + } + + if ($date_to != '') { + $udate_to = strtotime($date_to.' 23:59:59'); + $sql_post .= ' AND (utimestamp <= '.$udate_to.')'; + } + } + + // Search by tag. + if (!empty($tag_with) && (io_safe_output($tag_with) != '[]') && (io_safe_output($tag_with) != '["0"]')) { + $sql_post .= ' AND ( '; + $first = true; + foreach ($tag_with as $id_tag) { + if ($first) { + $first = false; + } else { + $sql_post .= ' OR '; + } + + $sql_post .= "tags = '".tags_get_name($id_tag)."'"; + } + + $sql_post .= ' ) '; + } + + if (!empty($tag_without) && (io_safe_output($tag_without) != '[]') && (io_safe_output($tag_with) != '["0"]')) { + $sql_post .= ' AND ( '; + $first = true; + foreach ($tag_without as $id_tag) { + if ($first) { + $first = false; + } else { + $sql_post .= ' AND '; + } + + $sql_post .= "tags <> '".tags_get_name($id_tag)."'"; + } + + $sql_post .= ' ) '; + } + + // Filter/Only alerts. + if (isset($filter_only_alert)) { + if ($filter_only_alert == 0) { + $sql_post .= " AND event_type NOT LIKE '%alert%'"; + } else if ($filter_only_alert == 1) { + $sql_post .= " AND event_type LIKE '%alert%'"; + } + } + + // Tags ACLS. + if ($id_group > 0 && in_array($id_group, array_keys($groups))) { + $group_array = (array) $id_group; + } else { + $group_array = array_keys($groups); + } + + $tags_acls_condition = tags_get_acl_tags( + $id_user, + $group_array, + 'ER', + 'event_condition', + 'AND', + '', + $meta, + [], + true + ); + // FORCE CHECK SQL "(TAG = tag1 AND id_grupo = 1)". + if (($tags_acls_condition != ERR_WRONG_PARAMETERS) && ($tags_acls_condition != ERR_ACL) && ($tags_acls_condition != -110000)) { + $sql_post .= $tags_acls_condition; + } + + // Metaconsole filters. + if ($meta) { + if ($server_id_search) { + $sql_post .= ' AND server_id = '.$server_id_search; + } else { + $enabled_nodes = db_get_all_rows_sql( + ' + SELECT id + FROM tmetaconsole_setup + WHERE disabled = 0' + ); + + if (empty($enabled_nodes)) { + $sql_post .= ' AND 1 = 0'; + } else { + if ($strict_user == 1) { + $enabled_nodes_id = []; + } else { + $enabled_nodes_id = [0]; + } + + foreach ($enabled_nodes as $en) { + $enabled_nodes_id[] = $en['id']; + } + + $sql_post .= ' AND server_id IN ('.implode(',', $enabled_nodes_id).')'; + } + } + } + + return $sql_post; +} + + +/** + * Retrieve list of events grouped by agents. + * + * @param string $sql SQL. + * + * @return string HTML. + */ +function events_list_events_grouped_agents($sql) +{ + global $config; + + $table = events_get_events_table(is_metaconsole(), $history); + + $sql = sprintf( + 'SELECT * FROM %s + LEFT JOIN tagent_secondary_group + ON tagent_secondary_group.id_agent = id_agente + WHERE %s', + $table, + $sql + ); + + $result = db_get_all_rows_sql($sql); + $group_rep = 0; + $meta = is_metaconsole(); + + // Fields that the user has selected to show. + if ($meta) { + $show_fields = events_meta_get_custom_fields_user(); + } else { + $show_fields = explode(',', $config['event_fields']); + } + + // Headers. + $i = 0; + $table = new stdClass(); + if (!isset($table->width)) { + $table->width = '100%'; + } + + $table->id = 'eventtable'; + $table->cellpadding = 4; + $table->cellspacing = 4; + if (!isset($table->class)) { + $table->class = 'databox data'; + } + + $table->head = []; + $table->data = []; + + $table->head[$i] = __('ID'); + $table->align[$i] = 'left'; + $i++; + if (in_array('server_name', $show_fields)) { + $table->head[$i] = __('Server'); + $table->align[$i] = 'left'; + $i++; + } + + if (in_array('estado', $show_fields)) { + $table->head[$i] = __('Status'); + $table->align[$i] = 'left'; + $i++; + } + + if (in_array('id_evento', $show_fields)) { + $table->head[$i] = __('Event ID'); + $table->align[$i] = 'left'; + $i++; + } + + if (in_array('evento', $show_fields)) { + $table->head[$i] = __('Event Name'); + $table->align[$i] = 'left'; + $table->style[$i] = 'min-width: 200px; max-width: 350px; word-break: break-all;'; + $i++; + } + + if (in_array('id_agente', $show_fields)) { + $table->head[$i] = __('Agent name'); + $table->align[$i] = 'left'; + $table->style[$i] = 'max-width: 350px; word-break: break-all;'; + $i++; + } + + if (in_array('timestamp', $show_fields)) { + $table->head[$i] = __('Timestamp'); + $table->align[$i] = 'left'; + $i++; + } + + if (in_array('id_usuario', $show_fields)) { + $table->head[$i] = __('User'); + $table->align[$i] = 'left'; + $i++; + } + + if (in_array('owner_user', $show_fields)) { + $table->head[$i] = __('Owner'); + $table->align[$i] = 'left'; + $i++; + } + + if (in_array('id_grupo', $show_fields)) { + $table->head[$i] = __('Group'); + $table->align[$i] = 'left'; + $i++; + } + + if (in_array('event_type', $show_fields)) { + $table->head[$i] = __('Event type'); + $table->align[$i] = 'left'; + $table->style[$i] = 'min-width: 85px;'; + $i++; + } + + if (in_array('id_agentmodule', $show_fields)) { + $table->head[$i] = __('Agent Module'); + $table->align[$i] = 'left'; + $i++; + } + + if (in_array('id_alert_am', $show_fields)) { + $table->head[$i] = __('Alert'); + $table->align[$i] = 'left'; + $i++; + } + + if (in_array('criticity', $show_fields)) { + $table->head[$i] = __('Severity'); + $table->align[$i] = 'left'; + $i++; + } + + if (in_array('user_comment', $show_fields)) { + $table->head[$i] = __('Comment'); + $table->align[$i] = 'left'; + $i++; + } + + if (in_array('tags', $show_fields)) { + $table->head[$i] = __('Tags'); + $table->align[$i] = 'left'; + $i++; + } + + if (in_array('source', $show_fields)) { + $table->head[$i] = __('Source'); + $table->align[$i] = 'left'; + $i++; + } + + if (in_array('id_extra', $show_fields)) { + $table->head[$i] = __('Extra ID'); + $table->align[$i] = 'left'; + $i++; + } + + if (in_array('ack_utimestamp', $show_fields)) { + $table->head[$i] = __('ACK Timestamp'); + $table->align[$i] = 'left'; + $i++; + } + + if (in_array('instructions', $show_fields)) { + $table->head[$i] = __('Instructions'); + $table->align[$i] = 'left'; + $i++; + } + + if (in_array('data', $show_fields)) { + $table->head[$i] = __('Data'); + $table->align[$i] = 'left'; + $i++; + } + + if (in_array('module_status', $show_fields)) { + $table->head[$i] = __('Module status'); + $table->align[$i] = 'left'; + $i++; + } + + if ($i != 0 && $allow_action) { + $table->head[$i] = __('Action'); + $table->align[$i] = 'left'; + $table->size[$i] = '90px'; + $i++; + if (check_acl($config['id_user'], 0, 'EW') == 1 && !$readonly) { + $table->head[$i] = html_print_checkbox('all_validate_box', '1', false, true); + $table->align[$i] = 'left'; + } + } + + if ($meta) { + // Get info of the all servers to use it on hash auth. + $servers_url_hash = metaconsole_get_servers_url_hash(); + $servers = metaconsole_get_servers(); + } + + $show_delete_button = false; + $show_validate_button = false; + + $idx = 0; + // Arrange data. We already did ACL's in the query. + foreach ($result as $event) { + $data = []; + + if ($meta) { + $event['server_url_hash'] = $servers_url_hash[$event['server_id']]; + $event['server_url'] = $servers[$event['server_id']]['server_url']; + $event['server_name'] = $servers[$event['server_id']]['server_name']; + } + + // Clean url from events and store in array. + $event['clean_tags'] = events_clean_tags($event['tags']); + + // First pass along the class of this row. + $myclass = get_priority_class($event['criticity']); + + // Print status. + $estado = $event['estado']; + + // Colored box. + switch ($estado) { + case EVENT_NEW: + $img_st = 'images/star.png'; + $title_st = __('New event'); + break; + + case EVENT_VALIDATE: + $img_st = 'images/tick.png'; + $title_st = __('Event validated'); + break; + + case EVENT_PROCESS: + $img_st = 'images/hourglass.png'; + $title_st = __('Event in process'); + break; + + default: + // Ignore. + break; + } + + $i = 0; + + $data[$i] = '#'.$event['id_evento']; + $table->cellstyle[count($table->data)][$i] = 'background: #F3F3F3; color: #111 !important;'; + + // Pass grouped values in hidden fields to use it from modal window. + if ($group_rep) { + $similar_ids = $event['similar_ids']; + $timestamp_first = $event['timestamp_rep_min']; + $timestamp_last = $event['timestamp_rep']; + } else { + $similar_ids = $event['id_evento']; + $timestamp_first = $event['utimestamp']; + $timestamp_last = $event['utimestamp']; + } + + // Store group data to show in extended view. + $data[$i] .= html_print_input_hidden('similar_ids_'.$event['id_evento'], $similar_ids, true); + $data[$i] .= html_print_input_hidden('timestamp_first_'.$event['id_evento'], $timestamp_first, true); + $data[$i] .= html_print_input_hidden('timestamp_last_'.$event['id_evento'], $timestamp_last, true); + $data[$i] .= html_print_input_hidden('childrens_ids', json_encode($childrens_ids), true); + + // Store server id if is metaconsole. 0 otherwise. + if ($meta) { + $server_id = $event['server_id']; + + // If meta activated, propagate the id of the event on node (source id). + $data[$i] .= html_print_input_hidden('source_id_'.$event['id_evento'], $event['id_source_event'], true); + $table->cellclass[count($table->data)][$i] = $myclass; + } else { + $server_id = 0; + } + + $data[$i] .= html_print_input_hidden('server_id_'.$event['id_evento'], $server_id, true); + + if (empty($event['event_rep'])) { + $event['event_rep'] = 0; + } + + $data[$i] .= html_print_input_hidden('event_rep_'.$event['id_evento'], $event['event_rep'], true); + // Store concat comments to show in extended view. + $data[$i] .= html_print_input_hidden('user_comment_'.$event['id_evento'], base64_encode($event['user_comment']), true); + + $i++; + + if (in_array('server_name', $show_fields)) { + if ($meta) { + if (can_user_access_node()) { + $data[$i] = "".$event['server_name'].''; + } else { + $data[$i] = $event['server_name']; + } + } else { + $data[$i] = db_get_value('name', 'tserver'); + } + + $table->cellclass[count($table->data)][$i] = $myclass; + $i++; + } + + if (in_array('estado', $show_fields)) { + $data[$i] = html_print_image( + $img_st, + true, + [ + 'class' => 'image_status', + 'title' => $title_st, + 'id' => 'status_img_'.$event['id_evento'], + ] + ); + $table->cellstyle[count($table->data)][$i] = 'background: #F3F3F3;'; + $i++; + } + + if (in_array('id_evento', $show_fields)) { + $data[$i] = $event['id_evento']; + $table->cellclass[count($table->data)][$i] = $myclass; + $i++; + } + + switch ($event['criticity']) { + default: + case 0: + $img_sev = 'images/status_sets/default/severity_maintenance.png'; + break; + case 1: + $img_sev = 'images/status_sets/default/severity_informational.png'; + break; + + case 2: + $img_sev = 'images/status_sets/default/severity_normal.png'; + break; + + case 3: + $img_sev = 'images/status_sets/default/severity_warning.png'; + break; + + case 4: + $img_sev = 'images/status_sets/default/severity_critical.png'; + break; + + case 5: + $img_sev = 'images/status_sets/default/severity_minor.png'; + break; + + case 6: + $img_sev = 'images/status_sets/default/severity_major.png'; + break; + } + + if (in_array('evento', $show_fields)) { + // Event description. + $data[$i] = ''; + if ($allow_action) { + $data[$i] .= ''; + } + + $data[$i] .= ''.ui_print_truncate_text(io_safe_output($event['evento']), 160).''; + if ($allow_action) { + $data[$i] .= ''; + } + + $data[$i] .= ''; + $table->cellclass[count($table->data)][$i] = $myclass; + $i++; + } + + if (in_array('id_agente', $show_fields)) { + $data[$i] = ''; + + if ($event['id_agente'] > 0) { + // Agent name. + if ($meta) { + $agent_link = ''; + if (can_user_access_node()) { + $data[$i] = ''.$agent_link.$event['agent_name'].''; + } else { + $data[$i] = $event['agent_name']; + } + } else { + $data[$i] .= ui_print_agent_name($event['id_agente'], true); + } + } else { + $data[$i] .= ''; + } + + $data[$i] .= ''; + $table->cellclass[count($table->data)][$i] = $myclass; + $i++; + } + + if (in_array('timestamp', $show_fields)) { + // Time. + $data[$i] = ''; + if ($group_rep == 1) { + $data[$i] .= ui_print_timestamp($event['timestamp_rep'], true); + } else { + $data[$i] .= ui_print_timestamp($event['timestamp'], true); + } + + $data[$i] .= ''; + $table->cellclass[count($table->data)][$i] = $myclass; + $i++; + } + + if (in_array('id_usuario', $show_fields)) { + $user_name = db_get_value('fullname', 'tusuario', 'id_user', $event['id_usuario']); + if (empty($user_name)) { + $user_name = $event['id_usuario']; + } + + $data[$i] = $user_name; + $table->cellclass[count($table->data)][$i] = $myclass; + $i++; + } + + if (in_array('owner_user', $show_fields)) { + $owner_name = db_get_value('fullname', 'tusuario', 'id_user', $event['owner_user']); + if (empty($owner_name)) { + $owner_name = $event['owner_user']; + } + + $data[$i] = $owner_name; + $table->cellclass[count($table->data)][$i] = $myclass; + $i++; + } + + if (in_array('id_grupo', $show_fields)) { + if ($meta) { + $data[$i] = $event['group_name']; + } else { + $id_group = $event['id_grupo']; + $group_name = db_get_value('nombre', 'tgrupo', 'id_grupo', $id_group); + if ($id_group == 0) { + $group_name = __('All'); + } + + $data[$i] = $group_name; + } + + $table->cellclass[count($table->data)][$i] = $myclass; + $i++; + } + + if (in_array('event_type', $show_fields)) { + $data[$i] = events_print_type_description($event['event_type'], true); + $table->cellclass[count($table->data)][$i] = $myclass; + $i++; + } + + if (in_array('id_agentmodule', $show_fields)) { + if ($meta) { + $module_link = ''; + if (can_user_access_node()) { + $data[$i] = ''.$module_link.$event['module_name'].''; + } else { + $data[$i] = $event['module_name']; + } + } else { + $module_name = db_get_value('nombre', 'tagente_modulo', 'id_agente_modulo', $event['id_agentmodule']); + $data[$i] = ''.$module_name.''; + } + + $table->cellclass[count($table->data)][$i] = $myclass; + $i++; + } + + if (in_array('id_alert_am', $show_fields)) { + if ($meta) { + $data[$i] = $event['alert_template_name']; + } else { + if ($event['id_alert_am'] != 0) { + $sql = 'SELECT name + FROM talert_templates + WHERE id IN (SELECT id_alert_template + FROM talert_template_modules + WHERE id = '.$event['id_alert_am'].');'; + + $templateName = db_get_sql($sql); + $data[$i] = ''.$templateName.''; + } else { + $data[$i] = ''; + } + } + + $table->cellclass[count($table->data)][$i] = $myclass; + $i++; + } + + if (in_array('criticity', $show_fields)) { + $data[$i] = get_priority_name($event['criticity']); + $table->cellclass[count($table->data)][$i] = $myclass; + $i++; + } + + if (in_array('user_comment', $show_fields)) { + $safe_event_user_comment = strip_tags(io_safe_output($event['user_comment'])); + $line_breaks = [ + "\r\n", + "\n", + "\r", + ]; + $safe_event_user_comment = str_replace($line_breaks, '
', $safe_event_user_comment); + $event_user_comments = json_decode($safe_event_user_comment, true); + $event_user_comment_str = ''; + + if (!empty($event_user_comments)) { + $last_key = key(array_slice($event_user_comments, -1, 1, true)); + $date_format = $config['date_format']; + + foreach ($event_user_comments as $key => $event_user_comment) { + $event_user_comment_str .= sprintf( + '%s: %s
%s: %s
%s: %s
', + __('Date'), + date($date_format, $event_user_comment['utimestamp']), + __('User'), + $event_user_comment['id_user'], + __('Comment'), + $event_user_comment['comment'] + ); + if ($key != $last_key) { + $event_user_comment_str .= '
'; + } + } + } + + $comments_help_tip = ''; + if (!empty($event_user_comment_str)) { + $comments_help_tip = ui_print_help_tip($event_user_comment_str, true); + } + + $data[$i] = ''.$comments_help_tip.''; + $table->cellclass[count($table->data)][$i] = $myclass; + $i++; + } + + if (in_array('tags', $show_fields)) { + $data[$i] = tags_get_tags_formatted($event['tags']); + $table->cellclass[count($table->data)][$i] = $myclass; + $i++; + } + + if (in_array('source', $show_fields)) { + $data[$i] = $event['source']; + $table->cellclass[count($table->data)][$i] = $myclass; + $i++; + } + + if (in_array('id_extra', $show_fields)) { + $data[$i] = $event['id_extra']; + $table->cellclass[count($table->data)][$i] = $myclass; + $i++; + } + + if (in_array('ack_utimestamp', $show_fields)) { + if ($event['ack_utimestamp'] == 0) { + $data[$i] = ''; + } else { + $data[$i] = date($config['date_format'], $event['ack_utimestamp']); + } + + $table->cellclass[count($table->data)][$i] = $myclass; + $i++; + } + + if (in_array('instructions', $show_fields)) { + switch ($event['event_type']) { + case 'going_unknown': + if (!empty($event['unknown_instructions'])) { + $data[$i] = html_print_image('images/page_white_text.png', true, ['title' => str_replace("\n", '
', io_safe_output($event['unknown_instructions']))]); + } + break; + + case 'going_up_critical': + case 'going_down_critical': + if (!empty($event['critical_instructions'])) { + $data[$i] = html_print_image('images/page_white_text.png', true, ['title' => str_replace("\n", '
', io_safe_output($event['critical_instructions']))]); + } + break; + + case 'going_down_warning': + if (!empty($event['warning_instructions'])) { + $data[$i] = html_print_image('images/page_white_text.png', true, ['title' => str_replace("\n", '
', io_safe_output($event['warning_instructions']))]); + } + break; + + case 'system': + if (!empty($event['critical_instructions'])) { + $data[$i] = html_print_image('images/page_white_text.png', true, ['title' => str_replace("\n", '
', io_safe_output($event['critical_instructions']))]); + } else if (!empty($event['warning_instructions'])) { + $data[$i] = html_print_image('images/page_white_text.png', true, ['title' => str_replace("\n", '
', io_safe_output($event['warning_instructions']))]); + } else if (!empty($event['unknown_instructions'])) { + $data[$i] = html_print_image('images/page_white_text.png', true, ['title' => str_replace("\n", '
', io_safe_output($event['unknown_instructions']))]); + } + break; + + default: + // Ignore. + break; + } + + if (!isset($data[$i])) { + $data[$i] = ''; + } + + $table->cellclass[count($table->data)][$i] = $myclass; + $i++; + } + + if (in_array('data', $show_fields)) { + $data[$i] = $event['data']; + if (($data[$i] % 1) == 0) { + $data[$i] = number_format($data[$i], 0); + } else { + $data[$i] = number_format($data[$i], 2); + } + + $table->cellclass[count($table->data)][$i] = $myclass; + $i++; + } + + if (in_array('module_status', $show_fields)) { + $data[$i] = modules_get_modules_status($event['module_status']); + $table->cellclass[count($table->data)][$i] = $myclass; + $i++; + } + + if ($i != 0 && $allow_action) { + // Actions. + $data[$i] = ''; + + if (!$readonly) { + // Validate event. + if (($event['estado'] != 1) && (tags_checks_event_acl($config['id_user'], $event['id_grupo'], 'EW', $event['clean_tags'], $childrens_ids))) { + $show_validate_button = true; + $data[$i] .= ''; + $data[$i] .= html_print_image( + 'images/ok.png', + true, + ['title' => __('Validate event')] + ); + $data[$i] .= ''; + } + + // Delete event. + if ((tags_checks_event_acl($config['id_user'], $event['id_grupo'], 'EM', $event['clean_tags'], $childrens_ids) == 1)) { + if ($event['estado'] != 2) { + $show_delete_button = true; + $data[$i] .= ''; + $data[$i] .= html_print_image( + 'images/cross.png', + true, + [ + 'title' => __('Delete event'), + 'id' => 'delete_cross_'.$event['id_evento'], + ] + ); + $data[$i] .= ''; + } else { + $data[$i] .= html_print_image( + 'images/cross.disabled.png', + true, + ['title' => __('Is not allowed delete events in process')] + ).' '; + } + } + } + + $data[$i] .= ''; + $data[$i] .= html_print_input_hidden('event_title_'.$event['id_evento'], '#'.$event['id_evento'].' - '.$event['evento'], true); + $data[$i] .= html_print_image( + 'images/eye.png', + true, + ['title' => __('Show more')] + ); + $data[$i] .= ''; + + $table->cellstyle[count($table->data)][$i] = 'background: #F3F3F3;'; + + $i++; + + if (!$readonly) { + if (tags_checks_event_acl($config['id_user'], $event['id_grupo'], 'EM', $event['clean_tags'], $childrens_ids) == 1) { + // Checkbox. + // Class 'candeleted' must be the fist class to be parsed from javascript. Dont change. + $data[$i] = html_print_checkbox_extended('validate_ids[]', $event['id_evento'], false, false, false, 'class="candeleted chk_val"', true); + } else if (tags_checks_event_acl($config['id_user'], $event['id_grupo'], 'EW', $event['clean_tags'], $childrens_ids) == 1) { + // Checkbox. + $data[$i] = html_print_checkbox_extended('validate_ids[]', $event['id_evento'], false, false, false, 'class="chk_val"', true); + } else if (isset($table->header[$i]) || true) { + $data[$i] = ''; + } + } + + $table->cellstyle[count($table->data)][$i] = 'background: #F3F3F3;'; + } + + array_push($table->data, $data); + + $idx++; + } + + return html_print_table($table, true); +} + + +/** + * Retrieves SQL for custom order. + * + * @param string $sort_field Field. + * @param string $sort Order. + * @param integer $group_rep Group field. + * @param boolean $only-fields Return only fields. + * + * @return string SQL. + */ +function events_get_sql_order($sort_field='timestamp', $sort='DESC', $group_rep=0, $only_fields=false) +{ + $sort_field_translated = $sort_field; + switch ($sort_field) { + case 'event_id': + $sort_field_translated = 'id_evento'; + break; + + case 'event_name': + $sort_field_translated = 'evento'; + break; + + case 'status': + $sort_field_translated = 'estado'; + break; + + case 'agent_id': + $sort_field_translated = 'id_agente'; + break; + + case 'timestamp': + $sort_field_translated = ($group_rep == 0) ? 'timestamp' : 'timestamp_rep'; + break; + + case 'user_id': + $sort_field_translated = 'id_usuario'; + break; + + case 'owner': + $sort_field_translated = 'owner_user'; + break; + + case 'group_id': + $sort_field_translated = 'id_grupo'; + break; + + case 'alert_id': + $sort_field_translated = 'id_alert_am'; + break; + + case 'comment': + $sort_field_translated = 'user_comment'; + break; + + case 'extra_id': + $sort_field_translated = 'id_extra'; + break; + + default: + $sort_field_translated = $sort_field; + break; + } + + if (strtolower($sort) != 'asc' && strtolower($sort) != 'desc') { + $dir = ($sort == 'up') ? 'ASC' : 'DESC'; + } else { + $dir = $sort; + } + + if ($only_fields) { + return $sort_field_translated.' '.$dir; + } + + return 'ORDER BY '.$sort_field_translated.' '.$dir; +} + + +/** + * SQL left join of event queries to handle secondary groups. + * + * @param string $table Table to use based on environment. + * + * @return string With the query. + */ +function events_get_secondary_groups_left_join($table) +{ + if ($table == 'tevento') { + return 'LEFT JOIN tagent_secondary_group tasg ON te.id_agente = tasg.id_agent'; + } + + return 'LEFT JOIN tmetaconsole_agent_secondary_group tasg + ON te.id_agente = tasg.id_tagente AND te.server_id = tasg.id_tmetaconsole_setup'; +} + + +<<<<<<< HEAD +function events_get_instructions($event) +{ + if (!is_array($event)) { + return ''; + } + + switch ($event['event_type']) { + case 'going_unknown': + if ($event['unknown_instructions'] != '') { + $value = str_replace("\n", '
', io_safe_output($event['unknown_instructions'])); + } + break; + + case 'going_up_warning': + case 'going_down_warning': + if ($event['warning_instructions'] != '') { + $value = str_replace("\n", '
', io_safe_output($event['warning_instructions'])); + } + break; + + case 'going_up_critical': + case 'going_down_critical': + if ($event['critical_instructions'] != '') { + $value = str_replace("\n", '
', io_safe_output($event['critical_instructions'])); + } + break; + } + + if (!isset($value)) { + return ''; + } + + $max_text_length = 300; + $over_text = io_safe_output($value); + if (strlen($over_text) > ($max_text_length + 3)) { + $over_text = substr($over_text, 0, $max_text_length).'...'; + } + + $output = '
'; + $output .= ''; + $output .= ''; + $output .= html_print_image('images/default_list.png', true, ['title' => $over_text]).''; + $output .= ''; + + return $output; +======= +/** + * Replace macros in any string given an event id. + * If server_id > 0, it's a metaconsole query. + * + * @param integer $event_id Event identifier. + * @param integer $value String value in which we want to apply macros. + * + * @return string The response text with the macros applied. + */ +function events_get_field_value_by_event_id( + int $event_id, + $value +) { + global $config; + + $meta = false; + $event = db_get_row('tevento', 'id_evento', $event_id); + + // Replace each macro. + if (strpos($value, '_agent_address_') !== false) { + if ($meta) { + $agente_table_name = 'tmetaconsole_agent'; + $filter = [ + 'id_tagente' => $event['id_agente'], + 'id_tmetaconsole_setup' => $server_id, + ]; + } else { + $agente_table_name = 'tagente'; + $filter = ['id_agente' => $event['id_agente']]; + } + + $ip = db_get_value_filter('direccion', $agente_table_name, $filter); + // If agent does not have an IP, display N/A. + if ($ip === false) { + $ip = __('N/A'); + } + + $value = str_replace('_agent_address_', $ip, $value); + } + + if (strpos($value, '_agent_id_') !== false) { + $value = str_replace('_agent_id_', $event['id_agente'], $value); + } + + if (strpos($value, '_module_address_') !== false) { + if ($event['id_agentmodule'] != 0) { + if ($meta) { + $server = metaconsole_get_connection_by_id($server_id); + metaconsole_connect($server); + } + + $module = db_get_row('tagente_modulo', 'id_agente_modulo', $event['id_agentmodule']); + if (empty($module['ip_target'])) { + $module['ip_target'] = __('N/A'); + } + + $value = str_replace('_module_address_', $module['ip_target'], $value); + if (empty($module['nombre'])) { + $module['nombre'] = __('N/A'); + } + + if ($meta) { + metaconsole_restore_db(); + } + } else { + $value = str_replace('_module_address_', __('N/A'), $value); + } + } + + if (strpos($value, '_module_name_') !== false) { + if ($event['id_agentmodule'] != 0) { + if ($meta) { + $server = metaconsole_get_connection_by_id($server_id); + metaconsole_connect($server); + } + + $module = db_get_row('tagente_modulo', 'id_agente_modulo', $event['id_agentmodule']); + if (empty($module['ip_target'])) { + $module['ip_target'] = __('N/A'); + } + + $value = str_replace( + '_module_name_', + io_safe_output($module['nombre']), + $value + ); + + if ($meta) { + metaconsole_restore_db(); + } + } else { + $value = str_replace('_module_name_', __('N/A'), $value); + } + } + + if (strpos($value, '_event_id_') !== false) { + $value = str_replace('_event_id_', $event['id_evento'], $value); + } + + if (strpos($value, '_user_id_') !== false) { + if (!empty($event['id_usuario'])) { + $value = str_replace('_user_id_', $event['id_usuario'], $value); + } else { + $value = str_replace('_user_id_', __('N/A'), $value); + } + } + + if (strpos($value, '_group_id_') !== false) { + $value = str_replace('_group_id_', $event['id_grupo'], $value); + } + + if (strpos($value, '_group_name_') !== false) { + $value = str_replace( + '_group_name_', + groups_get_name($event['id_grupo'], true), + $value + ); + } + + if (strpos($value, '_event_utimestamp_') !== false) { + $value = str_replace( + '_event_utimestamp_', + $event['utimestamp'], + $value + ); + } + + if (strpos($value, '_event_date_') !== false) { + $value = str_replace( + '_event_date_', + date($config['date_format'], $event['utimestamp']), + $value + ); + } + + if (strpos($value, '_event_text_') !== false) { + $value = str_replace( + '_event_text_', + events_display_name($event['evento']), + $value + ); + } + + if (strpos($value, '_event_type_') !== false) { + $value = str_replace( + '_event_type_', + events_print_type_description($event['event_type'], true), + $value + ); + } + + if (strpos($value, '_alert_id_') !== false) { + $value = str_replace( + '_alert_id_', + empty($event['is_alert_am']) ? __('N/A') : $event['is_alert_am'], + $value + ); + } + + if (strpos($value, '_event_severity_id_') !== false) { + $value = str_replace('_event_severity_id_', $event['criticity'], $value); + } + + if (strpos($value, '_event_severity_text_') !== false) { + $value = str_replace( + '_event_severity_text_', + get_priority_name($event['criticity']), + $value + ); + } + + if (strpos($value, '_module_id_') !== false) { + $value = str_replace('_module_id_', $event['id_agentmodule'], $value); + } + + if (strpos($value, '_event_tags_') !== false) { + $value = str_replace('_event_tags_', $event['tags'], $value); + } + + if (strpos($value, '_event_extra_id_') !== false) { + if (empty($event['id_extra'])) { + $value = str_replace('_event_extra_id_', __('N/A'), $value); + } else { + $value = str_replace('_event_extra_id_', $event['id_extra'], $value); + } + } + + if (strpos($value, '_event_source_') !== false) { + $value = str_replace('_event_source_', $event['source'], $value); + } + + if (strpos($value, '_event_instruction_') !== false) { + $value = str_replace( + '_event_instruction_', + events_display_instructions($event['event_type'], $event, false), + $value + ); + } + + if (strpos($value, '_owner_user_') !== false) { + if (empty($event['owner_user'])) { + $value = str_replace('_owner_user_', __('N/A'), $value); + } else { + $value = str_replace('_owner_user_', $event['owner_user'], $value); + } + } + + if (strpos($value, '_event_status_') !== false) { + $event_st = events_display_status($event['estado']); + $value = str_replace('_event_status_', $event_st['title'], $value); + } + + if (strpos($value, '_group_custom_id_') !== false) { + $group_custom_id = db_get_value_sql( + sprintf( + 'SELECT custom_id FROM tgrupo WHERE id_grupo=%s', + $event['id_grupo'] + ) + ); + $event_st = events_display_status($event['estado']); + $value = str_replace('_group_custom_id_', $group_custom_id, $value); + } + + // Parse the event custom data. + if (!empty($event['custom_data'])) { + $custom_data = json_decode(base64_decode($event['custom_data'])); + foreach ($custom_data as $key => $val) { + $value = str_replace('_customdata_'.$key.'_', $val, $value); + } + } + + // This will replace the macro with the current logged user. + if (strpos($value, '_current_user_') !== false) { + $value = str_replace('_current_user_', $config['id_user'], $value); + } + + return $value; + +>>>>>>> origin/develop +} diff --git a/pandora_console/include/functions_graph.php b/pandora_console/include/functions_graph.php index d0e844c3bf..e44b93a97b 100644 --- a/pandora_console/include/functions_graph.php +++ b/pandora_console/include/functions_graph.php @@ -302,6 +302,15 @@ function grafico_modulo_sparse_data( $array_data['sum'.$series_suffix]['agent_alias'] = $data_module_graph['agent_alias']; $array_data['sum'.$series_suffix]['unit'] = $data_module_graph['unit']; + if ($params['percentil']) { + $array_data['percentil'.$series_suffix]['agent_module_id'] = $agent_module_id; + $array_data['percentil'.$series_suffix]['id_module_type'] = $data_module_graph['id_module_type']; + $array_data['percentil'.$series_suffix]['agent_name'] = $data_module_graph['agent_name']; + $array_data['percentil'.$series_suffix]['module_name'] = $data_module_graph['module_name']; + $array_data['percentil'.$series_suffix]['agent_alias'] = $data_module_graph['agent_alias']; + $array_data['percentil'.$series_suffix]['unit'] = $data_module_graph['unit']; + } + // This is for a specific type of report that consists in passing // an interval and doing the average sum and avg. if ($params['force_interval'] != '') { @@ -378,12 +387,6 @@ function grafico_modulo_sparse_data( $array_data['sum1']['data'] = $acum_array_data; } - if ($params['percentil']) { - $percentil_value = $array_data['percentil'.$series_suffix]['data'][0][1]; - } else { - $percentil_value = 0; - } - $events = []; if (isset($array_data['sum'.$series_suffix]['max'])) { $max = $array_data['sum'.$series_suffix]['max']; @@ -1471,8 +1474,6 @@ function graphic_combined_module( $min = $array_data['sum'.$i]['min']; $avg = $array_data['sum'.$i]['avg']; - $percentil_value = $array_data['percentil'.$i]['data'][0][1]; - if ($config['fixed_graph'] == false) { $water_mark = [ 'file' => $config['homedir'].'/images/logo_vertical_water.png', @@ -1521,7 +1522,8 @@ function graphic_combined_module( $array_data, $params_combined['average'], $params_combined['summatory'], - $params_combined['modules_series'] + $params_combined['modules_series'], + $date_array ); } } @@ -2222,7 +2224,7 @@ function graphic_combined_module( * @param boolean $average Average. * @param boolean $summatory Summatory. * @param boolean $modules_series Series module. - * @param boolean $baseline Baseline data. + * @param array $date_array Date data. * * @return array Data. */ @@ -2231,106 +2233,82 @@ function combined_graph_summatory_average( $average=false, $summatory=false, $modules_series=false, - $baseline=false + $date_array=[] ) { if (isset($array_data) && is_array($array_data)) { + $reduce_array = []; foreach ($array_data as $key => $value) { if (strpos($key, 'sum') !== false) { - $data_array_reverse[$key] = array_reverse($value['data']); - if (!$modules_series) { - unset($array_data[$key]); - } + $last = $date_array['start_date']; + $reduce_array = array_reduce( + $value['data'], + function ($carry, $item) use ($date_array, $last, $reduce_array) { + $slice_start = $date_array['start_date']; + $iterator = $last; + + // JS to PHP timestamp format. + $item[0] /= 1000; + while ($iterator <= $date_array['final_date']) { + if ($item[0] >= $slice_start && $item[0] < $iterator) { + $array = []; + $val = 0; + $n = 0; + + if (is_array($reduce_array[$slice_start])) { + $val = $reduce_array[$slice_start]['value']; + $n = ($reduce_array[$slice_start]['n'] + 1); + } + + $array['value'] = ($item[1] + $val); + $array['n'] = $n; + $array['t'] = ($slice_start * 1000); + + $carry[$slice_start] = $array; + $last = $iterator; + break; + } else { + $slice_start = $iterator; + $iterator += 300; + } + } + + $i++; + return $carry; + }, + $reduce_array + ); + } + + if (!$modules_series) { + unset($array_data[$key]); } } - if (isset($data_array_reverse) && is_array($data_array_reverse)) { - $array_sum_reverse = []; - $array_avg_reverse = []; - $data_array_prev = false; - $data_array_pop = []; - $count = 0; + $reduce_array_summatory = []; + $reduce_array_average = []; + $i = 0; + foreach ($reduce_array as $item) { + $reduce_array_summatory[$i][0] = $item['t']; + $reduce_array_summatory[$i][1] = $item['value']; - $count_data_array_reverse = count($data_array_reverse['sum0']); - while ($count_data_array_reverse > 0) { - foreach ($data_array_reverse as $key_reverse => $value_reverse) { - if (is_array($value_reverse) && count($value_reverse) > 0) { - $data_array_pop[$key_reverse] = array_pop( - $data_array_reverse[$key_reverse] - ); - } - } + $reduce_array_average[$i][0] = $item['t']; + $reduce_array_average[$i][1] = ($item['value'] / ($item['n'] + 1)); - if (isset($data_array_pop) && is_array($data_array_pop)) { - $acum_data = 0; - $acum_array = []; - $sum_data = 0; - $count_pop = 0; - foreach ($data_array_pop as $key_pop => $value_pop) { - if ($value_pop[0] > $acum_data) { - if ($acum_data != 0) { - $sum_data = ($sum_data + $data_array_prev[$key_pop][1]); - $data_array_reverse[$key_pop][] = $value_pop; - $data_array_prev[$acum_key] = $acum_array; - } else { - if ($data_array_prev[$key_pop] == false) { - $data_array_prev[$key_pop] = $value_pop; - } + $i++; + } - $acum_key = $key_pop; - $acum_data = $value_pop[0]; - $acum_array = $value_pop; - $sum_data = $value_pop[1]; - } - } else if ($value_pop[0] < $acum_data) { - $sum_data = ($sum_data + $data_array_prev[$key_pop][1]); - $data_array_reverse[$acum_key][] = $acum_array; - $data_array_prev[$key_pop] = $value_pop; - $acum_key = $key_pop; - $acum_data = $value_pop[0]; - $acum_array = $value_pop; - } else if ($value_pop[0] == $acum_data) { - $data_array_prev[$key_pop] = $value_pop; - $sum_data += $value_pop[1]; - } + if ($summatory && isset($reduce_array_summatory) + && is_array($reduce_array_summatory) + && count($reduce_array_summatory) > 0 + ) { + $array_data['summatory']['data'] = $reduce_array_summatory; + } - $count_pop++; - } - - if ($summatory) { - $array_sum_reverse[$count][0] = $acum_data; - $array_sum_reverse[$count][1] = $sum_data; - } - - if ($average) { - $array_avg_reverse[$count][0] = $acum_data; - $array_avg_reverse[$count][1] = ($sum_data / $count_pop); - } - } - - $count++; - $count_data_array_reverse--; - } - - if ($summatory && isset($array_sum_reverse) - && is_array($array_sum_reverse) - && count($array_sum_reverse) > 0 - ) { - $array_data['summatory']['data'] = $array_sum_reverse; - $array_data['summatory']['color'] = 'purple'; - } - - if ($average && isset($array_avg_reverse) - && is_array($array_avg_reverse) - && count($array_avg_reverse) > 0 - ) { - if ($baseline) { - $array_data['baseline']['data'] = $array_avg_reverse; - $array_data['baseline']['color'] = 'green'; - } else { - $array_data['average']['data'] = $array_avg_reverse; - $array_data['average']['color'] = 'orange'; - } - } + if ($average && isset($reduce_array_average) + && is_array($reduce_array_average) + && count($reduce_array_average) > 0 + ) { + $array_data['average']['data'] = $reduce_array_average; } return $array_data; diff --git a/pandora_console/include/functions_html.php b/pandora_console/include/functions_html.php index 5fa4824a1e..ab3aad9c7a 100644 --- a/pandora_console/include/functions_html.php +++ b/pandora_console/include/functions_html.php @@ -1475,6 +1475,14 @@ function html_print_input_password( $attr['class'] = $class; } + if ($disabled === false) { + // Trick to avoid password completion on most browsers. + if ($autocomplete !== 'on') { + $disabled = true; + $attr['onfocus'] = "this.removeAttribute('readonly');"; + } + } + return html_print_input_text_extended($name, $value, 'password-'.$name, $alt, $size, $maxlength, $disabled, '', $attr, $return, true, '', $autocomplete); } @@ -3375,3 +3383,116 @@ function html_print_input($data, $wrapper='div', $input_only=false) return $output; } + + +/** + * Print an autocomplete input filled out with Integria IMS users. + * + * @param string $name The name of ajax control, by default is "users". + * @param string $default The default value to show in the ajax control. + * @param boolean $return If it is true return a string with the output instead to echo the output. + * @param string $size Size. + * + * @return mixed If the $return is true, return the output as string. + */ +function html_print_autocomplete_users_from_integria( + $name='users', + $default='', + $return=false, + $size='30', + $disable=false, + $required=false +) { + global $config; + + ob_start(); + + $attrs = ['style' => 'background: url(images/user_green.png) no-repeat right;']; + + if ($required) { + $attrs['required'] = 'required'; + } + + html_print_input_text_extended( + $name, + $default, + 'text-'.$name, + '', + $size, + 100, + $disable, + '', + $attrs + ); + html_print_input_hidden($name.'_hidden', $id_agent_module); + + ui_print_help_tip(__('Type at least two characters to search the user.'), false); + + $javascript_ajax_page = ui_get_full_url('ajax.php', false, false, false, false); + ?> + + '.html_print_image('images/setup.png', true, ['title' => __('Configure Integria IMS')]).''; + $list_tab['text'] = ''.html_print_image('images/list.png', true, ['title' => __('Ticket list')]).''; + $create_tab['text'] = ''.html_print_image('images/pencil.png', true, ['title' => __('New ticket')]).''; + + switch ($active_tab) { + case 'setup_tab': + $setup_tab['active'] = true; + $list_tab['active'] = false; + $create_tab['active'] = false; + break; + + case 'list_tab': + $setup_tab['active'] = false; + $list_tab['active'] = true; + $create_tab['active'] = false; + break; + + case 'create_tab': + $setup_tab['active'] = false; + $list_tab['active'] = false; + $create_tab['active'] = true; + break; + + default: + $setup_tab['active'] = false; + $list_tab['active'] = false; + $create_tab['active'] = false; + break; + } + + if ($view) { + $create_tab['text'] = ''.html_print_image('images/pencil.png', true, ['title' => __('Edit ticket')]).''; + $view_tab['text'] = ''.html_print_image('images/operation.png', true, ['title' => __('View ticket')]).''; + // When the current page is the View page. + if (!$active_tab) { + $view_tab['active'] = true; + } + } + + $onheader = []; + + if (check_acl($config['id_user'], 0, 'IR') && $view) { + $onheader['view'] = $view_tab; + } + + if (check_acl($config['id_user'], 0, 'PM')) { + $onheader['configure'] = $setup_tab; + } + + if (check_acl($config['id_user'], 0, 'IR')) { + $onheader['list'] = $list_tab; + } + + if (check_acl($config['id_user'], 0, 'IW')) { + $onheader['create'] = $create_tab; + } + + return $onheader; +} + + +/** + * Gets all the details of Integria IMS API + * + * @param string $details Type of API call. + * @param number $detail_index Send index if you want return the text. + * + * @return string or array with result of API call. + */ +function integriaims_get_details($details, $detail_index=false) +{ + global $config; + + switch ($details) { + case 'status': + $operation = 'get_incidents_status'; + break; + + case 'group': + $operation = 'get_groups'; + break; + + case 'priority': + $operation = 'get_incident_priorities'; + break; + + case 'resolution': + $operation = 'get_incidents_resolutions'; + break; + + case 'type': + $operation = 'get_types'; + break; + + default: + // code... + break; + } + + $api_call = integria_api_call($config['integria_hostname'], $config['integria_user'], $config['integria_pass'], $config['integria_api_pass'], $operation); + $result = []; + get_array_from_csv_data_pair($api_call, $result); + + if ($detail_index !== false) { + if ($result[$detail_index] == '' || $result[$detail_index] === null) { + return __('None'); + } else { + return $result[$detail_index]; + } + } else { + return $result; + } +} + + +/** + * Perform an API call to Integria IMS. + * + * @param string API host URL. + * @param string User name. + * @param string User password. + * @param string API password. + * @param string API Operation. + * @param array Array with parameters required by the API function. + * + * @return boolean True if API request succeeded, false if API request failed. + */ +function integria_api_call($api_hostname, $user, $user_pass, $api_pass, $operation, $params_array=[], $show_credentials_error_msg=false) +{ + $params_string = implode(',', $params_array); + + $url_data = [ + 'user' => $user, + 'user_pass' => $user_pass, + 'pass' => $api_pass, + 'op' => $operation, + 'params' => html_entity_decode($params_string), + ]; + + // Build URL for API request. + $url = $api_hostname.'/integria/include/api.php'; + + // ob_start(); + // $out = fopen('php://output', 'w'); + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_POSTFIELDS, $url_data); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_VERBOSE, true); + curl_setopt($ch, CURLOPT_STDERR, $out); + $result = curl_exec($ch); + + // fclose($out); + // $debug = ob_get_clean(); + $http_status = curl_getinfo($ch, CURLINFO_HTTP_CODE); + + $error = false; + + if ($result === false) { + $error = curl_error($ch); + } + + curl_close($ch); + + if ($error === true || $http_status !== 200) { + if ($show_credentials_error_msg === true) { + ui_print_error_message(__('API request failed. Please check Integria IMS\' access credentials in Pandora setup.')); + } + + return false; + } else { + return $result; + } +} + + +// Parse CSV consisting of one or more lines of the form key-value pair into an array. +function get_array_from_csv_data_pair($csv_data, &$array_values) +{ + $csv_array = explode("\n", $csv_data); + + foreach ($csv_array as $csv_value) { + if (empty($csv_value)) { + continue; + } + + $new_csv_value = str_getcsv($csv_value); + + $array_values[$new_csv_value[0]] = $new_csv_value[1]; + } +} + + +/** + * Parse CSV consisting of all lines into an array. + * + * @param string $csv_data Data returned of csv api call. + * @param string $array_values Returned array. + * @param array $index Array to create an associative index (opcional). + */ +function get_array_from_csv_data_all($csv_data, &$array_values, $index=false) +{ + $csv_array = explode("\n", $csv_data); + + foreach ($csv_array as $csv_value) { + if (empty($csv_value)) { + continue; + } + + $new_csv_value = str_getcsv($csv_value); + + if ($index !== false) { + foreach ($new_csv_value as $key => $value) { + $new_csv_value_index[$index[$key]] = str_replace(':::', ',', $value); + ; + } + + $array_values[$new_csv_value[0]] = $new_csv_value_index; + } else { + $new_csv_value_comma = array_map( + function ($item) { + return str_replace(':::', ',', $item); + }, + $new_csv_value + ); + $array_values[$new_csv_value[0]] = $new_csv_value_comma; + } + } +} + + +/** + * Print priority for Integria IMS with colors. + * + * @param string $priority value of priority in Integria IMS. + * @param string $priority_label text shown in color box. + * + * @return HTML code to print the color box. + */ +function ui_print_integria_incident_priority($priority, $priority_label) +{ + global $config; + + $output = ''; + switch ($priority) { + case 0: + $color = COL_UNKNOWN; + break; + + case 1: + $color = COL_NORMAL; + break; + + case 10: + $color = COL_NOTINIT; + break; + + case 2: + $color = COL_WARNING; + break; + + case 3: + $color = COL_ALERTFIRED; + break; + + case 4: + $color = COL_CRITICAL; + break; + } + + $output = '
'; + $output .= $priority_label; + $output .= '
'; + + return $output; +} + + +/** + * Get tickets from Integria IMS. + * + * @param array $tickets_filters Filters to send to API. + * + * @return array Tickets returned by API call. + */ +function get_tickets_integriaims($tickets_filters) +{ + global $config; + + // Filters. + $incident_text = $tickets_filters['incident_text']; + $incident_status = $tickets_filters['incident_status']; + $incident_group = $tickets_filters['incident_group']; + $incident_owner = $tickets_filters['incident_owner']; + $incident_creator = $tickets_filters['incident_creator']; + $incident_priority = $tickets_filters['incident_priority']; + $incident_resolution = $tickets_filters['incident_resolution']; + $created_from = $tickets_filters['created_from']; + $created_to = $tickets_filters['created_to']; + + // API call. + $result_api_call_list = integria_api_call( + $config['integria_hostname'], + $config['integria_user'], + $config['integria_pass'], + $config['integria_api_pass'], + 'get_incidents', + [ + $incident_text, + $incident_status, + $incident_group, + $incident_priority, + '0', + $incident_owner, + $incident_creator, + ] + ); + + // Return array of api call 'get_incidents'. + $array_get_incidents = []; + get_array_from_csv_data_all($result_api_call_list, $array_get_incidents); + + // Modify $array_get_incidents if filter for resolution exists. + $filter_resolution = []; + foreach ($array_get_incidents as $key => $value) { + if ($incident_resolution !== '' && ($array_get_incidents[$key][12] == $incident_resolution)) { + $filter_resolution[$key] = $array_get_incidents[$key]; + continue; + } + } + + if ($incident_resolution !== '') { + $array_get_incidents = $filter_resolution; + } + + // Modify $array_get_incidents if filter for date is selected. + if ($created_from !== '' && $created_to !== '') { + $date = []; + $date_utimestamp = []; + foreach ($array_get_incidents as $key => $value) { + // Change format date / to -. + $date[$key] = date('Y-m-d', strtotime($array_get_incidents[$key][9])); + // Covert date to utimestamp. + $date_utimestamp[$key] = strtotime($date[$key]); + } + + // Change format date / to -. + $created_from_date = date('Y-m-d', strtotime($created_from)); + $created_to_date = date('Y-m-d', strtotime($created_to)); + + // Covert date to utimestamp. + $created_from_timestamp = strtotime($created_from_date); + $created_to_timestamp = strtotime($created_to_date); + + // Dates within the selected period. + $selected_period = array_filter( + $date_utimestamp, + function ($value) use ($created_from_timestamp, $created_to_timestamp) { + return ($value >= $created_from_timestamp && $value <= $created_to_timestamp); + } + ); + + // Return incidents with the correct dates. + $filter_date = []; + foreach ($array_get_incidents as $key => $value) { + foreach ($selected_period as $index => $value) { + if ($array_get_incidents[$key][0] == $index) { + $filter_date[$key] = $array_get_incidents[$key]; + continue; + } + } + } + + $array_get_incidents = $filter_date; + } + + return $array_get_incidents; +} diff --git a/pandora_console/include/functions_io.php b/pandora_console/include/functions_io.php index 32c66ca1e3..baddb18531 100755 --- a/pandora_console/include/functions_io.php +++ b/pandora_console/include/functions_io.php @@ -507,7 +507,7 @@ function ___($string /*, variable arguments */) } -/* +/** * json_encode for multibyte characters. * * @param string Text string to be encoded. @@ -528,7 +528,7 @@ function io_json_mb_encode($string, $encode_options=0) } -/* +/** * Prepare the given password to be stored in the Pandora FMS Database, * encrypting it if necessary. * @@ -541,16 +541,22 @@ function io_input_password($password) global $config; enterprise_include_once('include/functions_crypto.php'); - $ciphertext = enterprise_hook('openssl_encrypt_decrypt', ['encrypt', io_safe_output($password)]); + $ciphertext = enterprise_hook( + 'openssl_encrypt_decrypt', + [ + 'encrypt', + io_safe_input($password), + ] + ); if ($ciphertext === ENTERPRISE_NOT_HOOK) { - return $password; + return io_safe_input($password); } return $ciphertext; } -/* +/** * Process the given password read from the Pandora FMS Database, * decrypting it if necessary. * @@ -563,10 +569,17 @@ function io_output_password($password) global $config; enterprise_include_once('include/functions_crypto.php'); - $plaintext = enterprise_hook('openssl_encrypt_decrypt', ['decrypt', io_safe_output($password)]); + $plaintext = enterprise_hook( + 'openssl_encrypt_decrypt', + [ + 'decrypt', + $password, + ] + ); + if ($plaintext === ENTERPRISE_NOT_HOOK) { - return $password; + return io_safe_output($password); } - return $plaintext; + return io_safe_output($plaintext); } diff --git a/pandora_console/include/gettext.php b/pandora_console/include/gettext.php index 152040f327..5d3abd83fe 100644 --- a/pandora_console/include/gettext.php +++ b/pandora_console/include/gettext.php @@ -104,7 +104,7 @@ class gettext_reader { * @param boolean enable_cache Enable or disable caching of strings (default on) */ function gettext_reader($Reader, $enable_cache = true) { - $machine = @shell_exec('uname -m'); + $machine = php_uname("m"); $enabled64Bits = false; if (preg_match('/64/', $machine)) { diff --git a/pandora_console/include/graphs/flot/jquery.flot.exportdata.pandora.js b/pandora_console/include/graphs/flot/jquery.flot.exportdata.pandora.js index 5bc3ef32b1..0b745d8ff5 100644 --- a/pandora_console/include/graphs/flot/jquery.flot.exportdata.pandora.js +++ b/pandora_console/include/graphs/flot/jquery.flot.exportdata.pandora.js @@ -159,10 +159,9 @@ if (custom_graph) { dataObject = retrieveDataOject(dataObjects,0); - //dataObjects.forEach(function (element) { - //elements.push(processDataObject(element)); - //}); - elements.push(processDataObject(dataObject)); + dataObjects.forEach(function (element) { + elements.push(processDataObject(element)); + }); graphData = elements; } else { diff --git a/pandora_console/include/javascript/pandora.js b/pandora_console/include/javascript/pandora.js index 1fc5912fb9..33ab956a1f 100644 --- a/pandora_console/include/javascript/pandora.js +++ b/pandora_console/include/javascript/pandora.js @@ -1890,6 +1890,16 @@ function load_modal(settings) { width = settings.onshow.width; } + settings.target.html("Loading modal..."); + settings.target + .dialog({ + title: "Loading", + close: false, + width: 200, + buttons: [] + }) + .show(); + $.ajax({ method: "post", url: settings.url, @@ -1898,6 +1908,9 @@ function load_modal(settings) { data: data, success: function(data) { settings.target.html(data); + if (settings.onload != undefined) { + settings.onload(data); + } settings.target.dialog({ resizable: true, draggable: true, @@ -1927,6 +1940,9 @@ function load_modal(settings) { click: function() { if (AJAX_RUNNING) return; AJAX_RUNNING = 1; + if (settings.onsubmit.preaction != undefined) { + settings.onsubmit.preaction(); + } var formdata = new FormData(); if (settings.extradata) { settings.extradata.forEach(function(item) { @@ -1954,7 +1970,9 @@ function load_modal(settings) { contentType: false, data: formdata, success: function(data) { - settings.ajax_callback(data); + if (settings.ajax_callback != undefined) { + settings.ajax_callback(data); + } AJAX_RUNNING = 0; } }); diff --git a/pandora_console/include/styles/credential_store.css b/pandora_console/include/styles/credential_store.css index aa77985188..5707d90780 100644 --- a/pandora_console/include/styles/credential_store.css +++ b/pandora_console/include/styles/credential_store.css @@ -10,3 +10,33 @@ #new_key select { width: 60%; } + +ul.wizard li > label:not(.p-switch) { + width: auto; +} + +form.top-action-buttons ul.wizard { + display: flex; + flex-direction: row; +} + +ul.wizard li { + margin-right: 1em; +} + +form.modal ul.wizard li { + display: flex; + flex-direction: row; + width: 90%; + margin: 0 auto; + justify-items: center; +} + +form.modal ul.wizard li * { + flex: 1; +} + +ul.wizard li.flex-indep { + flex: 1; + margin: 0; +} diff --git a/pandora_console/include/styles/integriaims.css b/pandora_console/include/styles/integriaims.css new file mode 100644 index 0000000000..0c8440e739 --- /dev/null +++ b/pandora_console/include/styles/integriaims.css @@ -0,0 +1,125 @@ +/* --- Integria IMS --- */ +div.priority { + width: 80px; + color: #fff; + text-align: center; + border-radius: 5px; + padding: 5px; + display: table-cell; + vertical-align: middle; +} + +/* Details view */ +div.integria_details { + display: grid; + grid-column-gap: 10px; + grid-template-columns: repeat(3, minmax(min-content, auto)); + grid-auto-rows: 1fr; +} + +div.integria_details div.box-shadow.white_table_graph { + box-shadow: none; +} + +div.integria_details div.priority { + display: inline-block; +} + +div.integriaims_details_row { + display: grid; + grid-gap: 10px; + text-align: center; + margin-top: 8px; + margin-bottom: 8px; +} + +div.integriaims_details_titles { + font-weight: bold; +} + +div.integria_details_row_five { + grid-template-columns: repeat(5, 1fr); +} + +div.integria_details_row_three { + grid-template-columns: repeat(3, 1fr); +} + +div.integria_details_description { + width: 100%; +} + +div.integria_details_description textarea { + width: 100%; + background-color: #fbfbfb; + resize: vertical; +} + +.integriaims_details_box { + display: grid; + grid-column-gap: 10px; + grid-row-gap: 5px; + align-items: center; + width: 100%; + text-align: center; +} + +.integriaims_details_box_five { + grid-template-rows: repeat(3, 1fr); + grid-template-columns: repeat(5, 1fr); +} + +.integriaims_details_box_three { + grid-template-rows: repeat(3, 1fr); + grid-template-columns: repeat(3, 1fr); +} + +/* ui_toggles */ +.integria_details_shadow { + background-color: #fff; + border-bottom: 1px solid #e2e2e2; + border-left: 1px solid #e2e2e2; + border-top-left-radius: 5px; + border-right: 1px solid #e2e2e2; + border-top-right-radius: 5px; +} + +.integria_details_shadow .white_table_graph_header { + border-left: none; + border-right: none; +} + +.integria_details_content { + border-bottom: none; + border-left: none; + border-right: none; +} + +/* Inputs type text shown as a black line */ +.integria_incidents_options input[type="text"] { + background-color: transparent; + border: none; + border-radius: 0; + border-bottom: 1px solid #ccc; + font-family: "lato-bolder", "Open Sans", sans-serif; + font-size: 10pt; + padding: 2px 5px; + box-sizing: border-box; + background-repeat: no-repeat; + background-position: left bottom 2px; + margin-bottom: 4px; +} + +.integria_incident_options input[readonly] { + color: #848484; +} + +.comment_title { + padding: 5px 10px 5px 20px; + background-color: rgba(130, 185, 46, 0.16); + border-radius: 3px; +} + +.comment_body { + padding: 15px 20px 15px 20px; +} diff --git a/pandora_console/include/styles/pandoraPDF.css b/pandora_console/include/styles/pandoraPDF.css index 9a55b6a1e4..4d78d5c81b 100644 --- a/pandora_console/include/styles/pandoraPDF.css +++ b/pandora_console/include/styles/pandoraPDF.css @@ -27,9 +27,6 @@ * GNU General Public License for more details. * ============================================================================ */ -table { - text-align: center; -} table.header_table { width: 100%; @@ -72,6 +69,7 @@ table.table_beauty tbody tr td { table.databox { margin-bottom: 20px; + text-align: center; } th.title_table_pdf { diff --git a/pandora_console/include/web2image.js b/pandora_console/include/web2image.js index bf183a73c4..09f840e45e 100644 --- a/pandora_console/include/web2image.js +++ b/pandora_console/include/web2image.js @@ -61,6 +61,7 @@ page.onConsoleMessage = function(msg) { page.onError = function(msg) { console.log(msg); page.close(); + phantom.exit(); }; page.onCallback = function(st) { @@ -74,4 +75,9 @@ page.onCallback = function(st) { phantom.exit(); }; -page.open(url, "POST", post_data, function(status) {}); +page.open(url, "POST", post_data, function(status) { + if (status == "fail") { + console.out("Failed to generate chart."); + phantom.exit(); + } +}); diff --git a/pandora_console/install.php b/pandora_console/install.php index 01e1e99fbd..83207d97b3 100644 --- a/pandora_console/install.php +++ b/pandora_console/install.php @@ -128,8 +128,8 @@
width = '100%'; +$table->id = 'add_alert_table'; +$table->class = 'databox filters integria_incidents_options'; +$table->head = []; + +$table->data = []; +$table->size = []; +$table->size = []; +$table->style[0] = 'width: 33%; padding-right: 50px; padding-left: 100px;'; +$table->style[1] = 'width: 33%; padding-right: 50px; padding-left: 50px;'; +$table->style[2] = 'width: 33%; padding-right: 100px; padding-left: 50px;'; +$table->colspan[0][0] = 2; +$table->colspan[3][0] = 3; + +$help_macros = isset($_GET['from_event']) ? ui_print_help_icon('response_macros', true) : ''; + +if (isset($_GET['from_event'])) { + if ($update) { + $input_value_title = $incident_details[3]; + $input_value_type = $incident_details[17]; + $input_value_status = $incident_details[6]; + $input_value_group = $incident_details[8]; + $input_value_criticity = $incident_details[7]; + $input_value_owner = $incident_details[5]; + $input_value_content = $incident_details[4]; + } else if (isset($_GET['from_event'])) { + $input_value_title = $config['cr_incident_title']; + $input_value_type = $config['cr_incident_type']; + $input_value_status = $config['cr_incident_status']; + $input_value_group = $config['cr_default_group']; + $input_value_criticity = $config['cr_default_criticity']; + $input_value_owner = $config['cr_default_owner']; + $input_value_content = $config['cr_incident_content']; + } else { + $input_value_title = ''; + $input_value_type = ''; + $input_value_status = ''; + $input_value_group = ''; + $input_value_criticity = ''; + $input_value_owner = ''; + $input_value_content = ''; + } +} + +$table->data[0][0] = '

'.__('Title').': '.$help_macros.'

'; +$table->data[0][0] .= '
'.html_print_input_text( + 'incident_title', + $input_value_title, + __('Name'), + 50, + 100, + true, + false, + true, + '', + 'w100p' +).'
'; + +$table->data[0][2] = html_print_image('images/integria_logo_gray.png', true, ['style' => 'width: 70%; float: right;'], false); + +$table->data[1][0] = '

'.__('Type').':

'; +$table->data[1][0] .= '
'.html_print_select( + $integria_types_values, + 'type', + $input_value_type, + '', + __('Select'), + 0, + true, + false, + true, + '', + false, + 'width: 100%;' +).'
'; + +$table->data[2][0] = '

'.__('Status').':

'; +$table->data[2][0] .= '
'.html_print_select( + $integria_status_values, + 'status', + $input_value_status, + '', + __('Select'), + 1, + true, + false, + true, + '', + false, + 'width: 100%;' +).'
'; + +$table->data[1][1] = '

'.__('Group').':

'; +$table->data[1][1] .= '
'.html_print_select( + $integria_group_values, + 'group', + $input_value_group, + '', + '', + 0, + true, + false, + true, + '', + false, + 'width: 100%;' +).'
'; + +$table->data[2][1] = '

'.__('Creator').':

'; +$table->data[2][1] .= '
'.html_print_input_text( + 'creator', + $config['integria_user'], + '', + '30', + 100, + true, + true, + false, + '', + 'w100p' +).ui_print_help_tip(__('This field corresponds to the Integria IMS user specified in Integria IMS setup'), true).'
'; + +$table->data[1][2] = '

'.__('Priority').':

'; +$table->data[1][2] .= '
'.html_print_select( + $integria_criticity_values, + 'criticity', + $input_value_criticity, + '', + __('Select'), + 0, + true, + false, + true, + '', + false, + 'width: 100%;' +).'
'; + +$table->data[2][2] = '

'.__('Owner').':

'; + +$table->data[2][2] .= '
'.html_print_autocomplete_users_from_integria( + 'owner', + $input_value_owner, + true +).'
'; + +$table->data[3][0] = '

'.__('Description').': '.$help_macros.'

'; +$table->data[3][0] .= '
'.html_print_textarea( + 'incident_content', + 3, + 20, + $input_value_content, + '', + true +).'
'; + +// Print forms and stuff. +echo '
'; +html_print_table($table); + +if (!$update) { + html_print_input_hidden('create_incident', 1); +} else { + html_print_input_hidden('update_incident', 1); +} + +echo '
'; + +echo '
'; +if ($update) { + html_print_submit_button(__('Update'), 'accion', false, 'form="create_integria_incident_form" class="sub wand"'); +} else { + html_print_submit_button(__('Create'), 'accion', false, 'form="create_integria_incident_form" class="sub wand"'); +} + +echo '
'; diff --git a/pandora_console/operation/incidents/dashboard_detail_integriaims_incident.php b/pandora_console/operation/incidents/dashboard_detail_integriaims_incident.php new file mode 100644 index 0000000000..aab95b4cd7 --- /dev/null +++ b/pandora_console/operation/incidents/dashboard_detail_integriaims_incident.php @@ -0,0 +1,488 @@ +width = '100%'; +$table_files_section->id = 'files_section_table'; +$table_files_section->class = 'databox filters'; +$table_files_section->head = []; + +$table_files_section->data = []; +$table_files_section->size = []; +$table_files_section->colspan[2][0] = 3; + +// Files list table. +$table_files = new stdClass(); +$table_files->width = '100%'; +$table_files->class = 'info_table'; +$table_files->head = []; + +$table_files->head[0] = __('Filename'); +$table_files->head[1] = __('Timestamp'); +$table_files->head[2] = __('Description'); +$table_files->head[3] = __('User'); +$table_files->head[4] = __('Size'); + +if (check_acl($config['id_user'], 0, 'IW')) { + $table_files->head[5] = __('Delete'); +} + +$table_files->data = []; + +// Upload file. +if ($upload_file && ($_FILES['userfile']['name'] != '')) { + $filedescription = get_parameter('file_description', __('No description available')); + + $filename = io_safe_input($_FILES['userfile']['name']); + $filesize = io_safe_input($_FILES['userfile']['size']); + + $extension = pathinfo($filename, PATHINFO_EXTENSION); + $invalid_extensions = '/^(bat|exe|cmd|sh|php|php1|php2|php3|php4|php5|pl|cgi|386|dll|com|torrent|js|app|jar|iso| + pif|vb|vbscript|wsf|asp|cer|csr|jsp|drv|sys|ade|adp|bas|chm|cpl|crt|csh|fxp|hlp|hta|inf|ins|isp|jse|htaccess| + htpasswd|ksh|lnk|mdb|mde|mdt|mdw|msc|msi|msp|mst|ops|pcd|prg|reg|scr|sct|shb|shs|url|vbe|vbs|wsc|wsf|wsh)$/i'; + + if (!preg_match($invalid_extensions, $extension)) { + // The following is if you have clamavlib installed. + // (php5-clamavlib) and enabled in php.ini + // http://www.howtoforge.com/scan_viruses_with_php_clamavlib + if (extension_loaded('clamav')) { + cl_setlimits(5, 1000, 200, 0, 10485760); + $malware = cl_scanfile($_FILES['file']['tmp_name']); + if ($malware) { + $error = 'Malware detected: '.$malware.'
ClamAV version: '.clam_get_version(); + die($error); + // On malware, we die because it's not good to handle it + } + } + + $filecontent = base64_encode(file_get_contents($_FILES['userfile']['tmp_name'])); + + $result_api_call = integria_api_call($config['integria_hostname'], $config['integria_user'], $config['integria_pass'], $config['integria_api_pass'], 'attach_file', [$incident_id, $filename, $filesize, $filedescription, $filecontent]); + + // API method returns '0' string if success. + $file_added = ($result_api_call === '0') ? true : false; + + ui_print_result_message( + $file_added, + __('File successfully added'), + __('File could not be added') + ); + } else { + ui_print_error_message(__('File has an invalid extension')); + } +} + +// Delete file. +if (isset($_GET['delete_file'])) { + $result_api_call = integria_api_call($config['integria_hostname'], $config['integria_user'], $config['integria_pass'], $config['integria_api_pass'], 'delete_file', [$delete_file_id]); + + $file_deleted = false; + + if ($result_api_call === '0') { + $file_deleted = true; + } + + ui_print_result_message( + $file_deleted, + __('File successfully deleted'), + __('File could not be deleted') + ); +} + +// Download file. +if (isset($_GET['download_file'])) { + $file_base64 = integria_api_call($config['integria_hostname'], $config['integria_user'], $config['integria_pass'], $config['integria_api_pass'], 'download_file', [$download_file_id]); + ob_end_clean(); + + $decoded = base64_decode($file_base64); + + file_put_contents($download_file_name, $decoded); + ob_end_clean(); + + if (file_exists($download_file_name)) { + header('Content-Description: File Transfer'); + header('Content-Type: application/octet-stream'); + header('Content-Disposition: attachment; filename="'.basename($download_file_name).'"'); + header('Expires: 0'); + header('Cache-Control: must-revalidate'); + header('Pragma: public'); + header('Content-Length: '.filesize($download_file_name)); + ob_end_clean(); + readfile($download_file_name); + unlink($download_file_name); + exit; + } + + header('Location: index.php?sec=incident&sec2=operation/incidents/dashboard_detail_integriaims_incident&incident_id='.$incident_id); +} + +// Retrieve files belonging to incident and create list table. +$result_api_call = integria_api_call($config['integria_hostname'], $config['integria_user'], $config['integria_pass'], $config['integria_api_pass'], 'get_incident_files', [$incident_id]); + +if ($result_api_call != false && strlen($result_api_call) > 0) { + $files = []; + $csv_array = explode("\n", $result_api_call); + + foreach ($csv_array as $csv_line) { + if (!empty($csv_line)) { + $files[] = explode(',', $csv_line); + } + } +} + +$i = 0; + +foreach ($files as $key => $value) { + $table_files->data[$i][0] = ''.$value[11].''; + $table_files->data[$i][1] = $value[14]; + $table_files->data[$i][2] = $value[12]; + $table_files->data[$i][3] = $value[8]; + $table_files->data[$i][4] = $value[13]; + if (check_acl($config['id_user'], 0, 'IW')) { + $table_files->data[$i][5] .= ''; + $table_files->data[$i][5] .= html_print_image('images/cross.png', true, ['title' => __('Delete')]); + } + + $table_files->data[$i][5] .= ''; + + $i++; +} + +$table_files_section->data[0][0] = '

'.__('File name').':

'; +$table_files_section->data[0][0] .= html_print_input_file('userfile', true); +$table_files_section->data[1][0] = '

'.__('Description').':

'; +$table_files_section->data[1][0] .= html_print_textarea( + 'file_description', + 3, + 20, + '', + '', + true +); + +$table_files_section->data[2][0] .= '
'.html_print_submit_button(__('Upload'), 'accion', false, 'class="sub wand"', true).'
'; + +$upload_file_form = '
'; + +if (check_acl($config['id_user'], 0, 'IW')) { + $upload_file_form .= '
'.'

'.__('Add attachment').'

'.html_print_table($table_files_section, true).html_print_input_hidden('upload_file', 1, true); +} + +$upload_file_form .= '

'.__('Attached files').'

'.html_print_table($table_files, true).'
'; + +// Incident comments management. +$upload_comment = get_parameter('upload_comment'); +$comment_description = get_parameter('comment_description'); + +// Comments section table. +$table_comments_section = new stdClass(); +$table_comments_section->width = '100%'; +$table_comments_section->id = 'files_section_table'; +$table_comments_section->class = 'databox filters'; +$table_comments_section->head = []; + +$table_comments_section->data = []; +$table_comments_section->size = []; + +// Comments list table. +$table_comments = new stdClass(); +$table_comments->width = '100%'; +$table_comments->class = 'info_table'; +$table_comments->head = []; + +$table_comments->head[0] = __('Filename'); +$table_comments->head[1] = __('Timestamp'); +$table_comments->head[2] = __('Description'); +$table_comments->head[3] = __('User'); +$table_comments->head[4] = __('Size'); +$table_comments->head[5] = __('Delete'); + +$table_comments->data = []; + +$comment_disabled = ($array_get_incidents[6] == 7); + +if ($comment_disabled === true) { + $attribute = 'disabled=disabled'; +} + +$table_comments_section->data[0][0] = '

'.__('Description').':

'; +$table_comments_section->data[0][0] .= html_print_textarea( + 'comment_description', + 3, + 20, + '', + $attribute, + true +); + +$table_comments_section->data[1][1] .= '
'.html_print_submit_button(__('Add'), 'accion', $comment_disabled, 'class="sub wand"', true).'
'; + +// Upload comment. If ticket is closed, this action cannot be performed. +if ($upload_comment && $array_get_incidents[6] != 7) { + $result_api_call = integria_api_call($config['integria_hostname'], $config['integria_user'], $config['integria_pass'], $config['integria_api_pass'], 'create_workunit', [$incident_id, $comment_description, '0.00', 0, 1, '0']); + + // API method returns id of new comment if success. + $comment_added = ($result_api_call >= '0') ? true : false; + + ui_print_result_message( + $comment_added, + __('Comment successfully added'), + __('Comment could not be added') + ); +} + +// Retrieve comments belonging to incident and create comments table. +$result_api_call = integria_api_call($config['integria_hostname'], $config['integria_user'], $config['integria_pass'], $config['integria_api_pass'], 'get_incident_workunits', [$incident_id]); + +if ($result_api_call != false && strlen($result_api_call) > 0) { + $comments = []; + $csv_array = explode("\n", $result_api_call); + + foreach ($csv_array as $csv_line) { + if (!empty($csv_line)) { + $comments[] = explode(',', $csv_line); + } + } +} + +$comment_table = ''; + +if (!empty($comments)) { + foreach ($comments as $key => $value) { + $comment_table .= '
'.$value[3].' said '.$value[1].''.$value[2].' Hours
'; + $comment_table .= '
'.$value[4].'
'; + } +} else { + $comment_table = __('No comments found'); +} + +$upload_comment_form = '
'; + +if (check_acl($config['id_user'], 0, 'IW')) { + $upload_comment_form .= '

'.__('Add comment').'

'.html_print_table($table_comments_section, true).html_print_input_hidden('upload_comment', 1, true).'
'; +} + +$upload_comment_form .= '

'.__('Comments').'

'.$comment_table.'
'; + +// Details box. +$details_box = '
'; +$details_box .= ' +
'.__('Status').'
+
'.__('Resolution').'
+
'.__('Group').'
+
'.__('Priority').'
+
'.__('Type').'
'; +$details_box .= ' +
'.html_print_image('images/heart.png', true).'
+
'.html_print_image('images/builder.png', true).'
+
'.html_print_image('images/user_green.png', true).'
+
'.ui_print_integria_incident_priority($priority, $priority_text).'
+
'.html_print_image('images/incidents.png', true).'
'; +$details_box .= ' +
'.$status_text.'
+
'.$resolution_text.'
+
'.$group_text.'
+
'.$priority_text.'
+
'.$type_text.'
'; +$details_box .= '
'; + + +// People box. +$people_box = '
'; +$people_box .= ' +
'.html_print_image('images/header_user_green.png', true, ['width' => '21']).'
+
'.html_print_image('images/header_user_green.png', true, ['width' => '21']).'
+
'.html_print_image('images/header_user_green.png', true, ['width' => '21']).'
'; +$people_box .= ' +
'.__('Created by').':
+
'.__('Owned by').':
+
'.__('Closed by').':
'; +$people_box .= ' +
'.$creator.'
+
'.$owner.'
+
'.$closed_by.'
'; +$people_box .= '
'; + + +// Dates box. +$dates_box = '
'; +$dates_box .= ' +
'.html_print_image('images/tick.png', true).'
+
'.html_print_image('images/update.png', true, ['width' => '21']).'
+
'.html_print_image('images/mul.png', true).'
'; +$dates_box .= ' +
'.__('Created at').':
+
'.__('Updated at').':
+
'.__('Closed at').':
'; +$dates_box .= ' +
'.$created_at.'
+
'.$updated_at.'
+
'.$closed_at.'
'; +$dates_box .= '
'; + + +// Show details, people and dates. +echo '
'; + ui_toggle($details_box, __('Details'), '', 'details_box', false, false, '', 'integria_details_content white-box-content', 'integria_details_shadow box-shadow white_table_graph'); + ui_toggle($people_box, __('People'), '', 'people_box', false, false, '', 'integria_details_content white-box-content', 'integria_details_shadow box-shadow white_table_graph'); + ui_toggle($dates_box, __('Dates'), '', 'dates_box', false, false, '', 'integria_details_content white-box-content', 'integria_details_shadow box-shadow white_table_graph'); +echo '
'; + + // Show description. +$description_box = '
'.html_print_textarea( + 'integria_details_description', + 3, + 0, + $description, + 'disabled="disabled"', + true +).'
'; +ui_toggle($description_box, __('Description'), '', '', false); + +echo '
'; +ui_toggle( + $upload_file_form, + __('Attached files'), + '', + '', + true, + false, + 'white_box white_box_opened', + 'no-border flex' +); +echo '
'; + +echo '
'; +ui_toggle( + $upload_comment_form, + __('Comments'), + '', + '', + true, + false, + 'white_box white_box_opened', + 'no-border flex' +); +echo '
'; + +?> + \ No newline at end of file diff --git a/pandora_console/operation/incidents/integriaims_export_csv.php b/pandora_console/operation/incidents/integriaims_export_csv.php new file mode 100644 index 0000000000..03a038e58a --- /dev/null +++ b/pandora_console/operation/incidents/integriaims_export_csv.php @@ -0,0 +1,129 @@ + $value) { + // Status. + if ($tickets_csv_array[$key][6] == 0) { + $tickets_csv_array[$key][6] = 'None'; + } else { + $tickets_csv_array[$key][6] = $status_incident[$tickets_csv_array[$key][6]]; + } + + // Priority. + $tickets_csv_array[$key][7] = $priority_incident[$tickets_csv_array[$key][7]]; + + // Group. + $tickets_csv_array[$key][8] = $group_incident[$tickets_csv_array[$key][8]]; + + // Resolution. + if ($tickets_csv_array[$key][12] == 0) { + $tickets_csv_array[$key][12] = 'None'; + } else { + $tickets_csv_array[$key][12] = $resolution_incident[$tickets_csv_array[$key][12]]; + } + + $tickets_csv_array_filter[$key] = [ + 'id_incidencia' => $tickets_csv_array[$key][0], + 'titulo' => $tickets_csv_array[$key][3], + 'id_grupo' => $tickets_csv_array[$key][8], + 'estado' => $tickets_csv_array[$key][6], + 'resolution' => $tickets_csv_array[$key][12], + 'prioridad' => $tickets_csv_array[$key][7], + 'actualizacion' => $tickets_csv_array[$key][9], + 'inicio' => $tickets_csv_array[$key][1], + 'id_creator' => $tickets_csv_array[$key][10], + 'owner' => $tickets_csv_array[$key][5], + ]; +} + +// Header for CSV file. +$header = [ + __('ID Ticket'), + __('Title'), + __('Group/Company'), + __('Status'), + __('Resolution'), + __('Priority'), + __('Updated'), + __('Started'), + __('Creator'), + __('Owner'), +]; + +$header_csv = ''; +foreach ($header as $key => $value) { + $header_csv .= $value.','; +} + +$header_csv = io_safe_output($header_csv).PHP_EOL; + + +// Join header and content. +$tickets_csv = ''; +foreach ($tickets_csv_array_filter as $key => $value) { + $tickets_csv .= implode(',', $tickets_csv_array_filter[$key]).PHP_EOL; +} + +$tickets_csv = $header_csv.$tickets_csv; + + +// Create csv file. +$filename = 'tickets_export-'.date('Ymd').'-'.date('His').'.csv'; + +ob_clean(); + +header('Content-Type: text/csv; charset=utf-8'); +header('Content-Disposition: attachment; filename='.$filename); + +// BOM. +echo pack('C*', 0xEF, 0xBB, 0xBF); + +// CSV file. +echo io_safe_output($tickets_csv); diff --git a/pandora_console/operation/incidents/list_integriaims_incidents.php b/pandora_console/operation/incidents/list_integriaims_incidents.php new file mode 100644 index 0000000000..d97276c15e --- /dev/null +++ b/pandora_console/operation/incidents/list_integriaims_incidents.php @@ -0,0 +1,335 @@ +width = '100%'; +$table->class = 'databox filters'; +$table->styleTable = 'margin-bottom:0px'; +$table->cellpadding = '0'; +$table->cellspacing = '0'; +$table->data = []; + +$table->data[0][0] = __('Text filter'); +$table->data[0][1] = html_print_input_text('incident_text', $incident_text, '', 30, 100, true); + +$table->data[0][2] = __('Status'); +$table->data[0][3] = html_print_select( + $status_incident, + 'incident_status', + $incident_status, + '', + __('All'), + 0, + true +); + +$table->data[0][4] = __('Group'); +$table->data[0][5] = html_print_select( + $group_incident, + 'incident_group', + $incident_group, + '', + __('All'), + 1, + true +); + +$table->data[1][0] = __('Owner'); +$table->data[1][1] = html_print_autocomplete_users_from_integria('incident_owner', $incident_owner, true); + +$table->data[1][2] = __('Creator'); +$table->data[1][3] = html_print_autocomplete_users_from_integria('incident_creator', $incident_creator, true); + +$table->data[1][4] = __('Priority'); +$table->data[1][5] = html_print_select( + $priority_incident, + 'incident_priority', + $incident_priority, + '', + __('All'), + -1, + true +); + +$table->data[2][0] = __('Resolution'); +$table->data[2][1] = html_print_select( + $resolution_incident, + 'incident_resolution', + $incident_resolution, + '', + __('All'), + '', + true +); + +// TODO: field type date. +$table->data[2][2] = __('Date'); +$table->data[2][3] = html_print_input_text_extended( + 'created_from', + $created_from, + 'created_from', + '', + 12, + 50, + false, + '', + 'placeholder="'.__('Created from').'"', + true +); +$table->data[2][3] .= html_print_input_text_extended( + 'created_to', + $created_to, + 'created_to', + '', + 12, + 50, + false, + '', + 'style="margin-left:5px;" placeholder="'.__('Created to').'"', + true +); + +// TODO: image of Integria IMS. +$table->data[2][4] = ''; +$table->data[2][5] = ''; + + +// Send filters to get_tickets_integriaims(). +$tickets_filters = [ + 'incident_text' => $incident_text, + 'incident_status' => $incident_status, + 'incident_group' => $incident_group, + 'incident_owner' => $incident_owner, + 'incident_creator' => $incident_creator, + 'incident_priority' => $incident_priority, + 'incident_resolution' => $incident_resolution, + 'created_from' => $created_from, + 'created_to' => $created_to, +]; + +// Data to export to csv file. +$decode_csv = base64_encode(json_encode($tickets_filters)); + + +// ---- PRINT TABLE FILTERS ---- +$integria_incidents_form = '
'; +$integria_incidents_form .= html_print_table($table, true); +$integria_incidents_form .= '
'; +$integria_incidents_form .= '
'.html_print_button( + __('Export to CSV'), + 'csv_export', + false, + "location.href='operation/incidents/integriaims_export_csv.php?tickets_filters=$decode_csv'", + 'class="sub next"', + true +).'
'; +$integria_incidents_form .= '
'.html_print_submit_button(__('Filter'), 'filter_button', false, 'class="sub filter"', true).'
'; +$integria_incidents_form .= '
'; +$integria_incidents_form .= '
'; + +ui_toggle($integria_incidents_form, __('Filter'), '', '', false); + +/* + * Order api call 'get_incidents'. + * + * resolution = $array_get_incidents[$key][12] + * id_incidencia = $array_get_incidents[$key][0] + * titulo = $array_get_incidents[$key][3] + * id_grupo = $array_get_incidents[$key][8] + * estado = $array_get_incidents[$key][6] + * prioridad = $array_get_incidents[$key][7] + * actualizacion = $array_get_incidents[$key][9] + * id_creator = $array_get_incidents[$key][10] + * + */ + +// ---- LIST OF INCIDENTS ---- +// Get list of incidents. +$array_get_incidents = get_tickets_integriaims($tickets_filters); + +// Prepare pagination. +$incidents_limit = $config['block_size']; +$incidents_paginated = array_slice($array_get_incidents, $offset, $incidents_limit, true); + +// TABLE INCIDENTS. +$table = new stdClass(); +$table->width = '100%'; +$table->class = 'info_table'; +$table->head = []; + +$table->head[0] = __('ID'); +$table->head[1] = __('Ticket'); +$table->head[2] = __('Group/Company'); +$table->head[3] = __('Status/Resolution'); +$table->head[4] = __('Prior'); +$table->head[5] = __('Updated/Started'); +$table->head[6] = __('Creator'); +$table->head[7] = __('Owner'); +if (check_acl($config['id_user'], 0, 'IW') || check_acl($config['id_user'], 0, 'IM')) { + $table->head[8] = ''; +} + +$table->data = []; +$i = 0; + +foreach ($incidents_paginated as $key => $value) { + if ($array_get_incidents[$key][6] == 0) { + $status_incident[$array_get_incidents[$key][6]] = __('None'); + } + + if ($array_get_incidents[$key][12] == 0) { + $resolution_incident[$array_get_incidents[$key][12]] = __('None'); + } + + $table->data[$i][0] = '#'.$array_get_incidents[$key][0]; + $table->data[$i][1] = ''; + $table->data[$i][1] .= $array_get_incidents[$key][3]; + $table->data[$i][1] .= ''; + $table->data[$i][2] = $group_incident[$array_get_incidents[$key][8]]; + $table->data[$i][3] = $status_incident[$array_get_incidents[$key][6]].' / '.$resolution_incident[$array_get_incidents[$key][12]]; + $table->data[$i][4] = ui_print_integria_incident_priority($array_get_incidents[$key][7], $priority_incident[$array_get_incidents[$key][7]]); + $table->data[$i][5] = $array_get_incidents[$key][9].' / '.$array_get_incidents[$key][1]; + $table->data[$i][6] = $array_get_incidents[$key][10]; + $table->data[$i][7] = $array_get_incidents[$key][5]; + $table->data[$i][8] = ''; + $table->cellclass[$i][8] = 'action_buttons'; + if (check_acl($config['id_user'], 0, 'IW')) { + $table->data[$i][8] .= ''; + $table->data[$i][8] .= html_print_image('images/config.png', true, ['title' => __('Edit')]); + $table->data[$i][8] .= ''; + } + + if (check_acl($config['id_user'], 0, 'IM')) { + $table->data[$i][8] .= ''; + $table->data[$i][8] .= html_print_image('images/cross.png', true, ['title' => __('Delete')]); + $table->data[$i][8] .= ''; + } + + $i++; +} + +// Show table incidents. +ui_pagination(count($array_get_incidents), $url, $offset); +if (empty($table->data) === true) { + ui_print_info_message(['no_close' => true, 'message' => __('No tickets to show').'.' ]); +} else { + html_print_table($table); + ui_pagination(count($array_get_incidents), $url, $offset, 0, false, 'offset', true, 'pagination-bottom'); +} + +// Show button to create incident. +if (check_acl($config['id_user'], 0, 'IR')) { + echo '
'; + echo '
'; + html_print_submit_button(__('Create'), 'create_new_incident', false, 'class="sub next"'); + echo '
'; + echo '
'; +} + +// Datapicker library for show calendar. +ui_require_jquery_file('ui.datepicker-'.get_user_language(), 'include/javascript/i18n/'); +?> + + + \ No newline at end of file diff --git a/pandora_console/operation/menu.php b/pandora_console/operation/menu.php index 936cb6a814..58213ed8ee 100644 --- a/pandora_console/operation/menu.php +++ b/pandora_console/operation/menu.php @@ -479,6 +479,7 @@ if (check_acl($config['id_user'], 0, 'IR') $sub2 = []; $sub2['operation/incidents/incident']['text'] = __('List of Incidents'); $sub2[$sec2sub]['text'] = __('Statistics'); + $sub2['operation/incidents/list_integriaims_incidents']['text'] = __('Integria IMS Tickets'); $sub[$sec2]['sub2'] = $sub2; $sec2 = $temp_sec2; diff --git a/pandora_console/operation/reporting/reporting_viewer.php b/pandora_console/operation/reporting/reporting_viewer.php index 0bba865711..7528f15253 100755 --- a/pandora_console/operation/reporting/reporting_viewer.php +++ b/pandora_console/operation/reporting/reporting_viewer.php @@ -124,7 +124,7 @@ if (!defined('METACONSOLE')) { } // Page header for metaconsole -if ($config['metaconsole'] == 1 and defined('METACONSOLE')) { +if (is_metaconsole()) { // Bread crumbs ui_meta_add_breadcrumb(['link' => 'index.php?sec=reporting&sec2=godmode/reporting/reporting_builder', 'text' => __('Reporting')]); diff --git a/pandora_console/operation/users/user_edit.php b/pandora_console/operation/users/user_edit.php index b8b42ad945..ec065a6a52 100644 --- a/pandora_console/operation/users/user_edit.php +++ b/pandora_console/operation/users/user_edit.php @@ -333,7 +333,7 @@ $id_usr = $config['id_user']; if (!$meta) { - $home_screen = '

'.__('Home screen').ui_print_help_tip(__('User can customize the home page. By default, will display \'Agent Detail\'. Example: Select \'Other\' and type sec=estado&sec2=operation/agentes/estado_agente to show agent detail view'), true).'

'; + $home_screen = '

'.__('Home screen').ui_print_help_tip(__('User can customize the home page. By default, will display \'Agent Detail\'. Example: Select \'Other\' and type index.php?sec=estado&sec2=operation/agentes/ver_agente&id_agente=1 to show agent detail view'), true).'

'; $values = [ 'Default' => __('Default'), 'Visual console' => __('Visual console'), diff --git a/pandora_console/pandora_console.redhat.spec b/pandora_console/pandora_console.redhat.spec index 2e794ce981..c61b007b0a 100644 --- a/pandora_console/pandora_console.redhat.spec +++ b/pandora_console/pandora_console.redhat.spec @@ -2,8 +2,8 @@ # Pandora FMS Console # %define name pandorafms_console -%define version 7.0NG.738 -%define release 190920 +%define version 7.0NG.739 +%define release 191003 # User and Group under which Apache is running %define httpd_name httpd diff --git a/pandora_console/pandora_console.rhel7.spec b/pandora_console/pandora_console.rhel7.spec index d5938ce965..39d16c59d4 100644 --- a/pandora_console/pandora_console.rhel7.spec +++ b/pandora_console/pandora_console.rhel7.spec @@ -2,8 +2,8 @@ # Pandora FMS Console # %define name pandorafms_console -%define version 7.0NG.738 -%define release 190920 +%define version 7.0NG.739 +%define release 191003 # User and Group under which Apache is running %define httpd_name httpd diff --git a/pandora_console/pandora_console.spec b/pandora_console/pandora_console.spec index 618229adff..99572d8ce5 100644 --- a/pandora_console/pandora_console.spec +++ b/pandora_console/pandora_console.spec @@ -2,8 +2,8 @@ # Pandora FMS Console # %define name pandorafms_console -%define version 7.0NG.738 -%define release 190920 +%define version 7.0NG.739 +%define release 191003 %define httpd_name httpd # User and Group under which Apache is running %define httpd_name apache2 diff --git a/pandora_console/pandora_console_install b/pandora_console/pandora_console_install index bda06177d6..9ed0bd594a 100644 --- a/pandora_console/pandora_console_install +++ b/pandora_console/pandora_console_install @@ -9,7 +9,7 @@ # This code is licensed under GPL 2.0 license. # ********************************************************************** -PI_VERSION="7.0NG.738" +PI_VERSION="7.0NG.739" FORCE=0 DESTDIR="" LOG_TIMESTAMP=`date +"%Y/%m/%d %H:%M:%S"` diff --git a/pandora_console/pandoradb_data.sql b/pandora_console/pandoradb_data.sql index 01f069b8b6..686f4aa487 100644 --- a/pandora_console/pandoradb_data.sql +++ b/pandora_console/pandoradb_data.sql @@ -109,14 +109,36 @@ INSERT INTO `tconfig` (`token`, `value`) VALUES ('custom_report_front_logo', 'images/pandora_logo_white.jpg'), ('custom_report_front_header', ''), ('custom_report_front_footer', ''), -('MR', 31), +('MR', 32), ('identification_reminder', 1), ('identification_reminder_timestamp', 0), -('current_package_enterprise', '738'), +('current_package_enterprise', '739'), ('post_process_custom_values', '{"0.00000038580247":"Seconds to months","0.00000165343915":"Seconds to weeks","0.00001157407407":"Seconds to days","0.01666666666667":"Seconds to minutes","0.00000000093132":"Bytes to Gigabytes","0.00000095367432":"Bytes to Megabytes","0.0009765625":"Bytes to Kilobytes","0.00000001653439":"Timeticks to weeks","0.00000011574074":"Timeticks to days"}'), ('custom_docs_logo', 'default_docs.png'), ('custom_support_logo', 'default_support.png'), -('custom_logo_white_bg_preview', 'pandora_logo_head_white_bg.png'); +('custom_logo_white_bg_preview', 'pandora_logo_head_white_bg.png'), +('integria_enabled', 0), +('integria_user', ''), +('integria_pass', ''), +('integria_hostname', ''), +('integria_api_pass', ''), +('integria_req_timeout', 5), +('default_group', ''), +('default_criticity', ''), +('default_creator', ''), +('default_owner', ''), +('incident_type', ''), +('incident_status', ''), +('incident_title', ''), +('incident_content', ''), +('cr_default_group', ''), +('cr_default_criticity', ''), +('cr_default_creator', ''), +('cr_default_owner', ''), +('cr_incident_type', ''), +('cr_incident_status', ''), +('cr_incident_title', ''), +('cr_incident_content', ''); UNLOCK TABLES; -- diff --git a/pandora_server/DEBIAN/control b/pandora_server/DEBIAN/control index e52fbc8ba1..90d72f2297 100644 --- a/pandora_server/DEBIAN/control +++ b/pandora_server/DEBIAN/control @@ -1,5 +1,5 @@ package: pandorafms-server -Version: 7.0NG.738-190920 +Version: 7.0NG.739-191003 Architecture: all Priority: optional Section: admin diff --git a/pandora_server/DEBIAN/make_deb_package.sh b/pandora_server/DEBIAN/make_deb_package.sh index abde6de7e3..96d5ec0e9d 100644 --- a/pandora_server/DEBIAN/make_deb_package.sh +++ b/pandora_server/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.738-190920" +pandora_version="7.0NG.739-191003" package_cpan=0 package_pandora=1 diff --git a/pandora_server/conf/pandora_server.conf.new b/pandora_server/conf/pandora_server.conf.new index 6d92d7417d..3aa61de07f 100644 --- a/pandora_server/conf/pandora_server.conf.new +++ b/pandora_server/conf/pandora_server.conf.new @@ -1,7 +1,7 @@ ############################################################################# # Pandora FMS Server Parameters # Pandora FMS, the Flexible Monitoring System. -# Version 7.0NG.738 +# Version 7.0NG.739 # Licensed under GPL license v2, # (c) 2003-2017 Artica Soluciones Tecnologicas # http://www.pandorafms.com diff --git a/pandora_server/lib/PandoraFMS/Config.pm b/pandora_server/lib/PandoraFMS/Config.pm index 089e9cc15f..0e18476523 100644 --- a/pandora_server/lib/PandoraFMS/Config.pm +++ b/pandora_server/lib/PandoraFMS/Config.pm @@ -44,8 +44,8 @@ our @EXPORT = qw( ); # version: Defines actual version of Pandora Server for this module only -my $pandora_version = "7.0NG.738"; -my $pandora_build = "190920"; +my $pandora_version = "7.0NG.739"; +my $pandora_build = "191003"; our $VERSION = $pandora_version." ".$pandora_build; # Setup hash @@ -357,7 +357,15 @@ sub pandora_load_config { # Xprobe2 for recon OS fingerprinting and tcpscan (optional) $pa_config->{"xprobe2"} = "/usr/bin/xprobe2"; + # Winexe allows to exec commands on remote windows systems (optional) + $pa_config->{"winexe"} = "/usr/bin/winexe"; + + # PsExec allows to exec commands on remote windows systems from windows servers (optional) + $pa_config->{"psexec"} = 'C:\PandoraFMS\Pandora_Server\bin\PsExec.exe'; + # plink allows to exec commands on remote linux systems from windows servers (optional) + $pa_config->{"plink"} = 'C:\PandoraFMS\Pandora_Server\bin\plink.exe'; + # Snmpget for snmpget system command (optional) $pa_config->{"snmpget"} = "/usr/bin/snmpget"; @@ -838,6 +846,15 @@ sub pandora_load_config { elsif ($parametro =~ m/^xprobe2\s(.*)/i) { $pa_config->{'xprobe2'}= clean_blank($1); } + elsif ($parametro =~ m/^winexe\s(.*)/i) { + $pa_config->{'winexe'}= clean_blank($1); + } + elsif ($parametro =~ m/^psexec\s(.*)/i) { + $pa_config->{'psexec'}= clean_blank($1); + } + elsif ($parametro =~ m/^plink\s(.*)/i) { + $pa_config->{'plink'}= clean_blank($1); + } elsif ($parametro =~ m/^snmpget\s(.*)/i) { $pa_config->{'snmpget'}= clean_blank($1); } @@ -1037,7 +1054,7 @@ sub pandora_load_config { $pa_config->{'console_pass'}= safe_input(clean_blank($1)); } elsif ($parametro =~ m/^encryption_passphrase\s(.*)/i) { # 6.0 - $pa_config->{'encryption_passphrase'}= safe_input(clean_blank($1)); + $pa_config->{'encryption_passphrase'} = clean_blank($1); } elsif ($parametro =~ m/^unknown_interval\s+([0-9]*)/i) { # > 5.1SP2 $pa_config->{'unknown_interval'}= clean_blank($1); diff --git a/pandora_server/lib/PandoraFMS/Core.pm b/pandora_server/lib/PandoraFMS/Core.pm index e1556d74b6..644b983a5c 100644 --- a/pandora_server/lib/PandoraFMS/Core.pm +++ b/pandora_server/lib/PandoraFMS/Core.pm @@ -1137,7 +1137,9 @@ sub pandora_execute_action ($$$$$$$$$;$) { load_module_macros ($module->{'module_macros'}, \%macros); } - # User defined alerts + + #logger($pa_config, "Clean name ".$clean_name, 10); + # User defined alert if ($action->{'internal'} == 0) { $macros{_field1_} = subst_alert_macros ($field1, \%macros, $pa_config, $dbh, $agent, $module); $macros{_field2_} = subst_alert_macros ($field2, \%macros, $pa_config, $dbh, $agent, $module); @@ -1442,55 +1444,70 @@ sub pandora_execute_action ($$$$$$$$$;$) { # Integria IMS Ticket } elsif ($clean_name eq "Integria IMS Ticket") { + my $config_api_path = pandora_get_tconfig_token ($dbh, 'integria_hostname', ''); + my $config_api_pass = pandora_get_tconfig_token ($dbh, 'integria_api_pass', ''); + my $config_integria_user = pandora_get_tconfig_token ($dbh, 'integria_user', ''); + my $config_integria_user_pass = pandora_get_tconfig_token ($dbh, 'integria_pass', ''); $field1 = subst_alert_macros ($field1, \%macros, $pa_config, $dbh, $agent, $module); + $field2 = subst_alert_macros ($field2, \%macros, $pa_config, $dbh, $agent, $module); $field3 = subst_alert_macros ($field3, \%macros, $pa_config, $dbh, $agent, $module); + $field4 = subst_alert_macros ($field4, \%macros, $pa_config, $dbh, $agent, $module); $field5 = subst_alert_macros ($field5, \%macros, $pa_config, $dbh, $agent, $module); + $field6 = subst_alert_macros ($field6, \%macros, $pa_config, $dbh, $agent, $module); $field7 = subst_alert_macros ($field7, \%macros, $pa_config, $dbh, $agent, $module); - $field8 = subst_alert_macros ($field8, \%macros, $pa_config, $dbh, $agent, $module); - $field9 = subst_alert_macros ($field9, \%macros, $pa_config, $dbh, $agent, $module); - $field10 = subst_alert_macros ($field10, \%macros, $pa_config, $dbh, $agent, $module); - + # Field 1 (Integria IMS API path) - my $api_path = $field1; + my $api_path = $config_api_path . "/integria/include/api.php"; # Field 2 (Integria IMS API pass) - my $api_pass = $field2; + my $api_pass = $config_api_pass; # Field 3 (Integria IMS user) - my $integria_user = $field3; + my $integria_user = $config_integria_user; # Field 4 (Integria IMS user password) - my $integria_user_pass = $field4; + my $integria_user_pass = $config_integria_user_pass; - # Field 5 (Ticket name) - my $ticket_name = $field5; + # Field 1 (Ticket name) + my $ticket_name = safe_output($field1); if ($ticket_name eq "") { $ticket_name = $pa_config->{'rb_product_name'} . " alert action created by API"; } - # Field 6 (Ticket group ID) - my $ticket_group_id = $field6; + # Field 2 (Ticket group ID) + my $ticket_group_id = $field2; if ($ticket_group_id eq '') { $ticket_group_id = 0; } - # Field 7 (Ticket priority); - my $ticket_priority = $field7; - if ($ticket_priority eq '') { - $ticket_priority = 0; + # Field 3 (Ticket priority); + my $ticket_priority = $field3; + if ($ticket_priority eq '0') { + $ticket_priority = 1; + } + + # Field 4 (Ticket owner) + my $ticket_owner = $field4; + if ($ticket_owner eq '') { + $ticket_owner = 'admin'; } - # Field 8 (Ticket email) - my $ticket_email = $field8; - - # Field 9 (Ticket owner) - my $ticket_owner = $field9; - - # Field 10 (Ticket description); - my $ticket_description = $field10; + # Field 5 (Ticket type) + my $ticket_type = $field5; + if ($ticket_type eq '') { + $ticket_type = 0; + } - pandora_create_integria_ticket($pa_config, $api_path, $api_pass, $integria_user, $integria_user_pass, $ticket_name, $ticket_group_id, $ticket_priority, $ticket_email, $ticket_owner, $ticket_description); + # Field 6 (Ticket status) + my $ticket_status = $field6; + if ($ticket_status eq '0') { + $ticket_status = 1; + } + # Field 7 (Ticket description); + my $ticket_description = safe_output($field7); + + pandora_create_integria_ticket($pa_config, $api_path, $api_pass, $integria_user, $integria_user_pass, $ticket_name, $ticket_group_id, $ticket_priority, $ticket_owner, $ticket_type, $ticket_status, $ticket_description); # Generate notification } elsif ($clean_name eq "Generate Notification") { @@ -3155,11 +3172,20 @@ sub pandora_get_config_value ($$) { ########################################################################## ## Get credential from credential store ########################################################################## -sub pandora_get_credential ($$) { - my ($dbh, $identifier) = @_; +sub pandora_get_credential ($$$) { + my ($pa_config, $dbh, $identifier) = @_; my $key = get_db_single_row($dbh, 'SELECT * FROM tcredential_store WHERE identifier = ?', $identifier); + $key->{'username'} = pandora_output_password( + $pa_config, + safe_output($key->{'username'}) + ); + $key->{'password'} = pandora_output_password( + $pa_config, + safe_output($key->{'password'}) + ); + return $key; } @@ -5840,55 +5866,21 @@ sub pandora_edit_custom_graph ($$$$$$$$$$$) { } sub pandora_create_integria_ticket ($$$$$$$$$$$) { - my ($pa_config,$api_path,$api_pass,$integria_user,$user_pass,$ticket_name,$group_id,$ticket_priority,$ticket_email,$ticket_owner,$ticket_description) = @_; - + my ($pa_config,$api_path,$api_pass,$integria_user,$user_pass,$ticket_name,$ticket_group_id,$ticket_priority,$ticket_owner,$ticket_type,$ticket_status,$ticket_description) = @_; + my $data_ticket; my $call_api; - if ($api_path eq "") { - return 0; - } - if ($user_pass eq "") { - return 0; - } - if ($integria_user eq "") { - $integria_user = "admin"; - } - if ($ticket_name eq "") { - $ticket_name = "Ticket created by " . $pa_config->{'rb_product_name'}; - } - if ($group_id eq "") { - $group_id = 1; - } - if ($ticket_priority eq "") { - $ticket_priority = 1; - } - if ($ticket_owner eq "") { - $ticket_owner = "admin"; - } - - #~ $data_ticket = $ticket_name . - #~ "|;|" . $group_id . - #~ "|;|" . $ticket_priority . - #~ "|;|" . $ticket_description . - #~ "|;|" . #Id inventory - #~ "|;|" . #Id incident type - #~ "|;|" . $ticket_email . - #~ "|;|" . $ticket_owner . - #~ "|;|" . #Father ticket id - #~ "|;|" . #Status - #~ "|;|" . #Extra info - #~ "|;|"; #Resolution $data_ticket = $ticket_name . - "|;|" . $group_id . + "|;|" . $ticket_group_id . "|;|" . $ticket_priority . "|;|" . $ticket_description . "|;|" . - "|;|" . - "|;|" . $ticket_email . + "|;|" . $ticket_type . + "|;|" . "|;|" . $ticket_owner . "|;|" . - "|;|" . '1' . + "|;|" . $ticket_status . "|;|" . "|;|"; @@ -5899,9 +5891,9 @@ sub pandora_create_integria_ticket ($$$$$$$$$$$) { 'op=create_incident&' . 'params=' . $data_ticket .'&' . 'token=|;|'; - logger($pa_config, "Integria ticket call:" . $call_api . "", 3); + my $content = get($call_api); - logger($pa_config, "Integria ticket res:" . $content . "", 3); + if (is_numeric($content) && $content ne "-1") { return $content; } diff --git a/pandora_server/lib/PandoraFMS/DiscoveryServer.pm b/pandora_server/lib/PandoraFMS/DiscoveryServer.pm index 6f7eab4c68..b2d8e71619 100644 --- a/pandora_server/lib/PandoraFMS/DiscoveryServer.pm +++ b/pandora_server/lib/PandoraFMS/DiscoveryServer.pm @@ -343,6 +343,9 @@ sub exec_recon_script ($$$) { sub PandoraFMS::Recon::Base::guess_os($$) { my ($self, $device) = @_; + $DEVNULL = '/dev/null' if (!defined($DEVNULL)); + $DEVNULL = '/NUL' if ($^O =~ /win/i && !defined($DEVNULL)); + # OS detection disabled. Use the device type. if ($self->{'os_detection'} == 0) { my $device_type = $self->get_device_type($device); @@ -354,17 +357,20 @@ sub PandoraFMS::Recon::Base::guess_os($$) { } # Use xprobe2 if available - if (-e $self->{pa_config}->{xprobe2}) { - my $output = `"$self->{pa_config}->{xprobe2}" $device 2>$DEVNULL | grep 'Running OS' | head -1`; + if (-x $self->{'pa_config'}->{'xprobe2'}) { + my $return = `"$self->{pa_config}->{xprobe2}" $device 2>$DEVNULL`; if ($? == 0) { + my ($output) = $a =~ /Running OS:(.*)/; return pandora_get_os($self->{'dbh'}, $output); } } # Use nmap by default - if (-e $self->{pa_config}->{nmap}) { - my $output = `"$self->{pa_config}->{nmap}" -F -O $device 2>$DEVNULL | grep 'Aggressive OS guesses'`; + if (-x $self->{'pa_config'}->{'nmap'}) { + my $return = `"$self->{pa_config}->{nmap}" -F -O $device 2>$DEVNULL`; return OS_OTHER if ($? != 0); + + my ($output) = $return =~ /Aggressive OS guesses:\s*(.*)/; return pandora_get_os($self->{'dbh'}, $output); } @@ -377,7 +383,11 @@ sub PandoraFMS::Recon::Base::guess_os($$) { sub PandoraFMS::Recon::Base::tcp_scan ($$) { my ($self, $host) = @_; - my $open_ports = `"$self->{pa_config}->{nmap}" -p$self->{recon_ports} $host | grep open | wc -l`; + my $r = `"$self->{pa_config}->{nmap}" -p$self->{recon_ports} $host`; + + # Same as ""| grep open | wc -l" but multi-OS; + my $open_ports = () = $r =~ /open/gm; + return $open_ports; } diff --git a/pandora_server/lib/PandoraFMS/PluginTools.pm b/pandora_server/lib/PandoraFMS/PluginTools.pm index 650e809570..06f743a067 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.738"; -my $pandora_build = "190920"; +my $pandora_version = "7.0NG.739"; +my $pandora_build = "191003"; our $VERSION = $pandora_version." ".$pandora_build; our %EXPORT_TAGS = ( 'all' => [ qw() ] ); diff --git a/pandora_server/lib/PandoraFMS/Recon/Base.pm b/pandora_server/lib/PandoraFMS/Recon/Base.pm index 223c36f248..335504821e 100644 --- a/pandora_server/lib/PandoraFMS/Recon/Base.pm +++ b/pandora_server/lib/PandoraFMS/Recon/Base.pm @@ -36,7 +36,7 @@ use constant { DISCOVERY_DEPLOY_AGENTS => 9, }; -# /dev/null +# $DEVNULL my $DEVNULL = ($^O eq 'MSWin32') ? '/Nul' : '/dev/null'; # Some useful OIDs. @@ -852,7 +852,7 @@ sub get_routes($) { $self->{'routes'} = []; # Parse route's output. - my @output = `route -n 2>/dev/null`; + my @output = `route -n 2>$DEVNULL`; foreach my $line (@output) { chomp($line); if ($line =~ /^0\.0\.0\.0\s+(\d+\.\d+\.\d+\.\d+).*/) { @@ -1220,7 +1220,7 @@ sub snmp_responds_v3($$) { sub local_arp($) { my ($self) = @_; - my @output = `arp -an 2>/dev/null`; + my @output = `arp -an 2>$DEVNULL`; foreach my $line (@output) { next unless ($line =~ m/\((\S+)\) at ([0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+:[0-9a-f]+)/); $self->add_mac(parse_mac($2), $1); @@ -1286,7 +1286,7 @@ sub ping ($$$) { for (my $i = 0; $i < $retries; $i++) { # Note: There is no timeout option. - `$ping_command -s -n $host 56 $packets >/dev/null 2>&1`; + `$ping_command -s -n $host 56 $packets >$DEVNULL 2>&1`; return 1 if ($? == 0); } @@ -1299,7 +1299,7 @@ sub ping ($$$) { for (my $i = 0; $i < $retries; $i++) { # Note: There is no timeout option for ping6. - `$ping_command -q -n -c $packets $host >/dev/null 2>&1`; + `$ping_command -q -n -c $packets $host >$DEVNULL 2>&1`; return 1 if ($? == 0); } @@ -1312,7 +1312,7 @@ sub ping ($$$) { for (my $i = 0; $i < $retries; $i++) { # Note: There is no timeout option for ping6. - `$ping_command -q -n -c $packets $host >/dev/null 2>&1`; + `$ping_command -q -n -c $packets $host >$DEVNULL 2>&1`; if ($? == 0) { return 1; } @@ -1324,7 +1324,7 @@ sub ping ($$$) { # Assume Linux by default. my $ping_command = $host =~ /\d+:|:\d+/ ? "ping6" : "ping"; for (my $i = 0; $i < $retries; $i++) { - `$ping_command -q -W $timeout -n -c $packets $host >/dev/null 2>&1`; + `$ping_command -q -W $timeout -n -c $packets $host >$DEVNULL 2>&1`; return 1 if ($? == 0); } @@ -1529,99 +1529,101 @@ sub app_scan($) { $self->{'task_data'} ); - if (!$dbObj->is_connected()) { - call('message', 'Cannot connect to target ' . $target, 3); - $global_percent += $global_step; - $self->{'c_network_percent'} = 90; - # Update progress - $self->call('update_progress', $global_percent + (90 / (scalar @targets))); - $self->{'summary'}->{'not_alive'} += 1; - push @modules, { - name => $type . ' connection', - type => 'generic_proc', - data => 0, - description => $type . ' availability' - }; + if (defined($dbObj)) { + if (!$dbObj->is_connected()) { + call('message', 'Cannot connect to target ' . $target, 3); + $global_percent += $global_step; + $self->{'c_network_percent'} = 90; + # Update progress + $self->call('update_progress', $global_percent + (90 / (scalar @targets))); + $self->{'summary'}->{'not_alive'} += 1; + push @modules, { + name => $type . ' connection', + type => 'generic_proc', + data => 0, + description => $type . ' availability' + }; - } else { - my $dbObjCfg = $dbObj->get_config(); + } else { + my $dbObjCfg = $dbObj->get_config(); - $self->{'summary'}->{'discovered'} += 1; - $self->{'summary'}->{'alive'} += 1; + $self->{'summary'}->{'discovered'} += 1; + $self->{'summary'}->{'alive'} += 1; - push @modules, { - name => $type . ' connection', - type => 'generic_proc', - data => 1, - description => $type . ' availability' - }; + push @modules, { + name => $type . ' connection', + type => 'generic_proc', + data => 1, + description => $type . ' availability' + }; - # Analyze. - $self->{'step'} = STEP_STATISTICS; - $self->{'c_network_percent'} = 30; - $self->call('update_progress', $global_percent + (30 / (scalar @targets))); - $self->{'c_network_name'} = $dbObj->get_host(); + # Analyze. + $self->{'step'} = STEP_STATISTICS; + $self->{'c_network_percent'} = 30; + $self->call('update_progress', $global_percent + (30 / (scalar @targets))); + $self->{'c_network_name'} = $dbObj->get_host(); - # Retrieve connection statistics. - # Retrieve uptime statistics - # Retrieve query stats - # Retrieve connections - # Retrieve innodb - # Retrieve cache - $self->{'c_network_percent'} = 50; - $self->call('update_progress', $global_percent + (50 / (scalar @targets))); - push @modules, $dbObj->get_statistics(); + # Retrieve connection statistics. + # Retrieve uptime statistics + # Retrieve query stats + # Retrieve connections + # Retrieve innodb + # Retrieve cache + $self->{'c_network_percent'} = 50; + $self->call('update_progress', $global_percent + (50 / (scalar @targets))); + push @modules, $dbObj->get_statistics(); - # Custom queries. - $self->{'step'} = STEP_CUSTOM_QUERIES; - $self->{'c_network_percent'} = 80; - $self->call('update_progress', $global_percent + (80 / (scalar @targets))); - push @modules, $dbObj->execute_custom_queries(); + # Custom queries. + $self->{'step'} = STEP_CUSTOM_QUERIES; + $self->{'c_network_percent'} = 80; + $self->call('update_progress', $global_percent + (80 / (scalar @targets))); + push @modules, $dbObj->execute_custom_queries(); - if (defined($dbObjCfg->{'scan_databases'}) - && $dbObjCfg->{'scan_databases'} == 1) { - # Skip database scan in Oracle tasks - next if $self->{'type'} == DISCOVERY_APP_ORACLE; + if (defined($dbObjCfg->{'scan_databases'}) + && "$dbObjCfg->{'scan_databases'}" eq "1") { + # Skip database scan in Oracle tasks + next if $self->{'type'} == DISCOVERY_APP_ORACLE; - my $__data = $dbObj->scan_databases(); + my $__data = $dbObj->scan_databases(); - if (ref($__data) eq "ARRAY") { - if (defined($dbObjCfg->{'agent_per_database'}) - && $dbObjCfg->{'agent_per_database'} == 1) { - # Agent per database detected. - push @data, @{$__data}; - } else { - # Merge modules into engine agent. - my @_modules = map { - map { $_ } @{$_->{'module_data'}} - } @{$__data}; + if (ref($__data) eq "ARRAY") { + if (defined($dbObjCfg->{'agent_per_database'}) + && $dbObjCfg->{'agent_per_database'} == 1) { + # Agent per database detected. + push @data, @{$__data}; + } else { + # Merge modules into engine agent. + my @_modules = map { + map { $_ } @{$_->{'module_data'}} + } @{$__data}; - push @modules, @_modules; + push @modules, @_modules; + } } } } + + # Put engine agent at the beginning of the list. + my $version = $dbObj->get_version(); + unshift @data,{ + 'agent_data' => { + 'agent_name' => $dbObj->get_agent_name(), + 'os' => $type, + 'os_version' => (defined($version) ? $version : 'Discovery'), + 'interval' => $self->{'task_data'}->{'interval_sweep'}, + 'id_group' => $self->{'task_data'}->{'id_group'}, + 'address' => $dbObj->get_host(), + 'description' => '', + }, + 'module_data' => \@modules, + }; + + $self->call('create_agents', \@data); + + # Destroy item. + undef($dbObj); } - # Put engine agent at the beginning of the list. - my $version = $dbObj->get_version(); - unshift @data,{ - 'agent_data' => { - 'agent_name' => $dbObj->get_agent_name(), - 'os' => $type, - 'os_version' => (defined($version) ? $version : 'Discovery'), - 'interval' => $self->{'task_data'}->{'interval_sweep'}, - 'id_group' => $self->{'task_data'}->{'id_group'}, - 'address' => $dbObj->get_host(), - 'description' => '', - }, - 'module_data' => \@modules, - }; - - $self->call('create_agents', \@data); - - # Destroy item. - undef($dbObj); - $global_percent += $global_step; $self->{'c_network_percent'} = 100; $self->call('update_progress', $global_percent); @@ -1674,7 +1676,7 @@ sub deploy_scan($) { ########################################################################## sub scan($) { my ($self) = @_; - my ($progress, $step); + my ($progress, $step) = 1, 0; # 1% $self->call('update_progress', 1); @@ -1697,7 +1699,7 @@ sub scan($) { } # Find devices. - $self->call('message', "[1/5] Scanning the network...", 3); + $self->call('message', "[1/4] Scanning the network...", 3); $self->{'step'} = STEP_SCANNING; $self->call('update_progress', $progress); $self->scan_subnet(); @@ -1713,7 +1715,7 @@ sub scan($) { $self->call('delete_connections'); # Connectivity from address forwarding tables. - $self->call('message', "[1/4] Finding address forwarding table connectivity...", 3); + $self->call('message', "[2/4] Finding address forwarding table connectivity...", 3); $self->{'step'} = STEP_AFT; ($progress, $step) = (50, 20.0 / scalar(@hosts)); # From 50% to 70%. for (my $i = 0; defined($hosts[$i]); $i++) { @@ -1831,7 +1833,7 @@ sub snmp_get_command { my ($self, $device, $oid, $community, $vlan) = @_; $vlan = defined($vlan) ? "\@" . $vlan : ''; - my $command = "snmpwalk -M/dev/null -r$self->{'snmp_checks'} -t$self->{'snmp_timeout'} -v$self->{'snmp_version'} -On -Oe "; + my $command = "snmpwalk -M$DEVNULL -r$self->{'snmp_checks'} -t$self->{'snmp_timeout'} -v$self->{'snmp_version'} -On -Oe "; if ($self->{'snmp_version'} eq "3") { if ($self->{'community'}) { # Context $command .= " -N $self->{'community'} "; @@ -1847,7 +1849,7 @@ sub snmp_get_command { $command .= " -c$community$vlan "; } - return "$command $device $oid 2>/dev/null"; + return "$command $device $oid 2>$DEVNULL"; } diff --git a/pandora_server/lib/PandoraFMS/Tools.pm b/pandora_server/lib/PandoraFMS/Tools.pm index 9710913efe..e07a4e9ae6 100755 --- a/pandora_server/lib/PandoraFMS/Tools.pm +++ b/pandora_server/lib/PandoraFMS/Tools.pm @@ -624,7 +624,7 @@ sub logger ($$;$) { $message = safe_output ($message); $level = 1 unless defined ($level); - return if ($level > $pa_config->{'verbosity'}); + return if (!defined ($pa_config->{'verbosity'}) || $level > $pa_config->{'verbosity'}); if (!defined($pa_config->{'log_file'})) { print strftime ("%Y-%m-%d %H:%M:%S", localtime()) . " [V". $level ."] " . $message . "\n"; @@ -994,7 +994,8 @@ sub load_average { $load_average = ((split(/\s+/, `/sbin/sysctl -n vm.loadavg`))[1]); } elsif ($OSNAME eq "MSWin32") { # Windows hasn't got load average. - $load_average = undef; + $load_average = `powershell "(Get-WmiObject win32_processor | Measure-Object -property LoadPercentage -Average).average"`; + chop($load_average); } # by default LINUX calls else { diff --git a/pandora_server/pandora_server.redhat.spec b/pandora_server/pandora_server.redhat.spec index cad6da15d4..3b8c7a9374 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.738 -%define release 190920 +%define version 7.0NG.739 +%define release 191003 Summary: Pandora FMS Server Name: %{name} diff --git a/pandora_server/pandora_server.spec b/pandora_server/pandora_server.spec index e881c44d10..49872651f0 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.738 -%define release 190920 +%define version 7.0NG.739 +%define release 191003 Summary: Pandora FMS Server Name: %{name} diff --git a/pandora_server/pandora_server_installer b/pandora_server/pandora_server_installer index e60ea3d496..d60b1092a0 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.738" -PI_BUILD="190920" +PI_VERSION="7.0NG.739" +PI_BUILD="191003" MODE=$1 if [ $# -gt 1 ]; then diff --git a/pandora_server/util/integria_rticket.pl b/pandora_server/util/integria_rticket.pl index 609dd516b0..f296fd9145 100644 --- a/pandora_server/util/integria_rticket.pl +++ b/pandora_server/util/integria_rticket.pl @@ -38,6 +38,7 @@ Where options: Optional parameters: [-priority ] : 10 Maintance, 0 Informative, 1 Low, 2 Medium, 3 Serious, 4 Very serious + [-owner ] : Free text [-desc ] : Free text [-type ] : Type ID (must exist in Integria IMS) [-inventory ] : Inventory ID (must exist in Integria IMS) @@ -94,6 +95,7 @@ sub tool_api_main () { my $ticket_name = ""; my $group_id = -1; my $ticket_priority = 0; + my $ticket_owner = ''; my $ticket_description = ''; my $ticket_type = ''; my $ticket_inventory = ''; @@ -155,6 +157,9 @@ sub tool_api_main () { if ($line eq '-priority') { $ticket_priority = $ARGV[$i + 1]; } + if ($line eq '-owner') { + $ticket_owner = $ARGV[$i + 1]; + } if ($line eq '-desc') { $ticket_description = $ARGV[$i + 1]; } @@ -193,6 +198,7 @@ sub tool_api_main () { $data_ticket = $ticket_name . "|;|" . $group_id . "|;|" . $ticket_priority . + "|;|" . $ticket_owner . "|;|" . $ticket_description . "|;|" . $ticket_inventory . "|;|" . $ticket_type . diff --git a/pandora_server/util/pandora_db.pl b/pandora_server/util/pandora_db.pl index b6efec70f8..608acfe7e4 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.738 PS190920"; +my $version = "7.0NG.739 PS191003"; # Pandora server configuration my %conf; diff --git a/pandora_server/util/pandora_manage.pl b/pandora_server/util/pandora_manage.pl index 91c63ecc1c..d195c13833 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.738 PS190920"; +my $version = "7.0NG.739 PS191003"; # save program name for logging my $progname = basename($0);