Skip to content

bug: Open the Syslog database connection before the Syslog panels query it - #474

Merged
TheWitness merged 5 commits into
developfrom
fix/syslog-remote-db
Nov 11, 2026
Merged

TheWitness merged 5 commits into
developfrom
fix/syslog-remote-db

Conversation

@TheWitness

Copy link
Copy Markdown
Member

Description

The Intropage Syslog panels — plugin_syslog_trend, plugin_syslog_levels_trend, plugin_syslog_devices and plugin_syslog_devices_detail in panellib/syslog.php — require_once the Syslog plugin's database library and then run its syslog_db_*() helpers, but they never opened the Syslog database connection. Those helpers route queries through the global $syslog_cnn connection; with it unset they fall back to the main Cacti connection.

On an installation whose Syslog tables live in a database separate from Cacti's, the panels therefore fail during the Intropage poller run:

ERROR: A DB Row Failed!, Error: Table 'cacti.syslog' doesn't exist
ERROR: A DB Cell Failed!, Error: Table 'cacti.syslog_logs' doesn't exist
PHP WARNING: foreach() argument must be of type array|object, false given
    in .../plugins/intropage/panellib/syslog.php on line 206

(the foreach warning is the empty/false result of the failed query).

This PR adds 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.

Related Issue

Reported on the Cacti forums: https://forums.cacti.net/viewtopic.php?t=64912 (Edit 4). The Syslog-plugin-side fatals from the same thread are handled separately in Cacti/plugin_syslog#439.

Motivation and Context

With a split Syslog database, the Intropage Syslog panels never collect data and spam the log each poll. Opening the Syslog connection the way the Syslog plugin intends fixes both the wrong-database queries and the downstream foreach-on-false warning.

How Has This Been Tested?

  • php -l clean on panellib/syslog.php and the new test (PHP 8.3.36).
  • Full plugin suite via Cacti's own Composer vendor tree, plugin staged as a real dir at plugins/intropage:
    php include/vendor/bin/pest --configuration plugins/intropage/phpunit.xml
    → 218 passed, 102 skipped (the 2 remaining warnings are pre-existing, in setup.php file pruning, and unrelated to this change).
  • New tests/Unit/SyslogPanelConnectionTest.php covers the connection contract (ready when the connection opens; logs and fails when it cannot; panel collectors skip cleanly when unavailable). It replaces SyslogDatabaseLoaderTest, which asserted the old "load the library from either path" behaviour that this change supersedes.

Testing environment: AlmaLinux 9, PHP 8.3.36, Cacti 1.2.x vendor tree.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation repository accordingly.

…ry 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>
Copilot AI balanced review requested due to automatic review settings November 11, 2026 15:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The Syslog helper ignores an explicit connection failure when a connection remains populated, and failed query results still enter the loop, causing warnings and corrupting the priority-0 count.

2 open issues
What changed in this PR

This PR addresses Intropage Syslog panels querying Cacti’s database when Syslog uses a separate database.

Changes:

  • Adds a shared connection helper used by four Syslog callbacks, with failure handling.
  • Adds defensive handling for non-array levels-query results.
  • Replaces library-loading tests with connection-focused tests and documents the fix.
File Description
tests/​Unit/​SyslogPanelConnectionTest.php Adds connection success, failure, and panel-update tests.
tests/​Unit/​SyslogDatabaseLoaderTest.php Removes the previous library-path loading tests.
panellib/​syslog.php Centralizes Syslog connection setup and updates panel failure handling.
CHANGELOG.md Documents the separate-database connection fix.

Review effort: Balanced

Comment thread panellib/syslog.php Resolved
Comment thread panellib/syslog.php Resolved
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>
@TheWitness

Copy link
Copy Markdown
Member Author

Addressed both code-review findings in 289d332:

  1. Honor syslog_connect()'s return value — the helper now treats an explicit false return as "not ready" (not just an empty $syslog_cnn), so the populated-connection-but-schema-not-ready case correctly skips collection across all four panels.
  2. Guard the levels query — replaced the (array) $levels cast with an is_array() check so a false query result produces no iterations instead of [false], eliminating the array-offset warnings and preserving the priority-0 count.

Added regression tests for both (populated-connection failure; false levels query with no warnings/notices).

Validation on AlmaLinux 9 / PHP 8.3.36 against Cacti's vendor tree:

  • php -l clean on both files
  • PHPStan level 6: no errors
  • php-cs-fixer: clean on panellib/syslog.php
  • Full suite via pest --configuration plugins/intropage/phpunit.xml: 220 passed, 102 skipped (the 2 remaining warnings are the pre-existing setup.php file-pruning ones, unrelated to this change)

Note: CI itself is currently red due to an unrelated DNS failure in the runner, not these changes.

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>
xmacan
xmacan previously approved these changes Nov 11, 2026
# Conflicts:
#	CHANGELOG.md
#	locales/po/cacti.pot
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>
@sonarqubecloud

Copy link
Copy Markdown

@TheWitness
TheWitness merged commit ba75b7d into develop Nov 11, 2026
15 of 16 checks passed
@TheWitness
TheWitness deleted the fix/syslog-remote-db branch November 11, 2026 19:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants