Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ installation guidance and source-checked behavior.
--- develop ---
* issue#839: The plugin upgrade now migrates only from Thold 1.5 or later, which retires the 1.0.1 - 1.2.4 migrations and everything before 1.0 from `includes/database.php`. An older install is refused with a Cacti log error, nothing changed, until `php plugins/thold/cli/upgrade_legacy.php` (with `--dry-run` and `--debug`) has brought a 1.0 - 1.4 install to 1.5 and run the normal upgrade. Installs older than 1.0 are no longer supported. A forced upgrade now re-runs the migrations from 1.5
* issue#839: Move the non-polling command line scripts into `cli/` without the `cli_` prefix: `cli_thresholds.php` is now `cli/thresholds.php`, `cli_import.php` is `cli/import.php`, and `extras/upgrade.php` and `extras/apply_realms.php` are `cli/upgrade.php` and `cli/apply_realms.php`. The old paths and `extras/` are tombstoned, so an upgrade removes them
* issue#907: Keep the threshold log when Alert Log Retention is set to 'Indefinitely'. The setting's value of -1 was used as a number of days, so every poller run deleted all of `plugin_thold_log`
* issue#908: Stop cleanly when an install older than Thold 1.5 has not been through `cli/upgrade_legacy.php`: the poller no longer checks thresholds against the old schema (which ended in a fatal error and Cacti disabling the plugin), `cli/upgrade.php` reports the refusal and exits non-zero instead of printing 'Upgrade Complete!', and Thold pages show a message naming the legacy upgrade script
* issue#909: Scale a percentage threshold's HRULE with the CDEF of the first drawn graph item that has one, instead of the CDEF with the highest id, and strip whitespace from numeric CDEF values so a stray newline cannot break the graph
* issue#691: Validate a new threshold's values before its first save, so one created from Console > Data Sources can no longer be saved with impossible or missing limits; only the non-templated wizard's empty first save skips validation. A rejected save now returns to the edit form with its data source instead of a blank page
* issue#882: Replace triple percent (`%%%`) format specifiers with `%%` in `includes/arrays.php`, `includes/functions.php`, and `setup.php` so PHP 8 `sprintf()` and `msgfmt --check-format` handle percent signs cleanly, and refresh `locales/po/cacti.pot`; also fold the four alert/warning percentage HRULE blocks in `setup.php` into one loop with no behavior change, to keep new-code duplication under the project's quality gate; remove orphan `locales/LC_MESSAGES/ka.mo` and `locales/LC_MESSAGES/uk.mo`
* issue#889: Read the Threshold Daemon queue in bounded batches, check each queued threshold once, delete exactly the rows a worker read, and purge rows for deleted thresholds
Expand Down
9 changes: 8 additions & 1 deletion cli/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@
include_once('./plugins/thold/includes/database.php');

print 'Force Running Thold Upgrade' . PHP_EOL;
thold_upgrade_database(true);

if (thold_upgrade_database(true) === false) {
print 'ERROR: This Thold install is older than ' . THOLD_UPGRADE_BASELINE . ' and was not upgraded.' . PHP_EOL;
print 'Run \'php plugins/thold/cli/upgrade_legacy.php\' from the Cacti directory first.' . PHP_EOL;

exit(1);
}

print 'Upgrade Complete!' . PHP_EOL;
print 'Check your cacti.log for errors.' . PHP_EOL;
print 'If you find them, please report on GitHub!' . PHP_EOL;
10 changes: 10 additions & 0 deletions includes/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ function thold_upgrade_needs_legacy($oldv) {
return $oldv != '' && cacti_version_compare($oldv, THOLD_UPGRADE_BASELINE, '<');
}

/**
* thold_upgrade_legacy_message - the notice shown in the web interface when an install predates the
* upgrade baseline.
*
* @return string
*/
function thold_upgrade_legacy_message() {
return __('This Thold install is older than %s and cannot be upgraded automatically. Run \'php plugins/thold/cli/upgrade_legacy.php\' from the Cacti directory, then reload this page.', THOLD_UPGRADE_BASELINE, 'thold');
}

