Skip to content

refactor(evaluate): share the log, hook and SNMP trap actions across threshold types (#906, step 6) - #923

Merged
TheWitness merged 1 commit into
developfrom
refactor/906-shared-actions
Dec 4, 2026
Merged

TheWitness merged 1 commit into
developfrom
refactor/906-shared-actions

Conversation

@TheWitness

Copy link
Copy Markdown
Member

Step 6 of #906. Stacked on #921; merge after it. Behaviour is unchanged.

What changes

Every Hi / Low, Time Based and Baseline handler built three things by hand: the threshold log row (17 copies), the thold_action hook payload (11 copies) and the SNMP trap varbinds (11 copies). They now call shared helpers in includes/evaluate.php:

Helper Replaces
thold_record_transition() the thold_log([...]) blocks, including the "Only logging, maint device" note
thold_fire_action() the $save = [...]; api_plugin_hook_function('thold_action', $save); blocks. It keeps the same keys in the same order for each action class
thold_threshold_trap_varbinds() (pure) and thold_send_threshold_trap() the get_thold_snmp_data() � thold_snmptrap() blocks: event fields, device address, and the <FAIL_COUNT> / <FAIL_DURATION> tags

A script replaced each block, but only after checking that the block matched the canonical form exactly; any other block would have stopped it. It also dropped the context unpacks that were no longer used (29 of them). The two largest handlers, thold_time_alert and thold_time_warning, go from 152 and 164 lines to 112 each.

$$a (SonarCloud): the four queued-notification handlers in notify_queue.php read their fields with variable variables. They now use keyed destructuring of a new thold_notification_fields() helper, which keeps the same '' default for missing or null fields. This is the follow-up noted on #906.

Docblock: thold_snmptrap()'s docblock now types $overwrite as the SNMPAgent manager-override array (or false), which is what every caller passes. This clears 11 PHPStan errors.

The only array-order difference is in the Baseline restoral trap: eventDeviceIp is now set after eventNotificationType rather than before. Cacti's snmpagent_notification() looks varbinds up by name in MIB order, so the trap it sends is the same.

Tests

  • ThresholdTrapTest: drives all 11 trap sites through thold_check_threshold() with traps on. It checks the event class, severity, status, re-alert status, notification type, device address, and which fail tags are filled in the description. The same test passes unchanged against the code before this PR (refactor(evaluate): split Baseline evaluation into restored and breached handlers (#906, step 5) #921).
  • SharedActionsTest: the log row and maintenance note, the hook payload for alert and restoral classes, the three description-tag cases, and thold_notification_fields().
  • Fixture changes:
    • The test bootstrap gains a recording snmpagent_notification() stub and SNMPAGENT_EVENT_SEVERITY_MEDIUM, and the hook stub now records its calls.
    • The threshold fixture gains the snmp_event_warning_severity, snmp_event_category and thold_template_id columns that get_thold_snmp_data() reads.

Checks

somethingwithproof added a commit to somethingwithproof/plugin_thold that referenced this pull request Dec 4, 2026
…acti#906

This reverts commit 9309cc3. The variable-variable blocks in
notify_queue.php are replaced in Cacti#923 by thold_notification_fields(),
which the maintainer had already planned on Cacti#906, and the two changes
conflict. Any duplication left in this file can be removed on top of
that helper once it has merged.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
@TheWitness
TheWitness force-pushed the refactor/906-baseline branch from 24fb868 to b136efb Compare December 4, 2026 11:52
xmacan
xmacan previously approved these changes Dec 4, 2026
Base automatically changed from refactor/906-baseline to develop December 4, 2026 12:12
@TheWitness
TheWitness dismissed xmacan’s stale review December 4, 2026 12:12

The base branch was changed.

…threshold types (#906)

Every Hi / Low, Time Based and Baseline handler built the same thold_log()
row, the same thold_action hook payload and the same SNMP trap varbinds by
hand: 17, 11 and 11 copies. They now call four helpers in evaluate.php:

- thold_record_transition() writes the log row, adding the
  "Only logging, maint device" note when asked.
- thold_fire_action() calls the thold_action hook with the same keys, in
  the same order, per action class.
- thold_threshold_trap_varbinds() (pure) applies the event fields, the
  device address and the fail count / duration tags; and
  thold_send_threshold_trap() sends them.

Each block was replaced by a script that first checked it matched the
canonical form exactly. Context unpacks that are no longer used were
dropped.

The queued notification handlers in notify_queue.php read their fields
with variable variables ($$a). They now use keyed destructuring of a new
thold_notification_fields() helper, which keeps the same '' default for
missing or null fields.

Tests: ThresholdTrapTest drives all 11 trap sites with traps on and checks
the event class, status, notification type, device address and description
tags. It gives the same results against the code before this change.
SharedActionsTest covers the helpers. The test bootstrap gains a recording
snmpagent_notification() stub and SNMPAGENT_EVENT_SEVERITY_MEDIUM; the hook
stub now records its calls; and the threshold fixture gains the
snmp_event_warning_severity, snmp_event_category and thold_template_id
columns that get_thold_snmp_data() reads.

The .pot is regenerated: its content is unchanged, but the i18n gate
counts calls, and 17 calls to the maintenance note are now 1.

thold_snmptrap()'s docblock now says $overwrite is the manager override
array (or false), which is what callers pass.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@TheWitness
TheWitness force-pushed the refactor/906-shared-actions branch from fbf0885 to c6e0b59 Compare December 4, 2026 12:13
@sonarqubecloud

sonarqubecloud Bot commented Dec 4, 2026

Copy link
Copy Markdown

Quality Gate Failed Quality Gate failed

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

See analysis details on SonarQube Cloud

@TheWitness

Copy link
Copy Markdown
Member Author

Rebased onto develop after #921 was squash-merged. The patch is unchanged: git patch-id of c6e0b59 matches the approved fbf0885 (df2a55f2). This is the first full CI run, and it is green: PHP 8.2�8.6 integration and the translation check; SonarCloud fails on duplication only. @xmacan @somethingwithproof, please re-approve.

@TheWitness
TheWitness merged commit f7dc9c8 into develop Dec 4, 2026
7 of 8 checks passed
@TheWitness
TheWitness deleted the refactor/906-shared-actions branch December 4, 2026 12:17
TheWitness pushed a commit that referenced this pull request Dec 4, 2026
…Cloud gate can pass (#922)

* refactor(settings): build navigation and settings fields with helpers

thold_draw_navigation_text() repeated the same four-key breadcrumb array
27 times, and thold_config_settings() repeated the spacer, checkbox,
sized textbox and notification textarea field shapes 45 times. Each
shape is now built by one helper; the translatable strings stay literal
at the call site.

Behaviour is unchanged: a characterization test compares both arrays,
key order and value types included, with JSON captured from the
previous literals.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* refactor(logging): share threshold and recipient text in change log

plugin_thold_log_changes() repeated the same threshold lookup and
DataTemplate/DataSource text in seven cases that differed only in the
verb, and the 'modified' and 'modified_template' cases repeated the
same block that assembles the alert and warning recipients. The seven
cases now share one case and plugin_thold_log_threshold_desc(), and the
recipients come from plugin_thold_log_notify_emails().

Behaviour is unchanged: a characterization test pins every affected log
line, the queries and their parameters, and each recipient branch.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* refactor(notify_queue): share mail decoding and command execution

process_device_notifications() and process_non_device_notifications()
each carried the same mail field decoding (with attachment base64
reversal) and the same command execution and completion block. Both now
call thold_notification_mail_fields() and
thold_notification_run_command().

Behaviour is unchanged; the existing queue tests drive every mail and
command path of both functions.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* refactor(threshold_api): append validation errors through one helper

thold_validate_save() repeated the same three steps for each of its 16
checks: append the message to the banner, then mark one or two form
fields in error. thold_validation_error() now does both; each check
passes its literal message and field names in the original order.

Behaviour is unchanged: a characterization test pins the result, the
banner text and the highlighted fields for every check of each
threshold type, captured from the previous code.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* refactor(queries): share the clause builder of the allowed threshold queries

get_allowed_thresholds() and get_allowed_threshold_logs() began with the
same block that builds the LIMIT, ORDER BY and WHERE clauses, binds the
graph id, resolves the user and applies the graph permission policy. Both
now call thold_allowed_sql_clauses(), which runs the same lookups in the
same order and still makes the caller return no rows when no user can
be resolved.

Behaviour is unchanged; GetAllowedThresholdsTest drives both functions
through every branch of the shared code.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* refactor(notify): run the selected trigger command through one block

thold_command_execution() repeated the same expansion, environment,
queue-or-exec block for the high, low and normal trigger commands. It
now picks the command column with the same precedence and runs that
block once.

Behaviour is unchanged; TholdCommandExecutionTest covers each breach
direction, the precedence, queued and inline execution.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* test(logging): restore the config array in one step

SonarCloud read the save and restore of $config['is_web'] around the
call as an overwritten array key (php:S4143). Swap the whole array and
restore it in a finally block instead.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* revert: leave includes/notify_queue.php to the notification step of #906

This reverts commit 9309cc3. The variable-variable blocks in
notify_queue.php are replaced in #923 by thold_notification_fields(),
which the maintainer had already planned on #906, and the two changes
conflict. Any duplication left in this file can be removed on top of
that helper once it has merged.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* test(queries): pin the user id handed to the row-count cache

Assert the user id for an explicit user, the session user, -1 and an authentication-off 0, for both accessors.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

* test: pin change-log notes and recipients and clause order in allowed-row queries

Cover the deletion-only note, template contacts, the warning list id used
for warning recipients, and ORDER BY preceding LIMIT in the shared clause
builder.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>

---------

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
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.

2 participants