From a94f6be37eb4a744654b6c6dde9dbb42c145ff57 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Fri, 3 Dec 2021 12:59:50 +0100 Subject: [PATCH 1/3] force db_process_sql --- pandora_console/extras/mr/51.sql | 2 +- .../pandoradb_migrate_6.0_to_7.0.mysql.sql | 1 + pandora_console/include/db/mysql.php | 51 ------- pandora_console/include/functions_db.php | 128 +++++++++--------- pandora_console/pandoradb.sql | 1 + 5 files changed, 70 insertions(+), 113 deletions(-) diff --git a/pandora_console/extras/mr/51.sql b/pandora_console/extras/mr/51.sql index 3398bed0c3..0575fea01e 100644 --- a/pandora_console/extras/mr/51.sql +++ b/pandora_console/extras/mr/51.sql @@ -8,7 +8,7 @@ ALTER TABLE `tlocal_component` ADD COLUMN `percentage_critical` tinyint(1) UNSIG ALTER TABLE `tlocal_component` ADD COLUMN `percentage_warning` tinyint(1) UNSIGNED DEFAULT 0; ALTER TABLE `tpolicy_modules` ADD COLUMN `percentage_warning` tinyint(1) UNSIGNED DEFAULT 0; ALTER TABLE `tpolicy_modules` ADD COLUMN `percentage_critical` tinyint(1) UNSIGNED DEFAULT 0; - +ALTER TABLE `tsync_queue` ADD COLUMN `result` TEXT; ALTER TABLE tagente_modulo MODIFY debug_content TEXT; CREATE TABLE IF NOT EXISTS `talert_calendar` ( diff --git a/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql b/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql index ba0c0c32e3..0cec1167dd 100644 --- a/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql +++ b/pandora_console/extras/pandoradb_migrate_6.0_to_7.0.mysql.sql @@ -4089,6 +4089,7 @@ CREATE TABLE IF NOT EXISTS `tsync_queue` ( `operation` text, `table` text, `error` MEDIUMTEXT, + `result` TEXT, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; diff --git a/pandora_console/include/db/mysql.php b/pandora_console/include/db/mysql.php index ce6f814e95..dd3bc26846 100644 --- a/pandora_console/include/db/mysql.php +++ b/pandora_console/include/db/mysql.php @@ -1244,57 +1244,6 @@ function mysql_db_get_all_row_by_steps_sql($new=true, &$result, $sql=null) } -/** - * Starts a database transaction. - */ -function mysql_db_process_sql_begin() -{ - global $config; - - if ($config['mysqli']) { - mysqli_query($config['dbconnection'], 'SET AUTOCOMMIT = 0'); - mysqli_query($config['dbconnection'], 'START TRANSACTION'); - } else { - mysql_query('SET AUTOCOMMIT = 0'); - mysql_query('START TRANSACTION'); - } -} - - -/** - * Commits a database transaction. - */ -function mysql_db_process_sql_commit() -{ - global $config; - - if ($config['mysqli']) { - mysqli_query($config['dbconnection'], 'COMMIT'); - mysqli_query($config['dbconnection'], 'SET AUTOCOMMIT = 1'); - } else { - mysql_query('COMMIT'); - mysql_query('SET AUTOCOMMIT = 1'); - } -} - - -/** - * Rollbacks a database transaction. - */ -function mysql_db_process_sql_rollback() -{ - global $config; - - if ($config['mysqli']) { - mysqli_query($config['dbconnection'], 'ROLLBACK '); - mysqli_query($config['dbconnection'], 'SET AUTOCOMMIT = 1'); - } else { - mysql_query('ROLLBACK '); - mysql_query('SET AUTOCOMMIT = 1'); - } -} - - /** * Get last error. * diff --git a/pandora_console/include/functions_db.php b/pandora_console/include/functions_db.php index a4c21d3bc6..a60dc87b02 100644 --- a/pandora_console/include/functions_db.php +++ b/pandora_console/include/functions_db.php @@ -1377,42 +1377,56 @@ function db_process_sql($sql, $rettype='affected_rows', $dbconnection='', $cache break; } - if ($rc !== false) { - if (enterprise_hook('is_metaconsole') === true - && isset($config['centralized_management']) === true - && (bool) $config['centralized_management'] === true - && $dbconnection === '' - ) { - $errors = null; - try { - // Synchronize changes to nodes if needed. - $sync = new Synchronizer(); - if ($sync !== null) { - if ($sync->queue($sql) === false) { - // Launch events per failed query. - $errors = $sync->getLatestErrors(); - if ($errors !== null) { - $errors = join(', ', $errors); - } else { - $errors = ''; - } - } - } - } catch (\Exception $e) { - $errors = $e->getMessage(); - } - - if ($errors !== null) { - // TODO: Generate pandora event. - error_log($errors); - } - } - } + db_sync($dbconnection, $sql, $rc); return $rc; } +/** + * Propagate to nodes. + * + * @param mixed $dbconnection Dbconnection. + * @param mixed $sql Sql. + * @param mixed $rc Rc. + * + * @return void + */ +function db_sync($dbconnection, $sql, $rc) +{ + global $config; + if (enterprise_hook('is_metaconsole') === true + && isset($config['centralized_management']) === true + && (bool) $config['centralized_management'] === true + && $dbconnection === '' + ) { + $errors = null; + try { + // Synchronize changes to nodes if needed. + $sync = new Synchronizer(); + if ($sync !== null) { + if ($sync->queue($sql, $rc) === false) { + // Launch events per failed query. + $errors = $sync->getLatestErrors(); + if ($errors !== null) { + $errors = join(', ', $errors); + } else { + $errors = ''; + } + } + } + } catch (\Exception $e) { + $errors = $e->getMessage(); + } + + if ($errors !== null) { + // TODO: Generate pandora event. + error_log($errors); + } + } +} + + /** * Get all the rows in a table of the database. * @@ -1810,18 +1824,17 @@ function db_process_sql_begin() global $config; switch ($config['dbtype']) { - case 'mysql': - return mysql_db_process_sql_begin(); - - break; case 'postgresql': return postgresql_db_process_sql_begin(); - break; case 'oracle': return oracle_db_process_sql_begin(); - break; + default: + case 'mysql': + db_process_sql('SET AUTOCOMMIT = 0', 'affected_rows', '', false, null, false); + db_process_sql('START TRANSACTION', 'affected_rows', '', false, null, false); + break; } } @@ -1834,18 +1847,17 @@ function db_process_sql_commit() global $config; switch ($config['dbtype']) { - case 'mysql': - return mysql_db_process_sql_commit(); - - break; case 'postgresql': return postgresql_db_process_sql_commit(); - break; case 'oracle': return oracle_db_process_sql_commit(); - break; + default: + case 'mysql': + db_process_sql('COMMIT', 'affected_rows', '', false, null, false); + db_process_sql('SET AUTOCOMMIT = 1', 'affected_rows', '', false, null, false); + break; } } @@ -1858,18 +1870,17 @@ function db_process_sql_rollback() global $config; switch ($config['dbtype']) { - case 'mysql': - return mysql_db_process_sql_rollback(); - - break; case 'postgresql': return postgresql_db_process_sql_rollback(); - break; case 'oracle': return oracle_db_process_sql_rollback(); - break; + default: + case 'mysql': + db_process_sql('ROLLBACK', 'affected_rows', '', false, null, false); + db_process_sql('SET AUTOCOMMIT = 1', 'affected_rows', '', false, null, false); + break; } } @@ -1889,6 +1900,7 @@ function db_print_database_debug() echo '
'.__('Database debug').'
'; + $table = new stdClass(); $table->id = 'database_debug'; $table->cellpadding = '0'; $table->width = '95%'; @@ -1946,18 +1958,15 @@ function db_get_last_error() global $config; switch ($config['dbtype']) { - case 'mysql': - return mysql_db_get_last_error(); - - break; case 'postgresql': return postgresql_db_get_last_error(); - break; case 'oracle': return oracle_db_get_last_error(); - break; + case 'mysql': + default: + return mysql_db_get_last_error(); } } @@ -1975,18 +1984,15 @@ function db_get_type_field_table($table, $field) global $config; switch ($config['dbtype']) { - case 'mysql': - return mysql_db_get_type_field_table($table, $field); - - break; case 'postgresql': return postgresql_db_get_type_field_table($table, $field); - break; case 'oracle': return oracle_db_get_type_field_table($table, $field); - break; + case 'mysql': + default: + return mysql_db_get_type_field_table($table, $field); } } diff --git a/pandora_console/pandoradb.sql b/pandora_console/pandoradb.sql index f8b675c9d6..fe475df931 100644 --- a/pandora_console/pandoradb.sql +++ b/pandora_console/pandoradb.sql @@ -4069,6 +4069,7 @@ CREATE TABLE IF NOT EXISTS `tsync_queue` ( `operation` text, `table` text, `error` MEDIUMTEXT, + `result` TEXT, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; From 5b0641523593e5e60397dc355ff42cbf3ac74cca Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Fri, 3 Dec 2021 13:58:14 +0100 Subject: [PATCH 2/3] errata fix --- pandora_console/include/functions_db.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/pandora_console/include/functions_db.php b/pandora_console/include/functions_db.php index a60dc87b02..34bb53ef26 100644 --- a/pandora_console/include/functions_db.php +++ b/pandora_console/include/functions_db.php @@ -1822,6 +1822,7 @@ function db_process_sql_delete($table, $where, $where_join='AND') function db_process_sql_begin() { global $config; + $null = null; switch ($config['dbtype']) { case 'postgresql': @@ -1832,8 +1833,8 @@ function db_process_sql_begin() default: case 'mysql': - db_process_sql('SET AUTOCOMMIT = 0', 'affected_rows', '', false, null, false); - db_process_sql('START TRANSACTION', 'affected_rows', '', false, null, false); + db_process_sql('SET AUTOCOMMIT = 0', 'affected_rows', '', false, $null, false); + db_process_sql('START TRANSACTION', 'affected_rows', '', false, $null, false); break; } } @@ -1845,6 +1846,7 @@ function db_process_sql_begin() function db_process_sql_commit() { global $config; + $null = null; switch ($config['dbtype']) { case 'postgresql': @@ -1855,8 +1857,8 @@ function db_process_sql_commit() default: case 'mysql': - db_process_sql('COMMIT', 'affected_rows', '', false, null, false); - db_process_sql('SET AUTOCOMMIT = 1', 'affected_rows', '', false, null, false); + db_process_sql('COMMIT', 'affected_rows', '', false, $null, false); + db_process_sql('SET AUTOCOMMIT = 1', 'affected_rows', '', false, $null, false); break; } } @@ -1868,6 +1870,7 @@ function db_process_sql_commit() function db_process_sql_rollback() { global $config; + $null = null; switch ($config['dbtype']) { case 'postgresql': @@ -1878,8 +1881,8 @@ function db_process_sql_rollback() default: case 'mysql': - db_process_sql('ROLLBACK', 'affected_rows', '', false, null, false); - db_process_sql('SET AUTOCOMMIT = 1', 'affected_rows', '', false, null, false); + db_process_sql('ROLLBACK', 'affected_rows', '', false, $null, false); + db_process_sql('SET AUTOCOMMIT = 1', 'affected_rows', '', false, $null, false); break; } } From 6d469912fad3be57954a01e4f289f3d1ec08601c Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Fri, 3 Dec 2021 14:24:00 +0100 Subject: [PATCH 3/3] some fixes --- pandora_console/include/db/mysql.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pandora_console/include/db/mysql.php b/pandora_console/include/db/mysql.php index dd3bc26846..872f4a0496 100644 --- a/pandora_console/include/db/mysql.php +++ b/pandora_console/include/db/mysql.php @@ -1339,7 +1339,7 @@ function mysql_db_process_file($path, $handle_error=true) $query = ''; // Begin the transaction - mysql_db_process_sql_begin(); + db_process_sql_begin(); foreach ($file_content as $sql_line) { if (trim($sql_line) != '' && strpos($sql_line, '--') === false) { @@ -1354,7 +1354,7 @@ function mysql_db_process_file($path, $handle_error=true) if (!$result = $query_result) { // Error. Rollback the transaction - mysql_db_process_sql_rollback(); + db_process_sql_rollback(); if ($config['mysqli']) { $error_message = mysqli_error($config['dbconnection']); @@ -1391,7 +1391,7 @@ function mysql_db_process_file($path, $handle_error=true) } // No errors. Commit the transaction - mysql_db_process_sql_commit(); + db_process_sql_commit(); return true; } else { return false; @@ -1430,7 +1430,7 @@ function db_run_sql_file($location) $mysqli->query($config['dbconnection'], 'START TRANSACTION'); } else { // Run commands - mysql_db_process_sql_begin(); + db_process_sql_begin(); // Begin transaction } @@ -1456,7 +1456,7 @@ function db_run_sql_file($location) $mysqli->query($config['dbconnection'], 'COMMIT'); $mysqli->query($config['dbconnection'], 'SET AUTOCOMMIT = 1'); } else { - mysql_db_process_sql_commit(); + db_process_sql_commit(); // Save results } @@ -1466,7 +1466,7 @@ function db_run_sql_file($location) $mysqli->query($config['dbconnection'], 'ROLLBACK '); $mysqli->query($config['dbconnection'], 'SET AUTOCOMMIT = 1'); } else { - mysql_db_process_sql_rollback(); + db_process_sql_rollback(); // Undo results }