Skip to content
This repository has been archived by the owner on Dec 13, 2022. It is now read-only.

enh(core): update script to manage gorgone configuration file #8229

Merged
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
e7b1f5c
style : minor typo changes
sc979 Dec 24, 2019
966d3b0
enh: exception management
sc979 Jan 3, 2020
dbb7cb3
enh: remove exception code thrown
sc979 Jan 3, 2020
709283e
enh: replace thrown object with error message
sc979 Jan 3, 2020
6d09507
enh: add a specific log file for the update
sc979 Jan 3, 2020
a5b360a
replace PDOException with parent
sc979 Jan 3, 2020
9e066b3
remove erro message duplication
sc979 Jan 3, 2020
93eea14
restore queries
sc979 Jan 3, 2020
1ac83a1
restore queries
sc979 Jan 3, 2020
3b0c408
typo correction
sc979 Jan 3, 2020
c679e7e
Update www/install/php/Update-20.04.0-beta.1.php
kduret Jan 6, 2020
abdcae2
Update www/install/php/Update-20.04.0-beta.1.php
kduret Jan 6, 2020
cc0218a
Update www/install/php/Update-20.04.0-beta.1.php
kduret Jan 6, 2020
8c9fba6
Update www/install/php/Update-20.04.0-beta.1.php
kduret Jan 6, 2020
3571210
Update www/install/php/Update-20.04.0-beta.1.php
kduret Jan 6, 2020
0992da8
fix(msg): move the errorMessage displayed before the thrown exception
sc979 Jan 6, 2020
1bbc1e4
enh: displayed warning on step4
sc979 Jan 6, 2020
adcbab5
enh: improve comments
sc979 Jan 7, 2020
faa0843
Update www/install/php/Update-20.04.0-beta.1.php
sc979 Jan 7, 2020
654fac5
enh(update): generate gorgone conf file on update
sc979 Jan 7, 2020
0b2e4db
Merge branch 'MON-gorgone-integration' into MON-4403-migrate-centreon…
sc979 Jan 7, 2020
f3af174
enh: moving transaction() before first exception
sc979 Jan 7, 2020
63e9f0f
fix(translation): avoid empty gettext translation
sc979 Jan 9, 2020
837d63b
Merge branch 'MON-gorgone-integration' into MON-4403-migrate-centreon…
sc979 Jan 14, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lang/es_ES.UTF-8/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -3231,14 +3231,19 @@ msgid "You seem to be having trouble with your upgrade."
msgstr "Parece que hay un problema con tu actualización."

#: centreon-web/www/install/step_upgrade/step4.php:70
msgid "You may refer to the line that causes problem in order to find out more about the issue."
msgid "You may refer to the line in the specified file in order to correct the issue."
msgstr "Puede consultar la línea que es problemática para obtener más información sobre el incidente."

#: centreon-web/www/install/step_upgrade/step4.php:71
#, php-format
msgid "The SQL files are located in %s"
msgstr "Los archivos SQL se encuentran en %s"

#: centreon-web/www/install/step_upgrade/step4.php:87
#, php-format
msgid "Please check the \"upgrade.log\" and the \"sql-error.log\" located in \"%s\" for more details"
msgstr ""

#: centreon-web/www/install/step_upgrade/step4.php:72
msgid "But do not edit the SQL files unless you know what you are doing.Refresh this page when the problem is fixed."
msgstr ""
Expand Down
7 changes: 6 additions & 1 deletion lang/pt_BR.UTF-8/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -11996,14 +11996,19 @@ msgid "You seem to be having trouble with your upgrade."
msgstr "Parece que você esta tendo problemas com sua atualização."

#: centreon-web/www/install/step_upgrade/step4.php:70
msgid "You may refer to the line that causes problem in order to find out more about the issue."
msgid "You may refer to the line in the specified file in order to correct the issue."
msgstr "Você deve verificar a linha que esta causando o problema para poder-mos encontrar uma resolução para este caso."

#: centreon-web/www/install/step_upgrade/step4.php:71
#, php-format
msgid "The SQL files are located in %s"
msgstr "Os arquivos SQL estão localizados em %s"

#: centreon-web/www/install/step_upgrade/step4.php:87
#, php-format
msgid "Please check the \"upgrade.log\" and the \"sql-error.log\" located in \"%s\" for more details"
msgstr ""
sc979 marked this conversation as resolved.
Show resolved Hide resolved

