From 9fc3856f835a676e7cd515c234afb00bff13591d Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Thu, 28 Jan 2021 13:20:36 +0100 Subject: [PATCH] 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;