Repository navigation
refactor(evaluate): share the log, hook and SNMP trap actions across threshold types (#906, step 6) - #923
Merged
Conversation
3 tasks done
This was referenced Dec 4, 2026
Merged
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
force-pushed
the
refactor/906-baseline
branch
from
December 4, 2026 11:52
24fb868 to
b136efb
Compare
xmacan
previously approved these changes
Dec 4, 2026
…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
force-pushed
the
refactor/906-shared-actions
branch
from
December 4, 2026 12:13
fbf0885 to
c6e0b59
Compare
|
Member
Author
|
Rebased onto |
xmacan
approved these changes
Dec 4, 2026
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


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_actionhook payload (11 copies) and the SNMP trap varbinds (11 copies). They now call shared helpers inincludes/evaluate.php:thold_record_transition()thold_log([...])blocks, including the "Only logging, maint device" notethold_fire_action()$save = [...]; api_plugin_hook_function('thold_action', $save);blocks. It keeps the same keys in the same order for each action classthold_threshold_trap_varbinds()(pure) andthold_send_threshold_trap()get_thold_snmp_data()�thold_snmptrap()blocks: event fields, device address, and the<FAIL_COUNT>/<FAIL_DURATION>tagsA 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_alertandthold_time_warning, go from 152 and 164 lines to 112 each.$$a(SonarCloud): the four queued-notification handlers innotify_queue.phpread their fields with variable variables. They now use keyed destructuring of a newthold_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$overwriteas the SNMPAgent manager-override array (orfalse), which is what every caller passes. This clears 11 PHPStan errors.The only array-order difference is in the Baseline restoral trap:
eventDeviceIpis now set aftereventNotificationTyperather than before. Cacti'ssnmpagent_notification()looks varbinds up by name in MIB order, so the trap it sends is the same.Tests
ThresholdTrapTest: drives all 11 trap sites throughthold_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, andthold_notification_fields().snmpagent_notification()stub andSNMPAGENT_EVENT_SEVERITY_MEDIUM, and the hook stub now records its calls.snmp_event_warning_severity,snmp_event_categoryandthold_template_idcolumns thatget_thold_snmp_data()reads.Checks