diff --git a/CHANGELOG.md b/CHANGELOG.md index 83330ab..25fe046 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 * dev: Raise the minimum supported Cacti version (the `compat` value in `INFO`) from 1.2.17 to 1.2.29 * feature: Add a third-party panel registration API so other Cacti plugins can publish their own Intropage dashboard panels without shipping files into Intropage's `panellib/` directory. Plugins call `intropage_register_panel()` (plus `intropage_update_panel_registration()`, `intropage_enable_panel_registration()`, `intropage_disable_panel_registration()` and `intropage_unregister_panel()`) guarded by `function_exists()`; registrations are stored in the new `plugin_intropage_panel_registration` table (owning plugin, callback functions, declared request variables and their types, and the full panel attribute set), merged into the live panel library, and automatically pruned from every dashboard and the poller when their owning plugin is uninstalled. The API is exposed from `setup.php` so it is available to foreign `config_arrays` hooks regardless of plugin order, rejects numeric/over-length/built-in/cross-plugin panel ids, resolves the grant target from the configured admin user (never the current visitor), gates reload/detail dispatch on the `requires` plugin being enabled, seeds a fallback `$registry` category, and invalidates the panel-library cache on every mutation. The panel schema and all API calls are documented in `README.md` * feature: Add a Panel Library management page (Console > Intropage Panels) that lists every registered panel in a filterable table with a source-plugin dropdown and Enable/Disable bulk actions, backed by the poller keeping the registration table in sync with the `panellib/` directory on file change diff --git a/locales/po/cacti.pot b/locales/po/cacti.pot index 83944ac..4fb0141 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-11-07 15:37-0500\n" +"POT-Creation-Date: 2026-11-11 14:16-0500\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: Cacti Developers >\n" "Language-Team: Cacti Developers \n" @@ -16,8 +16,9 @@ msgstr "" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n" -#: display.php +#: display.php intropage_panels.php msgid "Intropage - Permission Denied" msgstr "" @@ -29,7 +30,7 @@ msgstr "" msgid "You can disable rows above in Configure > Settings > General > Hide Cacti Dashboard and use the whole page for Intropage " msgstr "" -#: display.php includes/functions.php +#: display.php includes/functions.php intropage_panels.php msgid "Default" msgstr "" @@ -420,7 +421,11 @@ msgstr "" msgid "Trend Update Frequencies" msgstr "" -#: includes/settings.php +#: includes/registration.php intropage_panels.php +msgid "Core" +msgstr "" + +#: includes/settings.php setup.php msgid "Intropage" msgstr "" @@ -534,8 +539,9 @@ msgstr "" msgid "From now to past. Affects row coloring, older events will not be highlighted, only displayed" msgstr "" -#: includes/variables.php panellib/graphs.php panellib/mactrack.php -#: panellib/poller.php panellib/system.php panellib/thold.php +#: includes/variables.php intropage_panels.php panellib/graphs.php +#: panellib/mactrack.php panellib/poller.php panellib/system.php +#: panellib/thold.php msgid "Disabled" msgstr "" @@ -861,6 +867,159 @@ msgstr "" msgid "Yellow / Red" msgstr "" +#: intropage_panels.php +msgid "Enable" +msgstr "" + +#: intropage_panels.php +msgid "Disable" +msgstr "" + +#: intropage_panels.php +msgid "Panels" +msgstr "" + +#: intropage_panels.php +msgid "Click 'Continue' to Enable the following Panel." +msgid_plural "Click 'Continue' to Enable the following Panels." +msgstr[0] "" +msgstr[1] "" + +#: intropage_panels.php +msgid "Enable Panel" +msgstr "" + +#: intropage_panels.php +msgid "Click 'Continue' to Disable the following Panel. Disabled panels are hidden from dashboards and skipped by the poller, but their data is retained." +msgid_plural "Click 'Continue' to Disable the following Panels. Disabled panels are hidden from dashboards and skipped by the poller, but their data is retained." +msgstr[0] "" +msgstr[1] "" + +#: intropage_panels.php +msgid "Disable Panel" +msgstr "" + +#: intropage_panels.php +msgid "Cancel" +msgstr "" + +#: intropage_panels.php +msgid "Continue" +msgstr "" + +#: intropage_panels.php +msgid "Panel Name" +msgstr "" + +#: intropage_panels.php +msgid "The display name of this panel." +msgstr "" + +#: intropage_panels.php +msgid "Source" +msgstr "" + +#: intropage_panels.php +msgid "The plugin that provides this panel. Intropage's own panels show as Core." +msgstr "" + +#: intropage_panels.php +msgid "Category" +msgstr "" + +#: intropage_panels.php +msgid "The panel category." +msgstr "" + +#: intropage_panels.php +msgid "Level" +msgstr "" + +#: intropage_panels.php +msgid "System panels are shared by all users; User panels are per-user." +msgstr "" + +#: intropage_panels.php +msgid "Requires" +msgstr "" + +#: intropage_panels.php +msgid "Plugins that must be enabled for this panel to render." +msgstr "" + +#: intropage_panels.php +msgid "Refresh" +msgstr "" + +#: intropage_panels.php +msgid "The panel data collection interval." +msgstr "" + +#: intropage_panels.php +msgid "Update Function" +msgstr "" + +#: intropage_panels.php +msgid "The function the poller calls to collect this panel's data." +msgstr "" + +#: intropage_panels.php panellib/analyze.php panellib/graphs.php +#: panellib/thold.php +msgid "Status" +msgstr "" + +#: intropage_panels.php +msgid "Whether the panel is enabled." +msgstr "" + +#: intropage_panels.php +msgid "System" +msgstr "" + +#: intropage_panels.php panellib/analyze.php +msgid "User" +msgstr "" + +#: intropage_panels.php +msgid "Enabled" +msgstr "" + +#: intropage_panels.php panellib/analyze.php +msgid "None" +msgstr "" + +#: intropage_panels.php +msgid "No Panels Found" +msgstr "" + +#: intropage_panels.php setup.php +msgid "Intropage Panel Library" +msgstr "" + +#: intropage_panels.php +msgid "Search" +msgstr "" + +#: intropage_panels.php +msgid "Any" +msgstr "" + +#: intropage_panels.php +msgid "Go" +msgstr "" + +#: intropage_panels.php +msgid "Set/Refresh Filters" +msgstr "" + +#: intropage_panels.php +msgid "Clear" +msgstr "" + +#: intropage_panels.php +msgid "Clear Filters" +msgstr "" + #: panellib/alert.php panellib/syslog.php msgid "Alerts" msgstr "" @@ -960,10 +1119,6 @@ msgstr "" msgid "IP Address" msgstr "" -#: panellib/analyze.php panellib/graphs.php panellib/thold.php -msgid "Status" -msgstr "" - #: panellib/analyze.php panellib/misc.php panellib/system.php panellib/top5.php msgid "Failed" msgstr "" @@ -981,10 +1136,6 @@ msgstr "" msgid "Total Failed Logins: %s" msgstr "" -#: panellib/analyze.php -msgid "None" -msgstr "" - #: panellib/analyze.php msgid "Active Users in Last Hour: " msgstr "" @@ -1246,10 +1397,6 @@ msgstr "" msgid "Analyze Logins Detail" msgstr "" -#: panellib/analyze.php -msgid "User" -msgstr "" - #: panellib/analyze.php panellib/misc.php msgid "Result" msgstr "" @@ -2389,6 +2536,14 @@ msgstr "" msgid "Intropage Administration" msgstr "" +#: setup.php +msgid "Management" +msgstr "" + +#: setup.php +msgid "Panel Library" +msgstr "" + #: setup.php msgid "System Administration" msgstr "" @@ -2424,3 +2579,7 @@ msgstr "" #, php-format msgid "%d Day" msgstr "" + +#: setup.php +msgid "Actions" +msgstr "" diff --git a/panellib/syslog.php b/panellib/syslog.php index 267475f..5793c28 100644 --- a/panellib/syslog.php +++ b/panellib/syslog.php @@ -104,6 +104,64 @@ 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. 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. + */ +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'; + + 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; + } + } + } + // @codeCoverageIgnoreEnd + + $connected = false; + + if (function_exists('syslog_connect')) { + $connected = (bool) syslog_connect(); + } + + if (!$connected || 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 +178,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 +229,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,6 +253,10 @@ function plugin_syslog_levels_trend(): void { GROUP BY priority_id', [2 * $pi, $pi]); + if (!is_array($levels)) { + $levels = []; + } + foreach ($levels as $level) { $l = (int) $level['priority_id']; $data[$l] = $level['mcount']; @@ -486,12 +540,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 +619,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..9c36a4b --- /dev/null +++ b/tests/Unit/SyslogPanelConnectionTest.php @@ -0,0 +1,165 @@ + 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['__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 () { + $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('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; + + 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(); +}); + +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(); +}); + +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'); +});