Repository navigation
Guard maint maintenance-window checks against a missing maint functions file #859
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| <?php | ||
| /* | ||
| +-------------------------------------------------------------------------+ | ||
| | Copyright (C) 2004-2026 The Cacti Group | | ||
| | | | ||
| | This program is free software; you can redistribute it and/or | | ||
| | modify it under the terms of the GNU General Public License | | ||
| | as published by the Free Software Foundation; either version 2 | | ||
| | of the License, or (at your option) any later version. | | ||
| +-------------------------------------------------------------------------+ | ||
| | Cacti: The Complete RRDtool-based Graphing Solution | | ||
| +-------------------------------------------------------------------------+ | ||
| | http://www.cacti.net/ | | ||
| +-------------------------------------------------------------------------+ | ||
| */ | ||
|
|
||
| /** | ||
| * thold_update_host_status() consults the maint plugin for both the | ||
| * recovering-device and down-device notification loops. The call is now | ||
| * guarded by function_exists('plugin_maint_check_cacti_host') so a maint | ||
| * plugin that is enabled but whose functions file could not be included no | ||
| * longer fatals with an undefined-function error. These tests drive both | ||
| * maintenance-gated branches so that guard stays in place. | ||
| */ | ||
| final class MaintCheckGuardTest extends TestCase { | ||
| /** | ||
| * @return void | ||
| */ | ||
| public static function setUpBeforeClass(): void { | ||
| self::loadPluginSource('includes/functions.php'); | ||
| self::loadPluginSource('includes/polling.php'); | ||
| self::loadPluginSourceAlways('includes/arrays.php'); | ||
| self::loadPluginConstants(); | ||
| } | ||
|
|
||
| /** | ||
| * @return string | ||
| */ | ||
| private function maintIncludesRoot() { | ||
| return dirname(__DIR__) . '/Fixtures/cacti-root-maint-includes'; | ||
| } | ||
|
|
||
| /** | ||
| * Run thold_update_host_status() against the newer maint layout. A device | ||
| * inside a maintenance window is logged and the loop moves on without the | ||
| * downstream notification work, so the call returns on its own. Expected | ||
| * engine warnings are suppressed, but no Throwable is swallowed, so an | ||
| * unexpected error (e.g. a removed guard fataling on an undefined maint | ||
| * function) fails the test. | ||
| * | ||
| * @return void | ||
| */ | ||
| private function runHostStatus() { | ||
| CactiStubs::willAlwaysReturn('api_plugin_is_enabled', true); | ||
| CactiStubs::willAlwaysReturn('plugin_maint_check_cacti_host', true); | ||
| CactiStubs::$configOptions['alert_deadnotify'] = 'on'; | ||
| $GLOBALS['config']['base_path'] = $this->maintIncludesRoot(); | ||
|
TheWitness marked this conversation as resolved.
|
||
|
|
||
| set_error_handler(static fn (): bool => true); | ||
| ob_start(); | ||
|
|
||
| try { | ||
| thold_update_host_status(); | ||
| } finally { | ||
| ob_end_clean(); | ||
| restore_error_handler(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * A recovered device inside a maintenance window is logged, not alerted. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function testRecoveringDeviceInMaintenanceIsLoggedNotAlerted(): void { | ||
| CactiStubs::willReturnFor('db_fetch_assoc', 'plugin_thold_host_failed', [['host_id' => 42]]); | ||
| CactiStubs::willReturnFor('db_fetch_row_prepared', 'FROM host WHERE id', [ | ||
| 'id' => 42, | ||
| 'hostname' => 'dev42', | ||
| 'description' => 'Device 42', | ||
| 'status' => HOST_UP, | ||
| 'snmp_community' => '', | ||
| 'snmp_username' => '', | ||
| 'snmp_version' => 0, | ||
| 'site_id' => 0, | ||
| 'location' => '', | ||
| 'notes' => '', | ||
| 'status_fail_date' => '0000-00-00 00:00:00', | ||
| 'availability' => 100, | ||
| 'total_polls' => 1, | ||
| 'failed_polls' => 0, | ||
| 'cur_time' => 0, | ||
| 'avg_time' => 0, | ||
| 'thold_send_email' => '0', | ||
| ]); | ||
|
|
||
| $this->runHostStatus(); | ||
|
|
||
| $this->assertContains( | ||
| 'WARNING: Device[42] Hostname[dev42] is recovering! Only logging, maint device', | ||
| CactiStubs::$log | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * A down device inside a maintenance window is logged, not alerted. | ||
| * | ||
| * @return void | ||
| */ | ||
| public function testDownDeviceInMaintenanceIsLoggedNotAlerted(): void { | ||
| CactiStubs::willReturnFor('db_fetch_assoc_prepared', 'availability_method', [[ | ||
| 'id' => 77, | ||
| 'hostname' => 'dev77', | ||
| ]]); | ||
|
|
||
| $this->runHostStatus(); | ||
|
|
||
| $this->assertContains( | ||
| 'WARNING: Device[77] Hostname[dev77] is DOWN. Only logging, maint device', | ||
| CactiStubs::$log | ||
| ); | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.