Skip to content

mactrack: tolerate transient OUI database download failures - #415

Merged
TheWitness merged 2 commits into
developfrom
fix/mactrack-oui-download-transient
Nov 11, 2026
Merged

TheWitness merged 2 commits into
developfrom
fix/mactrack-oui-download-transient

Conversation

@TheWitness

Copy link
Copy Markdown
Member

What

Make the IEEE OUI database download in import_oui_database() tolerant of transient connectivity failures so they no longer surface as PHP warnings.

Why

import_oui_database() fetched the IEEE OUI URL with a bare file($oui_file). When IEEE refuses the connection (a common transient hiccup), PHP raises an E_WARNING, which Cacti's CactiErrorHandler records as an ERROR PHP WARNING log entry, e.g.:

ERROR PHP WARNING in  Plugin 'mactrack': file(): Failed to open stream: Connection refused in file: .../plugins/mactrack/lib/mactrack_functions.php  on line: 3042

The plugin CI's Check Cacti Logs for Errors step runs grep -E "FATAL:|ERROR:|WARNING:" over cacti.log, so every transient IEEE failure fails an otherwise-unrelated pull request.

What changed

Added mactrack_download_oui_database() and call it in place of the bare file():

  • Suppresses the file() warning with @. Cacti's CactiErrorHandler honors the @ operator (if (!(error_reporting() & $level)) return true;), so no error log entry is written — the line CI greps for is never produced.
  • Reports the outcome at INFO level via cacti_log('INFO: ...', false, 'MACTRACK', POLLER_VERBOSITY_LOW). The INFO: prefix does not match CI's FATAL:|ERROR:|WARNING: pattern, so the message stays visible in the log without failing CI.
  • Handles transience — retries each URL and falls back to the alternate IEEE endpoint that was already defined in the function but previously unused.

Downstream behavior is unchanged: on total failure the helper returns false and the existing is_array() path prints the usual "FAILED" status.

Testing

  • php -l passes.
  • PHPStan level 6 (the same check CI runs) is clean for the new code.
  • Standalone harness simulating refused connections against a Cacti-style error handler: helper returns false, produces 0 handler hits (no PHP warning logged), emits INFO lines, and 0 of them match CI's FATAL:|ERROR:|WARNING: grep.

Notes for reviewers

Beyond this change, consider hardening CI so third-party network flakiness can't gate PRs — e.g. seed a local OUI fixture for the poller test, or narrow the log grep to exclude INFO:/NOTE: lines. Migrating the fetch to Cacti's cURL-based HTTP helpers (with explicit timeout/status checks) would also give cleaner transient-vs-real error handling than file().

import_oui_database() called file() directly on the IEEE OUI URL. When
the remote refused the connection, PHP raised an E_WARNING that Cacti
logged as an "ERROR PHP WARNING" entry. That fails the plugin CI (which
greps the Cacti log for FATAL:/ERROR:/WARNING:) and raises an alert on
every transient IEEE hiccup.

Add mactrack_download_oui_database(), which suppresses the file()
warning (Cacti's error handler honors the @ operator, so no error entry
is written), retries each URL, and falls back to the alternate IEEE
endpoint that was previously defined but unused. Failures are now
reported with a single INFO-level log message instead of a PHP warning.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings November 10, 2026 14:08

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 downloader lacks automated tests required by the 100% changed-line coverage gate, and its warning suppression still allows refused connections to produce ERROR logs on PHP 8.2+.

2 open issues
What changed in this PR

This PR aims to make Device Tracking’s IEEE OUI database import tolerate transient download failures without generating PHP warning logs.

Changes:

  • Adds a download helper with retries, delays, and fallback URLs.
  • Attempts warning suppression and logs failed downloads at INFO level.
  • Routes the importer through the helper while preserving its existing failure handling.
File Description
lib/​mactrack_functions.php Adds the resilient OUI downloader and integrates it into the import flow.

Review effort: Balanced

Comment thread lib/mactrack_functions.php Resolved
Comment thread lib/mactrack_functions.php Resolved
xmacan
xmacan previously approved these changes Nov 10, 2026
bmfmancini
bmfmancini previously approved these changes Nov 10, 2026
Addresses PR review feedback:

- The @ operator is unreliable across Cacti branches: 1.2.x's
  CactiErrorHandler checks error_reporting() == 0, which PHP 8.0+ never
  produces for @, so refused connections still logged an "ERROR PHP
  WARNING" there. Replace the @ suppression with a local
  set_error_handler()/restore_error_handler() (in a finally) around
  file(), so the warning is captured on every supported Cacti branch and
  PHP version and reported only as a single INFO log.

- Add tests/Unit/MactrackOuiDownloadTest.php covering success, fallback
  ordering, retry counts, exhaustion returning false, empty-URL
  filtering, the retries floor, local warning capture, the inter-attempt
  delay, and import_oui_database() routing through the helper. This
  satisfies the 100% changed-line coverage gate deterministically with
  local file paths instead of network access.

- Add a db_qstr() stub to the unit bootstrap so import_oui_database()
  can be exercised in isolation.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@TheWitness
TheWitness dismissed stale reviews from bmfmancini and xmacan via d77e0a5 November 10, 2026 21:10
@sonarqubecloud

Copy link
Copy Markdown

@TheWitness
TheWitness merged commit 998ec26 into develop Nov 11, 2026
9 checks passed
@TheWitness
TheWitness deleted the fix/mactrack-oui-download-transient branch November 11, 2026 00:23
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.

4 participants