From ab7b4e4b7b38431ae8dcf8df2ed3c458a2113c5f Mon Sep 17 00:00:00 2001 From: Thomas Vincent Date: Fri, 6 Mar 2026 14:31:55 -0800 Subject: [PATCH 1/3] fix: use create SQL text in replication table bootstrap Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Thomas Vincent --- .github/workflows/plugin-ci-workflow.yml | 10 +++ CHANGELOG.md | 1 + setup.php | 2 +- .../issue258_replication_create_sql_test.php | 83 +++++++++++++++++++ 4 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 tests/regression/issue258_replication_create_sql_test.php diff --git a/.github/workflows/plugin-ci-workflow.yml b/.github/workflows/plugin-ci-workflow.yml index 7580ee1..76612ac 100644 --- a/.github/workflows/plugin-ci-workflow.yml +++ b/.github/workflows/plugin-ci-workflow.yml @@ -187,6 +187,16 @@ jobs: echo "Syntax errors found!" exit 1 fi + + - name: Run Plugin Regression Tests + run: | + cd ${{ github.workspace }}/cacti/plugins/syslog + if [ -d tests/regression ]; then + for test in tests/regression/*.php; do + [ -f "$test" ] || continue + php "$test" + done + fi - name: Run Cacti Poller diff --git a/CHANGELOG.md b/CHANGELOG.md index 0f1e7f2..bdb5bee 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ --- develop --- +* issue#258: Execute CREATE TABLE SQL correctly during replication sync * issue: Making changes to support Cacti 1.3 * issue: Don't use MyISAM for non-analytical tables * issue: The install advisor for Syslog was broken in current Cacti releases diff --git a/setup.php b/setup.php index a90cb01..c23d0c3 100644 --- a/setup.php +++ b/setup.php @@ -828,7 +828,7 @@ function syslog_replace_data($table, &$data) { } if (!syslog_db_table_exists($table)) { - syslog_db_execute($create); + syslog_db_execute($create_sql); syslog_db_execute("TRUNCATE TABLE $table"); } diff --git a/tests/regression/issue258_replication_create_sql_test.php b/tests/regression/issue258_replication_create_sql_test.php new file mode 100644 index 0000000..6ed507b --- /dev/null +++ b/tests/regression/issue258_replication_create_sql_test.php @@ -0,0 +1,83 @@ + 'CREATE TABLE `syslog_alert` (`id` INT NOT NULL)'); + } +} + +if (!function_exists('syslog_db_table_exists')) { + function syslog_db_table_exists($table) { + return false; + } +} + +if (!function_exists('syslog_db_execute')) { + function syslog_db_execute($sql) { + $GLOBALS['syslog_replace_data_execute_calls'][] = $sql; + + return true; + } +} + +if (!function_exists('syslog_db_execute_prepared')) { + function syslog_db_execute_prepared($sql, $params) { + $GLOBALS['syslog_replace_data_prepared_calls'][] = array( + 'sql' => $sql, + 'params' => $params + ); + + return true; + } +} + +require_once dirname(__DIR__, 2) . '/setup.php'; + +$data = array( + array( + 'id' => 1, + 'hash' => 'abc123', + 'name' => 'sample' + ) +); + +syslog_replace_data('syslog_alert', $data); + +$executed = $GLOBALS['syslog_replace_data_execute_calls']; + +if (cacti_sizeof($executed) < 2) { + fwrite(STDERR, "Expected CREATE + TRUNCATE calls to run.\n"); + exit(1); +} + +if ($executed[0] !== 'CREATE TABLE `syslog_alert` (`id` INT NOT NULL)') { + fwrite(STDERR, "Expected CREATE TABLE SQL to be executed from create_sql.\n"); + exit(1); +} + +if ($executed[1] !== 'TRUNCATE TABLE syslog_alert') { + fwrite(STDERR, "Expected TRUNCATE TABLE to run after CREATE TABLE.\n"); + exit(1); +} + +$prepared = $GLOBALS['syslog_replace_data_prepared_calls']; + +if (cacti_sizeof($prepared) !== 1) { + fwrite(STDERR, "Expected a single prepared INSERT statement execution.\n"); + exit(1); +} + +echo "issue258_replication_create_sql_test passed\n"; From 62855a844aa32ab1fbde779f55b64a1911c86d42 Mon Sep 17 00:00:00 2001 From: Thomas Vincent Date: Fri, 6 Mar 2026 15:02:25 -0800 Subject: [PATCH 2/3] fix: guard missing CREATE TABLE SQL during replication Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: Thomas Vincent --- setup.php | 6 +++ .../issue258_replication_create_sql_test.php | 40 ++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/setup.php b/setup.php index c23d0c3..cb9c099 100644 --- a/setup.php +++ b/setup.php @@ -817,6 +817,7 @@ function syslog_replace_data($table, &$data) { $sqlData = array(); $sqlQuery = array(); $columns = array_keys($data[0]); + $create_sql = ''; $create = db_fetch_row('SHOW CREATE TABLE ' . $table); if (isset($create["CREATE TABLE `$table`"]) || isset($create['Create Table'])) { @@ -828,6 +829,11 @@ function syslog_replace_data($table, &$data) { } if (!syslog_db_table_exists($table)) { + if ($create_sql == '') { + cacti_log('WARNING: Unable to derive CREATE TABLE SQL for `' . $table . '` during Syslog replication.', false, 'REPLICATE'); + return; + } + syslog_db_execute($create_sql); syslog_db_execute("TRUNCATE TABLE $table"); } diff --git a/tests/regression/issue258_replication_create_sql_test.php b/tests/regression/issue258_replication_create_sql_test.php index 6ed507b..36e810b 100644 --- a/tests/regression/issue258_replication_create_sql_test.php +++ b/tests/regression/issue258_replication_create_sql_test.php @@ -1,5 +1,19 @@ 'CREATE TABLE `syslog_alert` (`id` INT NOT NULL)'); if (!function_exists('db_fetch_row')) { function db_fetch_row($sql) { - return array('Create Table' => 'CREATE TABLE `syslog_alert` (`id` INT NOT NULL)'); + return $GLOBALS['issue258_show_create']; } } @@ -44,6 +60,12 @@ function syslog_db_execute_prepared($sql, $params) { } } +if (!function_exists('cacti_log')) { + function cacti_log($message, $output = false, $facility = 'SYSTEM', $level = '') { + $GLOBALS['issue258_logs'][] = $message; + } +} + require_once dirname(__DIR__, 2) . '/setup.php'; $data = array( @@ -80,4 +102,20 @@ function syslog_db_execute_prepared($sql, $params) { exit(1); } +$GLOBALS['syslog_replace_data_execute_calls'] = array(); +$GLOBALS['syslog_replace_data_prepared_calls'] = array(); +$GLOBALS['issue258_show_create'] = false; + +syslog_replace_data('syslog_alert', $data); + +if (cacti_sizeof($GLOBALS['syslog_replace_data_execute_calls']) !== 0) { + fwrite(STDERR, "Expected no execute calls when CREATE TABLE SQL is unavailable.\n"); + exit(1); +} + +if (cacti_sizeof($GLOBALS['syslog_replace_data_prepared_calls']) !== 0) { + fwrite(STDERR, "Expected no prepared inserts when CREATE TABLE SQL is unavailable.\n"); + exit(1); +} + echo "issue258_replication_create_sql_test passed\n"; From c410e961b84e304015b47aa15eca169497876c9e Mon Sep 17 00:00:00 2001 From: Thomas Vincent Date: Sun, 8 Mar 2026 03:55:27 -0700 Subject: [PATCH 3/3] chore: add CHANGELOG entry for develop Signed-off-by: Thomas Vincent --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bdb5bee..d0d61ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ --- develop --- -* issue#258: Execute CREATE TABLE SQL correctly during replication sync +* issue#258: Execute CREATE TABLE SQL when a replicated rules table is missing * issue: Making changes to support Cacti 1.3 * issue: Don't use MyISAM for non-analytical tables * issue: The install advisor for Syslog was broken in current Cacti releases