Skip to content

fix: show last runtime stats, keep historic VLANs in the filter, keep zero-only filters (#477, #478, #479) - #480

Merged
TheWitness merged 4 commits into
developfrom
fix/479-last-runtime-stats
Dec 3, 2026
Merged

TheWitness merged 4 commits into
developfrom
fix/479-last-runtime-stats

Conversation

@TheWitness

Copy link
Copy Markdown
Member

Fixes the three open issues reported by @CrazyNicc.

#479 – Last runtime statistics always show 0

The poller stored Time:.. ConcurrentProcesses:.. Devices:.. with a trailing space. mactrack_display_stats() split on single spaces and expected exactly three fields, so it got four and always fell back to the defaults (rendered as 0).

  • New mactrack_parse_stats() parses the stored string by key (any whitespace, any order), so values already stored by older pollers display correctly right away.
  • The poller no longer writes the trailing space.

#477 – VLAN names missing from the MAC Addresses VLAN filter

Each collection marked every VLAN present='0' and deleted any VLAN the current scan did not see. mac_track_ports / mac_track_aggregated_ports keep older sightings, so "Show: All" listed MACs whose VLAN was no longer in the filter.

  • A VLAN missed by a scan is only deleted once no row in mac_track_ports or mac_track_aggregated_ports still references it (same site scoping as before).
  • One-time backfill (mactrack_backfill_vlans(), gated by the mt_vlans_backfilled setting) restores VLANs that existing installs have already lost. It runs in the poller (CLI), not in a web request.
  • VLAN names in the filter list are now HTML-escaped.

#478 – "00" in a filter is truncated to "0"

Cacti 1.2's validate_store_request_vars() checks get_nfilter_request_var($variable) == '0' with a loose comparison. PHP treats "00", "000", "0.0" as numeric zero, so core replaces them with '0'.

  • New mactrack_validate_store_request_vars() wraps the core function and restores zero-only values for text (FILTER_CALLBACK/FILTER_DEFAULT) filters, re-running the field's own sanitizer and updating the session copy. All 14 plugin pages now use it, so every filter field is covered (IP, MAC, Portname, Search, …).
  • The root cause is in Cacti core and should also be fixed there (strict === '0'). This workaround is still needed for 1.2.x installs.

Tests

  • New tests/Unit/StatsVlanZeroFilterTest.php covers stats parsing (old trailing-space format), the zero-only filter workaround, and the one-time VLAN backfill.
  • PollerSiteScopeTest updated for the new VLAN prune statement.
  • The bootstrap now has a validate_store_request_vars() stub that reproduces core's loose '0' comparison, and get_nfilter_request_var() reads the test request store.
  • Full Pest suite: 1177 passed, 13 skipped (DB integration, no DSN).
  • Ran the backfill and prune SQL on MariaDB 10.5: historic VLANs are restored, N/A VLAN ids are skipped, and a VLAN with no stored history is still pruned.

Fixes #477
Fixes #478
Fixes #479

… zero-only filters (#477, #478, #479)

* #479: The poller stored "Time:.. ConcurrentProcesses:.. Devices:.. "
  with a trailing space, so splitting on single spaces gave four fields
  and the view's three-field check always failed, showing 0. Parse the
  stats by key with mactrack_parse_stats(), which accepts both the old
  and new format, and drop the trailing space in the poller.
* #477: Every scan deleted each VLAN it did not see, while
  mac_track_ports and mac_track_aggregated_ports keep older sightings,
  so "Show: All" listed MACs whose VLAN was missing from the VLAN
  filter. Only remove a VLAN once no stored MAC row uses it, and
  backfill mac_track_vlans from stored history once
  (mt_vlans_backfilled) to restore VLANs already dropped. Escape the
  VLAN names in the filter list.
* #478: Cacti 1.2 compares each request value to '0' loosely, so "00"
  (and "000", "0.0") was stored as '0'. Route every page through
  mactrack_validate_store_request_vars(), which restores zero-only text
  filters after core validation.

Fixes #477
Fixes #478
Fixes #479

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI balanced review requested due to automatic review settings December 3, 2026 04:56

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 backfill completion flag is set even if an INSERT fails, preventing retries and potentially leaving historical VLANs missing from the filter.

1 open issue
What changed in this PR

Addresses #477–#479 in Device Tracking by correcting runtime statistics and preserving historical VLAN choices and zero-only search filters.

Changes:

  • Parse runtime statistics by key and remove the poller’s trailing space.
  • Retain and backfill VLANs referenced by stored MAC history; escape VLAN labels.
  • Preserve zero-only text filters across 14 pages and add regression coverage.
File Description
tests/​Unit/​StatsVlanZeroFilterTest.php Tests statistics, zero-only filters, and VLAN backfill.
tests/​Unit/​PollerSiteScopeTest.php Updates VLAN pruning assertions.
tests/​bootstrap-unit.php Models core’s zero-filter behavior in test stubs.
poller_mactrack.php Preserves historical VLANs and adjusts statistics output.
mactrack_view_sites.php Applies the filter workaround.
mactrack_view_macs.php Applies the workaround and escapes VLAN labels.
mactrack_view_ips.php Applies the filter workaround.
mactrack_view_interfaces.php Applies the filter workaround.
mactrack_view_dot1x.php Applies the filter workaround.
mactrack_view_devices.php Applies the filter workaround.
mactrack_view_arp.php Applies the filter workaround.
mactrack_vendormacs.php Applies the filter workaround.
mactrack_snmp.php Applies the filter workaround.
mactrack_sites.php Applies the filter workaround.
mactrack_macwatch.php Applies the filter workaround.
mactrack_macauth.php Applies the filter workaround.
mactrack_devices.php Applies the filter workaround.
mactrack_device_types.php Applies the filter workaround.
lib/​mactrack_functions.php Adds filter preservation, statistics parsing, and VLAN backfill helpers.

Review effort: Balanced

Comment thread lib/mactrack_functions.php Resolved
TheWitness and others added 3 commits December 3, 2026 00:02
The patch coverage gate flagged the 'clear' early return and an
unreachable non-string guard in mactrack_validate_store_request_vars().
Cover the clear path and drop the guard: both allowed filters return a
string from the plugin's sanitizers.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
mactrack_backfill_vlans() set mt_vlans_backfilled even when an INSERT
failed, so a transient database error skipped recovery for good. Stop
before setting the flag when db_execute() returns false.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Add CHANGELOG entries for this pull request and move the entries added after the v5.0 tag (4c9aba9) out of the 5.0 section into a new --- develop --- section, so the 5.0 section matches what shipped.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@sonarqubecloud

sonarqubecloud Bot commented Dec 3, 2026

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

Failed conditions
3.4% Duplication on New Code (required ≤ 3%)

See analysis details on SonarQube Cloud

@TheWitness
TheWitness merged commit 5d77204 into develop Dec 3, 2026
8 of 9 checks passed
@TheWitness
TheWitness deleted the fix/479-last-runtime-stats branch December 3, 2026 11:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants