From d9428ce2158c83d5f82c35d0ba1b0ab9107ae6dd Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Mon, 25 Jan 2021 13:33:02 +0100 Subject: [PATCH 01/18] WIP DBMainainer, fixed Websockets library placement --- .../extras/delete_files/delete_files.txt | 5 +- pandora_console/include/lib/Core/Config.php | 79 ++++++ .../include/lib/Core/DBMantainer.php | 250 ++++++++++++++++++ .../lib/{ => Websockets}/WSManager.php | 10 +- .../lib/{ => Websockets}/WebSocketServer.php | 0 .../lib/{ => Websockets}/WebSocketUser.php | 0 .../vendor/composer/ClassLoader.php | 6 +- .../vendor/composer/autoload_classmap.php | 40 +-- .../vendor/composer/autoload_real.php | 4 +- .../vendor/composer/autoload_static.php | 40 +-- .../vendor/composer/platform_check.php | 26 ++ pandora_console/ws.php | 2 +- 12 files changed, 380 insertions(+), 82 deletions(-) create mode 100644 pandora_console/include/lib/Core/Config.php create mode 100644 pandora_console/include/lib/Core/DBMantainer.php rename pandora_console/include/lib/{ => Websockets}/WSManager.php (98%) rename pandora_console/include/lib/{ => Websockets}/WebSocketServer.php (100%) rename pandora_console/include/lib/{ => Websockets}/WebSocketUser.php (100%) create mode 100644 pandora_console/vendor/composer/platform_check.php diff --git a/pandora_console/extras/delete_files/delete_files.txt b/pandora_console/extras/delete_files/delete_files.txt index c647447c84..d18163093d 100644 --- a/pandora_console/extras/delete_files/delete_files.txt +++ b/pandora_console/extras/delete_files/delete_files.txt @@ -68,4 +68,7 @@ enterprise/extensions/ipam/include/javascript/ipam.js enterprise/extensions/ipam/include/javascript/IpamMapController.js enterprise/extensions/ipam/ipam_action.php enterprise/extensions/ipam.php -enterprise/extensions/ipam \ No newline at end of file +enterprise/extensions/ipam +include/lib/WSManager.php +include/lib/WebSocketServer.php +include/lib/WebSocketUser.php \ No newline at end of file diff --git a/pandora_console/include/lib/Core/Config.php b/pandora_console/include/lib/Core/Config.php new file mode 100644 index 0000000000..cda3581ed7 --- /dev/null +++ b/pandora_console/include/lib/Core/Config.php @@ -0,0 +1,79 @@ +user = $params['user']; + $this->pass = $params['pass']; + $this->host = $params['host']; + $this->port = $params['port']; + $this->name = $params['name']; + $this->charset = $params['charset']; + + // Try to connect. + $this->connect(); + } + + + /** + * Connects (if not connected) to current definition. + * + * @return boolean True if successfully connected, false if not. + */ + private function connect() + { + if ($this->connected === true) { + return true; + } + + $dbc = new \mysqli( + $this->host, + $this->user, + $this->pass, + $this->name, + $this->port + ); + + if ($dbc->connect_error === false) { + $this->dbh = null; + $this->connected = false; + $this->lastError = $dbc->connect_errno.': '.$dbc->connect_error; + } else { + $this->dbh = $dbc; + if (empty($this->charset) === false) { + $dbc->set_charset($this->charset); + } + + $this->connected = true; + $this->lastError = null; + } + + } + + + /** + * Retrieve last error. + * + * @return string Error message. + */ + public function getLastError() + { + if ($this->lastError !== null) { + return $this->lastError; + } + + return ''; + } + + + /** + * Install PandoraFMS database schema in current target. + * + * @return boolean Installation is success or not. + */ + public function install() + { + if ($this->connect() !== true) { + return false; + } + + if ($this->installed === true) { + return true; + } + + $this->lastError = 'Pending installation'; + return false; + + } + + + /** + * Updates PandoraFMS database schema in current target. + * + * @return boolean Current installation is up to date. + */ + public function update() + { + if ($this->connect() !== true) { + return false; + } + + if ($this->install() !== true) { + return false; + } + + $this->lastError = 'Pending update'; + return false; + + } + + + /** + * Verifies current target database is connected, installed and updated. + * + * @return boolean Status of the installation. + */ + public function check() + { + if ($this->connect() !== true) { + return false; + } + + if ($this->install() !== true) { + return false; + } + + if ($this->update() !== true) { + return false; + } + + return true; + } + + +} diff --git a/pandora_console/include/lib/WSManager.php b/pandora_console/include/lib/Websockets/WSManager.php similarity index 98% rename from pandora_console/include/lib/WSManager.php rename to pandora_console/include/lib/Websockets/WSManager.php index f7ab3dbddc..a8d4226af7 100644 --- a/pandora_console/include/lib/WSManager.php +++ b/pandora_console/include/lib/Websockets/WSManager.php @@ -40,14 +40,12 @@ */ // Begin. -namespace PandoraFMS\WebSockets; +namespace PandoraFMS\Websockets; use \PandoraFMS\Websockets\WebSocketServer; -use \PandoraFMS\Websockets\WebSocketUser; use \PandoraFMS\User; - -require_once __DIR__.'/../functions.php'; +require_once __DIR__.'/../../functions.php'; /** * Redirects ws communication between two endpoints. @@ -212,7 +210,7 @@ class WSManager extends WebSocketServer */ public function readSocket($user) { - $buffer; + $buffer = ''; $numBytes = socket_recv( $user->socket, @@ -271,7 +269,7 @@ class WSManager extends WebSocketServer { global $config; - $match; + $match = []; $php_session_id = ''; \preg_match( '/PHPSESSID=(.*)/', diff --git a/pandora_console/include/lib/WebSocketServer.php b/pandora_console/include/lib/Websockets/WebSocketServer.php similarity index 100% rename from pandora_console/include/lib/WebSocketServer.php rename to pandora_console/include/lib/Websockets/WebSocketServer.php diff --git a/pandora_console/include/lib/WebSocketUser.php b/pandora_console/include/lib/Websockets/WebSocketUser.php similarity index 100% rename from pandora_console/include/lib/WebSocketUser.php rename to pandora_console/include/lib/Websockets/WebSocketUser.php diff --git a/pandora_console/vendor/composer/ClassLoader.php b/pandora_console/vendor/composer/ClassLoader.php index fce8549f07..1a58957d25 100644 --- a/pandora_console/vendor/composer/ClassLoader.php +++ b/pandora_console/vendor/composer/ClassLoader.php @@ -37,8 +37,8 @@ namespace Composer\Autoload; * * @author Fabien Potencier * @author Jordi Boggiano - * @see http://www.php-fig.org/psr/psr-0/ - * @see http://www.php-fig.org/psr/psr-4/ + * @see https://www.php-fig.org/psr/psr-0/ + * @see https://www.php-fig.org/psr/psr-4/ */ class ClassLoader { @@ -60,7 +60,7 @@ class ClassLoader public function getPrefixes() { if (!empty($this->prefixesPsr0)) { - return call_user_func_array('array_merge', $this->prefixesPsr0); + return call_user_func_array('array_merge', array_values($this->prefixesPsr0)); } return array(); diff --git a/pandora_console/vendor/composer/autoload_classmap.php b/pandora_console/vendor/composer/autoload_classmap.php index ef8c54358d..3cbf2a6065 100644 --- a/pandora_console/vendor/composer/autoload_classmap.php +++ b/pandora_console/vendor/composer/autoload_classmap.php @@ -6,6 +6,7 @@ $vendorDir = dirname(dirname(__FILE__)); $baseDir = dirname($vendorDir); return array( + 'Composer\\InstalledVersions' => $vendorDir . '/composer/InstalledVersions.php', 'DeepCopy\\DeepCopy' => $vendorDir . '/myclabs/deep-copy/src/DeepCopy/DeepCopy.php', 'DeepCopy\\Exception\\CloneException' => $vendorDir . '/myclabs/deep-copy/src/DeepCopy/Exception/CloneException.php', 'DeepCopy\\Exception\\PropertyException' => $vendorDir . '/myclabs/deep-copy/src/DeepCopy/Exception/PropertyException.php', @@ -44,7 +45,6 @@ return array( 'Egulias\\EmailValidator\\Exception\\DomainHyphened' => $vendorDir . '/egulias/email-validator/EmailValidator/Exception/DomainHyphened.php', 'Egulias\\EmailValidator\\Exception\\DotAtEnd' => $vendorDir . '/egulias/email-validator/EmailValidator/Exception/DotAtEnd.php', 'Egulias\\EmailValidator\\Exception\\DotAtStart' => $vendorDir . '/egulias/email-validator/EmailValidator/Exception/DotAtStart.php', - 'Egulias\\EmailValidator\\Exception\\ExpectedQPair' => $vendorDir . '/egulias/email-validator/EmailValidator/Exception/ExpectingQPair.php', 'Egulias\\EmailValidator\\Exception\\ExpectingAT' => $vendorDir . '/egulias/email-validator/EmailValidator/Exception/ExpectingAT.php', 'Egulias\\EmailValidator\\Exception\\ExpectingATEXT' => $vendorDir . '/egulias/email-validator/EmailValidator/Exception/ExpectingATEXT.php', 'Egulias\\EmailValidator\\Exception\\ExpectingCTEXT' => $vendorDir . '/egulias/email-validator/EmailValidator/Exception/ExpectingCTEXT.php', @@ -309,49 +309,20 @@ return array( 'Mpdf\\Utils\\PdfDate' => $vendorDir . '/mpdf/mpdf/src/Utils/PdfDate.php', 'Mpdf\\Utils\\UtfString' => $vendorDir . '/mpdf/mpdf/src/Utils/UtfString.php', 'PandoraFMS\\Agent' => $baseDir . '/include/lib/Agent.php', - 'PandoraFMS\\Dashboard\\AgentModuleWidget' => $baseDir . '/include/lib/Dashboard/Widgets/agent_module.php', - 'PandoraFMS\\Dashboard\\AlertsFiredWidget' => $baseDir . '/include/lib/Dashboard/Widgets/alerts_fired.php', 'PandoraFMS\\Dashboard\\Cell' => $baseDir . '/include/lib/Dashboard/Cell.php', - 'PandoraFMS\\Dashboard\\ClockWidget' => $baseDir . '/include/lib/Dashboard/Widgets/clock.php', - 'PandoraFMS\\Dashboard\\CustomGraphWidget' => $baseDir . '/include/lib/Dashboard/Widgets/custom_graph.php', - 'PandoraFMS\\Dashboard\\EventsListWidget' => $baseDir . '/include/lib/Dashboard/Widgets/events_list.php', - 'PandoraFMS\\Dashboard\\GraphModuleHistogramWidget' => $baseDir . '/include/lib/Dashboard/Widgets/graph_module_histogram.php', - 'PandoraFMS\\Dashboard\\GroupsStatusWidget' => $baseDir . '/include/lib/Dashboard/Widgets/groups_status.php', 'PandoraFMS\\Dashboard\\Manager' => $baseDir . '/include/lib/Dashboard/Manager.php', - 'PandoraFMS\\Dashboard\\MapsMadeByUser' => $baseDir . '/include/lib/Dashboard/Widgets/maps_made_by_user.php', - 'PandoraFMS\\Dashboard\\MapsStatusWidget' => $baseDir . '/include/lib/Dashboard/Widgets/maps_status.php', - 'PandoraFMS\\Dashboard\\ModuleIconWidget' => $baseDir . '/include/lib/Dashboard/Widgets/module_icon.php', - 'PandoraFMS\\Dashboard\\ModuleStatusWidget' => $baseDir . '/include/lib/Dashboard/Widgets/module_status.php', - 'PandoraFMS\\Dashboard\\ModuleTableValueWidget' => $baseDir . '/include/lib/Dashboard/Widgets/module_table_value.php', - 'PandoraFMS\\Dashboard\\ModuleValueWidget' => $baseDir . '/include/lib/Dashboard/Widgets/module_value.php', - 'PandoraFMS\\Dashboard\\MonitorHealthWidget' => $baseDir . '/include/lib/Dashboard/Widgets/monitor_health.php', - 'PandoraFMS\\Dashboard\\NetworkMapWidget' => $baseDir . '/include/lib/Dashboard/Widgets/network_map.php', - 'PandoraFMS\\Dashboard\\PostWidget' => $baseDir . '/include/lib/Dashboard/Widgets/post.php', - 'PandoraFMS\\Dashboard\\ReportsWidget' => $baseDir . '/include/lib/Dashboard/Widgets/reports.php', - 'PandoraFMS\\Dashboard\\SLAPercentWidget' => $baseDir . '/include/lib/Dashboard/Widgets/sla_percent.php', - 'PandoraFMS\\Dashboard\\ServiceMapWidget' => $baseDir . '/include/lib/Dashboard/Widgets/service_map.php', - 'PandoraFMS\\Dashboard\\SingleGraphWidget' => $baseDir . '/include/lib/Dashboard/Widgets/single_graph.php', - 'PandoraFMS\\Dashboard\\SystemGroupStatusWidget' => $baseDir . '/include/lib/Dashboard/Widgets/system_group_status.php', - 'PandoraFMS\\Dashboard\\TacticalWidget' => $baseDir . '/include/lib/Dashboard/Widgets/tactical.php', - 'PandoraFMS\\Dashboard\\TopNEventByGroupWidget' => $baseDir . '/include/lib/Dashboard/Widgets/top_n_events_by_group.php', - 'PandoraFMS\\Dashboard\\TopNEventByModuleWidget' => $baseDir . '/include/lib/Dashboard/Widgets/top_n_events_by_module.php', - 'PandoraFMS\\Dashboard\\TopNWidget' => $baseDir . '/include/lib/Dashboard/Widgets/top_n.php', - 'PandoraFMS\\Dashboard\\TreeViewWidget' => $baseDir . '/include/lib/Dashboard/Widgets/tree_view.php', - 'PandoraFMS\\Dashboard\\UrlWidget' => $baseDir . '/include/lib/Dashboard/Widgets/url.php', - 'PandoraFMS\\Dashboard\\WelcomeWidget' => $baseDir . '/include/lib/Dashboard/Widgets/example.php', 'PandoraFMS\\Dashboard\\Widget' => $baseDir . '/include/lib/Dashboard/Widget.php', - 'PandoraFMS\\Dashboard\\WuxStatsWidget' => $baseDir . '/include/lib/Dashboard/Widgets/wux_transaction_stats.php', - 'PandoraFMS\\Dashboard\\WuxWidget' => $baseDir . '/include/lib/Dashboard/Widgets/wux_transaction.php', 'PandoraFMS\\Entity' => $baseDir . '/include/lib/Entity.php', + 'PandoraFMS\\Event' => $baseDir . '/include/lib/Event.php', 'PandoraFMS\\Group' => $baseDir . '/include/lib/Group.php', 'PandoraFMS\\Module' => $baseDir . '/include/lib/Module.php', 'PandoraFMS\\ModuleStatus' => $baseDir . '/include/lib/ModuleStatus.php', 'PandoraFMS\\ModuleType' => $baseDir . '/include/lib/ModuleType.php', 'PandoraFMS\\User' => $baseDir . '/include/lib/User.php', 'PandoraFMS\\View' => $baseDir . '/include/lib/View.php', - 'PandoraFMS\\WebSockets\\WSManager' => $baseDir . '/include/lib/WSManager.php', - 'PandoraFMS\\Websockets\\WebSocketServer' => $baseDir . '/include/lib/WebSocketServer.php', - 'PandoraFMS\\Websockets\\WebSocketUser' => $baseDir . '/include/lib/WebSocketUser.php', + 'PandoraFMS\\Websockets\\WSManager' => $baseDir . '/include/lib/Websockets/WSManager.php', + 'PandoraFMS\\Websockets\\WebSocketServer' => $baseDir . '/include/lib/Websockets/WebSocketServer.php', + 'PandoraFMS\\Websockets\\WebSocketUser' => $baseDir . '/include/lib/Websockets/WebSocketUser.php', 'Psr\\Log\\AbstractLogger' => $vendorDir . '/psr/log/Psr/Log/AbstractLogger.php', 'Psr\\Log\\InvalidArgumentException' => $vendorDir . '/psr/log/Psr/Log/InvalidArgumentException.php', 'Psr\\Log\\LogLevel' => $vendorDir . '/psr/log/Psr/Log/LogLevel.php', @@ -360,7 +331,6 @@ return array( 'Psr\\Log\\LoggerInterface' => $vendorDir . '/psr/log/Psr/Log/LoggerInterface.php', 'Psr\\Log\\LoggerTrait' => $vendorDir . '/psr/log/Psr/Log/LoggerTrait.php', 'Psr\\Log\\NullLogger' => $vendorDir . '/psr/log/Psr/Log/NullLogger.php', - 'Psr\\Log\\Test\\DummyTest' => $vendorDir . '/psr/log/Psr/Log/Test/LoggerInterfaceTest.php', 'Psr\\Log\\Test\\LoggerInterfaceTest' => $vendorDir . '/psr/log/Psr/Log/Test/LoggerInterfaceTest.php', 'fpdi_pdf_parser' => $vendorDir . '/setasign/fpdi/fpdi_pdf_parser.php', 'pdf_context' => $vendorDir . '/setasign/fpdi/pdf_context.php', diff --git a/pandora_console/vendor/composer/autoload_real.php b/pandora_console/vendor/composer/autoload_real.php index 8576380cc1..33d719ba60 100644 --- a/pandora_console/vendor/composer/autoload_real.php +++ b/pandora_console/vendor/composer/autoload_real.php @@ -22,13 +22,15 @@ class ComposerAutoloaderInitfdecadadce22e6dde51e9535fe4ad7aa return self::$loader; } + require __DIR__ . '/platform_check.php'; + spl_autoload_register(array('ComposerAutoloaderInitfdecadadce22e6dde51e9535fe4ad7aa', 'loadClassLoader'), true, true); self::$loader = $loader = new \Composer\Autoload\ClassLoader(); spl_autoload_unregister(array('ComposerAutoloaderInitfdecadadce22e6dde51e9535fe4ad7aa', 'loadClassLoader')); $useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded()); if ($useStaticLoader) { - require_once __DIR__ . '/autoload_static.php'; + require __DIR__ . '/autoload_static.php'; call_user_func(\Composer\Autoload\ComposerStaticInitfdecadadce22e6dde51e9535fe4ad7aa::getInitializer($loader)); } else { diff --git a/pandora_console/vendor/composer/autoload_static.php b/pandora_console/vendor/composer/autoload_static.php index ee0f05a330..62260fc4b8 100644 --- a/pandora_console/vendor/composer/autoload_static.php +++ b/pandora_console/vendor/composer/autoload_static.php @@ -88,6 +88,7 @@ class ComposerStaticInitfdecadadce22e6dde51e9535fe4ad7aa ); public static $classMap = array ( + 'Composer\\InstalledVersions' => __DIR__ . '/..' . '/composer/InstalledVersions.php', 'DeepCopy\\DeepCopy' => __DIR__ . '/..' . '/myclabs/deep-copy/src/DeepCopy/DeepCopy.php', 'DeepCopy\\Exception\\CloneException' => __DIR__ . '/..' . '/myclabs/deep-copy/src/DeepCopy/Exception/CloneException.php', 'DeepCopy\\Exception\\PropertyException' => __DIR__ . '/..' . '/myclabs/deep-copy/src/DeepCopy/Exception/PropertyException.php', @@ -126,7 +127,6 @@ class ComposerStaticInitfdecadadce22e6dde51e9535fe4ad7aa 'Egulias\\EmailValidator\\Exception\\DomainHyphened' => __DIR__ . '/..' . '/egulias/email-validator/EmailValidator/Exception/DomainHyphened.php', 'Egulias\\EmailValidator\\Exception\\DotAtEnd' => __DIR__ . '/..' . '/egulias/email-validator/EmailValidator/Exception/DotAtEnd.php', 'Egulias\\EmailValidator\\Exception\\DotAtStart' => __DIR__ . '/..' . '/egulias/email-validator/EmailValidator/Exception/DotAtStart.php', - 'Egulias\\EmailValidator\\Exception\\ExpectedQPair' => __DIR__ . '/..' . '/egulias/email-validator/EmailValidator/Exception/ExpectingQPair.php', 'Egulias\\EmailValidator\\Exception\\ExpectingAT' => __DIR__ . '/..' . '/egulias/email-validator/EmailValidator/Exception/ExpectingAT.php', 'Egulias\\EmailValidator\\Exception\\ExpectingATEXT' => __DIR__ . '/..' . '/egulias/email-validator/EmailValidator/Exception/ExpectingATEXT.php', 'Egulias\\EmailValidator\\Exception\\ExpectingCTEXT' => __DIR__ . '/..' . '/egulias/email-validator/EmailValidator/Exception/ExpectingCTEXT.php', @@ -391,49 +391,20 @@ class ComposerStaticInitfdecadadce22e6dde51e9535fe4ad7aa 'Mpdf\\Utils\\PdfDate' => __DIR__ . '/..' . '/mpdf/mpdf/src/Utils/PdfDate.php', 'Mpdf\\Utils\\UtfString' => __DIR__ . '/..' . '/mpdf/mpdf/src/Utils/UtfString.php', 'PandoraFMS\\Agent' => __DIR__ . '/../..' . '/include/lib/Agent.php', - 'PandoraFMS\\Dashboard\\AgentModuleWidget' => __DIR__ . '/../..' . '/include/lib/Dashboard/Widgets/agent_module.php', - 'PandoraFMS\\Dashboard\\AlertsFiredWidget' => __DIR__ . '/../..' . '/include/lib/Dashboard/Widgets/alerts_fired.php', 'PandoraFMS\\Dashboard\\Cell' => __DIR__ . '/../..' . '/include/lib/Dashboard/Cell.php', - 'PandoraFMS\\Dashboard\\ClockWidget' => __DIR__ . '/../..' . '/include/lib/Dashboard/Widgets/clock.php', - 'PandoraFMS\\Dashboard\\CustomGraphWidget' => __DIR__ . '/../..' . '/include/lib/Dashboard/Widgets/custom_graph.php', - 'PandoraFMS\\Dashboard\\EventsListWidget' => __DIR__ . '/../..' . '/include/lib/Dashboard/Widgets/events_list.php', - 'PandoraFMS\\Dashboard\\GraphModuleHistogramWidget' => __DIR__ . '/../..' . '/include/lib/Dashboard/Widgets/graph_module_histogram.php', - 'PandoraFMS\\Dashboard\\GroupsStatusWidget' => __DIR__ . '/../..' . '/include/lib/Dashboard/Widgets/groups_status.php', 'PandoraFMS\\Dashboard\\Manager' => __DIR__ . '/../..' . '/include/lib/Dashboard/Manager.php', - 'PandoraFMS\\Dashboard\\MapsMadeByUser' => __DIR__ . '/../..' . '/include/lib/Dashboard/Widgets/maps_made_by_user.php', - 'PandoraFMS\\Dashboard\\MapsStatusWidget' => __DIR__ . '/../..' . '/include/lib/Dashboard/Widgets/maps_status.php', - 'PandoraFMS\\Dashboard\\ModuleIconWidget' => __DIR__ . '/../..' . '/include/lib/Dashboard/Widgets/module_icon.php', - 'PandoraFMS\\Dashboard\\ModuleStatusWidget' => __DIR__ . '/../..' . '/include/lib/Dashboard/Widgets/module_status.php', - 'PandoraFMS\\Dashboard\\ModuleTableValueWidget' => __DIR__ . '/../..' . '/include/lib/Dashboard/Widgets/module_table_value.php', - 'PandoraFMS\\Dashboard\\ModuleValueWidget' => __DIR__ . '/../..' . '/include/lib/Dashboard/Widgets/module_value.php', - 'PandoraFMS\\Dashboard\\MonitorHealthWidget' => __DIR__ . '/../..' . '/include/lib/Dashboard/Widgets/monitor_health.php', - 'PandoraFMS\\Dashboard\\NetworkMapWidget' => __DIR__ . '/../..' . '/include/lib/Dashboard/Widgets/network_map.php', - 'PandoraFMS\\Dashboard\\PostWidget' => __DIR__ . '/../..' . '/include/lib/Dashboard/Widgets/post.php', - 'PandoraFMS\\Dashboard\\ReportsWidget' => __DIR__ . '/../..' . '/include/lib/Dashboard/Widgets/reports.php', - 'PandoraFMS\\Dashboard\\SLAPercentWidget' => __DIR__ . '/../..' . '/include/lib/Dashboard/Widgets/sla_percent.php', - 'PandoraFMS\\Dashboard\\ServiceMapWidget' => __DIR__ . '/../..' . '/include/lib/Dashboard/Widgets/service_map.php', - 'PandoraFMS\\Dashboard\\SingleGraphWidget' => __DIR__ . '/../..' . '/include/lib/Dashboard/Widgets/single_graph.php', - 'PandoraFMS\\Dashboard\\SystemGroupStatusWidget' => __DIR__ . '/../..' . '/include/lib/Dashboard/Widgets/system_group_status.php', - 'PandoraFMS\\Dashboard\\TacticalWidget' => __DIR__ . '/../..' . '/include/lib/Dashboard/Widgets/tactical.php', - 'PandoraFMS\\Dashboard\\TopNEventByGroupWidget' => __DIR__ . '/../..' . '/include/lib/Dashboard/Widgets/top_n_events_by_group.php', - 'PandoraFMS\\Dashboard\\TopNEventByModuleWidget' => __DIR__ . '/../..' . '/include/lib/Dashboard/Widgets/top_n_events_by_module.php', - 'PandoraFMS\\Dashboard\\TopNWidget' => __DIR__ . '/../..' . '/include/lib/Dashboard/Widgets/top_n.php', - 'PandoraFMS\\Dashboard\\TreeViewWidget' => __DIR__ . '/../..' . '/include/lib/Dashboard/Widgets/tree_view.php', - 'PandoraFMS\\Dashboard\\UrlWidget' => __DIR__ . '/../..' . '/include/lib/Dashboard/Widgets/url.php', - 'PandoraFMS\\Dashboard\\WelcomeWidget' => __DIR__ . '/../..' . '/include/lib/Dashboard/Widgets/example.php', 'PandoraFMS\\Dashboard\\Widget' => __DIR__ . '/../..' . '/include/lib/Dashboard/Widget.php', - 'PandoraFMS\\Dashboard\\WuxStatsWidget' => __DIR__ . '/../..' . '/include/lib/Dashboard/Widgets/wux_transaction_stats.php', - 'PandoraFMS\\Dashboard\\WuxWidget' => __DIR__ . '/../..' . '/include/lib/Dashboard/Widgets/wux_transaction.php', 'PandoraFMS\\Entity' => __DIR__ . '/../..' . '/include/lib/Entity.php', + 'PandoraFMS\\Event' => __DIR__ . '/../..' . '/include/lib/Event.php', 'PandoraFMS\\Group' => __DIR__ . '/../..' . '/include/lib/Group.php', 'PandoraFMS\\Module' => __DIR__ . '/../..' . '/include/lib/Module.php', 'PandoraFMS\\ModuleStatus' => __DIR__ . '/../..' . '/include/lib/ModuleStatus.php', 'PandoraFMS\\ModuleType' => __DIR__ . '/../..' . '/include/lib/ModuleType.php', 'PandoraFMS\\User' => __DIR__ . '/../..' . '/include/lib/User.php', 'PandoraFMS\\View' => __DIR__ . '/../..' . '/include/lib/View.php', - 'PandoraFMS\\WebSockets\\WSManager' => __DIR__ . '/../..' . '/include/lib/WSManager.php', - 'PandoraFMS\\Websockets\\WebSocketServer' => __DIR__ . '/../..' . '/include/lib/WebSocketServer.php', - 'PandoraFMS\\Websockets\\WebSocketUser' => __DIR__ . '/../..' . '/include/lib/WebSocketUser.php', + 'PandoraFMS\\Websockets\\WSManager' => __DIR__ . '/../..' . '/include/lib/Websockets/WSManager.php', + 'PandoraFMS\\Websockets\\WebSocketServer' => __DIR__ . '/../..' . '/include/lib/Websockets/WebSocketServer.php', + 'PandoraFMS\\Websockets\\WebSocketUser' => __DIR__ . '/../..' . '/include/lib/Websockets/WebSocketUser.php', 'Psr\\Log\\AbstractLogger' => __DIR__ . '/..' . '/psr/log/Psr/Log/AbstractLogger.php', 'Psr\\Log\\InvalidArgumentException' => __DIR__ . '/..' . '/psr/log/Psr/Log/InvalidArgumentException.php', 'Psr\\Log\\LogLevel' => __DIR__ . '/..' . '/psr/log/Psr/Log/LogLevel.php', @@ -442,7 +413,6 @@ class ComposerStaticInitfdecadadce22e6dde51e9535fe4ad7aa 'Psr\\Log\\LoggerInterface' => __DIR__ . '/..' . '/psr/log/Psr/Log/LoggerInterface.php', 'Psr\\Log\\LoggerTrait' => __DIR__ . '/..' . '/psr/log/Psr/Log/LoggerTrait.php', 'Psr\\Log\\NullLogger' => __DIR__ . '/..' . '/psr/log/Psr/Log/NullLogger.php', - 'Psr\\Log\\Test\\DummyTest' => __DIR__ . '/..' . '/psr/log/Psr/Log/Test/LoggerInterfaceTest.php', 'Psr\\Log\\Test\\LoggerInterfaceTest' => __DIR__ . '/..' . '/psr/log/Psr/Log/Test/LoggerInterfaceTest.php', 'fpdi_pdf_parser' => __DIR__ . '/..' . '/setasign/fpdi/fpdi_pdf_parser.php', 'pdf_context' => __DIR__ . '/..' . '/setasign/fpdi/pdf_context.php', diff --git a/pandora_console/vendor/composer/platform_check.php b/pandora_console/vendor/composer/platform_check.php new file mode 100644 index 0000000000..f79e574be7 --- /dev/null +++ b/pandora_console/vendor/composer/platform_check.php @@ -0,0 +1,26 @@ += 70000)) { + $issues[] = 'Your Composer dependencies require a PHP version ">= 7.0.0". You are running ' . PHP_VERSION . '.'; +} + +if ($issues) { + if (!headers_sent()) { + header('HTTP/1.1 500 Internal Server Error'); + } + if (!ini_get('display_errors')) { + if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') { + fwrite(STDERR, 'Composer detected issues in your platform:' . PHP_EOL.PHP_EOL . implode(PHP_EOL, $issues) . PHP_EOL.PHP_EOL); + } elseif (!headers_sent()) { + echo 'Composer detected issues in your platform:' . PHP_EOL.PHP_EOL . str_replace('You are running '.PHP_VERSION.'.', '', implode(PHP_EOL, $issues)) . PHP_EOL.PHP_EOL; + } + } + trigger_error( + 'Composer detected issues in your platform: ' . implode(' ', $issues), + E_USER_ERROR + ); +} diff --git a/pandora_console/ws.php b/pandora_console/ws.php index 0c9509720c..96148761b9 100644 --- a/pandora_console/ws.php +++ b/pandora_console/ws.php @@ -28,7 +28,7 @@ // Begin. require_once __DIR__.'/vendor/autoload.php'; -use \PandoraFMS\WebSockets\WSManager; +use \PandoraFMS\Websockets\WSManager; // Set to true to get full output. $debug = false; From 5799c3e19987a3029ef27a70a7a62cbfa057a6a3 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Mon, 25 Jan 2021 13:35:58 +0100 Subject: [PATCH 02/18] composer paths updated --- pandora_console/vendor/composer/autoload_classmap.php | 2 ++ pandora_console/vendor/composer/autoload_static.php | 2 ++ 2 files changed, 4 insertions(+) diff --git a/pandora_console/vendor/composer/autoload_classmap.php b/pandora_console/vendor/composer/autoload_classmap.php index 3cbf2a6065..4938966edb 100644 --- a/pandora_console/vendor/composer/autoload_classmap.php +++ b/pandora_console/vendor/composer/autoload_classmap.php @@ -309,6 +309,8 @@ return array( 'Mpdf\\Utils\\PdfDate' => $vendorDir . '/mpdf/mpdf/src/Utils/PdfDate.php', 'Mpdf\\Utils\\UtfString' => $vendorDir . '/mpdf/mpdf/src/Utils/UtfString.php', 'PandoraFMS\\Agent' => $baseDir . '/include/lib/Agent.php', + 'PandoraFMS\\Core\\Config' => $baseDir . '/include/lib/Core/Config.php', + 'PandoraFMS\\Core\\DBMantainer' => $baseDir . '/include/lib/Core/DBMantainer.php', 'PandoraFMS\\Dashboard\\Cell' => $baseDir . '/include/lib/Dashboard/Cell.php', 'PandoraFMS\\Dashboard\\Manager' => $baseDir . '/include/lib/Dashboard/Manager.php', 'PandoraFMS\\Dashboard\\Widget' => $baseDir . '/include/lib/Dashboard/Widget.php', diff --git a/pandora_console/vendor/composer/autoload_static.php b/pandora_console/vendor/composer/autoload_static.php index 62260fc4b8..dcc588664c 100644 --- a/pandora_console/vendor/composer/autoload_static.php +++ b/pandora_console/vendor/composer/autoload_static.php @@ -391,6 +391,8 @@ class ComposerStaticInitfdecadadce22e6dde51e9535fe4ad7aa 'Mpdf\\Utils\\PdfDate' => __DIR__ . '/..' . '/mpdf/mpdf/src/Utils/PdfDate.php', 'Mpdf\\Utils\\UtfString' => __DIR__ . '/..' . '/mpdf/mpdf/src/Utils/UtfString.php', 'PandoraFMS\\Agent' => __DIR__ . '/../..' . '/include/lib/Agent.php', + 'PandoraFMS\\Core\\Config' => __DIR__ . '/../..' . '/include/lib/Core/Config.php', + 'PandoraFMS\\Core\\DBMantainer' => __DIR__ . '/../..' . '/include/lib/Core/DBMantainer.php', 'PandoraFMS\\Dashboard\\Cell' => __DIR__ . '/../..' . '/include/lib/Dashboard/Cell.php', 'PandoraFMS\\Dashboard\\Manager' => __DIR__ . '/../..' . '/include/lib/Dashboard/Manager.php', 'PandoraFMS\\Dashboard\\Widget' => __DIR__ . '/../..' . '/include/lib/Dashboard/Widget.php', From 192a635599975cdb5b80ebbd54ef347dedcb5947 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Mon, 25 Jan 2021 19:29:22 +0100 Subject: [PATCH 03/18] WIP setup.hdb --- .../godmode/wizards/Wizard.main.php | 2 - pandora_console/include/functions_ui.php | 14 +- pandora_console/include/lib/Core/Config.php | 70 +++++-- .../include/lib/Core/DBMantainer.php | 194 +++++++++++++++++- pandora_console/include/styles/pandora.css | 4 + pandora_console/include/styles/setup.css | 5 + 6 files changed, 265 insertions(+), 24 deletions(-) create mode 100644 pandora_console/include/styles/setup.css diff --git a/pandora_console/godmode/wizards/Wizard.main.php b/pandora_console/godmode/wizards/Wizard.main.php index 6783ba20e1..3485dec627 100644 --- a/pandora_console/godmode/wizards/Wizard.main.php +++ b/pandora_console/godmode/wizards/Wizard.main.php @@ -32,8 +32,6 @@ global $config; require_once $config['homedir'].'/vendor/autoload.php'; require_once $config['homedir'].'/include/class/HTML.class.php'; -use \HTML; - /** * Global Wizard generic class. Needs to be inherited. * diff --git a/pandora_console/include/functions_ui.php b/pandora_console/include/functions_ui.php index cc72d93f5f..3ec6eec22c 100755 --- a/pandora_console/include/functions_ui.php +++ b/pandora_console/include/functions_ui.php @@ -3935,7 +3935,19 @@ function ui_toggle( /** * Simplified way of ui_toggle ussage. * - * @param array $data Arguments. + * @param array $data Arguments: + * - content + * - name + * - title + * - id + * - hidden_default + * - return + * - toggle_class + * - container_class + * - main_class + * - img_a + * - img_b + * - clean. * * @return string HTML code with toggle content. */ diff --git a/pandora_console/include/lib/Core/Config.php b/pandora_console/include/lib/Core/Config.php index cda3581ed7..51b9f09c63 100644 --- a/pandora_console/include/lib/Core/Config.php +++ b/pandora_console/include/lib/Core/Config.php @@ -29,8 +29,8 @@ // Begin. namespace PandoraFMS\Core; -require_once __DIR__.'/../../include/config.php'; -require_once __DIR__.'/../../include/functions_config.php'; +require_once __DIR__.'/../../config.php'; +require_once __DIR__.'/../../functions_config.php'; /** * Config class to operate console configuration. @@ -38,21 +38,57 @@ require_once __DIR__.'/../../include/functions_config.php'; final class Config { + /** + * History database settings (tconfig). + * + * @var array + */ + private static $settings = []; + + + /** + * Load history database settings. + */ + private static function loadHistoryDBSettings() + { + if (self::$settings === null) { + $data = \db_get_all_rows_filter('tconfig', [], false, 'AND', true); + self::$settings = array_reduce( + $data, + function ($carry, $item) { + $carry[$item['token']] = $item['value']; + }, + [] + ); + } + } + /** * Retrieve configuration token. * - * @param string $token Token to retrieve. - * @param mixed $default Default value if not found. + * @param string $token Token to retrieve. + * @param mixed $default Default value if not found. + * @param boolean $history_db Search for token in history_db. * * @return mixed Configuration token. */ - public static function get(string $token, $default=null) - { - global $config; + public static function get( + string $token, + $default=null, + bool $history_db=false + ) { + if ($history_db === true) { + self::loadHistoryDBSettings(); + if (isset(self::$settings[$token]) === true) { + return self::$settings[$token]; + } + } else { + global $config; - if (isset($config[$token]) === true) { - return $config[$token]; + if (isset($config[$token]) === true) { + return $config[$token]; + } } return $default; @@ -63,15 +99,21 @@ final class Config /** * Set configuration token. * - * @param string $token Token to set. - * @param mixed $value Value to be. + * @param string $token Token to set. + * @param mixed $value Value to be. + * @param boolean $history_db Save to history_db settings. * * @return void */ - public static function set(string $token, $value) + public static function set(string $token, $value, bool $history_db=false) { - if (self::get($token) === null) { - config_update_value($token, $value); + if ($history_db !== false) { + if (self::get($token, null, $history_db) === null) { + } + } else { + if (self::get($token) === null) { + config_update_value($token, $value); + } } } diff --git a/pandora_console/include/lib/Core/DBMantainer.php b/pandora_console/include/lib/Core/DBMantainer.php index 1e8b6d80c3..d08a34bbd7 100644 --- a/pandora_console/include/lib/Core/DBMantainer.php +++ b/pandora_console/include/lib/Core/DBMantainer.php @@ -34,6 +34,12 @@ namespace PandoraFMS\Core; */ final class DBMantainer { + const ESSENTIAL_TABLES = [ + 'tagente_datos', + 'tagente_datos_string', + 'tevento', + 'tconfig', + ]; /** * Database user. @@ -105,6 +111,13 @@ final class DBMantainer */ private $lastError; + /** + * Connected to engine and database. + * + * @var boolean + */ + private $ready; + /** * Initialize DBMaintainer object. @@ -146,11 +159,11 @@ final class DBMantainer $this->host, $this->user, $this->pass, - $this->name, + null, $this->port ); - if ($dbc->connect_error === false) { + if ((bool) $dbc->connect_error === true) { $this->dbh = null; $this->connected = false; $this->lastError = $dbc->connect_errno.': '.$dbc->connect_error; @@ -160,10 +173,16 @@ final class DBMantainer $dbc->set_charset($this->charset); } - $this->connected = true; - $this->lastError = null; - } + if ($this->dbh->select_db($this->name) === false) { + $this->lastError = $this->dbh->errno.': '.$this->dbh->error; + $this->ready = false; + } else { + $this->lastError = null; + $this->ready = true; + } + $this->connected = true; + } } @@ -182,6 +201,100 @@ final class DBMantainer } + /** + * Retrieve all rows from given query in array format. + * + * @param string $query Query. + * + * @return array Results. + */ + private function getAllRows(string $query) + { + if ($this->ready !== true) { + $this->lastError = $this->dbh->errno.': '.$this->dbh->error; + return []; + } + + $rs = $this->dbh->query($query); + + $results = []; + + do { + $row = $rs->fetch_array(MYSQLI_ASSOC); + if ((bool) $row !== false) { + $results[] = $row; + } + } while ((bool) $row !== false); + + return $results; + } + + + /** + * Verifies schema against running db. + * + * @return boolean Success or not. + */ + public function verifySchema() + { + if ($this->ready !== true) { + return false; + } + + $missing_essential_tables = $this->verifyTables(); + + return !(bool) count($missing_essential_tables); + } + + + /** + * Verifies tables against running db. + * + * @return boolean Applied or not. + */ + public function verifyTables() + { + global $config; + + $t = \db_get_all_rows_sql( + sprintf( + 'SHOW TABLES FROM %s', + $config['dbname'] + ) + ); + + $tables = []; + foreach ($t as $v) { + $tables[] = array_shift($v); + } + + $t = $this->getAllRows( + sprintf( + 'SHOW TABLES FROM %s', + $this->name + ) + ); + $myTables = []; + foreach ($t as $k => $v) { + $myTables[] = array_shift($v); + } + + $differences = array_diff($tables, $myTables); + + if (count($differences) > 0) { + $this->lastError = sprintf( + 'Warning, following tables does not exist in target: %s', + join(', ', $differences) + ); + } + + // Exclude extension tables. + $differences = array_intersect($differences, self::ESSENTIAL_TABLES); + + return $differences; + } + + /** * Install PandoraFMS database schema in current target. * @@ -197,8 +310,33 @@ final class DBMantainer return true; } - $this->lastError = 'Pending installation'; - return false; + if ($this->ready !== true) { + // Not ready, create database in target. + $rc = $this->dbh->query( + sprintf( + 'CREATE DATABASE %s', + $this->name + ) + ); + + if ($rc === false) { + $this->lastError = $this->dbh->errno.': '.$this->dbh->error; + return false; + } + + if ($this->dbh->select_db($this->name) === false) { + $this->lastError = $this->dbh->errno.': '.$this->dbh->error; + return false; + } + + // Already connected and ready to execute commands. + $this->ready = true; + } else if ($this->verifySchema() === true) { + $this->installed = true; + return true; + } + + return $this->applyDump(Config::get('homedir', '').'/pandoradb.sql'); } @@ -247,4 +385,46 @@ final class DBMantainer } + /** + * This function keeps same functionality as install.php:parse_mysqli_dump. + * + * @param string $path Path where SQL dump file is stored. + * + * @return boolean Success or not. + */ + private function applyDump(string $path) + { + if (file_exists($path) === true) { + $file_content = file($path); + $query = ''; + foreach ($file_content as $sql_line) { + if (trim($sql_line) !== '' + && strpos($sql_line, '-- ') === false + ) { + $query .= $sql_line; + if ((bool) preg_match("/;[\040]*\$/", $sql_line) === true) { + $result = $this->dbh->query($query); + if ((bool) $result === false) { + $this->lastError = $this->dbh->errnum.': '; + $this->lastERror .= $this->dbh->error; + return false; + } + + $query = ''; + } + } + } + + return true; + } + + // File does not exist. + $this->lastError = sprintf( + 'File %s does not exist', + $path + ); + return false; + } + + } diff --git a/pandora_console/include/styles/pandora.css b/pandora_console/include/styles/pandora.css index f3a6959cba..a1b21bd24b 100644 --- a/pandora_console/include/styles/pandora.css +++ b/pandora_console/include/styles/pandora.css @@ -478,6 +478,10 @@ select:-internal-list-box { width: 290px; max-width: 290px; } +.w600px { + width: 600px; + max-width: 600px; +} .mw120px { min-width: 120px; } diff --git a/pandora_console/include/styles/setup.css b/pandora_console/include/styles/setup.css new file mode 100644 index 0000000000..331bc7aa2e --- /dev/null +++ b/pandora_console/include/styles/setup.css @@ -0,0 +1,5 @@ +span.subtitle { + font-size: 1.3em; + font-weight: normal; + font-family: "lato-bolder"; +} From c7706a6bfaaa8cbb33080af0208baeccca6ac8f7 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Wed, 27 Jan 2021 12:35:37 +0100 Subject: [PATCH 04/18] WIP Historical database manager (setup) --- pandora_console/godmode/setup/setup.php | 12 ++ pandora_console/include/functions_config.php | 114 ++++++++++++++++-- pandora_console/include/functions_ui.php | 59 ++++++--- pandora_console/include/lib/Core/Config.php | 84 +++++++++++-- .../{DBMantainer.php => DBMaintainer.php} | 2 +- pandora_console/include/styles/pandora.css | 2 +- pandora_console/include/styles/setup.css | 10 ++ .../vendor/composer/autoload_classmap.php | 2 +- .../vendor/composer/autoload_static.php | 2 +- 9 files changed, 237 insertions(+), 50 deletions(-) rename pandora_console/include/lib/Core/{DBMantainer.php => DBMaintainer.php} (99%) diff --git a/pandora_console/godmode/setup/setup.php b/pandora_console/godmode/setup/setup.php index e77d314f4c..d1df683325 100644 --- a/pandora_console/godmode/setup/setup.php +++ b/pandora_console/godmode/setup/setup.php @@ -248,6 +248,18 @@ if (isset($config['error_config_update_config'])) { ui_print_success_message(__('Correct update the setup options')); } + if (is_array($config['error_config_update_config']['errors']) === true) { + foreach ($config['error_config_update_config']['errors'] as $msg) { + ui_print_error_message($msg); + } + } + + if (is_array($config['error_config_update_config']['warnings']) === true) { + foreach ($config['error_config_update_config']['warnings'] as $msg) { + ui_print_warning_message($msg); + } + } + unset($config['error_config_update_config']); } diff --git a/pandora_console/include/functions_config.php b/pandora_console/include/functions_config.php index b9b06fabc6..57058a59a0 100644 --- a/pandora_console/include/functions_config.php +++ b/pandora_console/include/functions_config.php @@ -26,7 +26,10 @@ * ============================================================================ */ - // Config functions. +// Config functions. +require_once __DIR__.'/../vendor/autoload.php'; +use PandoraFMS\Core\DBMaintainer; +use PandoraFMS\Core\Config; /** @@ -147,6 +150,8 @@ function config_update_config() } $error_update = []; + $errors = []; + $warnings = []; $sec2 = get_parameter('sec2'); @@ -1448,6 +1453,27 @@ function config_update_config() break; case 'hist_db': + if ($config['dbname'] == get_parameter('history_db_name') + && $config['dbport'] == get_parameter('history_db_port') + && $config['dbhost'] == io_input_password(get_parameter('history_db_host')) + ) { + // Same definition for active and historical database! + // This is a critical error. + $errors[] = __('Active and historical database cannot be the same.'); + } else { + if (!config_update_value('history_db_host', get_parameter('history_db_host'))) { + $error_update[] = __('Host'); + } + + if (!config_update_value('history_db_port', get_parameter('history_db_port'))) { + $error_update[] = __('Port'); + } + + if (!config_update_value('history_db_name', get_parameter('history_db_name'))) { + $error_update[] = __('Database name'); + } + } + if (!config_update_value('history_db_enabled', get_parameter('history_db_enabled'))) { $error_update[] = __('Enable history database'); } @@ -1456,18 +1482,6 @@ function config_update_config() $error_update[] = __('Enable history event'); } - if (!config_update_value('history_db_host', get_parameter('history_db_host'))) { - $error_update[] = __('Host'); - } - - if (!config_update_value('history_db_port', get_parameter('history_db_port'))) { - $error_update[] = __('Port'); - } - - if (!config_update_value('history_db_name', get_parameter('history_db_name'))) { - $error_update[] = __('Database name'); - } - if (!config_update_value('history_db_user', get_parameter('history_db_user'))) { $error_update[] = __('Database user'); } @@ -1507,6 +1521,72 @@ function config_update_config() ) { $error_update[] = __('Delay'); } + + if ((bool) $config['history_db_enabled'] === true) { + $dbm = new DBMaintainer( + [ + 'host' => $config['history_db_host'], + 'port' => $config['history_db_port'], + 'name' => $config['history_db_name'], + 'user' => $config['history_db_user'], + 'pass' => $config['history_db_pass'], + ] + ); + + // Performs several checks and installs if needed. + if ($dbm->check() === false) { + $errors[] = $dbm->getLastError(); + } + } + + // Historical configuration tokens (stored in historical db). + if (Config::set( + 'days_purge', + get_parameter('history_dbh_purge'), + true + ) !== true + ) { + $error_update[] = __('Historical database purge'); + } + + if (Config::set( + 'days_compact', + get_parameter('history_dbh_days_compact'), + true + ) !== true + ) { + $error_update[] = __('Historical database days compact'); + } + + if (Config::set( + 'step_compact', + get_parameter('history_dbh_step_compact'), + true + ) !== true + ) { + $error_update[] = __('Historical database step compact'); + } + + if (Config::set( + 'event_purge', + get_parameter('history_dbh_events_purge'), + true + ) !== true + ) { + $error_update[] = __('Historical database events purge'); + } + + if (Config::set( + 'string_purge', + get_parameter('history_dbh_string_purge'), + true + ) !== true + ) { + $error_update[] = __('Historical database string purge'); + } + + // Disable history db in history db. + Config::set('history_db_enabled', 0, true); break; case 'ehorus': @@ -1675,6 +1755,14 @@ function config_update_config() $config['error_config_update_config']['correct'] = true; } + if (count($errors) > 0) { + $config['error_config_update_config']['errors'] = $errors; + } + + if (count($warnings) > 0) { + $config['error_config_update_config']['warnings'] = $warnings; + } + enterprise_include_once('include/functions_policies.php'); $enterprise = enterprise_include_once('include/functions_skins.php'); if ($enterprise !== ENTERPRISE_NOT_HOOK) { diff --git a/pandora_console/include/functions_ui.php b/pandora_console/include/functions_ui.php index 3ec6eec22c..5b57d81870 100755 --- a/pandora_console/include/functions_ui.php +++ b/pandora_console/include/functions_ui.php @@ -3747,18 +3747,22 @@ function ui_print_event_priority( /** * Print a code into a DIV and enable a toggle to show and hide it. * - * @param string $code Html code. - * @param string $name Name of the link. - * @param string $title Title of the link. - * @param string $id Block id. - * @param boolean $hidden_default If the div will be hidden by default (default: true). - * @param boolean $return Whether to return an output string or echo now (default: true). - * @param string $toggle_class Toggle class. - * @param string $container_class Container class. - * @param string $main_class Main object class. - * @param string $img_a Image (closed). - * @param string $img_b Image (opened). - * @param string $clean Do not encapsulate with class boxes, clean print. + * @param string $code Html code. + * @param string $name Name of the link. + * @param string $title Title of the link. + * @param string $id Block id. + * @param boolean $hidden_default If the div will be hidden by default (default: true). + * @param boolean $return Whether to return an output string or echo now (default: true). + * @param string $toggle_class Toggle class. + * @param string $container_class Container class. + * @param string $main_class Main object class. + * @param string $img_a Image (closed). + * @param string $img_b Image (opened). + * @param string $clean Do not encapsulate with class boxes, clean print. + * @param boolean $reverseImg Reverse image. + * @param boolean $swtich Use switch input instead image. + * @param string $attributes_switch Switch attributes (class...). + * @param string|null $switch_name Use custom switch input name or generate one. * * @return string HTML. */ @@ -3777,7 +3781,8 @@ function ui_toggle( $clean=false, $reverseImg=false, $switch=false, - $attributes_switch='' + $attributes_switch='', + $switch_name=null ) { // Generate unique Id. $uniqid = uniqid(''); @@ -3813,7 +3818,9 @@ function ui_toggle( $main_class = ''; } - $container_class = 'white-box-content-clean'; + if ($container_class == 'white-box-content') { + $container_class = 'white-box-content-clean'; + } } // Link to toggle. @@ -3821,11 +3828,15 @@ function ui_toggle( $output .= '
'; if ($reverseImg === false) { if ($switch === true) { + if (empty($switch_name) === true) { + $switch_name = 'box_enable_toggle'.$uniqid; + } + $output .= html_print_div( [ 'class' => 'float-left', 'content' => html_print_checkbox_switch_extended( - 'box_enable_toggle'.$uniqid, + $switch_name, 1, ($hidden_default === true) ? 0 : 1, false, @@ -3895,7 +3906,7 @@ function ui_toggle( $output .= ' var hide_tgl_ctrl_'.$uniqid.' = '.(int) $hidden_default.";\n"; $output .= ' /* $vendorDir . '/mpdf/mpdf/src/Utils/UtfString.php', 'PandoraFMS\\Agent' => $baseDir . '/include/lib/Agent.php', 'PandoraFMS\\Core\\Config' => $baseDir . '/include/lib/Core/Config.php', - 'PandoraFMS\\Core\\DBMantainer' => $baseDir . '/include/lib/Core/DBMantainer.php', + 'PandoraFMS\\Core\\DBMaintainer' => $baseDir . '/include/lib/Core/DBMaintainer.php', 'PandoraFMS\\Dashboard\\Cell' => $baseDir . '/include/lib/Dashboard/Cell.php', 'PandoraFMS\\Dashboard\\Manager' => $baseDir . '/include/lib/Dashboard/Manager.php', 'PandoraFMS\\Dashboard\\Widget' => $baseDir . '/include/lib/Dashboard/Widget.php', diff --git a/pandora_console/vendor/composer/autoload_static.php b/pandora_console/vendor/composer/autoload_static.php index dcc588664c..e1953333f9 100644 --- a/pandora_console/vendor/composer/autoload_static.php +++ b/pandora_console/vendor/composer/autoload_static.php @@ -392,7 +392,7 @@ class ComposerStaticInitfdecadadce22e6dde51e9535fe4ad7aa 'Mpdf\\Utils\\UtfString' => __DIR__ . '/..' . '/mpdf/mpdf/src/Utils/UtfString.php', 'PandoraFMS\\Agent' => __DIR__ . '/../..' . '/include/lib/Agent.php', 'PandoraFMS\\Core\\Config' => __DIR__ . '/../..' . '/include/lib/Core/Config.php', - 'PandoraFMS\\Core\\DBMantainer' => __DIR__ . '/../..' . '/include/lib/Core/DBMantainer.php', + 'PandoraFMS\\Core\\DBMaintainer' => __DIR__ . '/../..' . '/include/lib/Core/DBMaintainer.php', 'PandoraFMS\\Dashboard\\Cell' => __DIR__ . '/../..' . '/include/lib/Dashboard/Cell.php', 'PandoraFMS\\Dashboard\\Manager' => __DIR__ . '/../..' . '/include/lib/Dashboard/Manager.php', 'PandoraFMS\\Dashboard\\Widget' => __DIR__ . '/../..' . '/include/lib/Dashboard/Widget.php', From bcb2447edb2a9798a99fad407ecc22fb76a7c253 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Wed, 27 Jan 2021 13:03:57 +0100 Subject: [PATCH 05/18] WIP Historical database manager (setup) --- pandora_console/godmode/setup/performance.php | 48 ++++--------------- 1 file changed, 9 insertions(+), 39 deletions(-) diff --git a/pandora_console/godmode/setup/performance.php b/pandora_console/godmode/setup/performance.php index 366d55d4d9..14e376415c 100644 --- a/pandora_console/godmode/setup/performance.php +++ b/pandora_console/godmode/setup/performance.php @@ -29,7 +29,10 @@ // Load global vars. global $config; -require_once 'include/config.php'; +require_once $config['homedir'].'/include/config.php'; +require_once $config['homedir'].'/vendor/autoload.php'; + +use PandoraFMS\Core\Config; check_login(); @@ -388,44 +391,11 @@ if ($config['history_db_enabled'] == 1) { ); } - $config_history = false; - if ($config['history_db_connection']) { - $history_connect = mysql_db_process_sql( - 'DESCRIBE tconfig', - 'affected_rows', - $config['history_db_connection'], - false - ); - - if ($history_connect !== false) { - $config_history_array = mysql_db_process_sql( - 'SELECT * FROM tconfig', - 'affected_rows', - $config['history_db_connection'], - false - ); - - if (isset($config_history_array) && is_array($config_history_array)) { - foreach ($config_history_array as $key => $value) { - $config_history[$value['token']] = $value['value']; - $config_history = true; - } - } - } else { - echo ui_print_error_message( - __('The tconfig table does not exist in the historical database') - ); - } - } - - if ($config_history === false) { - $config_history = []; - $config_history['days_purge'] = 180; - $config_history['days_compact'] = 120; - $config_history['step_compact'] = 1; - $config_history['event_purge'] = 180; - $config_history['string_purge'] = 180; - } + $config_history['days_purge'] = Config::get('days_purge', 180, true); + $config_history['days_compact'] = Config::get('days_compact', 120, true); + $config_history['step_compact'] = Config::get('step_compact', 1, true); + $config_history['event_purge'] = Config::get('event_purge', 180, true); + $config_history['string_purge'] = Config::get('string_purge', 180, true); $table_historical = new StdClass(); $table_historical->width = '100%'; From bbab6d9fe1dfc52b1cc575be066c8129f7e481c2 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Wed, 27 Jan 2021 14:06:22 +0100 Subject: [PATCH 06/18] WIP update process --- .../include/lib/Core/DBMaintainer.php | 63 ++++++++++++++++++- 1 file changed, 62 insertions(+), 1 deletion(-) diff --git a/pandora_console/include/lib/Core/DBMaintainer.php b/pandora_console/include/lib/Core/DBMaintainer.php index 2193f74421..a8c5fbc7b9 100644 --- a/pandora_console/include/lib/Core/DBMaintainer.php +++ b/pandora_console/include/lib/Core/DBMaintainer.php @@ -230,6 +230,62 @@ final class DBMaintainer } + /** + * Return first row available for given query. + * + * @param string $query Query to retrieve (1 row only). + * + * @return array Row. + */ + private function getRow(string $query) + { + if ($this->ready !== true) { + $this->lastError = $this->dbh->errno.': '.$this->dbh->error; + return []; + } + + $query .= ' LIMIT 1'; + $rs = $this->dbh->query($query); + if ($rs !== false) { + return $rs->fetch_array(MYSQLI_ASSOC); + } + + // Error. + return false; + } + + + /** + * Retrieve value from given query. + * + * @param string $table Table to query. + * @param string $key Field to retrieve. + * @param array $filter Filters to apply. + * @param string $join AND by default. + * + * @return mixed|null Value retrieved or null if not found. + */ + private function getValue( + string $table, + string $key, + array $filter, + string $join='AND' + ) { + $query = sprintf( + 'SELECT %s FROM %s WHERE 1=1 %s', + $key, + $table, + \db_format_array_where_clause_sql($filter, $join) + ); + $result = $this->getRow($query); + if ($result !== false) { + return $result[$key]; + } + + return false; + } + + /** * Verifies schema against running db. * @@ -356,9 +412,14 @@ final class DBMaintainer return false; } + $last_mr = Config::get('MR', null); + $last_mr_curr = $this->getValue('tconfig', 'value', ['token' => 'MR']); + + hd($last_mr); + hd($last_mr_curr); + $this->lastError = 'Pending update'; return false; - } From 25b15182e35a9ce4d144177d86f89d2331eda80f Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Wed, 27 Jan 2021 15:48:00 +0100 Subject: [PATCH 07/18] DBH auto-updater --- .../include/lib/Core/DBMaintainer.php | 107 ++++++++++++++++-- 1 file changed, 98 insertions(+), 9 deletions(-) diff --git a/pandora_console/include/lib/Core/DBMaintainer.php b/pandora_console/include/lib/Core/DBMaintainer.php index a8c5fbc7b9..db7aaa3670 100644 --- a/pandora_console/include/lib/Core/DBMaintainer.php +++ b/pandora_console/include/lib/Core/DBMaintainer.php @@ -272,7 +272,7 @@ final class DBMaintainer string $join='AND' ) { $query = sprintf( - 'SELECT %s FROM %s WHERE 1=1 %s', + 'SELECT %s FROM %s WHERE %s', $key, $table, \db_format_array_where_clause_sql($filter, $join) @@ -351,6 +351,45 @@ final class DBMaintainer } + /** + * Updates or creates a token in remote tconfig. + * + * @param string $token Token to be set. + * @param mixed $value Value for given token. + * + * @return boolean Success or not. + */ + private function setConfigToken(string $token, $value) + { + $prev = $this->getValue('tconfig', 'value', ['token' => $token]); + // If failed or not found, then insert. + if ($prev === false || $prev === null) { + // Create. + $rs = $this->dbh->query( + sprintf( + 'INSERT INTO `tconfig` (`token`, `value`) + VALUES ("%s", "%s")', + $token, + $value + ) + ); + } else { + // Update. + $rs = $this->dbh->query( + sprintf( + 'UPDATE `tconfig` + SET `value`= "%s" + WHERE `token` = "%s"', + $value, + $token + ) + ); + } + + return ($rs !== false); + } + + /** * Install PandoraFMS database schema in current target. * @@ -392,7 +431,19 @@ final class DBMaintainer return true; } - return $this->applyDump(Config::get('homedir', '').'/pandoradb.sql'); + $result = $this->applyDump(Config::get('homedir', '').'/pandoradb.sql'); + + // Set MR version according pandoradb_data. + $data_content = file_get_contents( + Config::get('homedir', '').'/pandoradb_data.sql' + ); + if (preg_match('/\(\'MR\'\,\s*(\d+)\)/', $data_content, $matches) > 0) { + $target_mr = $matches[1]; + } + + $cnf_update = $this->setConfigToken('MR', (int) $target_mr); + + return $result && $cnf_update; } @@ -412,13 +463,51 @@ final class DBMaintainer return false; } - $last_mr = Config::get('MR', null); - $last_mr_curr = $this->getValue('tconfig', 'value', ['token' => 'MR']); + $last_mr = (int) Config::get('MR', null); + $last_mr_curr = (int) $this->getValue( + 'tconfig', + 'value', + ['token' => 'MR'] + ); - hd($last_mr); - hd($last_mr_curr); + if ($last_mr_curr < $last_mr) { + while ($last_mr_curr < $last_mr) { + $last_mr_curr++; - $this->lastError = 'Pending update'; + $path = Config::get('homedir', ''); + $file = sprintf('/extras/mr/%d.sql', $last_mr_curr); + $updated_file = sprintf( + '/extras/mr/updated/%d.sql', + $last_mr_curr + ); + + $filename = $path.$file; + if (file_exists($path.$file) !== true) { + // File does not exist, maybe already udpated in active DB? + $filename = $path.$updated_file; + if (file_exists($filename) !== false) { + $this->lastError = 'Unable to locate MR update #'; + $this->lastError .= $last_mr_curr; + return false; + } + } + + if ($this->applyDump($filename) !== true) { + $err = 'Unable to apply MR update #'; + $err .= $last_mr_curr.': '; + $this->lastError = $err.$this->lastError; + return false; + } + } + } + + if ($last_mr_curr === $last_mr) { + $this->setConfigToken('MR', $last_mr_curr); + + return true; + } + + $this->lastError = 'Unknown database schema version, check MR in both active and historical database'; return false; } @@ -466,8 +555,8 @@ final class DBMaintainer if ((bool) preg_match("/;[\040]*\$/", $sql_line) === true) { $result = $this->dbh->query($query); if ((bool) $result === false) { - $this->lastError = $this->dbh->errnum.': '; - $this->lastERror .= $this->dbh->error; + $this->lastError = $this->dbh->errno.': '; + $this->lastError .= $this->dbh->error; return false; } From dd4849f7c355f805f53abe02bbf8f91c2d8ca625 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Wed, 27 Jan 2021 19:23:31 +0100 Subject: [PATCH 08/18] WIP Scheduled install & update of history database processes --- .../include/class/ConsoleSupervisor.php | 3 +- .../include/lib/Core/DBMaintainer.php | 106 ++++++++++++++++-- 2 files changed, 100 insertions(+), 9 deletions(-) diff --git a/pandora_console/include/class/ConsoleSupervisor.php b/pandora_console/include/class/ConsoleSupervisor.php index cc52f2062f..558aebde8d 100644 --- a/pandora_console/include/class/ConsoleSupervisor.php +++ b/pandora_console/include/class/ConsoleSupervisor.php @@ -149,7 +149,8 @@ class ConsoleSupervisor */ public function runBasic() { - global $config; + // Ensure functions are installed and up to date. + enterprise_hook('cron_extension_install_functions'); /* * PHP configuration warnings: diff --git a/pandora_console/include/lib/Core/DBMaintainer.php b/pandora_console/include/lib/Core/DBMaintainer.php index db7aaa3670..cf7b78abdb 100644 --- a/pandora_console/include/lib/Core/DBMaintainer.php +++ b/pandora_console/include/lib/Core/DBMaintainer.php @@ -393,9 +393,11 @@ final class DBMaintainer /** * Install PandoraFMS database schema in current target. * + * @param boolean $check_only Check and return, do not perform actions. + * * @return boolean Installation is success or not. */ - public function install() + public function install(bool $check_only=false) { if ($this->connect() !== true) { return false; @@ -407,6 +409,11 @@ final class DBMaintainer if ($this->ready !== true) { // Not ready, create database in target. + if ($check_only === true) { + $this->lastError = 'Database does not exist in target'; + return false; + } + $rc = $this->dbh->query( sprintf( 'CREATE DATABASE %s', @@ -428,9 +435,15 @@ final class DBMaintainer $this->ready = true; } else if ($this->verifySchema() === true) { $this->installed = true; + $this->lastError = null; return true; } + if ($check_only === true) { + $this->lastError = 'Schema not applied in target'; + return false; + } + $result = $this->applyDump(Config::get('homedir', '').'/pandoradb.sql'); // Set MR version according pandoradb_data. @@ -451,9 +464,11 @@ final class DBMaintainer /** * Updates PandoraFMS database schema in current target. * + * @param boolean $check_only Perform only test without update. + * * @return boolean Current installation is up to date. */ - public function update() + public function update(bool $check_only=false) { if ($this->connect() !== true) { return false; @@ -463,15 +478,43 @@ final class DBMaintainer return false; } - $last_mr = (int) Config::get('MR', null); + // Set MR version according pandoradb_data. + $data_content = file_get_contents( + Config::get('homedir', '').'/pandoradb_data.sql' + ); + if (preg_match('/\(\'MR\'\,\s*(\d+)\)/', $data_content, $matches) > 0) { + $target_mr = $matches[1]; + } + + $active_mr = (int) Config::get('MR', null); $last_mr_curr = (int) $this->getValue( 'tconfig', 'value', ['token' => 'MR'] ); - if ($last_mr_curr < $last_mr) { - while ($last_mr_curr < $last_mr) { + if ($check_only === true) { + if ($active_mr === $last_mr_curr) { + return true; + } + + $this->lastError = sprintf( + 'Database schema not up to date: #%d should be #%d', + $last_mr_curr, + $active_mr + ); + if ($active_mr < $target_mr) { + $this->lastError .= sprintf( + ' (latest available: #%d)', + $target_mr + ); + } + + return false; + } + + if ($last_mr_curr < $active_mr) { + while ($last_mr_curr < $active_mr) { $last_mr_curr++; $path = Config::get('homedir', ''); @@ -501,7 +544,7 @@ final class DBMaintainer } } - if ($last_mr_curr === $last_mr) { + if ($last_mr_curr === $active_mr) { $this->setConfigToken('MR', $last_mr_curr); return true; @@ -513,11 +556,12 @@ final class DBMaintainer /** - * Verifies current target database is connected, installed and updated. + * Process database checks perform required actions. + * Returns true if it is connected, installed and updated. * * @return boolean Status of the installation. */ - public function check() + public function process() { if ($this->connect() !== true) { return false; @@ -535,6 +579,52 @@ final class DBMaintainer } + /** + * Check if target has schema updated. + * + * @return boolean + */ + public function isUpdated() + { + return $this->update(true); + } + + + /** + * Check if target has schema installed. + * + * @return boolean + */ + public function isInstalled() + { + return $this->install(true); + } + + + /** + * Checks if current target is connected, installed and updated. + * + * @return boolean Status of the database schema. + */ + public function check() + { + if ($this->connect() !== true) { + return false; + } + + if ($this->isInstalled() !== true) { + return false; + } + + if ($this->isUpdated() !== true) { + return false; + } + + return true; + + } + + /** * This function keeps same functionality as install.php:parse_mysqli_dump. * From 9fc3856f835a676e7cd515c234afb00bff13591d Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Thu, 28 Jan 2021 13:20:36 +0100 Subject: [PATCH 09/18] WIP historical database automaintenance + minor fixes --- .../include/class/HelpFeedBack.class.php | 1 + pandora_console/include/config_process.php | 2 +- pandora_console/include/functions_config.php | 9 +++- pandora_console/include/lib/Core/Config.php | 6 +++ .../include/lib/Core/DBMaintainer.php | 52 +++++++++++++------ 5 files changed, 51 insertions(+), 19 deletions(-) diff --git a/pandora_console/include/class/HelpFeedBack.class.php b/pandora_console/include/class/HelpFeedBack.class.php index 77b54794ab..9c99fcf9db 100644 --- a/pandora_console/include/class/HelpFeedBack.class.php +++ b/pandora_console/include/class/HelpFeedBack.class.php @@ -236,6 +236,7 @@ class HelpFeedBack extends Wizard */ public function sendMailMethod() { + global $config; $suggestion = get_parameter('type', 'false'); $feedback_text = get_parameter('feedback_text', null); $feedback_mail = get_parameter('feedback_email', null); diff --git a/pandora_console/include/config_process.php b/pandora_console/include/config_process.php index 20ef0ffd12..093cfa0286 100644 --- a/pandora_console/include/config_process.php +++ b/pandora_console/include/config_process.php @@ -101,7 +101,7 @@ require_once $ownDir.'functions.php'; // We need a timezone BEFORE calling config_process_config. // If not we will get ugly warnings. Set Europe/Madrid by default // Later will be replaced by the good one. -if (!is_dir($_SERVER['DOCUMENT_ROOT'].$config['homeurl']) || !is_dir($_SERVER['DOCUMENT_ROOT'].$config['homeurl_static'])) { +if (!is_dir($config['homedir'])) { $url = explode('/', $_SERVER['REQUEST_URI']); $flag_url = 0; foreach ($url as $key => $value) { diff --git a/pandora_console/include/functions_config.php b/pandora_console/include/functions_config.php index 57058a59a0..97cb856511 100644 --- a/pandora_console/include/functions_config.php +++ b/pandora_console/include/functions_config.php @@ -28,6 +28,8 @@ // Config functions. require_once __DIR__.'/../vendor/autoload.php'; +require_once __DIR__.'/functions.php'; +enterprise_include_once('include/functions_config.php'); use PandoraFMS\Core\DBMaintainer; use PandoraFMS\Core\Config; @@ -1534,7 +1536,12 @@ function config_update_config() ); // Performs several checks and installs if needed. - if ($dbm->check() === false) { + if ($dbm->checkDatabaseDefinition() === true + && $dbm->isInstalled() === false + ) { + // Target is ready but several tasks are pending. + $dbm->process(); + } else if ($dbm->check() !== true) { $errors[] = $dbm->getLastError(); } } diff --git a/pandora_console/include/lib/Core/Config.php b/pandora_console/include/lib/Core/Config.php index fe73fe327d..4891cabcce 100644 --- a/pandora_console/include/lib/Core/Config.php +++ b/pandora_console/include/lib/Core/Config.php @@ -75,12 +75,18 @@ final class Config ob_get_clean(); } + ob_start(); $data = \db_get_all_rows_sql( 'SELECT * FROM `tconfig`', false, false, $config['history_db_connection'] ); + ob_get_clean(); + + if (is_array($data) !== true) { + return []; + } self::$settings = array_reduce( $data, diff --git a/pandora_console/include/lib/Core/DBMaintainer.php b/pandora_console/include/lib/Core/DBMaintainer.php index cf7b78abdb..b53311581c 100644 --- a/pandora_console/include/lib/Core/DBMaintainer.php +++ b/pandora_console/include/lib/Core/DBMaintainer.php @@ -390,6 +390,40 @@ final class DBMaintainer } + /** + * Create database only (not schema) in target. + * + * @return boolean Success or not. + */ + public function checkDatabaseDefinition() + { + if ($this->ready === true) { + return true; + } + + $rc = $this->dbh->query( + sprintf( + 'CREATE DATABASE %s', + $this->name + ) + ); + + if ($rc === false) { + $this->lastError = $this->dbh->errno.': '.$this->dbh->error; + return false; + } + + if ($this->dbh->select_db($this->name) === false) { + $this->lastError = $this->dbh->errno.': '.$this->dbh->error; + return false; + } + + // Already connected and ready to execute commands. + $this->ready = true; + return true; + } + + /** * Install PandoraFMS database schema in current target. * @@ -414,25 +448,9 @@ final class DBMaintainer return false; } - $rc = $this->dbh->query( - sprintf( - 'CREATE DATABASE %s', - $this->name - ) - ); - - if ($rc === false) { - $this->lastError = $this->dbh->errno.': '.$this->dbh->error; + if ($this->checkDatabaseDefinition() === false) { return false; } - - if ($this->dbh->select_db($this->name) === false) { - $this->lastError = $this->dbh->errno.': '.$this->dbh->error; - return false; - } - - // Already connected and ready to execute commands. - $this->ready = true; } else if ($this->verifySchema() === true) { $this->installed = true; $this->lastError = null; From b8980c189c2434e9c3c67ff4dd62d56b47b3ce8c Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Thu, 28 Jan 2021 18:50:55 +0100 Subject: [PATCH 10/18] Schedule history db schema check after MR update --- pandora_console/include/ajax/rolling_release.ajax.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pandora_console/include/ajax/rolling_release.ajax.php b/pandora_console/include/ajax/rolling_release.ajax.php index 43ceaa5f1d..194123aaac 100644 --- a/pandora_console/include/ajax/rolling_release.ajax.php +++ b/pandora_console/include/ajax/rolling_release.ajax.php @@ -88,6 +88,11 @@ if (is_ajax()) { $file_dest = $config['homedir']."/extras/mr/updated/$number.sql"; copy($file, $file_dest); + + // After successfully update, schedule history + // database upgrade. + enterprise_include_once('include/functions_config.php'); + enterprise_hook('history_db_install'); } } else { $error_file = fopen($config['homedir'].'/extras/mr/error.txt', 'w'); From 279bd5456d5ee44b7b865d07b491ac339fbe43cc Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Fri, 29 Jan 2021 10:25:14 +0100 Subject: [PATCH 11/18] Disabled compactdb while running in historical database --- pandora_server/util/pandora_db.pl | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/pandora_server/util/pandora_db.pl b/pandora_server/util/pandora_db.pl index 874c8bd758..27db50c38b 100755 --- a/pandora_server/util/pandora_db.pl +++ b/pandora_server/util/pandora_db.pl @@ -1021,8 +1021,8 @@ sub pandora_delete_old_session_data { ############################################################################### # Main ############################################################################### -sub pandoradb_main ($$$) { - my ($conf, $dbh, $history_dbh) = @_; +sub pandoradb_main ($$$;$) { + my ($conf, $dbh, $history_dbh, $running_in_history) = @_; log_message ('', "Starting at ". strftime ("%Y-%m-%d %H:%M:%S", localtime()) . "\n"); @@ -1043,8 +1043,12 @@ sub pandoradb_main ($$$) { } } + # Only active database should be compacted. Disabled for historical database. # Compact on if enable and DaysCompact are below DaysPurge - if (($conf->{'_onlypurge'} == 0) && ($conf->{'_days_compact'} < $conf->{'_days_purge'})) { + if (!$running_in_history + && ($conf->{'_onlypurge'} == 0) + && ($conf->{'_days_compact'} < $conf->{'_days_purge'}) + ) { pandora_compactdb ($conf, defined ($history_dbh) ? $history_dbh : $dbh, $dbh); } @@ -1126,7 +1130,8 @@ if (defined($history_dbh)) { pandoradb_main( $h_conf, $history_dbh, - undef + undef, + 1 # Disable certain funcionality while runningn in historical database. ); } From 04be237b7af78aa82f17a26b111197ebd75d6be1 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Fri, 29 Jan 2021 14:30:16 +0100 Subject: [PATCH 12/18] minor method added --- pandora_console/include/lib/Core/DBMaintainer.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pandora_console/include/lib/Core/DBMaintainer.php b/pandora_console/include/lib/Core/DBMaintainer.php index b53311581c..685ca1b126 100644 --- a/pandora_console/include/lib/Core/DBMaintainer.php +++ b/pandora_console/include/lib/Core/DBMaintainer.php @@ -186,6 +186,17 @@ final class DBMaintainer } + /** + * Return connection statuis. + * + * @return boolean + */ + public function isConnected() + { + return $this->connected; + } + + /** * Retrieve last error. * From 96c218f21ee1f6bb92a29bb35e62b1d9877b10b8 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Mon, 1 Feb 2021 13:31:42 +0100 Subject: [PATCH 13/18] Some updates and minor fixes DBMaintainer --- pandora_console/include/lib/Core/Config.php | 21 ++--- .../include/lib/Core/DBMaintainer.php | 81 +++++++++++++------ pandora_console/include/styles/pandora.css | 5 ++ pandora_console/include/styles/setup.css | 4 + 4 files changed, 77 insertions(+), 34 deletions(-) diff --git a/pandora_console/include/lib/Core/Config.php b/pandora_console/include/lib/Core/Config.php index 4891cabcce..620c63f84a 100644 --- a/pandora_console/include/lib/Core/Config.php +++ b/pandora_console/include/lib/Core/Config.php @@ -72,18 +72,19 @@ final class Config $config['history_db_port'], false ); + + if ($config['history_db_connection'] !== false) { + $data = \db_get_all_rows_sql( + 'SELECT * FROM `tconfig`', + false, + false, + $config['history_db_connection'] + ); + } + ob_get_clean(); } - ob_start(); - $data = \db_get_all_rows_sql( - 'SELECT * FROM `tconfig`', - false, - false, - $config['history_db_connection'] - ); - ob_get_clean(); - if (is_array($data) !== true) { return []; } @@ -119,6 +120,8 @@ final class Config if (isset(self::$settings[$token]) === true) { return self::$settings[$token]; } + + return $default; } else { global $config; diff --git a/pandora_console/include/lib/Core/DBMaintainer.php b/pandora_console/include/lib/Core/DBMaintainer.php index 685ca1b126..8b3fc75bab 100644 --- a/pandora_console/include/lib/Core/DBMaintainer.php +++ b/pandora_console/include/lib/Core/DBMaintainer.php @@ -230,12 +230,14 @@ final class DBMaintainer $results = []; - do { - $row = $rs->fetch_array(MYSQLI_ASSOC); - if ((bool) $row !== false) { - $results[] = $row; - } - } while ((bool) $row !== false); + if ($rs !== false) { + do { + $row = $rs->fetch_array(MYSQLI_ASSOC); + if ((bool) $row !== false) { + $results[] = $row; + } + } while ((bool) $row !== false); + } return $results; } @@ -412,6 +414,10 @@ final class DBMaintainer return true; } + if ($this->dbh === null) { + return false; + } + $rc = $this->dbh->query( sprintf( 'CREATE DATABASE %s', @@ -503,7 +509,7 @@ final class DBMaintainer return false; } - if ($this->install() !== true) { + if ($this->install($check_only) !== true) { return false; } @@ -564,7 +570,7 @@ final class DBMaintainer } } - if ($this->applyDump($filename) !== true) { + if ($this->applyDump($filename, true) !== true) { $err = 'Unable to apply MR update #'; $err .= $last_mr_curr.': '; $this->lastError = $err.$this->lastError; @@ -630,6 +636,23 @@ final class DBMaintainer } + /** + * Checks if target is ready to connect. + * + * @return boolean + */ + public function isReady() + { + if ($this->ready === true) { + return true; + } + + $this->connect(); + + return $this->ready; + } + + /** * Checks if current target is connected, installed and updated. * @@ -657,29 +680,37 @@ final class DBMaintainer /** * This function keeps same functionality as install.php:parse_mysqli_dump. * - * @param string $path Path where SQL dump file is stored. + * @param string $path Path where SQL dump file is stored. + * @param boolean $transactional Use transactions from file (true) (MRs). * * @return boolean Success or not. */ - private function applyDump(string $path) + private function applyDump(string $path, bool $transactional=false) { if (file_exists($path) === true) { - $file_content = file($path); - $query = ''; - foreach ($file_content as $sql_line) { - if (trim($sql_line) !== '' - && strpos($sql_line, '-- ') === false - ) { - $query .= $sql_line; - if ((bool) preg_match("/;[\040]*\$/", $sql_line) === true) { - $result = $this->dbh->query($query); - if ((bool) $result === false) { - $this->lastError = $this->dbh->errno.': '; - $this->lastError .= $this->dbh->error; - return false; - } + if ($transactional === true) { + global $config; + // MR are loaded in transactions. + include_once $config['homedir'].'/include/db/mysql.php'; + return db_run_sql_file($path); + } else { + $file_content = file($path); + $query = ''; + foreach ($file_content as $sql_line) { + if (trim($sql_line) !== '' + && strpos($sql_line, '-- ') === false + ) { + $query .= $sql_line; + if ((bool) preg_match("/;[\040]*\$/", $sql_line) === true) { + $result = $this->dbh->query($query); + if ((bool) $result === false) { + $this->lastError = $this->dbh->errno.': '; + $this->lastError .= $this->dbh->error; + return false; + } - $query = ''; + $query = ''; + } } } } diff --git a/pandora_console/include/styles/pandora.css b/pandora_console/include/styles/pandora.css index 12f5825a8f..aff419a62a 100644 --- a/pandora_console/include/styles/pandora.css +++ b/pandora_console/include/styles/pandora.css @@ -603,6 +603,11 @@ select:-internal-list-box { .flex-nowrap { flex-wrap: nowrap; } + +.flex-evenly { + justify-content: space-evenly; +} + .flex-row-baseline { display: flex; flex-direction: row; diff --git a/pandora_console/include/styles/setup.css b/pandora_console/include/styles/setup.css index 5c7b304772..c7f9ededaf 100644 --- a/pandora_console/include/styles/setup.css +++ b/pandora_console/include/styles/setup.css @@ -18,3 +18,7 @@ input[type="text"], input[type="number"] { width: 220px; } + +.fit > tbody > tr > td img { + width: 15px; +} From 712079c30e1edd9fd43c43dbbcf1349ce941e613 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Mon, 1 Feb 2021 15:00:54 +0100 Subject: [PATCH 14/18] RC1 #4755 --- pandora_console/include/functions_config.php | 4 ++-- pandora_console/include/lib/Core/Config.php | 18 +++++++++--------- pandora_server/util/pandora_db.pl | 3 +++ 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/pandora_console/include/functions_config.php b/pandora_console/include/functions_config.php index 55a78f16da..601741a0c5 100644 --- a/pandora_console/include/functions_config.php +++ b/pandora_console/include/functions_config.php @@ -1558,11 +1558,11 @@ function config_update_config() if (Config::set( 'history_partitions_auto', - get_parameter_switch('history_partitions_auto'), + get_parameter_switch('history_partitions_auto', 0), true ) !== true ) { - $error_update[] = __('Historical database days compact'); + $error_update[] = __('Historical database partitions'); } if (Config::set( diff --git a/pandora_console/include/lib/Core/Config.php b/pandora_console/include/lib/Core/Config.php index 620c63f84a..8970d03835 100644 --- a/pandora_console/include/lib/Core/Config.php +++ b/pandora_console/include/lib/Core/Config.php @@ -73,18 +73,18 @@ final class Config false ); - if ($config['history_db_connection'] !== false) { - $data = \db_get_all_rows_sql( - 'SELECT * FROM `tconfig`', - false, - false, - $config['history_db_connection'] - ); - } - ob_get_clean(); } + if ($config['history_db_connection'] !== false) { + $data = \db_get_all_rows_sql( + 'SELECT * FROM `tconfig`', + false, + false, + $config['history_db_connection'] + ); + } + if (is_array($data) !== true) { return []; } diff --git a/pandora_server/util/pandora_db.pl b/pandora_server/util/pandora_db.pl index 27db50c38b..d3d7387d7e 100755 --- a/pandora_server/util/pandora_db.pl +++ b/pandora_server/util/pandora_db.pl @@ -1134,6 +1134,9 @@ if (defined($history_dbh)) { 1 # Disable certain funcionality while runningn in historical database. ); + # Handle partitions. + enterprise_hook('handle_partitions', [$h_conf, $history_dbh]); + } # Release the lock From 4b0f96126bf2fba378b0d166cf73aca50f41f9bd Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Mon, 22 Feb 2021 12:25:24 +0100 Subject: [PATCH 15/18] minor fix --- pandora_console/include/functions_config.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/pandora_console/include/functions_config.php b/pandora_console/include/functions_config.php index ac24435712..95265cebff 100644 --- a/pandora_console/include/functions_config.php +++ b/pandora_console/include/functions_config.php @@ -1469,7 +1469,11 @@ function config_update_config() ) { // Same definition for active and historical database! // This is a critical error. - $errors[] = __('Active and historical database cannot be the same.'); + $config['error_config_update_config']['correct'] = false; + $config['error_config_update_config']['message'] = __( + 'Active and historical database cannot be the same.' + ); + return; } else { if (!config_update_value('history_db_host', get_parameter('history_db_host'))) { $error_update[] = __('Host'); From d312f764eddf12b119665f1a6da5d2cf2ac220b4 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Wed, 24 Mar 2021 13:43:14 +0100 Subject: [PATCH 16/18] Hack to use old functions from PandoraDB --- .../include/lib/Core/DBMaintainer.php | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/pandora_console/include/lib/Core/DBMaintainer.php b/pandora_console/include/lib/Core/DBMaintainer.php index 8b3fc75bab..8e8f009b7b 100644 --- a/pandora_console/include/lib/Core/DBMaintainer.php +++ b/pandora_console/include/lib/Core/DBMaintainer.php @@ -690,9 +690,37 @@ final class DBMaintainer if (file_exists($path) === true) { if ($transactional === true) { global $config; + + // Adapt to PandoraFMS classic way to do things... + $backup_dbhost = $config['dbhost']; + $backup_dbuser = $config['dbuser']; + $backup_dbpass = $config['dbpass']; + $backup_dbname = $config['dbname']; + $backup_dbport = $config['dbport']; + $backup_mysqli = $config['mysqli']; + + $config['dbhost'] = $this->host; + $config['dbuser'] = $this->user; + $config['dbpass'] = $this->pass; + $config['dbname'] = $this->name; + $config['dbport'] = $this->port; + + // Not using mysqli in > php 7 is a completely non-sense. + $config['mysqli'] = true; + // MR are loaded in transactions. include_once $config['homedir'].'/include/db/mysql.php'; - return db_run_sql_file($path); + $return = db_run_sql_file($path); + + // Revert global variable. + $config['dbhost'] = $backup_dbhost; + $config['dbuser'] = $backup_dbuser; + $config['dbpass'] = $backup_dbpass; + $config['dbname'] = $backup_dbname; + $config['dbport'] = $backup_dbport; + $config['mysqli'] = $backup_mysqli; + + return (bool) $return; } else { $file_content = file($path); $query = ''; From f046069de1487a15c7954a8229796c3a6839e269 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Wed, 24 Mar 2021 17:49:42 +0100 Subject: [PATCH 17/18] old-school style setup --- pandora_console/include/styles/setup.css | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/pandora_console/include/styles/setup.css b/pandora_console/include/styles/setup.css index c7f9ededaf..c3e1bb276d 100644 --- a/pandora_console/include/styles/setup.css +++ b/pandora_console/include/styles/setup.css @@ -22,3 +22,12 @@ input[type="number"] { .fit > tbody > tr > td img { width: 15px; } + +.clean-toggles .white_table_graph_header { + border: none; +} + +.dbhist tr td:first-child { + width: 45%; + min-width: 450px; +} From 7b7ef3a757ab0b48c4c4e975d38f5e310dc26f73 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Thu, 25 Mar 2021 14:25:09 +0100 Subject: [PATCH 18/18] minor improvement (err. messages) --- pandora_console/include/db/mysql.php | 1 + .../include/lib/Core/DBMaintainer.php | 21 ++++++++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/pandora_console/include/db/mysql.php b/pandora_console/include/db/mysql.php index 13e59f7247..8b7c81c9f9 100644 --- a/pandora_console/include/db/mysql.php +++ b/pandora_console/include/db/mysql.php @@ -1496,6 +1496,7 @@ function db_run_sql_file($location) // Undo results } + $config['db_run_sql_file_error'] = $mysqli->error; return false; } } diff --git a/pandora_console/include/lib/Core/DBMaintainer.php b/pandora_console/include/lib/Core/DBMaintainer.php index 8e8f009b7b..dc9754f01d 100644 --- a/pandora_console/include/lib/Core/DBMaintainer.php +++ b/pandora_console/include/lib/Core/DBMaintainer.php @@ -521,7 +521,10 @@ final class DBMaintainer $target_mr = $matches[1]; } - $active_mr = (int) Config::get('MR', null); + // Active database MR version. + $active_mr = (int) Config::get('MR', 0); + + // Historical database MR version. $last_mr_curr = (int) $this->getValue( 'tconfig', 'value', @@ -575,18 +578,19 @@ final class DBMaintainer $err .= $last_mr_curr.': '; $this->lastError = $err.$this->lastError; return false; + } else { + // Update MR value. + $this->setConfigToken('MR', $last_mr_curr); } } } - if ($last_mr_curr === $active_mr) { - $this->setConfigToken('MR', $last_mr_curr); - - return true; + if ($last_mr_curr !== $active_mr) { + $this->lastError = 'Unknown database schema version, check MR in both active and historical database'; + return false; } - $this->lastError = 'Unknown database schema version, check MR in both active and historical database'; - return false; + return true; } @@ -711,6 +715,9 @@ final class DBMaintainer // MR are loaded in transactions. include_once $config['homedir'].'/include/db/mysql.php'; $return = db_run_sql_file($path); + if ($return === false) { + $this->lastError = $config['db_run_sql_file_error']; + } // Revert global variable. $config['dbhost'] = $backup_dbhost;