#: centreon-web/www/install/step_upgrade/step4.php:72
msgid "But do not edit the SQL files unless you know what you are doing.Refresh this page when the problem is fixed."
msgstr ""
Expand Down
7 changes: 6 additions & 1 deletion lang/pt_PT.UTF-8/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -11997,14 +11997,19 @@ msgid "You seem to be having trouble with your upgrade."
msgstr "Parece que você esta tendo problemas com sua atualização."

#: centreon-web/www/install/step_upgrade/step4.php:70
msgid "You may refer to the line that causes problem in order to find out more about the issue."
msgid "You may refer to the line in the specified file in order to correct the issue."
msgstr "Você deve verificar a linha que esta causando o problema para poder-mos encontrar uma resolução para este caso."

#: centreon-web/www/install/step_upgrade/step4.php:71
#, php-format
msgid "The SQL files are located in %s"
msgstr "Os ficheiros SQL estão localizados em %s"

#: centreon-web/www/install/step_upgrade/step4.php:87
#, php-format
msgid "Please check the \"upgrade.log\" and the \"sql-error.log\" located in \"%s\" for more details"
msgstr ""
sc979 marked this conversation as resolved.
Show resolved Hide resolved

#: centreon-web/www/install/step_upgrade/step4.php:72
msgid "But do not edit the SQL files unless you know what you are doing.Refresh this page when the problem is fixed."
msgstr ""
Expand Down
2 changes: 2 additions & 0 deletions www/class/centreonLog.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public function __construct($uid, $pearDB)
$this->errorType[1] = $this->path . "/login.log";
$this->errorType[2] = $this->path . "/sql-error.log";
$this->errorType[3] = $this->path . "/ldap.log";
$this->errorType[4] = $this->path . "/upgrade.log";
}

/*
Expand Down Expand Up @@ -146,6 +147,7 @@ public function __construct($customLogs = array())
$this->errorType[1] = $this->path . "/login.log";
$this->errorType[2] = $this->path . "/sql-error.log";
$this->errorType[3] = $this->path . "/ldap.log";
$this->errorType[4] = $this->path . "/upgrade.log";

foreach ($customLogs as $key => $value) {
if (!preg_match('@' . $this->path . '@', $value)) {
Expand Down
232 changes: 196 additions & 36 deletions www/install/php/Update-20.04.0-beta.1.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,140 @@
include_once __DIR__ . "/../../class/centreonLog.class.php";
$centreonLog = new CentreonLog();

//error specific content
$versionOfTheUpgrade = 'UPGRADE - 20.04.0-beta.1 : ';
$errorMessage = '';

/**
* Queries needing exception management and rollback if failing
*/
try {
$pearDB->query(
"UPDATE `contact` SET `contact_autologin_key` = NULL WHERE `contact_autologin_key` =''"
);
} catch (\PDOException $e) {
$centreonLog->insertLog(
2,
"UPGRADE : 19.10.2 Unable to set default contact_autologin_key"
);
}
$pearDB->beginTransaction();
/*
* Get user data to generate a new config file for the gorgone daemon module
*/

//set a pattern for values to get from file
$patterns = [
'instance_mode' => '/--INSTANCEMODE--/',
'db_host' => '/--ADDRESS--:--DBPORT--/',
'db_user' => '/--DBUSER--/',
'db_passwd' => '/--DBPASS--/',
'centreon_db' => '/--CONFDB--/',
'centstorage_db' => '/--STORAGEDB--/',
'VarLib' => '/--CENTREON_VARLIB--/'
];