/**
* thold_upgrade_blocked_by_legacy - logs and reports an install that predates the upgrade baseline,
* so callers can stop before registering hooks, changing the schema or pruning files.
Expand Down
8 changes: 8 additions & 0 deletions locales/po/cacti.pot
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,14 @@ msgstr ""
msgid "> %d %% of Devices in a Site"
msgstr ""

#: includes/database.php
#, php-format
msgid ""
"This Thold install is older than %s and cannot be upgraded automatically. "
"Run 'php plugins/thold/cli/upgrade_legacy.php' from the Cacti directory, "
"then reload this page."
msgstr ""

#: includes/functions.php
msgid "< 1 Minute"
msgstr ""
Expand Down
20 changes: 17 additions & 3 deletions poller_thold.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,27 @@ function perform_thold_processes() {
$empty_value = read_config_option('thold_empty_if_speed_default', true);
}

include_once($config['base_path'] . '/plugins/thold/includes/database.php');

// An install older than the upgrade baseline still has its old schema. Checking thresholds
// against it fails part way through, and Cacti then disables the plugin.
$installed = db_fetch_cell('SELECT version
FROM plugin_config
WHERE directory="thold"');

if (thold_upgrade_blocked_by_legacy($installed)) {
print 'ERROR: Thold ' . $installed . ' must be upgraded with cli/upgrade_legacy.php before thresholds can be checked.' . PHP_EOL;

return;
}

// Force upgrade the database if there is a problem
if (!db_column_exists('thold_data', 'name_cache')) {
thold_debug('Upgrading Thold database now.');

include_once($config['base_path'] . '/plugins/thold/includes/database.php');

thold_upgrade_database(true);
if (thold_upgrade_database(true) === false) {
return;
}
} else {
thold_debug('Not upgrading Thold database.');
}
Expand Down
5 changes: 5 additions & 0 deletions setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,11 @@ function plugin_thold_upgrade() {
include_once(__DIR__ . '/includes/database.php');

if (thold_upgrade_blocked_by_legacy($old)) {
// The log entry alone leaves an administrator with broken pages and no reason
if (!empty($config['is_web'])) {
raise_message('thold_legacy_upgrade', thold_upgrade_legacy_message(), MESSAGE_LEVEL_ERROR);
}

return true;
}

Expand Down
19 changes: 19 additions & 0 deletions tests/Integration/PluginLifecycleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,22 @@
$GLOBALS['config']['base_path'] = $restoreBase;
}
});

it('tells a web user why an install older than the upgrade baseline was left alone', function (bool $isWeb, int $notices) {
CactiStubs::willReturn('get_current_page', 'thold.php');
CactiStubs::willReturnFor('db_fetch_cell', 'plugin_config', '1.2.4');

$restoreWeb = $GLOBALS['config']['is_web'] ?? null;
$GLOBALS['config']['is_web'] = $isWeb;

try {
expect(plugin_thold_upgrade())->toBeTrue();
expect(CactiStubs::callsTo('raise_message'))->toHaveCount($notices);
expect(CactiStubs::callsTo('api_plugin_register_hook'))->toBeEmpty();
} finally {
$GLOBALS['config']['is_web'] = $restoreWeb;
}
})->with([
'web page' => [true, 1],
'poller' => [false, 0],
]);
38 changes: 38 additions & 0 deletions tests/Unit/UpgradeBaselineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,42 @@ public function testLegacyCliCarriesTheRetiredMigrations(): void {
$this->assertStringNotContainsString("cacti_version_compare(\$oldv, '1.0', '<')", $source);
$this->assertStringNotContainsString("cacti_version_compare(\$oldv, '0.", $source);
}

/**
* @return void
*/
public function testTheWebNoticeNamesTheLegacyCliAndTheBaseline(): void {
$message = thold_upgrade_legacy_message();

$this->assertStringContainsString('cli/upgrade_legacy.php', $message);
$this->assertStringContainsString('older than 1.5', $message);
}

/**
* A refused upgrade returns false. Every caller has to stop there instead of carrying on
* against the old schema or reporting success (#908).
*
* @return void
*/
public function testCallersStopWhenTheUpgradeIsRefused(): void {
$root = dirname(__DIR__, 2);

foreach (['cli/upgrade.php', 'poller_thold.php', 'thold.php'] as $file) {
$source = (string) file_get_contents($root . '/' . $file);

$this->assertSame(
substr_count($source, 'thold_upgrade_database(true)'),
substr_count($source, 'thold_upgrade_database(true) === false'),
"$file calls thold_upgrade_database() without checking for a refusal"
);
}

$poller = (string) file_get_contents($root . '/poller_thold.php');

// The poller must leave before it reads thresholds from a schema older than the baseline
$this->assertLessThan(
strpos($poller, "db_column_exists('thold_data', 'name_cache')"),
strpos($poller, 'thold_upgrade_blocked_by_legacy($installed)')
);
}
}
9 changes: 8 additions & 1 deletion thold.php
Original file line number Diff line number Diff line change
Expand Up @@ -1259,7 +1259,14 @@ function thold_edit() {
if (!isset($thold_data['local_graph_id'])) {
include_once($config['base_path'] . '/plugins/thold/includes/database.php');

thold_upgrade_database(true);
if (thold_upgrade_database(true) === false) {
// The page header has been sent, so say why here instead of redirecting
html_start_box('', '100%', false, '3', 'center', '');
print "<tr class='tableRow'><td class='textError'>" . html_escape(thold_upgrade_legacy_message()) . '</td></tr>';
html_end_box();

return;
}
}
} elseif (isset_request_var('local_data_id') &&
isset_request_var('local_graph_id') &&
Expand Down