From 1e1df33270b6813672f150c1829f61a13a4fdcac Mon Sep 17 00:00:00 2001 From: TheWitness Date: Wed, 11 Nov 2026 10:27:47 -0500 Subject: [PATCH 1/4] bug: Open the Syslog database connection before the Syslog panels query it The Intropage Syslog panels (plugin_syslog_trend, plugin_syslog_levels_trend, plugin_syslog_devices and plugin_syslog_devices_detail) require the Syslog plugin's database library and then call its syslog_db_*() helpers, but never opened the Syslog database connection. Those helpers route through the global $syslog_cnn connection; with it unset they fall back to the Cacti connection, so on an installation whose Syslog tables live in a database separate from Cacti's the panels fail in the poller with: ERROR: A DB Row Failed!, Error: Table 'cacti.syslog' doesn't exist and the empty result then triggers "foreach() argument must be of type array|object, false given". Add an intropage_syslog_connect() helper that loads the Syslog library and calls syslog_connect() -- the entry point every Syslog consumer uses, which populates $syslog_cnn and is internally guarded to run its one-time schema check only once per process. Each panel now routes through the helper and skips its update cleanly (logging once) when the connection cannot be opened, and the levels collector tolerates a non-array result defensively. Replace the now-obsolete SyslogDatabaseLoaderTest, which asserted the old "load the library from either path" behaviour, with SyslogPanelConnectionTest covering the connection contract. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- CHANGELOG.md | 1 + panellib/syslog.php | 86 ++++++++---- tests/Unit/SyslogDatabaseLoaderTest.php | 172 ----------------------- tests/Unit/SyslogPanelConnectionTest.php | 101 +++++++++++++ 4 files changed, 163 insertions(+), 197 deletions(-) delete mode 100644 tests/Unit/SyslogDatabaseLoaderTest.php create mode 100644 tests/Unit/SyslogPanelConnectionTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index dbace7b..e7544d9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ --- develop --- +* bug: Open the Syslog plugin's own database connection before the Syslog panels query it, so a Syslog database that is separate from Cacti's no longer fails with "Table 'cacti.syslog' doesn't exist"; the panels now skip cleanly when that connection cannot be opened * issue#471: Do not return a phantom empty row when tailing newline-terminated log files; return no rows for empty files * dev: Run the CI poller with the shared runner group so RRD files and structured directories pass permission verification * issue#466: Tolerate a small overshoot over 100% in the busiest Used Space panels so DSSTATS averaging artifacts (e.g. used reading 100.004% of total) no longer log a false "Problem with DSSTAT data" warning; only a meaningful overshoot (over 110%) now warns diff --git a/panellib/syslog.php b/panellib/syslog.php index 267475f..3ac9d21 100644 --- a/panellib/syslog.php +++ b/panellib/syslog.php @@ -104,6 +104,55 @@ function register_syslog(): array { return $panels; } +/** + * Load the Syslog plugin's database library and open its database + * connection for the Intropage panels in this file. + * + * Syslog's tables may live in a database separate from Cacti's, reached + * through the global $syslog_cnn connection that syslog_connect() opens. + * Without that call the syslog_db_*() helpers fall back to the Cacti + * connection and fail with "Table 'cacti.syslog' doesn't exist" on a split + * database. Including setup.php makes syslog_connect() and the syslog_db_*() + * helpers available in the Intropage poller context; syslog_connect() is the + * entry point every Syslog consumer uses and is guarded to run its one-time + * schema check only once per process. + * + * @return bool True when the Syslog database connection is ready. + */ +function intropage_syslog_connect(): bool { + global $config, $syslog_cnn; + + if (!function_exists('syslog_connect')) { + $setup = $config['base_path'] . '/plugins/syslog/setup.php'; + + if (file_exists($setup)) { + include_once($setup); + } + } + + if (!function_exists('syslog_db_fetch_assoc_prepared')) { + foreach (['/plugins/syslog/database.php', '/plugins/syslog/includes/database.php'] as $lib) { + if (file_exists($config['base_path'] . $lib)) { + require_once($config['base_path'] . $lib); + + break; + } + } + } + + if (function_exists('syslog_connect')) { + syslog_connect(); + } + + if (empty($syslog_cnn)) { + cacti_log('ERROR: Intropage could not open the Syslog database connection', false, 'INTROPAGE'); + + return false; + } + + return true; +} + /** * Trend-collection function for the 'plugin_syslog' panel: records * point-in-time snapshots of the syslog_incoming/syslog table row @@ -120,12 +169,8 @@ function plugin_syslog_trend(): void { global $config; if (api_plugin_is_enabled('syslog')) { - if (file_exists($config['base_path'] . '/plugins/syslog/database.php')) { - require_once($config['base_path'] . '/plugins/syslog/database.php'); - } else if (file_exists($config['base_path'] . '/plugins/syslog/includes/database.php')) { - require_once($config['base_path'] . '/plugins/syslog/includes/database.php'); - } else { - cacti_log('ERROR: cannot include maint functions', false, 'INTROPAGE'); + if (!intropage_syslog_connect()) { + return; } // Grab row counts from the information schema, it's faster @@ -175,12 +220,8 @@ function plugin_syslog_levels_trend(): void { global $config; if (api_plugin_is_enabled('syslog')) { - if (file_exists($config['base_path'] . '/plugins/syslog/database.php')) { - require_once($config['base_path'] . '/plugins/syslog/database.php'); - } else if (file_exists($config['base_path'] . '/plugins/syslog/includes/database.php')) { - require_once($config['base_path'] . '/plugins/syslog/includes/database.php'); - } else { - cacti_log('ERROR: cannot include maint functions', false, 'INTROPAGE'); + if (!intropage_syslog_connect()) { + return; } $data = [ @@ -203,7 +244,7 @@ function plugin_syslog_levels_trend(): void { GROUP BY priority_id', [2 * $pi, $pi]); - foreach ($levels as $level) { + foreach ((array) $levels as $level) { $l = (int) $level['priority_id']; $data[$l] = $level['mcount']; } @@ -486,12 +527,11 @@ function plugin_syslog_devices($panel, $user_id, $timespan = 0): void { } if (api_plugin_is_enabled('syslog')) { - if (file_exists($config['base_path'] . '/plugins/syslog/database.php')) { - require_once($config['base_path'] . '/plugins/syslog/database.php'); - } else if (file_exists($config['base_path'] . '/plugins/syslog/includes/database.php')) { - require_once($config['base_path'] . '/plugins/syslog/includes/database.php'); - } else { - cacti_log('ERROR: cannot include maint functions', false, 'INTROPAGE'); + if (!intropage_syslog_connect()) { + $panel['data'] = __('Waiting for data', 'intropage'); + save_panel_result($panel, $user_id); + + return; } $lines = get_panel_lines_count($panel['height'], $user_id); @@ -566,12 +606,8 @@ function plugin_syslog_devices_detail(): array { } if (api_plugin_is_enabled('syslog')) { - if (file_exists($config['base_path'] . '/plugins/syslog/database.php')) { - require_once($config['base_path'] . '/plugins/syslog/database.php'); - } else if (file_exists($config['base_path'] . '/plugins/syslog/includes/database.php')) { - require_once($config['base_path'] . '/plugins/syslog/includes/database.php'); - } else { - cacti_log('ERROR: cannot include maint functions', false, 'INTROPAGE'); + if (!intropage_syslog_connect()) { + return $panel; } $devices = syslog_db_fetch_assoc_prepared('SELECT sh.host AS ip ,count(*) AS hcount diff --git a/tests/Unit/SyslogDatabaseLoaderTest.php b/tests/Unit/SyslogDatabaseLoaderTest.php deleted file mode 100644 index e41fbb1..0000000 --- a/tests/Unit/SyslogDatabaseLoaderTest.php +++ /dev/null @@ -1,172 +0,0 @@ - true); - $GLOBALS['__test_cacti_log'] = array(); - $GLOBALS['__intropage_syslog_library'] = null; - $callback(); - - return array( - 'library' => $GLOBALS['__intropage_syslog_library'], - 'logs' => $GLOBALS['__test_cacti_log'], - ); -} - -it('loads the Syslog database library from either supported path in plugin_syslog_trend', function () { - $invoke = static function () { - plugin_syslog_trend(); - }; - - foreach (array('legacy', 'modern') as $layout) { - $result = intropage_syslog_loader_run($invoke, $layout); - - expect($result['library'])->toBe($layout) - ->and($result['logs'])->toBeEmpty(); - } - - $result = intropage_syslog_loader_run($invoke, null); - - expect($result['library'])->toBeNull() - ->and($result['logs'])->toContain('ERROR: cannot include maint functions'); -}); - -it('loads the Syslog database library from either supported path in plugin_syslog_levels_trend', function () { - $invoke = static function () { - plugin_syslog_levels_trend(); - }; - - foreach (array('legacy', 'modern') as $layout) { - $result = intropage_syslog_loader_run($invoke, $layout); - - expect($result['library'])->toBe($layout) - ->and($result['logs'])->toBeEmpty(); - } - - $result = intropage_syslog_loader_run($invoke, null); - - expect($result['library'])->toBeNull() - ->and($result['logs'])->toContain('ERROR: cannot include maint functions'); -}); - -it('loads the Syslog database library from either supported path in plugin_syslog_devices', function () { - $panel = array('id' => 1, 'height' => 'normal', 'refresh' => 300); - $invoke = static function () use ($panel) { - plugin_syslog_devices($panel, 1, 60); - }; - - foreach (array('legacy', 'modern') as $layout) { - $result = intropage_syslog_loader_run($invoke, $layout); - - expect($result['library'])->toBe($layout) - ->and($result['logs'])->toBeEmpty(); - } - - $result = intropage_syslog_loader_run($invoke, null); - - expect($result['library'])->toBeNull() - ->and($result['logs'])->toContain('ERROR: cannot include maint functions'); -}); - -it('loads the Syslog database library from either supported path in plugin_syslog_devices_detail', function () { - $invoke = static function () { - plugin_syslog_devices_detail(); - }; - - foreach (array('legacy', 'modern') as $layout) { - $result = intropage_syslog_loader_run($invoke, $layout); - - expect($result['library'])->toBe($layout) - ->and($result['logs'])->toBeEmpty(); - } - - $result = intropage_syslog_loader_run($invoke, null); - - expect($result['library'])->toBeNull() - ->and($result['logs'])->toContain('ERROR: cannot include maint functions'); -}); diff --git a/tests/Unit/SyslogPanelConnectionTest.php b/tests/Unit/SyslogPanelConnectionTest.php new file mode 100644 index 0000000..f8b9fba --- /dev/null +++ b/tests/Unit/SyslogPanelConnectionTest.php @@ -0,0 +1,101 @@ + true); + $GLOBALS['__test_cacti_log'] = array(); + $GLOBALS['syslog_cnn'] = null; +}); + +afterEach(function () { + $GLOBALS['__test_enabled_plugins'] = $GLOBALS['__intropage_syslog_enabled_restore']; + $GLOBALS['__test_cacti_log'] = $GLOBALS['__intropage_syslog_log_restore']; + $GLOBALS['syslog_cnn'] = null; + unset($GLOBALS['__test_syslog_connect_ok'], $GLOBALS['__intropage_syslog_enabled_restore'], $GLOBALS['__intropage_syslog_log_restore']); +}); + +it('reports the syslog database ready when the connection opens', function () { + $GLOBALS['__test_syslog_connect_ok'] = true; + + expect(intropage_syslog_connect())->toBeTrue() + ->and($GLOBALS['syslog_cnn'])->not->toBeNull() + ->and($GLOBALS['__test_cacti_log'])->toBeEmpty(); +}); + +it('logs and reports failure when the syslog database cannot be opened', function () { + $GLOBALS['__test_syslog_connect_ok'] = false; + + expect(intropage_syslog_connect())->toBeFalse() + ->and(implode("\n", $GLOBALS['__test_cacti_log']))->toContain('could not open the Syslog database connection'); +}); + +it('skips plugin_syslog_levels_trend collection when the database is unavailable', function () { + $GLOBALS['__test_syslog_connect_ok'] = false; + + plugin_syslog_levels_trend(); + + expect(implode("\n", $GLOBALS['__test_cacti_log']))->toContain('could not open the Syslog database connection'); +}); + +it('runs a syslog panel update without errors when the database is available', function () { + $GLOBALS['__test_syslog_connect_ok'] = true; + + $panel = array('id' => 1, 'height' => 'normal', 'refresh' => 300); + plugin_syslog_devices($panel, 1, 60); + + expect($GLOBALS['__test_cacti_log'])->toBeEmpty(); +}); From 289d3329f9d2f8deb8938082f5c5b3331b1b5d35 Mon Sep 17 00:00:00 2001 From: TheWitness Date: Wed, 11 Nov 2026 12:14:35 -0500 Subject: [PATCH 2/4] bug: Honor syslog_connect() result and guard false levels query Address code-review feedback on the Syslog connection helper: - intropage_syslog_connect() now honors syslog_connect()'s boolean return. The connection object can open while the Syslog schema readiness check fails, so the helper returned a populated \ yet an overall false; treating that as success let all four panels query an unready database. The helper now fails (and logs) on an explicit false return as well as an empty connection. - plugin_syslog_levels_trend() replaces the (array) \ cast with an is_array() guard. Casting a false query result produced [false], so the loop ran once, emitted array-offset warnings, and overwrote the priority-0 count with null. A false result now yields no iterations. - SyslogPanelConnectionTest covers the populated-connection failure case and a false levels query (no warnings, counts preserved). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- panellib/syslog.php | 16 +++++-- tests/Unit/SyslogPanelConnectionTest.php | 54 +++++++++++++++++++----- 2 files changed, 55 insertions(+), 15 deletions(-) diff --git a/panellib/syslog.php b/panellib/syslog.php index 3ac9d21..48c943f 100644 --- a/panellib/syslog.php +++ b/panellib/syslog.php @@ -115,7 +115,9 @@ function register_syslog(): array { * database. Including setup.php makes syslog_connect() and the syslog_db_*() * helpers available in the Intropage poller context; syslog_connect() is the * entry point every Syslog consumer uses and is guarded to run its one-time - * schema check only once per process. + * schema check only once per process. Its boolean result is honored: it can + * return false (for example when that schema check fails) while leaving + * $syslog_cnn populated, so a false return means the database is not ready. * * @return bool True when the Syslog database connection is ready. */ @@ -140,11 +142,13 @@ function intropage_syslog_connect(): bool { } } + $connected = false; + if (function_exists('syslog_connect')) { - syslog_connect(); + $connected = (bool) syslog_connect(); } - if (empty($syslog_cnn)) { + if (!$connected || empty($syslog_cnn)) { cacti_log('ERROR: Intropage could not open the Syslog database connection', false, 'INTROPAGE'); return false; @@ -244,7 +248,11 @@ function plugin_syslog_levels_trend(): void { GROUP BY priority_id', [2 * $pi, $pi]); - foreach ((array) $levels as $level) { + if (!is_array($levels)) { + $levels = []; + } + + foreach ($levels as $level) { $l = (int) $level['priority_id']; $data[$l] = $level['mcount']; } diff --git a/tests/Unit/SyslogPanelConnectionTest.php b/tests/Unit/SyslogPanelConnectionTest.php index f8b9fba..54d5643 100644 --- a/tests/Unit/SyslogPanelConnectionTest.php +++ b/tests/Unit/SyslogPanelConnectionTest.php @@ -15,23 +15,21 @@ /* * Flag-driven Syslog connection stub. syslog_connect() can only be declared - * once per process, so its behaviour is controlled through a global flag that - * each test sets. It mirrors the real helper: it opens the global $syslog_cnn - * connection when the flag is set and leaves it closed otherwise. + * once per process, so its behaviour is controlled through globals that each + * test sets. It mirrors the real helper: the connection object can open even + * when the schema-readiness check fails, so $syslog_cnn may be populated while + * the function still returns false. __test_syslog_connect_ok drives the bool + * result; __test_syslog_cnn_open forces a populated connection regardless. */ if (!function_exists('syslog_connect')) { function syslog_connect(): bool { global $syslog_cnn; - if (!empty($GLOBALS['__test_syslog_connect_ok'])) { - $syslog_cnn = new stdClass(); + $ready = !empty($GLOBALS['__test_syslog_connect_ok']); - return true; - } - - $syslog_cnn = null; + $syslog_cnn = ($ready || !empty($GLOBALS['__test_syslog_cnn_open'])) ? new stdClass() : null; - return false; + return $ready; } } @@ -49,6 +47,10 @@ function syslog_db_fetch_cell_prepared($sql, $params = array()) { if (!function_exists('syslog_db_fetch_assoc_prepared')) { function syslog_db_fetch_assoc_prepared($sql, $params = array()) { + if (!empty($GLOBALS['__test_syslog_query_false'])) { + return false; + } + return array(); } } @@ -65,7 +67,7 @@ function syslog_db_fetch_assoc_prepared($sql, $params = array()) { $GLOBALS['__test_enabled_plugins'] = $GLOBALS['__intropage_syslog_enabled_restore']; $GLOBALS['__test_cacti_log'] = $GLOBALS['__intropage_syslog_log_restore']; $GLOBALS['syslog_cnn'] = null; - unset($GLOBALS['__test_syslog_connect_ok'], $GLOBALS['__intropage_syslog_enabled_restore'], $GLOBALS['__intropage_syslog_log_restore']); + unset($GLOBALS['__test_syslog_connect_ok'], $GLOBALS['__test_syslog_cnn_open'], $GLOBALS['__test_syslog_query_false'], $GLOBALS['__intropage_syslog_enabled_restore'], $GLOBALS['__intropage_syslog_log_restore']); }); it('reports the syslog database ready when the connection opens', function () { @@ -83,6 +85,15 @@ function syslog_db_fetch_assoc_prepared($sql, $params = array()) { ->and(implode("\n", $GLOBALS['__test_cacti_log']))->toContain('could not open the Syslog database connection'); }); +it('reports failure when the connection is populated but syslog_connect() returns false', function () { + $GLOBALS['__test_syslog_connect_ok'] = false; + $GLOBALS['__test_syslog_cnn_open'] = true; + + expect(intropage_syslog_connect())->toBeFalse() + ->and($GLOBALS['syslog_cnn'])->not->toBeNull() + ->and(implode("\n", $GLOBALS['__test_cacti_log']))->toContain('could not open the Syslog database connection'); +}); + it('skips plugin_syslog_levels_trend collection when the database is unavailable', function () { $GLOBALS['__test_syslog_connect_ok'] = false; @@ -99,3 +110,24 @@ function syslog_db_fetch_assoc_prepared($sql, $params = array()) { expect($GLOBALS['__test_cacti_log'])->toBeEmpty(); }); + +it('tolerates a false levels query without warnings or corrupting the priority-0 count', function () { + $GLOBALS['__test_syslog_connect_ok'] = true; + $GLOBALS['__test_syslog_query_false'] = true; + + $errors = array(); + set_error_handler(function ($severity, $message) use (&$errors) { + $errors[] = $message; + + return true; + }); + + try { + plugin_syslog_levels_trend(); + } finally { + restore_error_handler(); + } + + expect($errors)->toBeEmpty() + ->and($GLOBALS['__test_cacti_log'])->toBeEmpty(); +}); From 76d85481117e00c9bda23b59160a8dd2c884355c Mon Sep 17 00:00:00 2001 From: TheWitness Date: Wed, 11 Nov 2026 14:07:41 -0500 Subject: [PATCH 3/4] chore(i18n): regenerate cacti.pot for syslog panel strings The syslog panels now wrap their Waiting for data status text in __(), so the extracted i18n string set changed. Regenerate locales/po/cacti.pot with locales/build_gettext.sh to satisfy the i18n template check. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- locales/po/cacti.pot | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/locales/po/cacti.pot b/locales/po/cacti.pot index ff2e65b..70741d0 100644 --- a/locales/po/cacti.pot +++ b/locales/po/cacti.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: Cacti \n" "Report-Msgid-Bugs-To: developers@cacti.net\n" -"POT-Creation-Date: 2026-10-23 21:21-0600\n" +"POT-Creation-Date: 2026-11-11 14:05-0500\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: Cacti Developers >\n" "Language-Team: Cacti Developers \n" From d6f380e97707044e441b8d9e50b500090423d305 Mon Sep 17 00:00:00 2001 From: TheWitness Date: Wed, 11 Nov 2026 14:29:42 -0500 Subject: [PATCH 4/4] test: cover the Syslog panel database-connection guards Bring the changed lines in panellib/syslog.php to full coverage after the develop merge pulled in the strict patch-coverage gate: - Add unit tests driving the "database unavailable" path of plugin_syslog_trend(), plugin_syslog_devices() and plugin_syslog_devices_detail(). - Mark the one-time Syslog-library include guards in intropage_syslog_connect() with @codeCoverageIgnore; they only run in a real poller context, and the unit harness always has the Syslog functions declared (PHP cannot un-declare them). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- panellib/syslog.php | 5 ++++ tests/Unit/SyslogPanelConnectionTest.php | 32 ++++++++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/panellib/syslog.php b/panellib/syslog.php index 48c943f..5793c28 100644 --- a/panellib/syslog.php +++ b/panellib/syslog.php @@ -124,6 +124,10 @@ function register_syslog(): array { function intropage_syslog_connect(): bool { global $config, $syslog_cnn; + // The Syslog library is already loaded in the unit harness (its functions + // are declared up front), so these one-time include guards for a real poller + // context cannot be exercised there; PHP cannot un-declare a function. + // @codeCoverageIgnoreStart if (!function_exists('syslog_connect')) { $setup = $config['base_path'] . '/plugins/syslog/setup.php'; @@ -141,6 +145,7 @@ function intropage_syslog_connect(): bool { } } } + // @codeCoverageIgnoreEnd $connected = false; diff --git a/tests/Unit/SyslogPanelConnectionTest.php b/tests/Unit/SyslogPanelConnectionTest.php index 54d5643..9c36a4b 100644 --- a/tests/Unit/SyslogPanelConnectionTest.php +++ b/tests/Unit/SyslogPanelConnectionTest.php @@ -131,3 +131,35 @@ function syslog_db_fetch_assoc_prepared($sql, $params = array()) { expect($errors)->toBeEmpty() ->and($GLOBALS['__test_cacti_log'])->toBeEmpty(); }); + +it('skips plugin_syslog_trend collection when the database is unavailable', function () { + $GLOBALS['__test_syslog_connect_ok'] = false; + + plugin_syslog_trend(); + + expect(implode("\n", $GLOBALS['__test_cacti_log']))->toContain('could not open the Syslog database connection'); +}); + +it('shows a waiting message in the devices panel when the database is unavailable', function () { + $GLOBALS['__test_syslog_connect_ok'] = false; + $GLOBALS['__test_db_calls'] = array(); + + $panel = array('id' => 1, 'height' => 'normal', 'refresh' => 300); + plugin_syslog_devices($panel, 1, 60); + + expect(implode("\n", $GLOBALS['__test_cacti_log']))->toContain('could not open the Syslog database connection'); + + $updates = array_values(array_filter($GLOBALS['__test_db_calls'], static fn ($c) => strpos($c['sql'], 'UPDATE plugin_intropage_panel_data') !== false)); + expect($updates)->not->toBeEmpty() + ->and($updates[0]['params'][0])->toContain('Waiting for data'); +}); + +it('returns an empty devices detail when the database is unavailable', function () { + $GLOBALS['__test_syslog_connect_ok'] = false; + + $detail = plugin_syslog_devices_detail(); + + expect($detail)->toBeArray() + ->and($detail['detail'])->toBe('') + ->and(implode("\n", $GLOBALS['__test_cacti_log']))->toContain('could not open the Syslog database connection'); +});