// Move broker xml files to json format
try {
//set new mandatory parameters pattern used by gorgone
$pattern = [
'/--CENTREON_SPOOL--/',
'/--CENTREON_TRAPDIR--/',
'/--HTTPSERVERADDRESS--/',
'/--HTTPSERVERPORT--/',
'/--SSLMODE--/'
];

//set default values for these parameters
$userValues = [
'/var/spool/centreon',
'/etc/snmp/centreon_traps',
'0.0.0.0',
'8085',
'false'
];

$isACentral = false;

// get user's centreon cache folder path
$fileToOpen = _CENTREON_ETC_ . '/instCentWeb.conf';
$errorMessage = 'Missing or empty \'instCentWeb.conf\' file';
if (!file_exists($fileToOpen) || 0 === filesize($fileToOpen)) {
throw new \InvalidArgumentException($errorMessage);
}
$file = fopen($fileToOpen, 'r');

while (!feof($file)) {
$line = fgets($file);
if (strpos($line, "CENTREON_CACHEDIR") !== false) {
$line = explode("=", $line);
$pattern[] = '/--CENTREON_CACHEDIR--/';
// if no value is found, a default value is required
$userValues[] = $line[1] ?? '/var/cache/centreon';
}
}
fclose($file);

// get user's values from conf.pm
$fileToOpen = _CENTREON_ETC_ . '/conf.pm';
$errorMessage = 'Missing or empty centreon \'conf.pm\' file';
if (!file_exists($fileToOpen) || 0 === filesize($fileToOpen)) {
throw new \InvalidArgumentException($errorMessage);
}
$start = false;
$stop = false;
$file = fopen($fileToOpen, 'r');

while (!feof($file) && $stop === false) {
// removing indentation and carriage return
$line = rtrim(ltrim(fgets($file), " "), ",\n");

// getting only line in a specific array
if (strpos($line, '$centreon_config = {') !== false) {
$start = true;
continue;
} elseif ($start === true
&& strpos($line, '$instance_mode =') !== false
) {
$stop = true;
$isACentral = strpos($line, 'central') ? true : false;
continue;
} elseif ($start === true
&& strlen($line) > 5
&& substr($line, 0, 1) !== "#"
&& strpos($line, ' => ') !== false
) {
$line = explode(' => ', $line);
if (!isset($line[1])) {
continue;
}

list($currentLine, $currentValue) = $line;
$currentLine = trim($currentLine, '"');
if ($currentLine === 'db_passwd') {
$currentValue = trim($currentValue, '\'');
} else {
$currentValue = trim($currentValue, '"');
}

if (array_key_exists($currentLine, $patterns)) {
$pattern[] = $patterns[$currentLine];
$userValues[] = $currentValue;
}
}
}
fclose($file);

// checking if the instance is a central and generating the configuration file
if ($isACentral === true) {
$errorMessage = 'Gorgone template file is missing';
if (!file_exists(__DIR__ . '/../var/configFileGorgoneTemplate')) {
throw new \InvalidArgumentException($errorMessage);
}
$content = file_get_contents(__DIR__ . '/../var/configFileGorgoneTemplate');
$content = preg_replace($pattern, $userValues, $content);
$finalFile = _CENTREON_ETC_ . '/gorgoned.yml';
file_put_contents($finalFile, $content);

$errorMessage = 'Gorgone configuration file is not created properly';
if (!file_exists($finalFile) || 0 === filesize($finalFile)) {
throw new \InvalidArgumentException($errorMessage);
}
}

/*
* Move broker xml files to json format
*/
$errorMessage = "Unable to replace broker configuration from xml format to json format";
$result = $pearDB->query(
"SELECT config_id, config_filename
FROM cfg_centreonbroker"
Expand All @@ -48,21 +168,20 @@
$configFilenames = [];
while ($row = $result->fetch()) {
$fileName = str_replace('.xml', '.json', $row['config_filename']);

// saving data for next engine module modifications
$configFilenames[$row['config_filename']] = $fileName;

$statement->bindValue(':value', $fileName, \PDO::PARAM_STR);
$statement->bindValue(':id', $row['config_id'], \PDO::PARAM_INT);

$statement->execute();
}
} catch (\PDOException $e) {
$centreonLog->insertLog(
2,
"UPGRADE : 20.04.0-beta.1 Unable to move broker configuration from xml format to json format"
);
throw new \PDOException($e);
}

// Move engine module xml files to json format
try {
/*
* Move engine module xml files to json format
*/
$errorMessage = "Unable to replace engine's broker modules configuration from xml to json format";
$result = $pearDB->query(
"SELECT bk_mod_id, broker_module
FROM cfg_nagios_broker_module"
Expand All @@ -80,47 +199,88 @@
}
$statement->bindValue(':value', $fileName, \PDO::PARAM_STR);
$statement->bindValue(':id', $row['bk_mod_id'], \PDO::PARAM_INT);

$statement->execute();
}
} catch (\PDOException $e) {
$centreonLog->insertLog(
2,
"UPGRADE : 20.04.0-beta.1 Unable to move engine's broker modules configuration from xml to json format"
);
throw new \PDOException($e);
}


// Change broker sql output form
/*
* Change broker sql output form
*/
// set common error message on failure
$partialErrorMessage = $errorMessage;

