mirror of
https://github.com/Cacti/cacti.git
synced 2026-10-29 08:06:58 +09:00
* fix(boost): harden data handoffs and cache writes Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * Define $updates_ok when there is nothing to flush PHPStan level 6 also flags the poller ownership check, which is the POLLER_ID constant case the file already annotates above. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * Use a line comment to match the fixer Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * Record the boost process columns in the audit schema Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * test: resolve includes through Cacti path constants Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * Fix Boost handoff review concerns Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * test: seed settings in Boost database fixture Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * test: refresh SQL interpolation baseline Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * docs: record Boost handoff hardening Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * fix: stop boost discarding a cycle and wedging a shard Three defects in the handoff hardening. A rejected ownership handoff returned false, which the poller reads as boost having consumed the data, so it skipped the RRD write. The poller_output rows were already deleted by then, so a stale poller_item copy on a remote poller silently lost the whole cycle. Return true instead and let the poller write directly, matching the database failure path. Any non-OK RRDtool response retained the shard, and the retry then replayed rows already written, which RRDtool rejects as an illegal update time. That failed again on every pass, so the archive tables accumulated and boost never drained. Treat an at-or-behind timestamp as applied, since the sample is in the RRD; genuine errors still retain the shard. The two new poller_output_boost_processes columns passed 'null' where db_install_update_table reads 'NULL', so NOT NULL was never emitted and upgraded installs diverged from cacti.sql. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * fix: spell the NULL column key so upgrades emit NOT NULL db_add_column() reads $column['NULL'] and PHP array keys are case sensitive, so the thirteen definitions in the 1.3.0 upgrade that passed a lowercase 'null' key had the flag discarded and created nullable columns. Fresh installs built from cacti.sql got NOT NULL, upgraded installs did not, and Cacti's own schema audit reports the difference. The same file already spells the key in upper case 144 times, and the wrapper's own docblock example uses the upper case form. Guard the class with a test over every upgrade file rather than only the one that was wrong. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * test: assert the NULL key plainly toContain() takes needles rather than a failure message. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * fix(boost): close remaining handoff review gaps Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * fix(boost): guard nullable graph output Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * fix(boost): keep in-flight completion rows through the 1.3.0 upgrade The truncate only existed so the run_child UNIQUE key could build over rows that predate run_id; deleting just those rows leaves a running boost its completion records. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * refactor(boost): drop the poller ownership gate Its boost_poller_on_demand() call sits behind the boost_redirect early return that poller.php forces on for every multi-collector install, and its recovery call turns a device deletion on main into a recovery that never brings the collector back online. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * fix(boost): bound atomic cache write allocations Guard upgrade source reads so failures report clearly, write graph cache data in fixed-size chunks, and exercise a multi-chunk payload. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * fix(boost): support Windows cache replacement Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * test(boost): expand handoff failure coverage Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * test(boost): support ANSI_QUOTES upgrade checks Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * fix(boost): stop staging after chunk failure Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * fix(recovery): use numeric poller status Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * fix(sql): support ANSI_QUOTES in recovery paths Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * fix(boost): preserve cache on replacement failure Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * fix(boost): retain readable cache permissions Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * fix(boost): repair completion schema at runtime Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * fix(recovery): bound timestamp batch fetches Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * fix(boost): satisfy runtime schema analysis Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * fix(recovery): restore status after transfer failure Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * test(boost): rely on unit bootstrap helpers Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * fix(boost): refresh generated cache secret Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * fix(boost): normalize cache path separators Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * test(boost): fail clearly on unreadable fixtures * fix(boost): name ignored duplicate sample keys The staging warning only said rows were dropped, so a retry could not be told from a colliding writer. Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> * Lack of pre-commit hook execution allowed lint failure --------- Signed-off-by: Thomas Vincent <thomasvincent@gmail.com> Co-authored-by: TheWitness <thewitness@cacti.net>
153 lines
7.4 KiB
PHP
153 lines
7.4 KiB
PHP
<?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 |
|
|
+-------------------------------------------------------------------------+
|
|
| Cacti: The Complete RRDTool-based Graphing Solution |
|
|
+-------------------------------------------------------------------------+
|
|
*/
|
|
|
|
if (!function_exists('boostSource')) {
|
|
function boostSource(string $path): string {
|
|
$source = file_get_contents(CACTI_PATH_BASE . '/' . $path);
|
|
|
|
if ($source === false) {
|
|
throw new RuntimeException("Unable to read Boost source file: $path");
|
|
}
|
|
|
|
return $source;
|
|
}
|
|
}
|
|
|
|
test('Boost cache paths normalize either trailing separator style', function () {
|
|
$boost = boostSource('lib/boost.php');
|
|
|
|
expect($boost)->toContain("rtrim(\$cache_directory, '/\\\\')");
|
|
});
|
|
|
|
test('Boost hand-off helper tolerates repeated test-file loading', function () {
|
|
$source = file_get_contents(__FILE__);
|
|
|
|
expect($source)->not->toBeFalse();
|
|
$source = (string) $source;
|
|
|
|
$declaration = strpos($source, 'function ' . 'boostSource');
|
|
|
|
expect($declaration)->not->toBeFalse();
|
|
expect(substr($source, max(0, $declaration - 80), 80))->toContain("if (!function_exists('boostSource')) {");
|
|
});
|
|
|
|
test('Boost hand-off batches expose database acknowledgement', function () {
|
|
$boost = boostSource('lib/boost.php');
|
|
|
|
expect($boost)->toContain('function boost_flush_output_batch(array $value_tuples, mixed $conn = false) : bool');
|
|
expect($boost)->toContain('$acknowledged = db_execute($sql_prefix . $out_buffer, true, $conn) !== false;');
|
|
expect($boost)->toContain('$return_value = !boost_flush_output_batch($value_tuples, $conn);');
|
|
});
|
|
|
|
test('Recovery deletes only the exact rows acknowledged by the main collector', function () {
|
|
$recovery = boostSource('poller_recovery.php');
|
|
|
|
expect($recovery)->toContain('function recovery_delete_acknowledged_rows(array $rows, mixed $conn) : bool');
|
|
expect($recovery)->toContain('function recovery_transfer_rows(array $rows, mixed $remote_conn, mixed $local_conn) : int|false');
|
|
expect($recovery)->toContain('(local_data_id = ? AND rrd_name = ? AND time = ?)');
|
|
expect($recovery)->toContain('if (!boost_flush_output_batch($sql_array, $remote_conn))');
|
|
expect($recovery)->toContain('array_slice($rows, $offset, $transfer_chunk_size)');
|
|
expect($recovery)->toContain('recovery_transfer_rows($chunk, $remote_db_cnn_id, $local_db_cnn_id)');
|
|
expect($recovery)->toContain("ORDER BY time ASC, local_data_id ASC\n\t\t\t\tLIMIT %d', (int) \$record_limit)");
|
|
expect($recovery)->not->toContain('DELETE FROM poller_output_boost WHERE time <=');
|
|
});
|
|
|
|
test('Recovery status SQL remains valid when ANSI_QUOTES is enabled', function () {
|
|
$recovery = boostSource('poller_recovery.php');
|
|
|
|
expect($recovery)->toContain('SET status=2')
|
|
->and($recovery)->toContain('SET status = 5')
|
|
->and($recovery)->toContain("VALUES (\\'recovery_pid\\', ?)")
|
|
->and($recovery)->not->toContain("if (!\$transfer_failed) {\n\t\t// let the console know you are in online mode")
|
|
->and($recovery)->not->toMatch('/(?:status\s*=|VALUES\s*\()\s*"/');
|
|
});
|
|
|
|
test('Scheduled Boost retains shard ownership after an RRD update failure', function () {
|
|
$poller = boostSource('poller_boost.php');
|
|
|
|
expect($poller)->toContain('function boost_process_output(int $local_data_id, array $outarray, string $rrd_path, array $rrd_tmplp, mixed $rrdtool_pipe) : bool');
|
|
expect($poller)->toContain('if ($updates_ok && $results !== false)');
|
|
expect($poller)->toContain('return $updates_ok && $results !== false ? cacti_sizeof($results) : -1;');
|
|
expect($poller)->toContain('if ($pass_rows < 0)');
|
|
expect($poller)->toContain('if ($total_rows == 0) {');
|
|
expect($poller)->toContain('return 0;');
|
|
expect($poller)->not->toContain('elseif ($rrd_updates == -1)');
|
|
expect(boostSource('lib/boost.php'))->toContain("return is_string(\$result) && \$result !== '' ? \$result : 'ERROR: RRDtool did not acknowledge the update';");
|
|
});
|
|
|
|
test('Boost completion-table upgrades recover when the legacy table is missing', function () {
|
|
$upgrade = boostSource('install/upgrades/1_3_0.php');
|
|
|
|
expect($upgrade)->toContain("if (!db_table_exists('poller_output_boost_processes'))")
|
|
->and($upgrade)->toContain('CREATE TABLE `poller_output_boost_processes`')
|
|
->and($upgrade)->toContain('UNIQUE KEY `run_child` (`run_id`, `child_id`)');
|
|
});
|
|
|
|
test('Boost child completion and archive deletion are scoped to a parent run', function () {
|
|
$poller = boostSource('poller_boost.php');
|
|
$schema = boostSource('cacti.sql');
|
|
|
|
expect($poller)->toContain('bin2hex(random_bytes(16));');
|
|
expect($poller)->toContain("'--run-id=' . \$run_id");
|
|
expect($poller)->toContain('function boost_completed_children(string $run_id) : int');
|
|
expect($poller)->toContain('function boost_failed_children(string $run_id) : int');
|
|
expect($poller)->toContain('if ($rrd_updates > 0 && $failed_children === 0)');
|
|
expect($schema)->toContain('UNIQUE KEY `run_child` (`run_id`,`child_id`)');
|
|
expect($schema)->toContain('`child_id` int(10) unsigned NOT NULL default 0');
|
|
});
|
|
|
|
test('direct Boost redirection does not stage the same rows twice', function () {
|
|
$boost = boostSource('lib/boost.php');
|
|
|
|
expect($boost)->toContain("if (read_config_option('boost_redirect') == 'on')")
|
|
->and($boost)->toContain('cmd.php already staged these rows');
|
|
});
|
|
|
|
test('Archive discovery validates every dynamic table identifier', function () {
|
|
$boost = boostSource('lib/boost.php');
|
|
|
|
expect($boost)->toContain("preg_match('/^poller_output_boost_arch_\\d+$/D', \$table)");
|
|
expect($boost)->toContain("array_filter(\$tableNames, 'boost_is_valid_archive_table')");
|
|
});
|
|
|
|
test('Remote tuples are quoted by their destination connection', function () {
|
|
$cmd = boostSource('cmd.php');
|
|
$recovery = boostSource('poller_recovery.php');
|
|
|
|
expect($cmd)->toContain("db_qstr(\$item['rrd_name'], \$poller_db_cnn_id)");
|
|
expect($cmd)->toContain('db_qstr($output, $poller_db_cnn_id)');
|
|
expect($cmd)->toContain("db_qstr('U', \$poller_db_cnn_id)");
|
|
expect($cmd)->toContain("IFNULL(s.disabled, \\'\\') != \\'on\\'");
|
|
expect($recovery)->toContain("db_qstr(\$row['rrd_name'], \$remote_conn)");
|
|
});
|
|
|
|
test('Graph cache names are opaque and writes are atomically published', function () {
|
|
$boost = boostSource('lib/boost.php');
|
|
|
|
expect($boost)->toContain("hash_hmac('sha256', \$cache_key, \$secret) . '.png'");
|
|
expect($boost)->toContain("tempnam(dirname(\$cache_file), '.boost-')");
|
|
expect($boost)->toContain("PHP_OS_FAMILY === 'Windows'");
|
|
expect($boost)->toContain('boost_replace_cache_file_on_windows($temp_file, $cache_file)');
|
|
expect($boost)->toContain('if (is_string($output) && strlen($output) > 10)');
|
|
expect($boost)->toContain('if (!$flushed || !chmod($temp_file, 0644))');
|
|
expect($boost)->toContain("read_config_option('boost_png_cache_secret', true)");
|
|
expect($boost)->not->toContain("get_selected_theme() . '_lgi_'");
|
|
});
|
|
|
|
test('Boost restores both the caller error handler and reporting level', function () {
|
|
$boost = boostSource('lib/boost.php');
|
|
|
|
expect(substr_count($boost, '$previous_error_reporting = error_reporting();'))->toBeGreaterThanOrEqual(4);
|
|
expect(substr_count($boost, 'error_reporting($previous_error_reporting);'))->toBeGreaterThanOrEqual(8);
|
|
});
|