Skip to content

Commit 15cdd05

Browse files
fix: execute replication CREATE TABLE SQL from SHOW CREATE output
Refs Cacti#267 Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
1 parent 869c035 commit 15cdd05

4 files changed

Lines changed: 139 additions & 1 deletion

File tree

.github/workflows/plugin-ci-workflow.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,16 @@ jobs:
187187
echo "Syntax errors found!"
188188
exit 1
189189
fi
190+
191+
- name: Run Plugin Regression Tests
192+
run: |
193+
cd ${{ github.workspace }}/cacti/plugins/syslog
194+
if [ -d tests/regression ]; then
195+
for test in tests/regression/*.php; do
196+
[ -f "$test" ] || continue
197+
php "$test"
198+
done
199+
fi
190200
191201

192202
- name: Run Cacti Poller

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
--- develop ---
44

55
* issue#250: Fix date filter persistence by validating before shift_span detection
6+
* issue#258: Execute CREATE TABLE SQL correctly during replication sync
67
* issue: Making changes to support Cacti 1.3
78
* issue: Don't use MyISAM for non-analytical tables
89
* issue: The install advisor for Syslog was broken in current Cacti releases

setup.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,7 @@ function syslog_replace_data($table, &$data) {
817817
$sqlData = array();
818818
$sqlQuery = array();
819819
$columns = array_keys($data[0]);
820+
$create_sql = '';
820821

821822
$create = db_fetch_row('SHOW CREATE TABLE ' . $table);
822823
if (isset($create["CREATE TABLE `$table`"]) || isset($create['Create Table'])) {
@@ -828,7 +829,12 @@ function syslog_replace_data($table, &$data) {
828829
}
829830

830831
if (!syslog_db_table_exists($table)) {
831-
syslog_db_execute($create);
832+
if ($create_sql == '') {
833+
cacti_log('WARNING: Unable to derive CREATE TABLE SQL for `' . $table . '` during Syslog replication.', false, 'REPLICATE');
834+
return;
835+
}
836+
837+
syslog_db_execute($create_sql);
832838
syslog_db_execute("TRUNCATE TABLE $table");
833839
}
834840

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
$existing_functions = array(
4+
'db_fetch_row',
5+
'syslog_db_table_exists',
6+
'syslog_db_execute',
7+
'syslog_db_execute_prepared'
8+
);
9+
10+
foreach ($existing_functions as $existing_function) {
11+
if (function_exists($existing_function)) {
12+
fwrite(STDERR, "This test must run in isolated mode; function already exists: $existing_function\n");
13+
exit(1);
14+
}
15+
}
16+
17+
if (!function_exists('cacti_sizeof')) {
18+
function cacti_sizeof($value) {
19+
if (is_array($value) || $value instanceof Countable) {
20+
return count($value);
21+
}
22+
23+
return 0;
24+
}
25+
}
26+
27+
$GLOBALS['syslog_replace_data_execute_calls'] = array();
28+
$GLOBALS['syslog_replace_data_prepared_calls'] = array();
29+
$GLOBALS['issue258_logs'] = array();
30+
$GLOBALS['issue258_show_create'] = array('Create Table' => 'CREATE TABLE `syslog_alert` (`id` INT NOT NULL)');
31+
32+
if (!function_exists('db_fetch_row')) {
33+
function db_fetch_row($sql) {
34+
return $GLOBALS['issue258_show_create'];
35+
}
36+
}
37+
38+
if (!function_exists('syslog_db_table_exists')) {
39+
function syslog_db_table_exists($table) {
40+
return false;
41+
}
42+
}
43+
44+
if (!function_exists('syslog_db_execute')) {
45+
function syslog_db_execute($sql) {
46+
$GLOBALS['syslog_replace_data_execute_calls'][] = $sql;
47+
48+
return true;
49+
}
50+
}
51+
52+
if (!function_exists('syslog_db_execute_prepared')) {
53+
function syslog_db_execute_prepared($sql, $params) {
54+
$GLOBALS['syslog_replace_data_prepared_calls'][] = array(
55+
'sql' => $sql,
56+
'params' => $params
57+
);
58+
59+
return true;
60+
}
61+
}
62+
63+
if (!function_exists('cacti_log')) {
64+
function cacti_log($message, $output = false, $facility = 'SYSTEM', $level = '') {
65+
$GLOBALS['issue258_logs'][] = $message;
66+
}
67+
}
68+
69+
require_once dirname(__DIR__, 2) . '/setup.php';
70+
71+
$data = array(
72+
array(
73+
'id' => 1,
74+
'hash' => 'abc123',
75+
'name' => 'sample'
76+
)
77+
);
78+
79+
syslog_replace_data('syslog_alert', $data);
80+
81+
$executed = $GLOBALS['syslog_replace_data_execute_calls'];
82+
83+
if (cacti_sizeof($executed) < 2) {
84+
fwrite(STDERR, "Expected CREATE + TRUNCATE calls to run.\n");
85+
exit(1);
86+
}
87+
88+
if ($executed[0] !== 'CREATE TABLE `syslog_alert` (`id` INT NOT NULL)') {
89+
fwrite(STDERR, "Expected CREATE TABLE SQL to be executed from create_sql.\n");
90+
exit(1);
91+
}
92+
93+
if ($executed[1] !== 'TRUNCATE TABLE syslog_alert') {
94+
fwrite(STDERR, "Expected TRUNCATE TABLE to run after CREATE TABLE.\n");
95+
exit(1);
96+
}
97+
98+
$prepared = $GLOBALS['syslog_replace_data_prepared_calls'];
99+
100+
if (cacti_sizeof($prepared) !== 1) {
101+
fwrite(STDERR, "Expected a single prepared INSERT statement execution.\n");
102+
exit(1);
103+
}
104+
105+
$GLOBALS['syslog_replace_data_execute_calls'] = array();
106+
$GLOBALS['syslog_replace_data_prepared_calls'] = array();
107+
$GLOBALS['issue258_show_create'] = false;
108+
109+
syslog_replace_data('syslog_alert', $data);
110+
111+
if (cacti_sizeof($GLOBALS['syslog_replace_data_execute_calls']) !== 0) {
112+
fwrite(STDERR, "Expected no execute calls when CREATE TABLE SQL is unavailable.\n");
113+
exit(1);
114+
}
115+
116+
if (cacti_sizeof($GLOBALS['syslog_replace_data_prepared_calls']) !== 0) {
117+
fwrite(STDERR, "Expected no prepared inserts when CREATE TABLE SQL is unavailable.\n");
118+
exit(1);
119+
}
120+
121+
echo "issue258_replication_create_sql_test passed\n";

0 commit comments

Comments
 (0)