try {
// reorganise existing input form
$errorMessage = $partialErrorMessage . " - While trying to update 'cb_type_field_relation' table data";
$pearDB->query(
"UPDATE cb_type_field_relation AS A INNER JOIN cb_type_field_relation AS B ON A.cb_type_id = B.cb_type_id
SET A.`order_display` = 8
WHERE B.`cb_field_id` = (SELECT f.cb_field_id FROM cb_field f WHERE f.fieldname = 'buffering_timeout')"
);

// add new connections_count input
$errorMessage = $partialErrorMessage . " - While trying to insert in 'cb_field' table new values";
$pearDB->query(
"INSERT INTO `cb_field` (`fieldname`, `displayname`, `description`, `fieldtype`, `external`)
VALUES ('connections_count', 'Number of connection to the database', 'Usually cpus/2', 'int', NULL)"
);

// add relation
$errorMessage = $partialErrorMessage . " - While trying to insert in 'cb_type_field_relation' table new values";
$pearDB->query(
"INSERT INTO `cb_type_field_relation` (`cb_type_id`, `cb_field_id`, `is_required`, `order_display`, `jshook_name`, `jshook_arguments`)
"INSERT INTO `cb_type_field_relation` (
`cb_type_id`,
`cb_field_id`,
`is_required`,
`order_display`,
`jshook_name`,
`jshook_arguments`
)
VALUES (
(SELECT `cb_type_id` FROM `cb_type` WHERE `type_shortname` = 'sql'),
(SELECT `cb_field_id` FROM `cb_field` WHERE `fieldname` = 'connections_count'),
0, 7, 'countConnections', '{\"target\": \"connections_count\"}'
0,
7,
'countConnections',
'{\"target\": \"connections_count\"}'
)"
);

} catch (\PDOException $e) {
$pearDB->commit();
$errorMessage = "";
} catch (\Exception $e) {
$pearDB->rollBack();
$centreonLog->insertLog(
4,
$versionOfTheUpgrade . $errorMessage .
" - Code : " . (int)$e->getCode() .
" - Error : " . $e->getMessage() .
" - Trace : " . $e->getTraceAsString()
);
throw new \Exception($versionOfTheUpgrade . $errorMessage, (int)$e->getCode(), $e);
}

/**
* Queries which don't need rollback and won't throw an exception
*/
try {
/*
* replace autologin keys using NULL instead of empty string
*/
$pearDB->query("UPDATE `contact` SET `contact_autologin_key` = NULL WHERE `contact_autologin_key` = ''");
} catch (\Exception $e) {
$errorMessage = "Unable to set default contact_autologin_key.";
$centreonLog->insertLog(
4,
$versionOfTheUpgrade . $errorMessage .
" - Code : " . (int)$e->getCode() .
" - Error : " . $e->getMessage() .
" - Trace : " . $e->getTraceAsString()
);
}

if (empty($errorMessage)) {
$centreonLog->insertLog(
2,
"UPGRADE : 20.04.0-beta.1 Unable to change sql broker form"
4,
$versionOfTheUpgrade . " - Successful Update"
);
throw new \PDOException($e);
}
11 changes: 8 additions & 3 deletions www/install/step_upgrade/step4.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,18 @@
</table>";

$troubleshootTxt1 = _('You seem to be having trouble with your upgrade.');
$troubleshootTxt2 = _('You may refer to the line that causes problem in order to find out more about the issue.');
$troubleshootTxt3 = sprintf(_('The SQL files are located in %s'), _CENTREON_PATH_ . 'www/install/sql/');
$troubleshootTxt1bis = sprintf(
_("Please check the \"upgrade.log\" and the \"sql-error.log\" located in \"%s\" for more details"),
_CENTREON_LOG_
);
$troubleshootTxt2 = _('You may refer to the line in the specified file in order to correct the issue.');
$troubleshootTxt3 = sprintf(_('The SQL files are located in "%s"'), _CENTREON_PATH_ . 'www/install/sql/');
$troubleshootTxt4 = _('But do not edit the SQL files unless you know what you are doing.'
. 'Refresh this page when the problem is fixed.');
$contents .= sprintf(
'<br/><p id="troubleshoot" style="display:none;">%s<br/>%s<br/>%s<br/>%s</p>',
'<br/><p id="troubleshoot" style="display:none;">%s<br/><br/>%s<br/>%s<br/><br/>%s<br/>%s</p>',
$troubleshootTxt1,
$troubleshootTxt1bis,
$troubleshootTxt2,
$troubleshootTxt3,
$troubleshootTxt4
Expand Down
2 changes: 1 addition & 1 deletion www/install/steps/process/configFileSetup.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
file_put_contents($centreonConfPmFile, $contents);

/**
* gorgone.yaml
* gorgoned.yml
* configuration file used by the Gorgone module in a full ZMQ mode
*/
$centreonGorgonedConfFile = $centreonEtcPath . '/gorgoned.yml';
Expand Down
Loading