From f701a5ad04f4aaa94c2167b3ab667c8ad8c06c35 Mon Sep 17 00:00:00 2001 From: TheWitness Date: Fri, 20 Nov 2026 08:14:18 -0500 Subject: [PATCH 01/12] syslog: fix device purge crash and harden partition rebuilds Purge Syslog Devices called syslog_db_execute() from the utilities hook without ever connecting, so includes/database.php was never loaded and the plugin was disabled (#472). Connect first, report a failed connection, qualify the Syslog database, and use NOT EXISTS so a NULL host_id cannot silently prevent purging. Address the #471 review of cli/upgrade_partitioning.php: - Refuse to start while Syslog processing is enabled, and hold the syslog/master process slot for the duration. - Copy stored and removed messages without seq. The replacement's AUTO_INCREMENT is set above the original high-water mark before the first row, so sequence values are never reused and copied rows cannot collide with live inserts. - Verify each 10,000-row batch by count and MD5 checksum against its source range, then checkpoint it in syslog_rebuild_progress. A retry deletes unverified rows above the checkpoint and resumes. - Widen syslog_incoming in place; its seq is an archival identity. - Drop legacy DATETIME conversion: refuse such history before any change unless --discard-data is used, and document manual conversion and rollback. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- CHANGELOG.md | 2 + README.md | 70 ++- cli/upgrade_partitioning.php | 467 ++++++++++++------ includes/utilities.php | 44 +- locales/po/cacti.pot | 4 + tests/Security/CsrfPurgeTest.php | 49 +- tests/Unit/PartitionRebuildTest.php | 89 +++- .../partition_rebuild_database_test.php | 132 +++-- 8 files changed, 624 insertions(+), 233 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fa1d1229..35c4d750 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ --- develop --- +* bug: Purge Syslog Devices (Utilities) crashed with "Call to undefined function syslog_db_execute()" because the hook never connected to the Syslog database; it now connects first, reports connection failures, qualifies the Syslog database, and uses NOT EXISTS so a NULL host_id no longer prevents purging (#472) +* bug: The partition rebuild CLI now refuses to start while Syslog processing is enabled and holds the master process slot; copies stored and removed messages without seq into a replacement whose AUTO_INCREMENT starts above the original high-water mark; verifies each 10,000-row batch by checksum and checkpoints it for resumable retries; widens the incoming queue in place to keep its sequence identities; and refuses legacy DATETIME history before changing anything * docs: Refresh README release/source compatibility, installation guidance and source-checked behavior; add published release notes and 5.0 upgrade guidance * bug: Keep log column resizing within the results area on both log tabs, preserve message ellipsis, and use an accessible Font Awesome export button * feature: Standardize both schema and partition CLI help with Cacti-style version banners and options, separate maintenance warnings, and replace the unreleased millisecond converter with --convert-ms diff --git a/README.md b/README.md index cd9ff206..e9da8bb9 100644 --- a/README.md +++ b/README.md @@ -166,12 +166,12 @@ php plugins/syslog/cli/upgrade_partitioning.php --convert-ms ``` The `--convert-ms` option is **MariaDB only**. It rebuilds the stored/removed -message tables and converts incoming messages to `TIMESTAMP(3)`, preserving -data in batches of at most **10,000 rows**. Already-converted incoming tables -are left alone. The replication outbox (`syslog_replication_output`) is widened -in place; undelivered rows are never discarded. Sequence high-water marks and -replication identities are preserved, including when sequence numbers repeat -at different timestamps. Existing second-resolution messages gain `.000`; +message tables to `TIMESTAMP(3)`, preserving data in verified batches of at +most **10,000 rows**. The incoming queue (`syslog_incoming`) and replication +outbox (`syslog_replication_output`) are widened in place with `ALTER TABLE`, +so their sequence numbers, which archival and replication use as identities, +are unchanged and undelivered rows are never discarded. Already-converted +tables are left alone. Existing second-resolution messages gain `.000`; conversion cannot recover fractional timestamps that were never recorded. The default is preservation. The explicit @@ -195,10 +195,34 @@ sequence-only primary key when necessary, and creates UTC daily It does not offer weekly or monthly partitioning, since Syslog's maintenance expects daily partitions. -Legacy `DATETIME` columns are converted to `TIMESTAMP` using the connection's -original session time zone; verify that this matches the zone used to write -those historical wall-clock values. Existing `TIMESTAMP` rows are copied in UTC -to preserve distinct events across daylight-saving clock changes. +Stored and removed messages are copied **without** their `seq` column, in their +original order, so each replacement numbers them from its own +`AUTO_INCREMENT`. That counter is set above the original high-water mark before +the first row is copied, so a sequence value is never reused; links that +captured an old `seq` find nothing rather than a different message. All other +columns, including `replication_source_event_id`, are copied unchanged. +Existing `TIMESTAMP` rows are copied in UTC, preserving distinct events across +daylight-saving clock changes. + +Legacy `DATETIME` `logtime` columns are **not supported**. A `DATETIME` holds +wall-clock time in an unknown zone; values in a daylight-saving gap do not +exist as `TIMESTAMP` and values in the repeated hour are ambiguous, so no +automatic conversion is safe. The utility refuses to preserve such tables +before changing anything. Either rebuild them with `--discard-data`, or, after +verifying the zone the values were written in, convert them yourself with +Syslog processing stopped. First confirm that every value round-trips in +that zone, then convert and repartition in one statement: + +```sql +SET SESSION time_zone = 'America/New_York'; -- the zone the values were written in +SELECT COUNT(*) FROM syslog + WHERE FROM_UNIXTIME(UNIX_TIMESTAMP(logtime)) <> logtime; -- must be 0 +ALTER TABLE syslog MODIFY COLUMN logtime timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' + PARTITION BY RANGE (UNIX_TIMESTAMP(logtime)) (/* daily partitions */ + PARTITION dMaxValue VALUES LESS THAN MAXVALUE); +``` + +Then run this utility to apply the configured layout. Every existing message is copied, including older-than-retention messages; those older rows fit in the earliest RANGE partition. Normal retention pruning @@ -217,9 +241,11 @@ rebuild utility for legacy layouts or engine changes, not routine daily rotation **Maintenance requirements for both commands:** -* Back up the database, stop Syslog processing, partition maintenance, - replication and **all external database writers**, and buffer new logs - upstream. The utility does not stop these services for you. Keep them stopped +* Back up the database and uncheck **Settings > Syslog > Syslog Enabled**. The + utility refuses to start while processing is enabled, and holds the Syslog + master process slot so no processing run can start during the rebuild. It + does **not** stop remote collectors, replication or external database + writers: stop them and buffer new logs upstream. Keep everything stopped until the rebuild or recovery finishes. Live log tables are temporarily absent or incomplete; avoid using log views during the rebuild. DDL waits at most 30 seconds for metadata locks before reporting failure; let active queries @@ -233,13 +259,21 @@ rebuild utility for legacy layouts or engine changes, not routine daily rotation **several hours**. `--confirm` skips the prompt but not these warnings. * DDL is **not transactional**. Each original is renamed to `_rebuild`; a replacement is created and populated using bounded primary-key ranges. - Originals are dropped automatically only after row counts and column values - match and sequence high-water marks have been restored. + After each batch, the row count and an MD5-based checksum of every copied + column are compared with the source range. Only a matching batch is + recorded in `syslog_rebuild_progress`. Originals are dropped automatically + only after every batch and the total row counts match. * A failure leaves originals in the live or `_rebuild` tables. Keep writers stopped, fix the reported error, then retry with `--preserve-data` and the - same `--convert-ms` option, if used. Existing copied rows are checked rather - than overwritten; mismatches stop recovery without dropping the original. - Do not change settings or manually delete `_rebuild` tables during recovery. + same `--convert-ms` option, if used. A retry deletes replacement rows copied + after the last recorded batch and resumes from there. Do not change settings + or manually delete `_rebuild` or `syslog_rebuild_progress` tables during + recovery. +* To abandon a rebuild instead, with writers still stopped: + `DROP TABLE syslog; RENAME TABLE syslog_rebuild TO syslog;` (likewise for + `syslog_removed`), then `DROP TABLE syslog_rebuild_progress;`. A + `syslog_incoming_rebuild` table left by an earlier development build is + restored the same way. ### Delivering sub-second timestamps from your log forwarders diff --git a/cli/upgrade_partitioning.php b/cli/upgrade_partitioning.php index 2a513ae4..f52658a6 100644 --- a/cli/upgrade_partitioning.php +++ b/cli/upgrade_partitioning.php @@ -92,7 +92,7 @@ * DDL is not transactional; failed copies retain originals for a safe retry. */ function syslog_upart_run(bool $confirm, bool $convert_ms, bool $preserve_data = true): bool { - global $syslogdb_default; + global $config, $syslogdb_default; $mariadb = syslog_db_is_mariadb(); @@ -120,7 +120,35 @@ function syslog_upart_run(bool $confirm, bool $convert_ms, bool $preserve_data = return false; } - $engine = $engines[strtolower($engine)]; + // Writers would interleave with copied rows and break verification, so + // processing must be disabled and its master slot held for the duration. + if ((string) read_config_option('syslog_enabled', true) !== '') { + fwrite(STDERR, 'ERROR: Syslog processing is enabled. Uncheck Settings > Syslog > Syslog Enabled, stop remote collectors and any other Syslog database writers, then retry. No changes were made.' . PHP_EOL); + + return false; + } + + $poller_id = (int) ($config['poller_id'] ?? 1); + + if (!register_process_start('syslog', 'master', $poller_id, 7 * 86400)) { + fwrite(STDERR, 'ERROR: A Syslog processing run is still active; wait for it to finish, then retry. No changes were made.' . PHP_EOL); + + return false; + } + + try { + return syslog_upart_run_locked($confirm, $convert_ms, $preserve_data, $engines[strtolower($engine)], (int) $days, $ahead); + } finally { + unregister_process('syslog', 'master', $poller_id); + } +} + +/** + * Rebuild while holding the Syslog master process slot. + */ +function syslog_upart_run_locked(bool $confirm, bool $convert_ms, bool $preserve_data, string $engine, int $days, int $ahead): bool { + global $syslogdb_default; + $lock = hash('sha256', $syslogdb_default . '.syslog_upgrade_partitioning'); if ((int) syslog_db_fetch_cell_prepared('SELECT GET_LOCK(?, 0)', [$lock]) !== 1) { @@ -130,32 +158,73 @@ function syslog_upart_run(bool $confirm, bool $convert_ms, bool $preserve_data = } try { + if (syslog_db_table_exists('syslog_incoming_rebuild', false)) { + fwrite(STDERR, 'ERROR: syslog_incoming_rebuild was left by an earlier development build. Restore it as described in the README, then retry. No changes were made.' . PHP_EOL); + + return false; + } + $tables = []; - foreach (['syslog', 'syslog_removed', 'syslog_incoming'] as $table) { + foreach (['syslog', 'syslog_removed'] as $table) { $backup_exists = syslog_db_table_exists($table . '_rebuild', false); - if ($backup_exists && (!$preserve_data || ($table === 'syslog_incoming' && !$convert_ms))) { + if ($backup_exists && !$preserve_data) { fwrite(STDERR, "ERROR: {$table}_rebuild exists from an unfinished rebuild. Retry with --preserve-data and the original --convert-ms option, if used. No data was discarded." . PHP_EOL); return false; } - if ($backup_exists || (syslog_db_table_exists($table, false) - && ($table !== 'syslog_incoming' || ($convert_ms && syslog_detect_logtime_precision($table) !== 'ms')))) { + if ($backup_exists || syslog_db_table_exists($table, false)) { $tables[] = $table; } } - $outbox = $convert_ms && syslog_db_table_exists('syslog_replication_output', false) - && syslog_detect_logtime_precision('syslog_replication_output') !== 'ms'; + // The incoming queue and replication outbox keep their sequence identities + // (archival and delivery deduplicate on them), so they are widened in place. + $widen = []; - if (!$tables && !$outbox) { + foreach (['syslog_incoming', 'syslog_replication_output'] as $table) { + if ($convert_ms && syslog_db_table_exists($table, false) && syslog_detect_logtime_precision($table) !== 'ms') { + $widen[] = $table; + } + } + + if (!$tables && !$widen) { print 'No tables require rebuilding.' . PHP_EOL; return true; } + // Legacy DATETIME history is wall-clock time in an unknown zone; it is + // not converted. Only an explicit discard may replace such tables. + $checked = $widen; + + foreach ($tables as $table) { + if ($preserve_data) { + $checked[] = $table; + $checked[] = $table . '_rebuild'; + } + } + + $legacy = syslog_db_fetch_assoc_prepared('SELECT TABLE_NAME FROM information_schema.COLUMNS + WHERE TABLE_SCHEMA = ? AND COLUMN_NAME = \'logtime\' AND DATA_TYPE = \'datetime\' + AND TABLE_NAME IN (' . implode(', ', array_fill(0, count($checked), '?')) . ')', + array_merge([$syslogdb_default], $checked)); + + if (!is_array($legacy)) { + fwrite(STDERR, 'ERROR: Cannot read the logtime column types; no tables were changed.' . PHP_EOL); + + return false; + } + + if ($legacy) { + fwrite(STDERR, 'ERROR: Legacy DATETIME logtime columns are not supported: ' . implode(', ', array_column($legacy, 'TABLE_NAME')) + . '. Rebuild stored and removed history with --discard-data, or convert it manually as described in the README. No changes were made.' . PHP_EOL); + + return false; + } + $bytes = syslog_db_fetch_cell_prepared('SELECT SUM(DATA_LENGTH + INDEX_LENGTH) FROM information_schema.TABLES WHERE TABLE_SCHEMA = ?', [$syslogdb_default]); @@ -173,15 +242,16 @@ function syslog_upart_run(bool $confirm, bool $convert_ms, bool $preserve_data = print 'Depending on the size of the Syslog tables and storage speed, this can' . PHP_EOL; print 'take SEVERAL HOURS. Database metadata is an estimate, not a disk-space check.' . PHP_EOL; print "Rebuilding daily partitions: engine=$engine, retention=$days days, ahead=$ahead days." . PHP_EOL; - print 'Tables: ' . implode(', ', $tables) . ($outbox ? '; replication outbox widened in place' : '') . PHP_EOL; + print 'Tables: ' . implode(', ', $tables) . ($widen ? '; widened in place: ' . implode(', ', $widen) : '') . PHP_EOL; print $preserve_data - ? 'Messages are preserved in 10,000-row copies; originals are dropped only after verification.' . PHP_EOL + ? 'Messages are preserved in verified 10,000-row copies and receive new sequence numbers; originals are dropped only after verification.' . PHP_EOL : 'DESTRUCTIVE: stored messages in the listed rebuild tables will be DELETED.' . PHP_EOL; print $convert_ms ? 'Converting the complete message pipeline to TIMESTAMP(3).' . PHP_EOL : 'Existing timestamp precision is preserved.' . PHP_EOL; - print 'Take a database backup. STOP all processing, partition maintenance,' . PHP_EOL; - print 'replication and external database writers; buffer new logs upstream.' . PHP_EOL; - print 'This utility does NOT pause them. Keep them stopped until completion or recovery.' . PHP_EOL; + print 'Take a database backup. Syslog processing is disabled and its process slot' . PHP_EOL; + print 'is held, but this utility does NOT pause remote collectors, replication or' . PHP_EOL; + print 'external database writers; stop them and buffer new logs upstream.' . PHP_EOL; + print 'Keep them stopped until completion or recovery.' . PHP_EOL; print 'Live log tables are temporarily absent or incomplete during rebuilding.' . PHP_EOL; print '========================================================================' . PHP_EOL; @@ -206,10 +276,7 @@ function syslog_upart_run(bool $confirm, bool $convert_ms, bool $preserve_data = } $lock_wait = (int) $lock_wait; - $copy_mode = implode(',', array_unique(array_merge( - array_diff(explode(',', $mode), ['NO_ZERO_DATE', 'NO_ZERO_IN_DATE', '']), - ['NO_AUTO_VALUE_ON_ZERO'] - ))); + $copy_mode = implode(',', array_diff(explode(',', $mode), ['NO_ZERO_DATE', 'NO_ZERO_IN_DATE', ''])); try { if (!syslog_db_execute("SET SESSION time_zone = '+00:00'") @@ -221,17 +288,16 @@ function syslog_upart_run(bool $confirm, bool $convert_ms, bool $preserve_data = } foreach ($tables as $table) { - if (!syslog_upart_rebuild($table, $convert_ms, $preserve_data, $engine, (int) $days, $ahead, $zone)) { + if (!syslog_upart_rebuild($table, $convert_ms, $preserve_data, $engine, $days, $ahead)) { return false; } } - if ($outbox) { - $column = syslog_logtime_sql_type('ms'); + $column = syslog_logtime_sql_type('ms'); - if (!syslog_db_execute_prepared('SET SESSION time_zone = ?', [$zone]) - || !syslog_db_execute("ALTER TABLE `$syslogdb_default`.`syslog_replication_output` MODIFY COLUMN logtime $column")) { - fwrite(STDERR, 'ERROR: Cannot widen the replication outbox; the precision setting was not changed.' . PHP_EOL); + foreach ($widen as $table) { + if (!syslog_db_execute("ALTER TABLE `$syslogdb_default`.`$table` MODIFY COLUMN logtime $column")) { + fwrite(STDERR, "ERROR: Cannot widen $table; the precision setting was not changed." . PHP_EOL); return false; } @@ -259,7 +325,7 @@ function syslog_upart_run(bool $confirm, bool $convert_ms, bool $preserve_data = } cacti_log('SYSLOG: Completed cli/upgrade_partitioning.php' . ($convert_ms ? ' with --convert-ms' : ''), false, 'SYSLOG'); - print 'Rebuild complete.' . ($convert_ms ? ' Syslog now stores millisecond timestamps; configure forwarders accordingly.' : '') . PHP_EOL; + print 'Rebuild complete. Re-enable Syslog processing.' . ($convert_ms ? ' Syslog now stores millisecond timestamps; configure forwarders accordingly.' : '') . PHP_EOL; return true; } finally { @@ -269,17 +335,23 @@ function syslog_upart_run(bool $confirm, bool $convert_ms, bool $preserve_data = /** * Rebuild one message table without dropping its original until data agrees. - * A retained original is authoritative on retry; existing copied rows are - * checked, not overwritten, so a failed copy never discards either version. + * + * Rows are copied without seq, in original order, so the replacement numbers + * them from its own AUTO_INCREMENT, which starts above the original high-water + * mark: a sequence value is never reused. Each 10,000-row batch is checksummed + * against its source range before a checkpoint records it. A retry discards + * replacement rows above the last checkpoint and resumes after it. */ -function syslog_upart_rebuild(string $table, bool $convert_ms, bool $preserve_data, string $engine, int $days, int $ahead, ?string $local_zone = null): bool { +function syslog_upart_rebuild(string $table, bool $convert_ms, bool $preserve_data, string $engine, int $days, int $ahead): bool { global $syslogdb_default; $backup = $table . '_rebuild'; - $source = syslog_db_table_exists($backup, false) ? $backup : $table; + $resume = syslog_db_table_exists($backup, false); + $source = $resume ? $backup : $table; $original = "`$syslogdb_default`.`$source`"; $target = "`$syslogdb_default`.`$table`"; $saved = "`$syslogdb_default`.`$backup`"; + $progress = "`$syslogdb_default`.`syslog_rebuild_progress`"; $fail = static function (string $reason) use ($table, $backup): bool { fwrite(STDERR, "ERROR: $table: $reason. Originals are retained in $table or $backup; keep writers stopped and retry with --preserve-data and the same --convert-ms option, if used. The precision setting was not changed." . PHP_EOL); @@ -293,8 +365,6 @@ function syslog_upart_rebuild(string $table, bool $convert_ms, bool $preserve_da $keys = syslog_db_fetch_assoc_prepared("SELECT COLUMN_NAME FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? AND INDEX_NAME = 'PRIMARY' ORDER BY SEQ_IN_INDEX", [$syslogdb_default, $source]); - $next = syslog_db_fetch_cell_prepared('SELECT AUTO_INCREMENT - FROM information_schema.TABLES WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?', [$syslogdb_default, $source]); $triggers = syslog_db_fetch_cell_prepared('SELECT COUNT(*) FROM information_schema.TRIGGERS WHERE TRIGGER_SCHEMA = ? AND EVENT_OBJECT_TABLE IN (?, ?)', [$syslogdb_default, $table, $backup]); $foreign_keys = syslog_db_fetch_cell_prepared('SELECT COUNT(*) FROM information_schema.KEY_COLUMN_USAGE @@ -304,7 +374,7 @@ function syslog_upart_rebuild(string $table, bool $convert_ms, bool $preserve_da [$syslogdb_default, $table, $backup, $syslogdb_default, $table, $backup]); if (!is_array($columns) || !$columns || !is_array($keys) || !in_array(array_column($keys, 'COLUMN_NAME'), [['seq'], ['seq', 'logtime']], true) - || !ctype_digit((string) $next) || !is_numeric($triggers) || !is_numeric($foreign_keys)) { + || !is_numeric($triggers) || !is_numeric($foreign_keys)) { return $fail('Cannot read the table metadata or the primary key is unsupported'); } @@ -313,155 +383,184 @@ function syslog_upart_rebuild(string $table, bool $convert_ms, bool $preserve_da } $logtime = array_values(array_filter($columns, static fn($column) => $column['COLUMN_NAME'] === 'logtime')); + $type = $logtime ? $logtime[0]['DATA_TYPE'] : ''; - if (!$logtime || !in_array($logtime[0]['DATA_TYPE'], ['timestamp', 'datetime'], true)) { - return $fail('Unsupported original logtime type'); + if ($type === 'datetime' && $preserve_data) { + return $fail('Legacy DATETIME logtime is not supported; rebuild with --discard-data or convert it manually'); } - // Legacy DATETIME holds wall-clock time; TIMESTAMP must use UTC to avoid - // duplicate cursor values during a daylight-saving fall-back hour. - $zone = $logtime[0]['DATA_TYPE'] === 'timestamp' ? '+00:00' - : ($local_zone ?? syslog_db_fetch_cell('SELECT @@SESSION.time_zone')); + if (!in_array($type, ['timestamp', 'datetime'], true)) { + return $fail('Unsupported original logtime type'); + } - if (!is_string($zone) || !syslog_db_execute_prepared('SET SESSION time_zone = ?', [$zone])) { + // TIMESTAMP is stored in UTC; a UTC session copies it without conversion. + if (!syslog_db_execute("SET SESSION time_zone = '+00:00'")) { return $fail('Cannot configure the timestamp copy time zone'); } $precision = $convert_ms ? 'ms' : syslog_detect_logtime_precision($source); - $partitioned = $table !== 'syslog_incoming'; - $partition_sql = null; if (!in_array($precision, ['sec', 'ms'], true)) { return $fail('Cannot determine the original timestamp precision'); } - if ($partitioned) { - $unique_keys = syslog_db_fetch_assoc_prepared("SELECT INDEX_NAME, - GROUP_CONCAT(COLUMN_NAME ORDER BY SEQ_IN_INDEX) AS columns_list - FROM information_schema.STATISTICS - WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? AND NON_UNIQUE = 0 GROUP BY INDEX_NAME", - [$syslogdb_default, $source]); - $range = syslog_db_fetch_row("SELECT UNIX_TIMESTAMP(MIN(logtime)) AS first_time, - UNIX_TIMESTAMP(MAX(logtime)) AS last_time FROM $original WHERE logtime > '1970-01-01 00:00:00'"); + $unique_keys = syslog_db_fetch_assoc_prepared("SELECT INDEX_NAME, + GROUP_CONCAT(COLUMN_NAME ORDER BY SEQ_IN_INDEX) AS columns_list + FROM information_schema.STATISTICS + WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? AND NON_UNIQUE = 0 GROUP BY INDEX_NAME", + [$syslogdb_default, $source]); + $range = syslog_db_fetch_row("SELECT UNIX_TIMESTAMP(MIN(logtime)) AS first_time, + UNIX_TIMESTAMP(MAX(logtime)) AS last_time FROM $original WHERE logtime > '1970-01-01 00:00:00'"); - if (!is_array($unique_keys) || !is_array($range) || !array_key_exists('first_time', $range) || !array_key_exists('last_time', $range)) { - return $fail('Cannot read the unique indexes or timestamp range'); - } + if (!is_array($unique_keys) || !is_array($range) || !array_key_exists('first_time', $range) || !array_key_exists('last_time', $range)) { + return $fail('Cannot read the unique indexes or timestamp range'); + } - foreach ($unique_keys as $key) { - if ($key['INDEX_NAME'] !== 'PRIMARY' && !in_array('logtime', explode(',', $key['columns_list']), true)) { - return $fail('A custom unique index excludes logtime; partitioning requires a manual migration'); - } + foreach ($unique_keys as $key) { + if ($key['INDEX_NAME'] !== 'PRIMARY' && !in_array('logtime', explode(',', $key['columns_list']), true)) { + return $fail('A custom unique index excludes logtime; partitioning requires a manual migration'); } + } - $partition_sql = syslog_upart_partition_sql($days, $ahead, - $range['first_time'] === null ? null : (int) $range['first_time'], - $range['last_time'] === null ? null : (int) $range['last_time']); + $partition_sql = syslog_upart_partition_sql($days, $ahead, + $range['first_time'] === null ? null : (int) $range['first_time'], + $range['last_time'] === null ? null : (int) $range['last_time']); - if ($partition_sql === null) { - return $fail('The requested history and future horizon exceed the 8192-partition limit; reduce retention or archive outlying timestamps first'); - } + if ($partition_sql === null) { + return $fail('The requested history and future horizon exceed the 8192-partition limit; reduce retention or archive outlying timestamps first'); + } + + // information_schema may cache AUTO_INCREMENT, so MAX(seq) is the floor. + $next = syslog_db_fetch_cell_prepared("SELECT GREATEST(CAST(COALESCE(MAX(seq), 0) AS DECIMAL(21,0)) + 1, + CAST(COALESCE((SELECT AUTO_INCREMENT FROM information_schema.TABLES + WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?), 1) AS DECIMAL(21,0))) FROM $original", [$syslogdb_default, $source]); + + if (!is_numeric($next) || !ctype_digit((string) $next)) { + return $fail('Cannot read the sequence high-water mark'); } - if ($source === $table) { + if (!$resume) { + $rows = $preserve_data ? syslog_db_fetch_cell("SELECT COUNT(*) FROM $original") : 0; + $room = is_numeric($rows) ? syslog_db_fetch_cell_prepared('SELECT CAST(? AS DECIMAL(21,0)) + CAST(? AS DECIMAL(21,0)) <= 18446744073709551615', + [$next, $rows]) : false; + + if ((string) $room !== '1') { + return $fail('Too few sequence values remain above the high-water mark to renumber every row'); + } + if (!syslog_db_execute("RENAME TABLE $target TO $saved")) { return $fail('Cannot rename the original table'); } } - if (!syslog_db_table_exists($table, false) - && !syslog_db_execute("CREATE TABLE $target LIKE $saved")) { - return $fail('Cannot create the replacement table'); + $state = $preserve_data && syslog_db_table_exists('syslog_rebuild_progress', false) + ? syslog_db_fetch_row_prepared("SELECT source_seq, source_logtime, target_seq, copied FROM $progress WHERE table_name = ?", [$table]) + : []; + + if (!is_array($state)) { + return $fail('Cannot read the copy checkpoint'); } - $count = syslog_db_fetch_cell("SELECT COUNT(*) FROM $target"); - $parts = syslog_db_fetch_assoc_prepared('SELECT PARTITION_NAME, PARTITION_DESCRIPTION, PARTITION_EXPRESSION - FROM information_schema.PARTITIONS WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? - ORDER BY PARTITION_ORDINAL_POSITION', [$syslogdb_default, $table]); + // A replacement without a checkpoint was interrupted during setup. + if (syslog_db_table_exists($table, false) && !$state) { + $count = syslog_db_fetch_cell("SELECT COUNT(*) FROM $target"); - if (!is_numeric($count) || !is_array($parts) || !$parts) { - return $fail('Cannot read the replacement row count or partition metadata'); + if (!is_numeric($count) || (int) $count !== 0) { + return $fail('The replacement has rows but no copy checkpoint; inspect it, drop it, and retry'); + } + + if (!syslog_db_execute("DROP TABLE $target")) { + return $fail('Cannot drop the incomplete replacement'); + } } - if ((int) $count === 0) { + if (!syslog_db_table_exists($table, false)) { + if (!syslog_db_execute("CREATE TABLE $target LIKE $saved")) { + return $fail('Cannot create the replacement table'); + } + + $parts = syslog_db_fetch_assoc_prepared('SELECT PARTITION_NAME FROM information_schema.PARTITIONS + WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?', [$syslogdb_default, $table]); + + if (!is_array($parts) || !$parts) { + return $fail('Cannot read the replacement partition metadata'); + } + $column = syslog_logtime_sql_type($precision); + $row_format = $engine === 'Aria' ? 'PAGE' : 'DYNAMIC'; $steps = []; - if ($partitioned && $parts[0]['PARTITION_NAME'] !== null) { + if ($parts[0]['PARTITION_NAME'] !== null) { $steps[] = "ALTER TABLE $target REMOVE PARTITIONING"; } $steps[] = "ALTER TABLE $target MODIFY COLUMN logtime $column"; + $steps[] = "ALTER TABLE $target ENGINE=$engine ROW_FORMAT=$row_format"; - if ($partitioned) { - $row_format = $engine === 'Aria' ? 'PAGE' : 'DYNAMIC'; - $steps[] = "ALTER TABLE $target ENGINE=$engine ROW_FORMAT=$row_format"; + if (array_column($keys, 'COLUMN_NAME') === ['seq']) { + $steps[] = "ALTER TABLE $target DROP PRIMARY KEY, ADD PRIMARY KEY (seq, logtime)"; + } - if (array_column($keys, 'COLUMN_NAME') === ['seq']) { - $steps[] = "ALTER TABLE $target DROP PRIMARY KEY, ADD PRIMARY KEY (seq, logtime)"; - } + $steps[] = "ALTER TABLE $target $partition_sql"; - $steps[] = "ALTER TABLE $target $partition_sql"; - } + // Before any row arrives, so copied rows never reuse an original seq. + $steps[] = "ALTER TABLE $target AUTO_INCREMENT = $next"; foreach ($steps as $sql) { if (!syslog_db_execute($sql)) { return $fail('Cannot configure the replacement schema (check the Cacti log)'); } } - } elseif (!$preserve_data || syslog_detect_logtime_precision($table) !== $precision) { - return $fail('The nonempty replacement does not match the requested conversion; retry with the original options'); - } elseif ($partitioned) { - $actual_engine = syslog_db_fetch_cell_prepared('SELECT ENGINE FROM information_schema.TABLES - WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?', [$syslogdb_default, $table]); - $previous = null; - - if ($actual_engine !== $engine || count($parts) < 2) { - return $fail('The partially copied replacement has a different engine or partition layout'); - } - - foreach ($parts as $index => $part) { - $expression = strtolower(str_replace(['`', ' '], '', (string) $part['PARTITION_EXPRESSION'])); - $boundary = (string) $part['PARTITION_DESCRIPTION']; - - if ($expression !== 'unix_timestamp(logtime)') { - return $fail('The partially copied replacement does not use UTC epoch partitioning'); - } - if ($index === count($parts) - 1) { - if ($part['PARTITION_NAME'] !== 'dMaxValue' || $boundary !== 'MAXVALUE') { - return $fail('The partially copied replacement has no final dMaxValue partition'); - } - } elseif (!ctype_digit($boundary) || (int) $boundary % 86400 !== 0 - || $part['PARTITION_NAME'] !== 'd' . gmdate('Ymd', (int) $boundary - 86400) - || ($previous !== null && (int) $boundary !== $previous + 86400)) { - return $fail('The partially copied replacement does not have contiguous UTC daily partitions'); + if ($preserve_data) { + $state = ['source_seq' => null, 'source_logtime' => null, 'target_seq' => '0', 'copied' => '0']; + + if (!syslog_db_execute("CREATE TABLE IF NOT EXISTS $progress ( + table_name varchar(64) NOT NULL PRIMARY KEY, + source_seq bigint unsigned NULL, + source_logtime varchar(32) NULL, + target_seq bigint unsigned NOT NULL, + copied bigint unsigned NOT NULL) ENGINE=InnoDB") + || !syslog_db_execute_prepared("REPLACE INTO $progress VALUES (?, NULL, NULL, 0, 0)", [$table])) { + return $fail('Cannot record the copy checkpoint'); } - - $previous = (int) $boundary; } + } elseif (syslog_detect_logtime_precision($table) !== $precision) { + return $fail('The partially copied replacement does not match the requested conversion; retry with the original options'); + } elseif (!syslog_upart_layout_ok($table, $engine)) { + return $fail('The partially copied replacement has a different engine or partition layout'); } $names = []; - $values = []; - $equal = []; + $hash = []; foreach ($columns as $column) { - $name = '`' . str_replace('`', '``', $column['COLUMN_NAME']) . '`'; - $binary = $column['COLUMN_NAME'] === 'logtime' ? '' : 'BINARY '; - $equal[] = "({$binary}copied.$name <=> {$binary}source.$name)"; - - if (preg_match('/(?:VIRTUAL|STORED|PERSISTENT) GENERATED/i', $column['EXTRA']) !== 1) { - $names[] = $name; - $values[] = "source.$name"; + if ($column['COLUMN_NAME'] === 'seq' || preg_match('/(?:VIRTUAL|STORED|PERSISTENT) GENERATED/i', $column['EXTRA']) === 1) { + continue; } + + $name = '`' . str_replace('`', '``', $column['COLUMN_NAME']) . '`'; + $value = $column['COLUMN_NAME'] === 'logtime' ? "DATE_FORMAT($name, '%Y-%m-%d %H:%i:%s.%f')" : "CAST($name AS BINARY)"; + $names[] = $name; + $hash[] = "IFNULL(CONCAT(OCTET_LENGTH($value), ':', $value), '-')"; } - $join = 'copied.seq = source.seq AND copied.logtime = source.logtime'; + $names_sql = implode(', ', $names); + $digest = 'MD5(CONCAT(' . implode(', ', $hash) . '))'; + $checksum = 'COUNT(*) AS row_count, SUM(CAST(CONV(LEFT(digest, 15), 16, 10) AS UNSIGNED)) AS digest_sum, + BIT_XOR(CAST(CONV(RIGHT(digest, 16), 16, 10) AS UNSIGNED)) AS digest_xor'; $composite = count($keys) === 2; $order = $composite ? 'source.seq, source.logtime' : 'source.seq'; - $cursor = null; - $copied = 0; + $reverse = $composite ? 'source.seq DESC, source.logtime DESC' : 'source.seq DESC'; + $cursor = $state && $state['source_seq'] !== null ? ['seq' => $state['source_seq'], 'logtime' => $state['source_logtime']] : null; + $high = $state ? (string) $state['target_seq'] : '0'; + $copied = $state ? (int) $state['copied'] : 0; + + if ($preserve_data && !syslog_db_execute_prepared("DELETE FROM $target WHERE seq > ?", [$high])) { + return $fail('Cannot discard unverified rows copied after the last checkpoint'); + } + + $batches = 0; while ($preserve_data) { $where = '1 = 1'; @@ -472,56 +571,81 @@ function syslog_upart_rebuild(string $table, bool $convert_ms, bool $preserve_da $params = $composite ? [$cursor['seq'], $cursor['seq'], $cursor['logtime']] : [$cursor['seq']]; } - $rows = syslog_db_fetch_assoc_prepared("SELECT source.seq, source.logtime FROM $saved AS source - WHERE $where ORDER BY $order LIMIT 10000", $params); + $last = syslog_db_fetch_row_prepared("SELECT source.seq, source.logtime FROM $saved AS source + WHERE $where ORDER BY $order LIMIT 1 OFFSET 9999", $params); + $final = $last === []; + + if ($final) { + $last = syslog_db_fetch_row_prepared("SELECT source.seq, source.logtime FROM $saved AS source + WHERE $where ORDER BY $reverse LIMIT 1", $params); + } - if (!is_array($rows)) { + if (!is_array($last)) { return $fail('Cannot read the next copy batch'); } - if (!$rows) { + if (!$last) { break; } - $last = $rows[count($rows) - 1]; - $upper = $composite ? '(source.seq < ? OR (source.seq = ? AND source.logtime <= ?))' : 'source.seq <= ?'; + $where .= $composite ? ' AND (source.seq < ? OR (source.seq = ? AND source.logtime <= ?))' : ' AND source.seq <= ?'; $params = array_merge($params, $composite ? [$last['seq'], $last['seq'], $last['logtime']] : [$last['seq']]); - $names_sql = implode(', ', $names); - $values_sql = implode(', ', $values); if (!syslog_db_execute_prepared("INSERT INTO $target ($names_sql) - SELECT $values_sql FROM $saved AS source - LEFT JOIN $target AS copied ON $join - WHERE $where AND $upper - AND copied.seq IS NULL ORDER BY $order", $params)) { + SELECT $names_sql FROM $saved AS source WHERE $where ORDER BY $order", $params)) { return $fail('Cannot copy the next batch'); } + $expected = syslog_db_fetch_row_prepared("SELECT $checksum FROM (SELECT $digest AS digest + FROM $saved AS source WHERE $where) AS batch", $params); + $actual = syslog_db_fetch_row_prepared("SELECT $checksum, MAX(seq) AS high FROM (SELECT seq, $digest AS digest + FROM $target WHERE seq > ?) AS batch", [$high]); + + if (!is_array($expected) || !is_array($actual) || !isset($expected['row_count'], $actual['row_count']) + || (int) $expected['row_count'] === 0 + || (string) $expected['row_count'] !== (string) $actual['row_count'] + || (string) $expected['digest_sum'] !== (string) $actual['digest_sum'] + || (string) $expected['digest_xor'] !== (string) $actual['digest_xor']) { + return $fail('A copied batch does not match its original rows'); + } + + $high = (string) $actual['high']; + $copied += (int) $expected['row_count']; $cursor = $last; - $copied += count($rows); - print "$table: checked $copied source rows" . PHP_EOL; - if (count($rows) < 10000) { + if (!syslog_db_execute_prepared("REPLACE INTO $progress VALUES (?, ?, ?, ?, ?)", + [$table, $cursor['seq'], $cursor['logtime'], $high, $copied])) { + return $fail('Cannot record the copy checkpoint'); + } + + if ($final || ++$batches % 100 === 0) { + print "$table: copied and verified $copied rows" . PHP_EOL; + } + + if ($final) { break; } } - $old_count = $preserve_data ? syslog_db_fetch_cell("SELECT COUNT(*) FROM $saved") : 0; + $old_count = $preserve_data ? syslog_db_fetch_cell("SELECT COUNT(*) FROM $saved") : '0'; $new_count = syslog_db_fetch_cell("SELECT COUNT(*) FROM $target"); - $equal_sql = implode(' AND ', $equal); - $different = $preserve_data ? syslog_db_fetch_cell("SELECT EXISTS (SELECT 1 FROM $saved AS source - LEFT JOIN $target AS copied ON $join - WHERE copied.seq IS NULL OR NOT ($equal_sql))") : 0; if (!is_numeric($old_count) || !is_numeric($new_count) || (string) $old_count !== (string) $new_count - || !is_numeric($different) || (int) $different !== 0 + || ($preserve_data && (string) $copied !== (string) $new_count) || syslog_detect_logtime_precision($table) !== $precision) { - return $fail('Copied rows, values or timestamp precision do not match'); + return $fail('Copied row counts or timestamp precision do not match'); + } + + if (!syslog_db_execute("DROP TABLE $saved")) { + return $fail('Cannot drop the verified original'); } - if (!syslog_db_execute("ALTER TABLE $target AUTO_INCREMENT = $next") - || !syslog_db_execute("DROP TABLE $saved")) { - return $fail('Cannot restore the sequence high-water mark or drop the verified original'); + if ($preserve_data) { + syslog_db_execute_prepared("DELETE FROM $progress WHERE table_name = ?", [$table]); + + if ((string) syslog_db_fetch_cell("SELECT COUNT(*) FROM $progress") === '0') { + syslog_db_execute("DROP TABLE IF EXISTS $progress"); + } } print "$table: verified $new_count rows; removed $backup" . PHP_EOL; @@ -529,6 +653,48 @@ function syslog_upart_rebuild(string $table, bool $convert_ms, bool $preserve_da return true; } +/** + * Check that a partially copied replacement still has the requested engine and + * contiguous UTC daily partitions ending in dMaxValue. + */ +function syslog_upart_layout_ok(string $table, string $engine): bool { + global $syslogdb_default; + + $actual_engine = syslog_db_fetch_cell_prepared('SELECT ENGINE FROM information_schema.TABLES + WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?', [$syslogdb_default, $table]); + $parts = syslog_db_fetch_assoc_prepared('SELECT PARTITION_NAME, PARTITION_DESCRIPTION, PARTITION_EXPRESSION + FROM information_schema.PARTITIONS WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? + ORDER BY PARTITION_ORDINAL_POSITION', [$syslogdb_default, $table]); + $previous = null; + + if ($actual_engine !== $engine || !is_array($parts) || count($parts) < 2) { + return false; + } + + foreach ($parts as $index => $part) { + $expression = strtolower(str_replace(['`', ' '], '', (string) $part['PARTITION_EXPRESSION'])); + $boundary = (string) $part['PARTITION_DESCRIPTION']; + + if ($expression !== 'unix_timestamp(logtime)') { + return false; + } + + if ($index === count($parts) - 1) { + return $part['PARTITION_NAME'] === 'dMaxValue' && $boundary === 'MAXVALUE'; + } + + if (!ctype_digit($boundary) || (int) $boundary % 86400 !== 0 + || $part['PARTITION_NAME'] !== 'd' . gmdate('Ymd', (int) $boundary - 86400) + || ($previous !== null && (int) $boundary !== $previous + 86400)) { + return false; + } + + $previous = (int) $boundary; + } + + return false; +} + /** * Plan UTC daily partitions without deleting out-of-retention rows: the first * RANGE partition also accepts older timestamps. Indefinite retention starts @@ -566,7 +732,10 @@ function syslog_upart_display_help(): void { print 'Rebuilds syslog and syslog_removed with UTC daily partitions matching the' . PHP_EOL; print 'configured storage engine, message retention and future-partition horizon.' . PHP_EOL . PHP_EOL; print 'Existing precision is preserved unless --convert-ms is selected.' . PHP_EOL . PHP_EOL; - print 'With preservation, originals are dropped only after row counts and values are verified.' . PHP_EOL . PHP_EOL; + print 'With preservation, rows are copied in verified batches and receive new sequence' . PHP_EOL; + print 'numbers above the original high-water mark; originals are dropped only after' . PHP_EOL; + print 'every batch and the row counts are verified.' . PHP_EOL . PHP_EOL; + print 'Legacy DATETIME logtime columns are not converted; use --discard-data for them.' . PHP_EOL . PHP_EOL; print 'Options:' . PHP_EOL; print ' --convert-ms - Also convert pipeline timestamps to TIMESTAMP(3); MariaDB only.' . PHP_EOL; print ' --preserve-data - Preserve existing messages (the default), copying 10,000 rows at a time.' . PHP_EOL; @@ -575,7 +744,7 @@ function syslog_upart_display_help(): void { print ' --version - Display version information.' . PHP_EOL; print ' --help - Display this help.' . PHP_EOL . PHP_EOL; print 'Maintenance:' . PHP_EOL; - print 'Take a database backup and stop ALL processing and database writers first.' . PHP_EOL . PHP_EOL; + print 'Take a database backup, uncheck Syslog Enabled and stop ALL database writers first.' . PHP_EOL . PHP_EOL; print 'The current database size is reported before changes. Have at least that' . PHP_EOL; print 'much ADDITIONAL free disk space on the database server, plus temporary' . PHP_EOL; print 'file and binary-log headroom. Large transfers may take several hours.' . PHP_EOL . PHP_EOL; diff --git a/includes/utilities.php b/includes/utilities.php index 298eb2c2..18f8494d 100644 --- a/includes/utilities.php +++ b/includes/utilities.php @@ -49,27 +49,33 @@ function syslog_utilities_action($action) { exit; } + // Cacti loads only this hook file, so open the Syslog database (and its + // syslog_db_* helpers) before purging. + if (!syslog_connect()) { + cacti_log('ERROR: syslog purge failed -- unable to connect to the Syslog database', false, 'SYSLOG'); + raise_message('syslog_connect_error', __('Unable to connect to the Syslog database. No Device records were removed.', 'syslog'), MESSAGE_LEVEL_ERROR); + header('Location: utilities.php?header=false'); + exit; + } + + global $syslogdb_default; + $records = 0; - syslog_db_execute('DELETE FROM syslog_hosts - WHERE host_id NOT IN ( - SELECT DISTINCT host_id - FROM syslog - UNION - SELECT DISTINCT host_id - FROM syslog_removed - )'); - $records += syslog_db_affected_rows(); - - syslog_db_execute('DELETE FROM syslog_host_facilities - WHERE host_id NOT IN ( - SELECT DISTINCT host_id - FROM syslog - UNION - SELECT DISTINCT host_id - FROM syslog_removed - )'); - $records += syslog_db_affected_rows(); + // NOT EXISTS probes the host_id indexes and, unlike NOT IN, is not + // defeated by a NULL host_id in the message tables. + foreach (['syslog_hosts', 'syslog_host_facilities'] as $table) { + syslog_db_execute("DELETE FROM `$syslogdb_default`.`$table` + WHERE NOT EXISTS ( + SELECT 1 FROM `$syslogdb_default`.`syslog` AS s + WHERE s.host_id = `$table`.host_id + ) + AND NOT EXISTS ( + SELECT 1 FROM `$syslogdb_default`.`syslog_removed` AS r + WHERE r.host_id = `$table`.host_id + )"); + $records += syslog_db_affected_rows(); + } raise_message('syslog_info', __('There were %s Device records removed from the Syslog database', $records, 'syslog'), MESSAGE_LEVEL_INFO); diff --git a/locales/po/cacti.pot b/locales/po/cacti.pot index bff09f32..6f155ee0 100644 --- a/locales/po/cacti.pot +++ b/locales/po/cacti.pot @@ -1321,6 +1321,10 @@ msgstr "" msgid "There were %s Device records removed from the Syslog database" msgstr "" +#: includes/utilities.php +msgid "Unable to connect to the Syslog database. No Device records were removed." +msgstr "" + #: includes/utilities.php msgid "Syslog Utilities" msgstr "" diff --git a/tests/Security/CsrfPurgeTest.php b/tests/Security/CsrfPurgeTest.php index 62303abe..d1ce466b 100644 --- a/tests/Security/CsrfPurgeTest.php +++ b/tests/Security/CsrfPurgeTest.php @@ -66,6 +66,8 @@ $root . '/includes/navigation.php' => $sandbox . '/includes/navigation.php', $root . '/includes/installer.php' => $sandbox . '/includes/installer.php', $root . '/includes/utilities.php' => $sandbox . '/includes/utilities.php', + $root . '/includes/database.php' => $sandbox . '/includes/database.php', + $root . '/includes/recommendations.php' => $sandbox . '/includes/recommendations.php', ]; foreach ($staged as $src => $dst) { @@ -74,7 +76,10 @@ } } - file_put_contents($sandbox . '/config.php', " must not close the block it sits in. @@ -110,17 +115,28 @@ function raise_message($id, $message = '', $level = MESSAGE_LEVEL_INFO) { function html_header($items, $span = 1) { } - function syslog_db_execute($sql) { - print "DBEXEC\n"; + // Cacti core: the real includes/database.php delegates to these. + function db_execute($sql, $log = true, $connection = false) { + print 'DBEXEC:' . preg_replace('/\s+/', ' ', $sql) . "\n"; - return true; + return is_object($connection); } - function syslog_db_affected_rows() { + function db_affected_rows($connection = false) { return 1; } - if ($scenario === 'action_valid_token') { + function api_plugin_is_enabled($plugin) { + return false; + } + + $config = ['poller_id' => 1]; + + if ($scenario !== 'action_db_unavailable') { + $local_db_cnn_id = new stdClass(); + } + + if ($scenario === 'action_valid_token' || $scenario === 'action_db_unavailable') { function csrf_check($fatal = true) { return true; } @@ -195,7 +211,7 @@ function csrf_check($fatal = true) { foreach (['unrelated', 'unconfigured'] as $scenario) { expect($runHarness($scenario))->toBe("other_plugin_actionREACHED_END\n"); } - file_put_contents($sandbox . '/config.php', "toBeTrue(); }); diff --git a/tests/Unit/PartitionRebuildTest.php b/tests/Unit/PartitionRebuildTest.php index 598e048b..737b7af5 100644 --- a/tests/Unit/PartitionRebuildTest.php +++ b/tests/Unit/PartitionRebuildTest.php @@ -5,12 +5,15 @@ function partition_rebuild_runner(): string { if (!function_exists($name)) { $source = plugin_test_read_source('cli/upgrade_partitioning.php'); - preg_match('/^function syslog_upart_run\(.*?^\}/ms', $source, $match); + preg_match('/^function syslog_upart_run\(.*?^\}/ms', $source, $run); + preg_match('/^function syslog_upart_run_locked\(.*?^\}/ms', $source, $locked); $path = tempnam(sys_get_temp_dir(), 'syslog-rebuild-unit-'); file_put_contents($path, ' [], 'settings' => [], 'executions' => [], - 'precision' => 'sec' + 'statements' => [], + 'precision' => 'sec', + 'enabled' => '', + 'slot' => true, + 'slots' => [], + 'legacy' => [] ]; + $GLOBALS['config']['poller_id'] = 1; test_override('read_config_option', static fn($key) => [ 'syslog_install_engine' => 'InnoDB', 'syslog_retention' => '30', - 'syslog_partition_ahead_days' => '3' + 'syslog_partition_ahead_days' => '3', 'syslog_enabled' => $state->enabled ][$key] ?? ''); + test_override('register_process_start', static function (...$args) use ($state) { + $state->slots[] = array_merge(['start'], $args); + + return $state->slot; + }); + test_override('unregister_process', static function (...$args) use ($state) { + $state->slots[] = array_merge(['end'], $args); + + return true; + }); + test_override('syslog_db_fetch_assoc_prepared', static fn($sql) => str_contains($sql, "DATA_TYPE = 'datetime'") ? $state->legacy : []); + test_override('syslog_db_execute', static function ($sql) use ($state) { + $state->statements[] = $sql; + + return true; + }); test_override('syslog_db_table_exists', static fn($table) => in_array($table, $state->tables, true)); test_override('syslog_db_fetch_row', static fn() => ['Value' => $state->version]); test_override('set_config_option', static function ($key, $value) use ($state) { @@ -91,13 +116,62 @@ function partition_rebuild_setup(): stdClass { ->and($output)->toContain('10.00 GiB (10,737,418,240 bytes)') ->toContain('AT LEAST another 10.00 GiB (10,737,418,240 bytes)') ->toContain('DATABASE SERVER')->toContain('SEVERAL HOURS') - ->toContain('does NOT pause them')->toContain('10,000-row copies') + ->toContain('does NOT pause')->toContain('10,000-row copies') + ->toContain('receive new sequence numbers') ->not->toContain('Type "yes"') - ->and($state->rebuilds)->toBe([['syslog', false, true, 'InnoDB', 30, 3, '+05:30']]); + ->and($state->rebuilds)->toBe([['syslog', false, true, 'InnoDB', 30, 3]]) + ->and($state->slots)->toBe([['start', 'syslog', 'master', 1, 604800], ['end', 'syslog', 'master', 1]]); expect($state->settings)->toBe([]); expect(array_column($state->queries, 0))->toContain('SELECT RELEASE_LOCK(?)'); }); +it('refuses to rebuild while Syslog processing is enabled', function () { + $state = partition_rebuild_setup(); + $state->enabled = 'on'; + $run = partition_rebuild_runner(); + + expect($run(true, false))->toBeFalse() + ->and($state->slots)->toBe([]) + ->and($state->queries)->toBe([]) + ->and($state->rebuilds)->toBe([]); +}); + +it('refuses to rebuild while a processing run holds the master slot', function () { + $state = partition_rebuild_setup(); + $state->slot = false; + $run = partition_rebuild_runner(); + + expect($run(true, false))->toBeFalse() + ->and($state->slots)->toBe([['start', 'syslog', 'master', 1, 604800]]) + ->and($state->queries)->toBe([]) + ->and($state->rebuilds)->toBe([]); +}); + +it('refuses legacy DATETIME history before any change and releases the slot', function () { + $state = partition_rebuild_setup(); + $state->legacy = [['TABLE_NAME' => 'syslog']]; + $run = partition_rebuild_runner(); + + expect($run(true, false))->toBeFalse() + ->and($state->rebuilds)->toBe([]) + ->and($state->statements)->toBe([]) + ->and(end($state->slots))->toBe(['end', 'syslog', 'master', 1]); +}); + +it('widens the incoming queue in place so its sequence identities are kept', function () { + $state = partition_rebuild_setup(); + $state->tables[] = 'syslog_incoming'; + $run = partition_rebuild_runner(); + ob_start(); + $ok = $run(true, true); + ob_end_clean(); + + expect($ok)->toBeTrue() + ->and(array_column($state->rebuilds, 0))->toBe(['syslog']) + ->and($state->statements)->toContain("ALTER TABLE `fixture`.`syslog_incoming` MODIFY COLUMN logtime timestamp(3) NOT NULL DEFAULT '0000-00-00 00:00:00.000'") + ->and($state->settings)->toBe(['syslog_install_logtime_precision' => 'ms']); +}); + it('refuses to rebuild when the database size is unreadable', function () { $state = partition_rebuild_setup(); $state->bytes = false; @@ -184,7 +258,8 @@ function partition_rebuild_setup(): stdClass { '--convert-ms is selected.' . PHP_EOL . PHP_EOL . 'With preservation', 'Syslog Partition Upgrade Utility, Version 5.0, Copyright (C)', PHP_EOL . PHP_EOL . 'usage: upgrade_partitioning.php', - 'values are verified.' . PHP_EOL . PHP_EOL . 'Options:', + 'row counts are verified.' . PHP_EOL . PHP_EOL . 'Legacy DATETIME', + '--discard-data for them.' . PHP_EOL . PHP_EOL . 'Options:', 'several hours.' . PHP_EOL . PHP_EOL . 'On failure', 'writers first.' . PHP_EOL . PHP_EOL . 'The current database size' ]], diff --git a/tests/regression/partition_rebuild_database_test.php b/tests/regression/partition_rebuild_database_test.php index d115a2cc..8cdd5643 100644 --- a/tests/regression/partition_rebuild_database_test.php +++ b/tests/regression/partition_rebuild_database_test.php @@ -23,8 +23,12 @@ $copy_attempts = 0; $fail_copy_at = 0; $fail_prefix = ''; +$corrupt_after_copy = ''; $batch_sizes = []; $checks = 0; +$config = ['poller_id' => 1]; +$slot_free = true; +$slot_calls = []; function rebuild_check(bool $ok, string $message): void { $GLOBALS['checks']++; @@ -35,7 +39,17 @@ function rebuild_check(bool $ok, string $message): void { } function syslog_connect() { return true; } -function read_config_option($name) { return $GLOBALS['options'][$name] ?? ''; } +function read_config_option($name, $force = false) { return $GLOBALS['options'][$name] ?? ''; } +function register_process_start($type, $name, $id = 0, $timeout = 300) { + $GLOBALS['slot_calls'][] = ['start', $type, $name, $id]; + + return $GLOBALS['slot_free']; +} +function unregister_process($type, $name, $id = 0, $pid = -1) { + $GLOBALS['slot_calls'][] = ['end', $type, $name, $id]; + + return true; +} function set_config_option($name, $value) { $GLOBALS['options'][$name] = $value; } function cacti_log(...$args) {} function syslog_db_execute_prepared($sql, $params = [], ...$args) { @@ -54,6 +68,11 @@ function syslog_db_execute_prepared($sql, $params = [], ...$args) { if ($copy) { $GLOBALS['batch_sizes'][] = $query->rowCount(); + + if ($GLOBALS['corrupt_after_copy'] !== '') { + $GLOBALS['pdo']->exec($GLOBALS['corrupt_after_copy']); + $GLOBALS['corrupt_after_copy'] = ''; + } } return $ok; @@ -65,11 +84,12 @@ function syslog_db_fetch_assoc_prepared($sql, $params = [], ...$args) { return $query->fetchAll(); } -function syslog_db_fetch_row($sql) { - $rows = syslog_db_fetch_assoc_prepared($sql); +function syslog_db_fetch_row_prepared($sql, $params = [], ...$args) { + $rows = syslog_db_fetch_assoc_prepared($sql, $params); return $rows[0] ?? []; } +function syslog_db_fetch_row($sql) { return syslog_db_fetch_row_prepared($sql); } function syslog_db_fetch_cell_prepared($sql, $params = [], ...$args) { $query = $GLOBALS['pdo']->prepare($sql); $query->execute($params); @@ -117,11 +137,11 @@ function rebuild_table(string $table, bool $convert = true, string $engine = 'In $pdo->exec("SET SESSION time_zone = '+00:00'"); $pdo->exec("SET SESSION sql_mode = 'STRICT_TRANS_TABLES,NO_AUTO_VALUE_ON_ZERO'"); $pdo->exec("CREATE TABLE syslog ( - seq BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, logtime DATETIME NOT NULL, + seq BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, logtime TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00', message TEXT COLLATE utf8mb4_general_ci NOT NULL, nullable_value VARCHAR(40), replication_source_event_id BIGINT UNSIGNED, PRIMARY KEY(seq,logtime), KEY logtime(logtime) - ) ENGINE=InnoDB PARTITION BY RANGE(TO_DAYS(logtime)) + ) ENGINE=InnoDB PARTITION BY RANGE(UNIX_TIMESTAMP(logtime)) (PARTITION legacy VALUES LESS THAN MAXVALUE)"); $pdo->exec("INSERT INTO syslog (seq,logtime,message,replication_source_event_id) SELECT seq,TIMESTAMPADD(SECOND,seq,UTC_DATE()),CONCAT('Event ',seq),seq FROM seq_1_to_10005"); @@ -129,57 +149,104 @@ function rebuild_table(string $table, bool $convert = true, string $engine = 'In (0,'0000-00-00 00:00:00','zero',NULL,0), (10000,TIMESTAMPADD(SECOND,10001,UTC_DATE()),'repeated seq','',10000), (10006,UTC_DATE() - INTERVAL 400 DAY,'outside retention',NULL,10006), - (18446744073709551600,UTC_DATE(),UNHEX('410042'),NULL,18446744073709551600)"); - $pdo->exec('ALTER TABLE syslog AUTO_INCREMENT=18446744073709551610'); + (20000,UTC_DATE(),UNHEX('410042'),NULL,20000)"); + $pdo->exec('ALTER TABLE syslog AUTO_INCREMENT=1000000'); $total = syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog'); + $original_order = $pdo->query('SELECT message FROM syslog ORDER BY seq, logtime')->fetchAll(PDO::FETCH_COLUMN); $fail_copy_at = 2; - rebuild_check(!rebuild_table('syslog'), 'The injected second-batch failure must stop the rebuild'); + rebuild_check(!rebuild_table('syslog', false), 'The injected second-batch failure must stop the rebuild'); rebuild_check(syslog_db_table_exists('syslog_rebuild'), 'A failed copy must retain the original'); - rebuild_check((int) syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog') === 10000, 'The first copy is exactly 10000 rows'); + rebuild_check((int) syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog') === 10000, 'The first verified copy is exactly 10000 rows'); + rebuild_check((int) syslog_db_fetch_cell('SELECT copied FROM syslog_rebuild_progress WHERE table_name=\'syslog\'') === 10000, 'The checkpoint records the verified batch'); rebuild_check((int) syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog_rebuild') === (int) $total, 'The original is intact'); $fail_copy_at = 0; - $pdo->exec("UPDATE syslog SET message='event 1' WHERE seq=1"); - rebuild_check(!rebuild_table('syslog'), 'Case-only differences must fail binary value verification'); + $pdo->exec("INSERT INTO syslog (logtime,message) VALUES (UTC_DATE(),'unverified partial batch')"); + $corrupt_after_copy = 'UPDATE syslog SET message=LOWER(message) ORDER BY seq DESC LIMIT 1'; + rebuild_check(!rebuild_table('syslog', false), 'Case-only differences must fail binary batch verification'); rebuild_check(syslog_db_table_exists('syslog_rebuild'), 'A value mismatch must retain the original even with matching counts'); - $pdo->exec('UPDATE syslog AS copied JOIN syslog_rebuild AS source - ON copied.seq=source.seq AND copied.logtime=source.logtime SET copied.message=source.message'); - rebuild_check(rebuild_table('syslog'), 'A repaired copy resumes successfully'); + rebuild_check((int) syslog_db_fetch_cell('SELECT copied FROM syslog_rebuild_progress WHERE table_name=\'syslog\'') === 10000, 'A mismatched batch is never checkpointed'); + rebuild_check(rebuild_table('syslog', false), 'A retry discards unverified rows and resumes after the checkpoint'); rebuild_check(!syslog_db_table_exists('syslog_rebuild'), 'Verified originals are removed automatically'); - rebuild_check((int) syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog') === (int) $total, 'Every row survives retries'); - rebuild_check((int) syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog WHERE seq=10000') === 2, 'The composite cursor preserves repeated sequence numbers across batches'); - rebuild_check(syslog_db_fetch_cell('SELECT HEX(message) FROM syslog WHERE seq=18446744073709551600') === '410042', 'Binary message contents survive'); - rebuild_check((int) syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog WHERE seq=0') === 1, 'Zero sequence values must not be regenerated'); - rebuild_check((string) syslog_db_fetch_cell("SELECT AUTO_INCREMENT FROM information_schema.TABLES - WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='syslog'") === '18446744073709551610', 'Unsigned high-water marks must not overflow PHP integers'); + rebuild_check(!syslog_db_table_exists('syslog_rebuild_progress'), 'The checkpoint table is removed after the last rebuild'); + rebuild_check((int) syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog') === (int) $total, 'Every row survives retries exactly once'); + rebuild_check((int) syslog_db_fetch_cell("SELECT COUNT(*) FROM syslog WHERE message IN ('unverified partial batch')") === 0, 'Unverified replacement rows are discarded'); + rebuild_check($pdo->query('SELECT message FROM syslog ORDER BY seq')->fetchAll(PDO::FETCH_COLUMN) === $original_order, 'Renumbered rows keep their original order, including repeated sequence values'); + rebuild_check((int) syslog_db_fetch_cell('SELECT COUNT(DISTINCT seq) FROM syslog') === (int) $total, 'Repeated original sequence values receive distinct new values'); + rebuild_check((int) syslog_db_fetch_cell('SELECT MIN(seq) FROM syslog') >= 1000000, 'Copied rows never reuse a sequence value at or below the original high-water mark'); + rebuild_check(syslog_db_fetch_cell("SELECT HEX(message) FROM syslog WHERE replication_source_event_id=20000") === '410042', 'Binary message contents survive'); + rebuild_check(syslog_db_fetch_cell("SELECT logtime FROM syslog WHERE message='zero'") === '0000-00-00 00:00:00', 'Zero timestamps survive'); + rebuild_check((int) syslog_db_fetch_cell("SELECT replication_source_event_id FROM syslog WHERE message='Event 5'") === 5, 'Non-sequence columns are copied unchanged'); + + $pdo->exec('CREATE TABLE syslog_removed (seq BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, logtime TIMESTAMP NOT NULL, message TEXT) ENGINE=InnoDB'); + $pdo->exec("INSERT INTO syslog_removed (logtime,message) SELECT UTC_DATE(),'near the limit' FROM seq_1_to_10"); + $pdo->exec('ALTER TABLE syslog_removed AUTO_INCREMENT=18446744073709551610'); + rebuild_check(!rebuild_table('syslog_removed', false), 'Too few remaining sequence values must stop the rebuild'); + rebuild_check(!syslog_db_table_exists('syslog_removed_rebuild') && (int) syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog_removed') === 10, 'The sequence headroom check runs before any change'); + $pdo->exec('DROP TABLE syslog_removed'); $pdo->exec('CREATE TABLE syslog_removed (seq BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, logtime TIMESTAMP NOT NULL, message TEXT) ENGINE=Aria'); rebuild_check(rebuild_table('syslog_removed', false, 'Aria'), 'An empty legacy table can be repartitioned'); rebuild_check(syslog_detect_logtime_precision('syslog_removed') === 'sec', 'A plain rebuild must preserve second precision'); rebuild_check(syslog_db_fetch_cell("SELECT ENGINE FROM information_schema.TABLES WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='syslog_removed'") === 'Aria', 'Configured Aria engine is supported'); - $pdo->exec('ALTER TABLE syslog_removed REMOVE PARTITIONING'); - $pdo->exec('ALTER TABLE syslog_removed MODIFY logtime DATETIME NOT NULL'); - $pdo->exec("SET SESSION time_zone = '+05:30'"); - $pdo->exec("INSERT INTO syslog_removed VALUES(8,CONCAT(UTC_DATE(),' 12:00:00'),'removed')"); + $pdo->exec("INSERT INTO syslog_removed (logtime,message) VALUES (CONCAT(UTC_DATE(),' 12:00:00'),'removed')"); $removed_epoch = syslog_db_fetch_cell('SELECT UNIX_TIMESTAMP(logtime) FROM syslog_removed'); $fail_prefix = "CREATE TABLE `$syslogdb_default`.`syslog_removed`"; rebuild_check(!rebuild_table('syslog_removed', false, 'Aria'), 'A create failure after rename must be recoverable'); rebuild_check(!syslog_db_table_exists('syslog_removed') && syslog_db_table_exists('syslog_removed_rebuild'), 'The renamed original survives a create failure'); + $fail_prefix = "ALTER TABLE `$syslogdb_default`.`syslog_removed` AUTO_INCREMENT"; + rebuild_check(!rebuild_table('syslog_removed', false, 'Aria'), 'A setup failure before the checkpoint must be recoverable'); + rebuild_check(syslog_db_table_exists('syslog_removed') && (int) syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog_removed') === 0, 'An interrupted setup leaves an empty replacement'); $fail_prefix = ''; - rebuild_check(rebuild_table('syslog_removed', false, 'Aria'), 'A missing replacement can be recreated on retry'); + rebuild_check(rebuild_table('syslog_removed', false, 'Aria'), 'An empty replacement without a checkpoint is recreated on retry'); rebuild_check((int) syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog_removed') === 1, 'Aria data survives recovery'); - rebuild_check(syslog_db_fetch_cell('SELECT UNIX_TIMESTAMP(logtime) FROM syslog_removed') === $removed_epoch, 'Legacy DATETIME history preserves its original non-UTC interpretation'); + rebuild_check(syslog_db_fetch_cell('SELECT UNIX_TIMESTAMP(logtime) FROM syslog_removed') === $removed_epoch, 'TIMESTAMP history keeps its UTC instant'); + + $pdo->exec('ALTER TABLE syslog_removed REMOVE PARTITIONING'); + $pdo->exec('ALTER TABLE syslog_removed MODIFY logtime DATETIME NOT NULL'); + rebuild_check(!rebuild_table('syslog_removed', false, 'Aria'), 'Preserving a legacy DATETIME table is refused'); + rebuild_check(!syslog_db_table_exists('syslog_removed_rebuild'), 'The DATETIME refusal happens before any change'); + $options['syslog_enabled'] = ''; + $slot_calls = []; + ob_start(); + rebuild_check(!syslog_upart_run(true, false), 'The command refuses legacy DATETIME history before any change'); + ob_end_clean(); + rebuild_check(syslog_db_fetch_cell("SELECT DATA_TYPE FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() + AND TABLE_NAME='syslog_removed' AND COLUMN_NAME='logtime'") === 'datetime' && !syslog_db_table_exists('syslog_removed_rebuild'), 'A refused command leaves DATETIME tables untouched'); + rebuild_check($slot_calls === [['start', 'syslog', 'master', 1], ['end', 'syslog', 'master', 1]], 'The processing slot is released after a refusal'); + ob_start(); + rebuild_check(syslog_upart_rebuild('syslog_removed', false, false, 'InnoDB', 2, 2), 'Explicit discard mode replaces a legacy DATETIME table'); + ob_end_clean(); + rebuild_check(syslog_detect_logtime_precision('syslog_removed') === 'sec' && syslog_db_fetch_cell("SELECT DATA_TYPE FROM information_schema.COLUMNS + WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='syslog_removed' AND COLUMN_NAME='logtime'") === 'timestamp', 'A discarded DATETIME table is recreated as TIMESTAMP'); + + $options['syslog_enabled'] = 'on'; + $slot_calls = []; + ob_start(); + rebuild_check(!syslog_upart_run(true, false), 'The command refuses to run while Syslog processing is enabled'); + ob_end_clean(); + rebuild_check($slot_calls === [], 'Enabled processing is refused before taking the process slot'); + $options['syslog_enabled'] = ''; + $slot_free = false; + ob_start(); + rebuild_check(!syslog_upart_run(true, false), 'The command refuses to run while a processing run holds the master slot'); + ob_end_clean(); + rebuild_check(!syslog_db_table_exists('syslog_rebuild') && !syslog_db_table_exists('syslog_removed_rebuild'), 'Refusals leave the message tables untouched'); + $slot_free = true; $pdo->exec('CREATE TABLE syslog_incoming (seq BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, logtime TIMESTAMP NOT NULL, status INT, message TEXT) ENGINE=InnoDB'); $pdo->exec("INSERT INTO syslog_incoming VALUES(30,UTC_DATE(),1,'queued'),(40,UTC_DATE(),0,'new')"); $pdo->exec("INSERT INTO syslog_incoming SELECT seq+100,UTC_DATE(),0,CONCAT('queued ',seq) FROM seq_1_to_10001"); $incoming_count = syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog_incoming'); + $incoming_seq = syslog_db_fetch_cell('SELECT SUM(seq) FROM syslog_incoming'); $pdo->exec("SET SESSION time_zone = '+05:30'"); - $pdo->exec('CREATE TABLE syslog_replication_output (seq BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, logtime DATETIME NOT NULL, message TEXT) ENGINE=InnoDB'); + $pdo->exec('CREATE TABLE syslog_replication_output (seq BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, logtime TIMESTAMP NOT NULL, message TEXT) ENGINE=InnoDB'); $pdo->exec("INSERT INTO syslog_replication_output VALUES(4,CONCAT(UTC_DATE(),' 12:00:00'),'undelivered')"); $outbox_epoch = syslog_db_fetch_cell('SELECT UNIX_TIMESTAMP(logtime) FROM syslog_replication_output'); + $stored_epochs = syslog_db_fetch_cell("SELECT SUM(UNIX_TIMESTAMP(logtime)) FROM syslog WHERE logtime > '1970-01-01'"); $options['syslog_install_logtime_precision'] = 'sec'; $fail_prefix = "ALTER TABLE `$syslogdb_default`.`syslog_replication_output`"; + $slot_calls = []; ob_start(); rebuild_check(!syslog_upart_run(true, true), 'Outbox conversion failure must fail the overall operation'); $output = ob_get_clean(); @@ -188,16 +255,19 @@ function rebuild_table(string $table, bool $convert = true, string $engine = 'In rebuild_check($options['syslog_install_logtime_precision'] === 'sec', 'Partial conversion must not update the precision setting'); rebuild_check(syslog_db_fetch_cell('SELECT @@SESSION.time_zone') === '+05:30', 'The original non-UTC session zone is restored after failure'); rebuild_check((int) syslog_db_fetch_cell("SELECT IS_FREE_LOCK('" . hash('sha256', $syslogdb_default . '.syslog_upgrade_partitioning') . "')") === 1, 'The named lock is released after failure'); + rebuild_check(end($slot_calls) === ['end', 'syslog', 'master', 1], 'The processing slot is released after failure'); $fail_prefix = ''; ob_start(); rebuild_check(syslog_upart_run(true, true), 'The complete pipeline can finish after the outbox failure is fixed'); ob_end_clean(); rebuild_check($options['syslog_install_logtime_precision'] === 'ms', 'Only complete conversion records millisecond precision'); - rebuild_check(syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog_incoming') === $incoming_count, 'All queued incoming messages survive multiple sequence-only batches'); + rebuild_check(syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog_incoming') === $incoming_count + && syslog_db_fetch_cell('SELECT SUM(seq) FROM syslog_incoming') === $incoming_seq, 'Queued incoming messages keep their sequence identities'); rebuild_check((int) syslog_db_fetch_cell('SELECT status FROM syslog_incoming WHERE seq=30') === 1, 'Incoming processing state survives'); rebuild_check((int) syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog_replication_output') === 1, 'Undelivered replication messages survive'); - rebuild_check((string) syslog_db_fetch_cell('SELECT UNIX_TIMESTAMP(logtime) FROM syslog_replication_output') === $outbox_epoch . '.000', 'A legacy DATETIME outbox uses its original session time zone'); - rebuild_check((int) syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog') === (int) $total, 'Previously converted history survives subsequent pipeline retries'); + rebuild_check((string) syslog_db_fetch_cell('SELECT UNIX_TIMESTAMP(logtime) FROM syslog_replication_output') === $outbox_epoch . '.000', 'The outbox keeps its UTC instant'); + rebuild_check((int) syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog') === (int) $total, 'Converted history keeps every row'); + rebuild_check(syslog_db_fetch_cell("SELECT SUM(UNIX_TIMESTAMP(logtime)) FROM syslog WHERE logtime > '1970-01-01'") === $stored_epochs . '.000', 'Converted history keeps every UTC instant'); rebuild_check((int) syslog_db_fetch_cell("SELECT COUNT(*) FROM information_schema.PARTITIONS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME IN ('syslog','syslog_removed') AND REPLACE(PARTITION_EXPRESSION,'`','')='unix_timestamp(logtime)'") > 2, 'Stored and removed tables use epoch partitions'); @@ -207,7 +277,7 @@ function rebuild_table(string $table, bool $convert = true, string $engine = 'In rebuild_check((int) syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog_removed') === 0, 'Only explicit discard mode empties a rebuilt table'); rebuild_check(!syslog_db_table_exists('syslog_removed_rebuild'), 'Explicit discard removes the original after verification'); rebuild_check((int) syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog_replication_output') === 1, 'Discard mode does not delete the outbox'); - rebuild_check(max($batch_sizes) <= 10000, 'No stored or incoming INSERT SELECT may copy more than 10000 rows'); + rebuild_check(max($batch_sizes) <= 10000, 'No stored INSERT SELECT may copy more than 10000 rows'); print "$checks partition rebuild database checks passed.\n"; } finally { From a420dc13774cf0ce43a40485b1188033dff60d54 Mon Sep 17 00:00:00 2001 From: TheWitness Date: Fri, 20 Nov 2026 10:08:56 -0500 Subject: [PATCH 02/12] syslog: add hourly/daily partitioning levels and prune future partitions Add a Partitioning Level of Daily (default) or Hourly for syslog and syslog_removed, matching MacTrack's partition-size workflow: - Settings: rename "Data Retention Settings" to "Data Retention and Partitioning" and add the Partitioning Level dropdown. The level is applied only by cli/upgrade_partitioning.php, or by its new --partition=hourly|daily option, which saves it once confirmed. Until then maintenance follows the tables' existing layout, and Settings and Status show the change as pending. - Install: replace the unused "Partitions per Day" advisor option with the level, and add $syslog_install_options['partitioning']. - Limits: refuse to save settings, install, or rebuild when retention, today, the pre-create window and dMaxValue need more partitions than the server allows (8192; 1024 before MariaDB 10.0.4 and MySQL 5.7), or when Hourly is combined with Indefinite retention. Cacti runs the config_settings hook before settings.php saves, on 1.2 and 1.3. - Hourly maintenance creates hYYYYMMDDHH partitions and keeps at least two days ahead, as MacTrack does, so dMaxValue splits stay instant. Recovery checks only the horizon in the steady state and binary-searches a gap. - Queries: get_syslog_messages() always bounds logtime at now, so empty future partitions are pruned whatever the filter says. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- CHANGELOG.md | 3 + README.md | 81 ++++-- cli/upgrade_partitioning.php | 111 ++++--- config.php.dist | 8 + config_local.php.dist | 8 + includes/functions.php | 272 +++++++++++++++--- includes/installer.php | 15 +- includes/schema.php | 41 +-- includes/settings.php | 87 +++++- locales/po/cacti.pot | 99 +++++-- setup.php | 21 ++ syslog.php | 20 +- tests/Security/LogicalMessageSearchTest.php | 3 +- tests/Security/PartitionTableLockingTest.php | 8 +- tests/Unit/PartitionGranularityTest.php | 153 ++++++++++ .../Unit/PartitionObservabilityDetailTest.php | 19 ++ tests/Unit/PartitionPrecreateBoundaryTest.php | 19 ++ tests/Unit/PartitionRebuildTest.php | 53 +++- .../Unit/PartitionReorganizeCoverageTest.php | 5 + .../partition_rebuild_database_test.php | 35 ++- .../regression/review_fixes_database_test.php | 28 ++ 21 files changed, 919 insertions(+), 170 deletions(-) create mode 100644 tests/Unit/PartitionGranularityTest.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 35c4d750..03c436b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ --- develop --- +* feature: Add an Hourly or Daily (default) Partitioning Level under the renamed Data Retention and Partitioning settings, the install advisor (replacing the unused Partitions per Day option) and silent install options. A changed level takes effect only when cli/upgrade_partitioning.php (or its new --partition=hourly|daily option) rebuilds the tables; until then maintenance follows the existing layout and Settings and Status show the change as pending. Hourly tables keep at least two days of partitions ahead so dMaxValue reorganizations stay instant +* feature: Refuse to save Syslog settings, install, or rebuild when retention, pre-create window and partitioning level need more partitions than the database server supports (8192, or 1024 before MariaDB 10.0.4 and MySQL 5.7), or when Hourly is combined with Indefinite retention +* bug: Bound every log query at the current time so empty future partitions are always pruned, whatever the filter's own date range * bug: Purge Syslog Devices (Utilities) crashed with "Call to undefined function syslog_db_execute()" because the hook never connected to the Syslog database; it now connects first, reports connection failures, qualifies the Syslog database, and uses NOT EXISTS so a NULL host_id no longer prevents purging (#472) * bug: The partition rebuild CLI now refuses to start while Syslog processing is enabled and holds the master process slot; copies stored and removed messages without seq into a replacement whose AUTO_INCREMENT starts above the original high-water mark; verifies each 10,000-row batch by checksum and checkpoints it for resumable retries; widens the incoming queue in place to keep its sequence identities; and refuses legacy DATETIME history before changing anything * docs: Refresh README release/source compatibility, installation guidance and source-checked behavior; add published release notes and 5.0 upgrade guidance diff --git a/README.md b/README.md index e9da8bb9..fb383912 100644 --- a/README.md +++ b/README.md @@ -151,7 +151,10 @@ MySQL installs always use second precision. to second precision. Silent installs can set `$syslog_install_options['logtime_precision'] = 'ms';` (or `'sec'`) in `config.php` / `config_local.php`; a millisecond request on MySQL is rejected - with a log message rather than silently downgraded. + with a log message rather than silently downgraded. The advisor's + *Partitioning Level* option is set silently with + `$syslog_install_options['partitioning'] = 'day';` (or `'hour'`); see + [Partitioning level](#partitioning-level). * **Upgrades** – the existing schema is authoritative and is **never** migrated automatically, even if the configured precision changes. This avoids the long, blocking table rewrite that an in-place precision change would require. @@ -182,18 +185,51 @@ is recorded only after the complete pipeline has converted. ### Rebuilding partition layouts ```console -php plugins/syslog/cli/upgrade_partitioning.php +php plugins/syslog/cli/upgrade_partitioning.php [--partition=hourly|daily] ``` Without `--convert-ms`, this rebuilds `syslog` and `syslog_removed` using the -configured storage engine (`syslog_install_engine`), message retention -(`syslog_retention`) and future-partition horizon -(`syslog_partition_ahead_days`), retaining their existing timestamp precision. -It preserves existing columns and indexes, adds `logtime` to a legacy -sequence-only primary key when necessary, and creates UTC daily -`UNIX_TIMESTAMP(logtime)` partitions with a final `dMaxValue` safety partition. -It does not offer weekly or monthly partitioning, since Syslog's maintenance -expects daily partitions. +**Partitioning Level** (`syslog_partition_granularity`), storage engine +(`syslog_install_engine`), message retention (`syslog_retention`) and +future-partition horizon (`syslog_partition_ahead_days`), retaining their +existing timestamp precision. It preserves existing columns and indexes, adds +`logtime` to a legacy sequence-only primary key when necessary, and creates UTC +hourly (`hYYYYMMDDHH`) or daily (`dYYYYMMDD`) `UNIX_TIMESTAMP(logtime)` +partitions with a final `dMaxValue` safety partition. Any existing RANGE +layout, hourly or daily, can be rebuilt to either level. + +### Partitioning level + +Settings > Syslog > **Data Retention and Partitioning** offers a **Partitioning +Level** of **Daily** (the default) or **Hourly**. Like MacTrack's partition +size, the level is chosen in Settings, or with `--partition=hourly|daily`, +which saves it, and applied by `cli/upgrade_partitioning.php`. Unlike MacTrack, +saving the setting alone changes nothing: poller maintenance keeps creating and +pruning partitions of the size the tables actually have, until the utility +rebuilds them. Settings and the Status tab show the change as pending until then. +There is no Disabled level; partitioned tables are the only supported +architecture. + +Hourly tables keep the configured pre-create window, but never less than two +days of hourly partitions ahead (MacTrack also keeps two days), so new +partitions are always split from an empty `dMaxValue` and the reorganization is +instant even when maintenance runs late. While the horizon exists, maintenance +checks only its last partition; a gap is found with a binary search and healed +in bounded steps. + +Saving Syslog settings is refused, with an error naming the required and +permitted partition counts, when a table would need more partitions than the +database server supports: retention plus today plus the pre-create window, +times 24 for Hourly, plus `dMaxValue`. The limit is 8192 partitions, or 1024 +on MariaDB before 10.0.4 and MySQL before 5.7. Hourly partitioning also +requires a finite retention. For example, Hourly with 183 days of retention +needs 4,489 partitions; with one year it needs 8,857 and is refused. The +installer and the utility apply the same check. + +Every message query has an automatic upper bound of the current time, whatever +its filter says, so the empty future partitions are always pruned. Messages +with timestamps in the future, for example from a device with a fast clock, +are not shown until that time. Stored and removed messages are copied **without** their `seq` column, in their original order, so each replacement numbers them from its own @@ -226,18 +262,19 @@ Then run this utility to apply the configured layout. Every existing message is copied, including older-than-retention messages; those older rows fit in the earliest RANGE partition. Normal retention pruning -resumes when processing restarts. Indefinite retention creates daily partitions -back to the oldest valid timestamp, and future-dated messages extend the planned -horizon. Plans exceeding the server's 8192-partition limit are rejected rather -than dropping history. Custom triggers, foreign keys, or unique indexes that -exclude `logtime` require a manual migration. +resumes when processing restarts. Indefinite retention (Daily only) creates +daily partitions back to the oldest valid timestamp, and future-dated messages +extend the planned horizon. Plans exceeding the server's partition limit are +rejected rather than dropping history. Custom triggers, foreign keys, or unique +indexes that exclude `logtime` require a manual migration. `cli/syslog_schema.php --upgrade` and `--repair` repair and upgrade schema and create missing tables; they do **not** rebuild an existing partition layout or change its timestamp precision. Ordinary retention/horizon changes normally -need no rebuild: poller maintenance creates missing daily partitions, +need no rebuild: poller maintenance creates missing hourly or daily partitions, reorganizes `dMaxValue` safely, and removes expired partitions. Use the explicit -rebuild utility for legacy layouts or engine changes, not routine daily rotation. +rebuild utility to change the partitioning level, for legacy layouts or engine +changes, not routine rotation. **Maintenance requirements for both commands:** @@ -429,7 +466,7 @@ regardless of their event age, including rows missing host or program references ### Partition Maintenance Partition retention uses actual upper boundaries against a UTC calendar cutoff, -with today counted as one retained day. A seven-day setting retains today plus +with today counted as one retained day, for hourly and daily partitions alike. A seven-day setting retains today plus the previous six UTC dates, independently of partition count or the future pre-create window. Fully expired partitions are dropped even after gaps in maintenance. A partition spanning the cutoff is kept whole to protect newer @@ -813,8 +850,9 @@ The settings are under Settings > Syslog. Selected names and defaults: | Enable Hostname Resolution (`syslog_resolve_hostname`) | off | | Skip DNS Resolution for incoming hosts (`syslog_no_dns`) | off | | Syslog Retention (`syslog_retention`) | 30 days | +| Partitioning Level (`syslog_partition_granularity`) | Daily | | Syslog Alert Retention (`syslog_alert_retention`) | 30 days | -| Partition Pre-create Window (`syslog_partition_ahead_days`) | 3 days | +| Partition Pre-create Window (`syslog_partition_ahead_days`) | 3 days (Hourly: at least 2) | | Collector Staleness Threshold (`syslog_stale_threshold`) | 300 seconds | | Collector Backlog Threshold (`syslog_backlog_threshold`) | 10000 rows | | Pipeline Health Reminder Interval (`syslog_pipeline_health_repeat`) | 3600 seconds | @@ -842,8 +880,9 @@ master process for its workers. `cli/syslog_schema.php` takes `--audit`, `--repair`, or `--upgrade`, as described under Installation. -`cli/upgrade_partitioning.php` rebuilds daily partitions to match the configured -engine, retention and future horizon, preserving messages by default. +`cli/upgrade_partitioning.php` rebuilds hourly or daily partitions to match the +Partitioning Level, engine, retention and future horizon, preserving messages +by default. `--partition=hourly|daily` saves and applies a level. `--convert-ms` also converts the message pipeline to millisecond timestamps on MariaDB. It reports database size and warns about additional disk space and potential several-hour duration before confirmation. diff --git a/cli/upgrade_partitioning.php b/cli/upgrade_partitioning.php index f52658a6..b11fe191 100644 --- a/cli/upgrade_partitioning.php +++ b/cli/upgrade_partitioning.php @@ -33,8 +33,21 @@ $confirm = false; $preserve_data = true; $convert_ms = false; +$partition = null; foreach ($parms as $arg) { + if (strpos($arg, '--partition=') === 0) { + $levels = ['hourly' => 'hour', 'hour' => 'hour', 'daily' => 'day', 'day' => 'day']; + $partition = $levels[strtolower(substr($arg, 12))] ?? null; + + if ($partition === null) { + fwrite(STDERR, 'ERROR: --partition must be hourly or daily.' . PHP_EOL); + exit(1); + } + + continue; + } + switch ($arg) { case '--confirm': $confirm = true; @@ -85,13 +98,16 @@ exit(1); } -exit(syslog_upart_run($confirm, $convert_ms, $preserve_data) ? 0 : 1); +exit(syslog_upart_run($confirm, $convert_ms, $preserve_data, $partition) ? 0 : 1); /** - * Rebuild daily partitions using the configured engine, retention and horizon. + * Rebuild partitions using the configured level, engine, retention and horizon. * DDL is not transactional; failed copies retain originals for a safe retry. + * + * @param string|null $partition 'hour' or 'day' from --partition, saved to + * Settings once confirmed; null uses Settings. */ -function syslog_upart_run(bool $confirm, bool $convert_ms, bool $preserve_data = true): bool { +function syslog_upart_run(bool $confirm, bool $convert_ms, bool $preserve_data = true, ?string $partition = null): bool { global $config, $syslogdb_default; $mariadb = syslog_db_is_mariadb(); @@ -111,7 +127,9 @@ function syslog_upart_run(bool $confirm, bool $convert_ms, bool $preserve_data = $engine = read_config_option('syslog_install_engine') ?: 'InnoDB'; $engines = ['innodb' => 'InnoDB', 'aria' => 'Aria']; $days = read_config_option('syslog_retention'); - $ahead = syslog_partition_ahead_days(); + $granularity = $partition ?? syslog_partition_requested_granularity(); + $ahead = syslog_partition_ahead_days_for($granularity); + $limit = syslog_partition_server_limit(); if (!isset($engines[strtolower($engine)]) || (strtolower($engine) === 'aria' && !$mariadb) || !ctype_digit((string) $days) || (int) $days > 8184) { @@ -120,6 +138,14 @@ function syslog_upart_run(bool $confirm, bool $convert_ms, bool $preserve_data = return false; } + $level_error = syslog_partition_settings_error($granularity, $days, syslog_partition_ahead_days(), $limit); + + if ($level_error !== '') { + fwrite(STDERR, "ERROR: $level_error No changes were made." . PHP_EOL); + + return false; + } + // Writers would interleave with copied rows and break verification, so // processing must be disabled and its master slot held for the duration. if ((string) read_config_option('syslog_enabled', true) !== '') { @@ -137,7 +163,7 @@ function syslog_upart_run(bool $confirm, bool $convert_ms, bool $preserve_data = } try { - return syslog_upart_run_locked($confirm, $convert_ms, $preserve_data, $engines[strtolower($engine)], (int) $days, $ahead); + return syslog_upart_run_locked($confirm, $convert_ms, $preserve_data, $engines[strtolower($engine)], (int) $days, $ahead, $granularity, $limit, $partition !== null); } finally { unregister_process('syslog', 'master', $poller_id); } @@ -146,7 +172,8 @@ function syslog_upart_run(bool $confirm, bool $convert_ms, bool $preserve_data = /** * Rebuild while holding the Syslog master process slot. */ -function syslog_upart_run_locked(bool $confirm, bool $convert_ms, bool $preserve_data, string $engine, int $days, int $ahead): bool { +function syslog_upart_run_locked(bool $confirm, bool $convert_ms, bool $preserve_data, string $engine, int $days, int $ahead, + string $granularity = 'day', int $limit = 8192, bool $save_level = false): bool { global $syslogdb_default; $lock = hash('sha256', $syslogdb_default . '.syslog_upgrade_partitioning'); @@ -241,7 +268,7 @@ function syslog_upart_run_locked(bool $confirm, bool $convert_ms, bool $preserve print 'Allow additional headroom for temporary files, indexes and binary logs.' . PHP_EOL; print 'Depending on the size of the Syslog tables and storage speed, this can' . PHP_EOL; print 'take SEVERAL HOURS. Database metadata is an estimate, not a disk-space check.' . PHP_EOL; - print "Rebuilding daily partitions: engine=$engine, retention=$days days, ahead=$ahead days." . PHP_EOL; + print 'Rebuilding ' . ($granularity === 'hour' ? 'hourly' : 'daily') . " partitions: engine=$engine, retention=$days days, ahead=$ahead days." . PHP_EOL; print 'Tables: ' . implode(', ', $tables) . ($widen ? '; widened in place: ' . implode(', ', $widen) : '') . PHP_EOL; print $preserve_data ? 'Messages are preserved in verified 10,000-row copies and receive new sequence numbers; originals are dropped only after verification.' . PHP_EOL @@ -265,6 +292,11 @@ function syslog_upart_run_locked(bool $confirm, bool $convert_ms, bool $preserve } } + // A retry must rebuild to the same level, so --partition is saved first. + if ($save_level) { + set_config_option('syslog_partition_granularity', $granularity); + } + $zone = syslog_db_fetch_cell('SELECT @@SESSION.time_zone'); $mode = syslog_db_fetch_cell('SELECT @@SESSION.sql_mode'); $lock_wait = syslog_db_fetch_cell('SELECT @@SESSION.lock_wait_timeout'); @@ -288,7 +320,7 @@ function syslog_upart_run_locked(bool $confirm, bool $convert_ms, bool $preserve } foreach ($tables as $table) { - if (!syslog_upart_rebuild($table, $convert_ms, $preserve_data, $engine, $days, $ahead)) { + if (!syslog_upart_rebuild($table, $convert_ms, $preserve_data, $engine, $days, $ahead, $granularity, $limit)) { return false; } } @@ -342,7 +374,8 @@ function syslog_upart_run_locked(bool $confirm, bool $convert_ms, bool $preserve * against its source range before a checkpoint records it. A retry discards * replacement rows above the last checkpoint and resumes after it. */ -function syslog_upart_rebuild(string $table, bool $convert_ms, bool $preserve_data, string $engine, int $days, int $ahead): bool { +function syslog_upart_rebuild(string $table, bool $convert_ms, bool $preserve_data, string $engine, int $days, int $ahead, + string $granularity = 'day', int $limit = 8192): bool { global $syslogdb_default; $backup = $table . '_rebuild'; @@ -424,10 +457,10 @@ function syslog_upart_rebuild(string $table, bool $convert_ms, bool $preserve_da $partition_sql = syslog_upart_partition_sql($days, $ahead, $range['first_time'] === null ? null : (int) $range['first_time'], - $range['last_time'] === null ? null : (int) $range['last_time']); + $range['last_time'] === null ? null : (int) $range['last_time'], null, $granularity, $limit); if ($partition_sql === null) { - return $fail('The requested history and future horizon exceed the 8192-partition limit; reduce retention or archive outlying timestamps first'); + return $fail("The requested history and future horizon exceed the server's $limit-partition limit; reduce retention, choose daily partitioning or archive outlying timestamps first"); } // information_schema may cache AUTO_INCREMENT, so MAX(seq) is the floor. @@ -527,7 +560,7 @@ function syslog_upart_rebuild(string $table, bool $convert_ms, bool $preserve_da } } elseif (syslog_detect_logtime_precision($table) !== $precision) { return $fail('The partially copied replacement does not match the requested conversion; retry with the original options'); - } elseif (!syslog_upart_layout_ok($table, $engine)) { + } elseif (!syslog_upart_layout_ok($table, $engine, $granularity)) { return $fail('The partially copied replacement has a different engine or partition layout'); } @@ -655,9 +688,9 @@ function syslog_upart_rebuild(string $table, bool $convert_ms, bool $preserve_da /** * Check that a partially copied replacement still has the requested engine and - * contiguous UTC daily partitions ending in dMaxValue. + * contiguous UTC hourly or daily partitions ending in dMaxValue. */ -function syslog_upart_layout_ok(string $table, string $engine): bool { +function syslog_upart_layout_ok(string $table, string $engine, string $granularity = 'day'): bool { global $syslogdb_default; $actual_engine = syslog_db_fetch_cell_prepared('SELECT ENGINE FROM information_schema.TABLES @@ -665,6 +698,7 @@ function syslog_upart_layout_ok(string $table, string $engine): bool { $parts = syslog_db_fetch_assoc_prepared('SELECT PARTITION_NAME, PARTITION_DESCRIPTION, PARTITION_EXPRESSION FROM information_schema.PARTITIONS WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? ORDER BY PARTITION_ORDINAL_POSITION', [$syslogdb_default, $table]); + $period = $granularity === 'hour' ? 3600 : 86400; $previous = null; if ($actual_engine !== $engine || !is_array($parts) || count($parts) < 2) { @@ -683,9 +717,11 @@ function syslog_upart_layout_ok(string $table, string $engine): bool { return $part['PARTITION_NAME'] === 'dMaxValue' && $boundary === 'MAXVALUE'; } - if (!ctype_digit($boundary) || (int) $boundary % 86400 !== 0 - || $part['PARTITION_NAME'] !== 'd' . gmdate('Ymd', (int) $boundary - 86400) - || ($previous !== null && (int) $boundary !== $previous + 86400)) { + $start = (int) $boundary - $period; + $name = $granularity === 'hour' ? 'h' . gmdate('YmdH', $start) : 'd' . gmdate('Ymd', $start); + + if (!ctype_digit($boundary) || (int) $boundary % $period !== 0 || $part['PARTITION_NAME'] !== $name + || ($previous !== null && (int) $boundary !== $previous + $period)) { return false; } @@ -696,23 +732,31 @@ function syslog_upart_layout_ok(string $table, string $engine): bool { } /** - * Plan UTC daily partitions without deleting out-of-retention rows: the first - * RANGE partition also accepts older timestamps. Indefinite retention starts - * at the oldest valid timestamp; future data gets concrete daily partitions. + * Plan UTC hourly or daily partitions without deleting out-of-retention rows: + * the first RANGE partition also accepts older timestamps. Retention counts + * today as one UTC day. Indefinite retention starts at the oldest valid + * timestamp; future data gets concrete partitions. + * + * @return string|null The PARTITION BY clause, or null above the partition limit. */ -function syslog_upart_partition_sql(int $days, int $ahead, ?int $first, ?int $last, ?int $now = null): ?string { - $today = intdiv($now ?? time(), 86400) * 86400; - $start = $days > 0 ? $today - ($days - 1) * 86400 : min($today, intdiv($first ?? $today, 86400) * 86400); - $end = max($today + $ahead * 86400, intdiv($last ?? $today, 86400) * 86400); - - if (($end - $start) / 86400 + 2 > 8192) { +function syslog_upart_partition_sql(int $days, int $ahead, ?int $first, ?int $last, ?int $now = null, + string $granularity = 'day', int $limit = 8192): ?string { + $period = $granularity === 'hour' ? 3600 : 86400; + $now = $now ?? time(); + $today = intdiv($now, 86400) * 86400; + $current = intdiv($now, $period) * $period; + $start = $days > 0 ? $today - ($days - 1) * 86400 : min($current, intdiv($first ?? $now, $period) * $period); + $end = max($current + $ahead * 86400, intdiv($last ?? $now, $period) * $period); + + if (($end - $start) / $period + 2 > $limit) { return null; } $parts = []; - for ($day = $start; $day <= $end; $day += 86400) { - $parts[] = 'PARTITION d' . gmdate('Ymd', $day) . ' VALUES LESS THAN (' . ($day + 86400) . ')'; + for ($time = $start; $time <= $end; $time += $period) { + $name = $granularity === 'hour' ? 'h' . gmdate('YmdH', $time) : 'd' . gmdate('Ymd', $time); + $parts[] = "PARTITION $name VALUES LESS THAN (" . ($time + $period) . ')'; } $parts[] = 'PARTITION dMaxValue VALUES LESS THAN MAXVALUE'; @@ -728,15 +772,18 @@ function syslog_upart_display_version(): void { function syslog_upart_display_help(): void { syslog_upart_display_version(); - print PHP_EOL . 'usage: upgrade_partitioning.php [--convert-ms] [--preserve-data | --discard-data] [--confirm]' . PHP_EOL . PHP_EOL; - print 'Rebuilds syslog and syslog_removed with UTC daily partitions matching the' . PHP_EOL; - print 'configured storage engine, message retention and future-partition horizon.' . PHP_EOL . PHP_EOL; + print PHP_EOL . 'usage: upgrade_partitioning.php [--partition=hourly|daily] [--convert-ms] [--preserve-data | --discard-data] [--confirm]' . PHP_EOL . PHP_EOL; + print 'Rebuilds syslog and syslog_removed with UTC hourly or daily partitions matching' . PHP_EOL; + print 'the Partitioning Level, storage engine, message retention and future-partition' . PHP_EOL; + print 'horizon. A Partitioning Level changed in Settings takes effect only when this' . PHP_EOL; + print 'utility rebuilds the tables.' . PHP_EOL . PHP_EOL; print 'Existing precision is preserved unless --convert-ms is selected.' . PHP_EOL . PHP_EOL; print 'With preservation, rows are copied in verified batches and receive new sequence' . PHP_EOL; print 'numbers above the original high-water mark; originals are dropped only after' . PHP_EOL; print 'every batch and the row counts are verified.' . PHP_EOL . PHP_EOL; print 'Legacy DATETIME logtime columns are not converted; use --discard-data for them.' . PHP_EOL . PHP_EOL; print 'Options:' . PHP_EOL; + print ' --partition - Save and apply this Partitioning Level instead of the one in Settings.' . PHP_EOL; print ' --convert-ms - Also convert pipeline timestamps to TIMESTAMP(3); MariaDB only.' . PHP_EOL; print ' --preserve-data - Preserve existing messages (the default), copying 10,000 rows at a time.' . PHP_EOL; print ' --discard-data - Delete messages in rebuilt tables; never discard the outbox.' . PHP_EOL; @@ -749,5 +796,5 @@ function syslog_upart_display_help(): void { print 'much ADDITIONAL free disk space on the database server, plus temporary' . PHP_EOL; print 'file and binary-log headroom. Large transfers may take several hours.' . PHP_EOL . PHP_EOL; print 'On failure keep writers stopped and retry with --preserve-data and the' . PHP_EOL; - print 'same --convert-ms option, if used; originals remain in *_rebuild tables.' . PHP_EOL . PHP_EOL; + print 'same --partition and --convert-ms options, if used; originals remain in *_rebuild tables.' . PHP_EOL . PHP_EOL; } diff --git a/config.php.dist b/config.php.dist index 486d3205..283ef5df 100644 --- a/config.php.dist +++ b/config.php.dist @@ -73,9 +73,17 @@ $syslog_replication_main_db_ssl_ca = $database_ssl_ca; //$syslog_install_options['engine'] = 'innodb'; //$syslog_install_options['db_type'] = 'part'; //$syslog_install_options['days'] = '30'; +//$syslog_install_options['partitioning'] = 'day'; //$syslog_install_options['mode'] = 'install'; //$syslog_install_options['id'] = 'syslog'; +/* Partitioning level for the syslog and syslog_removed tables on a NEW install: + * 'day' - one partition per UTC day (default) + * 'hour' - one partition per UTC hour; requires a finite retention ('days'), + * and the retention and level must fit the server's partition limit + * To change an existing install, change Partitioning Level in Settings and run + * cli/upgrade_partitioning.php, or run cli/upgrade_partitioning.php --partition=hourly|daily. */ + /* Timestamp precision for the logtime column on a NEW install: * 'ms' - millisecond TIMESTAMP(3), preserves sub-second ordering (MariaDB only, default) * 'sec' - second granularity TIMESTAMP diff --git a/config_local.php.dist b/config_local.php.dist index 4dead7b2..6309eaa5 100644 --- a/config_local.php.dist +++ b/config_local.php.dist @@ -85,9 +85,17 @@ $syslog_install_options['upgrade_type'] = 'truncate'; $syslog_install_options['engine'] = 'innodb'; $syslog_install_options['db_type'] = 'part'; $syslog_install_options['days'] = '30'; +$syslog_install_options['partitioning'] = 'day'; $syslog_install_options['mode'] = 'install'; $syslog_install_options['id'] = 'syslog'; +/* Partitioning level for the syslog and syslog_removed tables on a NEW install: + * 'day' - one partition per UTC day (default) + * 'hour' - one partition per UTC hour; requires a finite retention ('days'), + * and the retention and level must fit the server's partition limit + * To change an existing install, change Partitioning Level in Settings and run + * cli/upgrade_partitioning.php, or run cli/upgrade_partitioning.php --partition=hourly|daily. */ + /* Timestamp precision for the logtime column on a NEW install: * 'ms' - millisecond TIMESTAMP(3), preserves sub-second ordering (MariaDB only, default) * 'sec' - second granularity TIMESTAMP diff --git a/includes/functions.php b/includes/functions.php index 61dc9f2a..e19d9e80 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -2452,7 +2452,7 @@ function syslog_partition_blocked_state(): array { * TABLE_ROWS is an engine estimate, so callers must not present it as an exact * count. This is read-only and safe to call while maintenance is blocked. * - * @return array + * @return array */ function syslog_partition_observability(): array { global $syslogdb_default; @@ -2465,7 +2465,8 @@ function syslog_partition_observability(): array { 'coverage_end' => '', 'partitions' => 0, 'dmax_rows' => null, - 'dmax_bytes' => null + 'dmax_bytes' => null, + 'granularity' => 'day' ]; $rows = syslog_db_fetch_assoc_prepared('SELECT partition_name, table_rows, data_length, index_length @@ -2483,8 +2484,13 @@ function syslog_partition_observability(): array { foreach ($rows as $row) { $name = (string) ($row['partition_name'] ?? $row['PARTITION_NAME'] ?? ''); - if (preg_match('/^d(\d{8})$/', $name, $matches) === 1) { - $dates[] = $matches[1]; + // Daily labels are YYYYMMDD and hourly labels YYYYMMDDHH. + if (preg_match('/^(?:d(\d{8})|h(\d{10}))$/', $name, $matches) === 1) { + $dates[] = $matches[1] !== '' ? $matches[1] : $matches[2]; + + if ($name[0] === 'h') { + $observability[$table]['granularity'] = 'hour'; + } } if ($name !== 'dMaxValue') { @@ -2603,8 +2609,11 @@ function syslog_partition_manage(): int { } } - // Bounded recovery: heal missing future partitions a few at a time. - $recovery = syslog_partition_recover('syslog', $base_time, $ahead_days); + // Bounded recovery: heal missing future partitions a few at a time. Each + // table follows its existing layout until cli/upgrade_partitioning.php + // applies a changed partitioning level. + $granularity = syslog_partition_layout_granularity('syslog'); + $recovery = syslog_partition_recover('syslog', $base_time, syslog_partition_ahead_days_for($granularity, $ahead_days), $granularity); if ($recovery['stop_reason'] === '') { $recovery_removed = syslog_partition_remove('syslog'); @@ -2612,7 +2621,8 @@ function syslog_partition_manage(): int { $recovery_removed = 0; } - $recovery2 = syslog_partition_recover('syslog_removed', $base_time, $ahead_days); + $granularity = syslog_partition_layout_granularity('syslog_removed'); + $recovery2 = syslog_partition_recover('syslog_removed', $base_time, syslog_partition_ahead_days_for($granularity, $ahead_days), $granularity); if ($recovery2['stop_reason'] === '') { $recovery2_removed = syslog_partition_remove('syslog_removed'); @@ -2637,7 +2647,7 @@ function syslog_partition_manage(): int { '%s: %s; %s; %s', $failed_table, $recovery_failed['stop_reason'], - sprintf(__('remaining partition gap: %d day(s)', 'syslog'), $recovery_failed['missing']), + sprintf(__('remaining partition gap: %d partition(s)', 'syslog'), $recovery_failed['missing']), $recovery_failed['retention_deferred'] ? __('retention pruning deferred until the future horizon is restored', 'syslog') : __('no retention deferral', 'syslog') ); @@ -2664,16 +2674,17 @@ function syslog_partition_manage(): int { * replacement window. The dMaxValue partition stays in place as the * write-path safety net the whole time. * - * @param string $table The table to maintain. - * @param int $base_time Base timestamp for the maintenance window. - * @param int $ahead_days Number of future days to maintain. + * @param string $table The table to maintain. + * @param int $base_time Base timestamp for the maintenance window. + * @param int $ahead_days Number of future days to maintain. + * @param string $granularity The table's partitioning level, 'hour' or 'day'. * * @return array{created: int, missing: int, stop_reason: string, retention_deferred: bool, dmax_risk: bool} * Recovery telemetry: partitions created this run, the remaining * gap, why the run stopped, whether retention was deferred, and * whether writes landed in dMaxValue. */ -function syslog_partition_recover($table, $base_time, $ahead_days) { +function syslog_partition_recover($table, $base_time, $ahead_days, $granularity = 'day') { $limit = read_config_option('syslog_partition_recover_limit'); if (!is_numeric($limit) || (int) $limit < 1) { @@ -2690,17 +2701,16 @@ function syslog_partition_recover($table, $base_time, $ahead_days) { $missing = 0; $stop = ''; $dmax_risk = false; - $need_ahead = 0; - - for ($day = 0; $day <= $ahead_days; $day++) { - $time = $base_time + ($day * 86400); - - if (syslog_partition_check($table, $time)) { - $need_ahead++; - } - } + $period = syslog_partition_periods()[$granularity] ?? 86400; + $steps = intdiv((int) $ahead_days * 86400, $period); - if ($need_ahead === 0) { + /* + * syslog_partition_check() compares a period's boundary with the highest + * existing one, so missing periods always form a suffix of the window. + * Checking the horizon first keeps the steady state to one check, and a + * binary search finds the first gap without probing every hourly period. + */ + if (!syslog_partition_check($table, $base_time + $steps * $period, $granularity)) { return [ 'created' => 0, 'missing' => 0, @@ -2710,12 +2720,23 @@ function syslog_partition_recover($table, $base_time, $ahead_days) { ]; } - for ($day = 0; $day <= $ahead_days; $day++) { - $time = $base_time + ($day * 86400); + $first = 0; + $last = $steps; - if (!syslog_partition_check($table, $time)) { - continue; + while ($first < $last) { + $middle = intdiv($first + $last, 2); + + if (syslog_partition_check($table, $base_time + $middle * $period, $granularity)) { + $last = $middle; + } else { + $first = $middle + 1; } + } + + $need_ahead = $steps - $first + 1; + + for ($step = $first; $step <= $steps; $step++) { + $time = $base_time + ($step * $period); if ($created >= $limit) { $missing = $need_ahead - $created; @@ -2726,7 +2747,7 @@ function syslog_partition_recover($table, $base_time, $ahead_days) { $missing = $need_ahead - $created; - if (!syslog_partition_create($table, $time, $create_reason)) { + if (!syslog_partition_create($table, $time, $create_reason, $granularity)) { $stop = $create_reason !== '' ? $create_reason : __('partition creation failed', 'syslog'); /* * When the next concrete partition is still missing, every new @@ -2775,6 +2796,162 @@ function syslog_partition_ahead_days() { return $ahead_days; } +/** + * Supported partition sizes, in seconds. + * + * @return array Partition size keyed by level. + */ +function syslog_partition_periods(): array { + return ['hour' => 3600, 'day' => 86400]; +} + +/** + * The partitioning level chosen in Settings. It is applied to the tables only + * by cli/upgrade_partitioning.php; maintenance follows the existing layout. + * + * @return string 'hour' or 'day'. + */ +function syslog_partition_requested_granularity(): string { + return read_config_option('syslog_partition_granularity') === 'hour' ? 'hour' : 'day'; +} + +/** + * The partitioning level a table actually uses, read from its partition names. + * + * @param string $table The partitioned table. + * + * @return string 'hour' when the table has hYYYYMMDDHH partitions, otherwise 'day'. + */ +function syslog_partition_layout_granularity(string $table): string { + global $syslogdb_default; + + $hourly = syslog_db_fetch_cell_prepared("SELECT COUNT(*) FROM information_schema.PARTITIONS + WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? AND PARTITION_NAME LIKE 'h%'", + [$syslogdb_default, $table]); + + return is_numeric($hourly) && (int) $hourly > 0 ? 'hour' : 'day'; +} + +/** + * Name of the UTC partition that holds a time: hYYYYMMDDHH or dYYYYMMDD. + * + * @param string $granularity 'hour' or 'day'. + * @param int $time Epoch seconds. + * + * @return string The partition name. + */ +function syslog_partition_name(string $granularity, int $time): string { + return $granularity === 'hour' ? 'h' . gmdate('YmdH', $time) : 'd' . gmdate('Ymd', $time); +} + +/** + * Days of future partitions to keep for a level. Like MacTrack, hourly tables + * keep at least two days ahead so splitting the empty dMaxValue stays instant + * even when maintenance runs late. + * + * @param string $granularity 'hour' or 'day'. + * @param int|null $ahead_days The pre-create window, or null for the setting. + * + * @return int Days ahead to pre-create. + */ +function syslog_partition_ahead_days_for(string $granularity, ?int $ahead_days = null): int { + $ahead_days = $ahead_days ?? syslog_partition_ahead_days(); + + return $granularity === 'hour' ? max(2, $ahead_days) : $ahead_days; +} + +/** + * Most partitions a table may have on a database server: 8192, or 1024 before + * MariaDB 10.0.4 and MySQL 5.7. + * + * @param string|null $version A VERSION() string, or null to query the Syslog database. + * + * @return int The partition limit, dMaxValue included. + */ +function syslog_partition_server_limit(?string $version = null): int { + $version = $version ?? (string) syslog_db_fetch_cell('SELECT VERSION()'); + $mariadb = stripos($version, 'mariadb') !== false; + + // Some clients report MariaDB as "5.5.5-10.x" for old MySQL replication peers. + if (preg_match('/^(?:5\.5\.5-)?(\d+\.\d+(?:\.\d+)?)/', $version, $match) !== 1) { + return 1024; + } + + return version_compare($match[1], $mariadb ? '10.0.4' : '5.7.0', '>=') ? 8192 : 1024; +} + +/** + * Partitions one table needs: the retained days, today, the pre-create window + * and dMaxValue. + * + * @param string $granularity 'hour' or 'day'. + * @param int $retention_days Days of retention; 0 is Indefinite. + * @param int $ahead_days The pre-create window in days. + * + * @return int|null The partition count, or null when retention is Indefinite. + */ +function syslog_partition_projected_count(string $granularity, int $retention_days, int $ahead_days): ?int { + if ($retention_days < 1) { + return null; + } + + $per_day = intdiv(86400, syslog_partition_periods()[$granularity] ?? 86400); + + return $per_day * ($retention_days + syslog_partition_ahead_days_for($granularity, $ahead_days) + 1) + 1; +} + +/** + * Explain why a partitioning level, retention and pre-create window cannot be + * used together on a server, or return an empty string when they fit. + * + * @param string $granularity 'hour' or 'day'. + * @param int|string $retention_days Days of retention; 0 is Indefinite. + * @param int|string $ahead_days The pre-create window in days. + * @param int $limit The server partition limit. + * + * @return string The error, or '' when the combination is supported. + */ +function syslog_partition_settings_error(string $granularity, $retention_days, $ahead_days, int $limit): string { + if (!isset(syslog_partition_periods()[$granularity])) { + return __('Unknown partitioning level; choose Hourly or Daily.', 'syslog'); + } + + if (!is_numeric($retention_days) || (int) $retention_days < 0) { + return __('The Syslog retention is invalid.', 'syslog'); + } + + $ahead_days = is_numeric($ahead_days) && (int) $ahead_days >= 1 && (int) $ahead_days <= 7 ? (int) $ahead_days : 3; + + if ($granularity === 'hour' && (int) $retention_days === 0) { + return __('Hourly partitioning cannot be used with Indefinite retention, which would need an unbounded number of partitions. Choose a retention period or Daily partitioning.', 'syslog'); + } + + $count = syslog_partition_projected_count($granularity, (int) $retention_days, $ahead_days); + + if ($count !== null && $count > $limit) { + return __('%s partitioning with %s days of retention and a %s day pre-create window needs %s partitions per table, which exceeds this database server\'s limit of %s partitions. Reduce the retention, shorten the pre-create window, or choose Daily partitioning.', + $granularity === 'hour' ? __('Hourly', 'syslog') : __('Daily', 'syslog'), + number_format((int) $retention_days), number_format(syslog_partition_ahead_days_for($granularity, $ahead_days)), + number_format($count), number_format($limit), 'syslog'); + } + + return ''; +} + +/** + * Upper logtime bound added to every message query. Messages cannot be newer + * than now, so the empty future partitions are always pruned, whatever the + * filter's own range says. + * + * @param int|null $now The current time, for tests. + * + * @return string The SQL condition. + */ +function syslog_logtime_upper_bound_sql(?int $now = null): string { + // Exclusive bound one second ahead keeps fractional seconds of the current second. + return 'logtime < FROM_UNIXTIME(' . (($now ?? time()) + 1) . ')'; +} + /** * Validate tables that support partition maintenance. * @@ -3049,7 +3226,7 @@ function syslog_partition_report_state($table) { $valid = false; } - if (!is_string($name) || preg_match('/^(d\d{8}|dMaxValue)$/', $name) !== 1) { + if (!is_string($name) || preg_match('/^(d\d{8}|h\d{10}|dMaxValue)$/', $name) !== 1) { cacti_log("SYSLOG WARNING: Partition metadata for '$table' contains unexpected partition name '$name'", false, 'SYSLOG'); $valid = false; } @@ -3091,13 +3268,14 @@ function syslog_partition_report_state($table) { /** * Create a new partition for the specified table. * - * @param mixed $table The table to rotate. - * @param int $time Assume this time for the partition rotation. - * @param string|null $reason Receives an overflow deferral reason, otherwise an empty string. + * @param mixed $table The table to rotate. + * @param int $time Assume this time for the partition rotation. + * @param string|null $reason Receives an overflow deferral reason, otherwise an empty string. + * @param string|null $granularity The table's partitioning level, or null to read it. * * @return bool true on success, false on lock failure or disallowed table. */ -function syslog_partition_create($table, $time = null, &$reason = null) { +function syslog_partition_create($table, $time = null, &$reason = null, $granularity = null) { global $syslogdb_default; $reason = ''; @@ -3156,11 +3334,13 @@ function syslog_partition_create($table, $time = null, &$reason = null) { /* * Boundary arithmetic is done in PHP against the UTC epoch so the * result is independent of both the PHP and MySQL session time zones. - * $boundary_epoch is the next UTC midnight strictly after $time; it - * becomes the VALUES LESS THAN literal for UNIX_TIMESTAMP partitions - * and the source for the date string passed to TO_DAYS. + * $boundary_epoch is the next UTC hour or midnight strictly after + * $time; it becomes the VALUES LESS THAN literal for UNIX_TIMESTAMP + * partitions and the source for the date string passed to TO_DAYS. */ - $boundary_epoch = (intdiv($time, 86400) + 1) * 86400; + $granularity = $granularity ?? syslog_partition_layout_granularity($table); + $period = syslog_partition_periods()[$granularity] ?? 86400; + $boundary_epoch = (intdiv($time, $period) + 1) * $period; if ($boundary_epoch <= 0 || $boundary_epoch <= $time) { cacti_log("SYSLOG ERROR: Boundary epoch computation failed for '$table' (time=$time); leaving writes in dMaxValue until maintenance recovers", false, 'SYSLOG'); @@ -3168,10 +3348,10 @@ function syslog_partition_create($table, $time = null, &$reason = null) { return false; } - $cformat = 'd' . gmdate('Ymd', $time); + $cformat = syslog_partition_name($granularity, $time); $boundary_date = gmdate('Y-m-d', $boundary_epoch); - if (preg_match('/^d\d{8}$/', $cformat) !== 1 || preg_match('/^\d{4}-\d{2}-\d{2}$/', $boundary_date) !== 1) { + if (preg_match('/^(d\d{8}|h\d{10})$/', $cformat) !== 1 || preg_match('/^\d{4}-\d{2}-\d{2}$/', $boundary_date) !== 1) { cacti_log("SYSLOG ERROR: Derived partition values failed format validation for '$table'; leaving writes in dMaxValue until maintenance recovers", false, 'SYSLOG'); return false; @@ -3270,7 +3450,7 @@ function syslog_partition_create($table, $time = null, &$reason = null) { * syslog_partition_table_allowed() (two-value allowlist plus * regex guard). $cformat, $boundary_epoch, and $boundary_date * derive from integer arithmetic and gmdate(), so they contain - * only digits, hyphens, and the letter 'd'. + * only digits, hyphens, and the letter 'd' or 'h'. */ $create_syntax = syslog_db_fetch_row_prepared("SHOW CREATE TABLE `$syslogdb_default`.`$table`"); @@ -3282,7 +3462,7 @@ function syslog_partition_create($table, $time = null, &$reason = null) { $create_sql = $create_syntax['Create Table']; - if (stripos($create_sql, 'TO_DAYS') !== false) { + if (stripos($create_sql, 'TO_DAYS') !== false && $granularity === 'day') { $altered = syslog_db_execute_prepared("ALTER TABLE `$syslogdb_default`.`$table` REORGANIZE PARTITION dMaxValue INTO ( PARTITION $cformat VALUES LESS THAN (TO_DAYS('$boundary_date')), PARTITION dMaxValue VALUES LESS THAN MAXVALUE)", []); @@ -3443,12 +3623,13 @@ function syslog_partition_remove($table) { * * The untyped signature is intentional: security tests assert it exactly. * - * @param string $table The table to check - * @param int|null $time The time to assume for creation verification + * @param string $table The table to check + * @param int|null $time The time to assume for creation verification + * @param string|null $granularity The table's partitioning level, or null to read it * * @return bool If it's time to rotate the partition */ -function syslog_partition_check($table, $time = null) { +function syslog_partition_check($table, $time = null, $granularity = null) { global $syslogdb_default; if (!syslog_partition_table_allowed($table)) { @@ -3463,6 +3644,9 @@ function syslog_partition_check($table, $time = null) { $time = time() + 3600; } + $granularity = $granularity ?? syslog_partition_layout_granularity($table); + $period = syslog_partition_periods()[$granularity] ?? 86400; + /* * Decide whether a concrete partition still needs to be created for * $time by comparing boundaries, not partition names. A legacy or @@ -3485,7 +3669,7 @@ function syslog_partition_check($table, $time = null) { LIMIT 1', [$syslogdb_default, $table, 'MAXVALUE']); - $boundary_epoch = (intdiv((int) $time, 86400) + 1) * 86400; + $boundary_epoch = (intdiv((int) $time, $period) + 1) * $period; if (stripos((string) $partition_expression, 'to_days') !== false) { $target_boundary = syslog_db_fetch_cell_prepared('SELECT TO_DAYS(?)', [gmdate('Y-m-d', $boundary_epoch)]); diff --git a/includes/installer.php b/includes/installer.php index 112fa1c9..393601e3 100644 --- a/includes/installer.php +++ b/includes/installer.php @@ -120,17 +120,14 @@ function syslog_install_advisor(int $syslog_exists, array $submitted_options = [ ] ]; - $fields_syslog_update['dayparts'] = [ + $fields_syslog_update['partitioning'] = [ 'method' => 'drop_array', - 'friendly_name' => __('Partitions per Day', 'syslog'), - 'description' => __('Select the number of partitions per day that you wish to create.', 'syslog'), - 'value' => '1', + 'friendly_name' => __('Partitioning Level', 'syslog'), + 'description' => __('The size of each partition of the syslog and syslog_removed tables. Daily is recommended. Hourly requires a finite retention, and the retention and partitioning level together must fit the database server\'s partition limit. To change the level later, change it in Settings and run cli/upgrade_partitioning.php.', 'syslog'), + 'value' => isset($submitted_options['partitioning']) ? $submitted_options['partitioning'] : 'day', 'array' => [ - '1' => __('%d Per Day', 1, 'syslog'), - '2' => __('%d Per Day', 2, 'syslog'), - '4' => __('%d Per Day', 4, 'syslog'), - '6' => __('%d Per Day', 6, 'syslog'), - '12' => __('%d Per Day', 12, 'syslog') + 'day' => __('Daily', 'syslog'), + 'hour' => __('Hourly', 'syslog') ] ]; diff --git a/includes/schema.php b/includes/schema.php index 6003f7d6..53bbc178 100644 --- a/includes/schema.php +++ b/includes/schema.php @@ -571,10 +571,11 @@ function syslog_resolve_logtime_precision(string $requested = ''): string { * ahead of today, or false when unconfigured. * @param string $precision The logtime precision, 'ms' for TIMESTAMP(3) * or 'sec' for TIMESTAMP (the default). + * @param string $granularity The partitioning level, 'hour' or 'day' (the default). * * @return void */ -function syslog_create_partitioned_syslog_table($engine = 'InnoDB', $days = 30, $ahead_days = 3, $precision = 'sec') { +function syslog_create_partitioned_syslog_table($engine = 'InnoDB', $days = 30, $ahead_days = 3, $precision = 'sec', $granularity = 'day') { global $config, $syslogdb_default, $syslog_levels; syslog_connect(); @@ -641,26 +642,29 @@ function syslog_create_partitioned_syslog_table($engine = 'InnoDB', $days = 30, /* * Partition boundaries are integer epochs computed in PHP and injected * as numeric literals. This keeps both MySQL and PHP session time zones - * out of the equation: the boundary is always the next UTC midnight - * after the labeled day. + * out of the equation: each boundary is the next UTC hour or midnight + * after the labeled period. * - * $days counts today as one of the retained days, so the historical - * loop bound is $days - 1 (today plus $days - 1 prior days = $days - * total). This matches the UTC calendar cutoff in syslog_partition_remove(), - * so the initial oldest partition still contains retained events. + * $days counts today as one of the retained days, so history starts at + * UTC midnight $days - 1 days ago (today plus $days - 1 prior days = + * $days total). This matches the UTC calendar cutoff in + * syslog_partition_remove(), so the initial oldest partition still + * contains retained events. * * $days = 0 is a valid "Indefinite" retention setting (never pruned by - * syslog_partition_remove(), which only prunes when $days > 0), but - * $days - 1 would then be -1 and skip creating today's concrete - * partition entirely. Clamp the starting bound to 0 so today is always - * created regardless of retention. + * syslog_partition_remove(), which only prunes when $days > 0); history + * then starts today, so today's concrete partitions always exist. + * Hourly tables keep at least two days ahead, like maintenance does. */ - for ($i = max($days - 1, 0); $i >= (0 - $ahead_days); $i--) { - $day_epoch = $now - ($i * 86400); - $boundary_epoch = (intdiv($day_epoch, 86400) + 1) * 86400; - $format = gmdate('Ymd', $day_epoch); + $period = $granularity === 'hour' ? 3600 : 86400; + $ahead = $granularity === 'hour' ? max(2, $ahead_days) : $ahead_days; + $today = intdiv($now, 86400) * 86400; + $current = intdiv($now, $period) * $period; - $parts .= ($parts !== '' ? ",\n" : '(') . ' PARTITION d' . $format . ' VALUES LESS THAN (' . $boundary_epoch . ')'; + for ($start = $today - max($days - 1, 0) * 86400; $start <= $current + $ahead * 86400; $start += $period) { + $name = $granularity === 'hour' ? 'h' . gmdate('YmdH', $start) : 'd' . gmdate('Ymd', $start); + + $parts .= ($parts !== '' ? ",\n" : '(') . ' PARTITION ' . $name . ' VALUES LESS THAN (' . ($start + $period) . ')'; } $parts .= ",\nPARTITION dMaxValue VALUES LESS THAN MAXVALUE);"; @@ -765,12 +769,15 @@ function syslog_setup_table_new(array $options, bool $repair = false): void { $logtime_col = syslog_logtime_sql_type($precision); // set table construction settings for the remote pollers + $granularity = ($options['partitioning'] ?? read_config_option('syslog_partition_granularity')) === 'hour' ? 'hour' : 'day'; + if (!$repair) { set_config_option('syslog_install_upgrade_type', empty($options['upgrade_type']) ? '' : $options['upgrade_type'], true); set_config_option('syslog_install_engine', $engine, true); set_config_option('syslog_install_db_type', empty($options['db_type']) ? '' : $options['db_type'], true); set_config_option('syslog_install_days', empty($options['days']) ? '' : $options['days'], true); set_config_option('syslog_install_logtime_precision', $precision, true); + set_config_option('syslog_partition_granularity', $granularity, true); } if ($truncate) { @@ -779,7 +786,7 @@ function syslog_setup_table_new(array $options, bool $repair = false): void { // The syslog table is created partitioned; the helper also selects the // matching ROW_FORMAT for the chosen engine. - syslog_create_partitioned_syslog_table($engine, $options['days'], read_config_option('syslog_partition_ahead_days'), $precision); + syslog_create_partitioned_syslog_table($engine, $options['days'], read_config_option('syslog_partition_ahead_days'), $precision, $granularity); syslog_db_execute("CREATE TABLE IF NOT EXISTS `$syslogdb_default`.`syslog_alert` ( `id` int(10) NOT NULL auto_increment, diff --git a/includes/settings.php b/includes/settings.php index c3c547da..49857b6a 100644 --- a/includes/settings.php +++ b/includes/settings.php @@ -170,7 +170,7 @@ function syslog_config_settings(): void { 'array' => $formats ], 'syslog_retention_header' => [ - 'friendly_name' => __('Data Retention Settings', 'syslog'), + 'friendly_name' => __('Data Retention and Partitioning', 'syslog'), 'method' => 'spacer', 'collapsible' => 'true' ], @@ -181,9 +181,19 @@ function syslog_config_settings(): void { 'default' => '30', 'array' => $syslog_retentions ], + 'syslog_partition_granularity' => [ + 'friendly_name' => __('Partitioning Level', 'syslog'), + 'description' => __('The size of each partition of the syslog and syslog_removed tables. A change is saved here but takes effect only when cli/upgrade_partitioning.php rebuilds both tables; until then, partition maintenance keeps the existing size. Hourly partitioning requires a finite retention. The retention, pre-create window and partitioning level together must fit the database server\'s partition limit, or the settings are not saved.', 'syslog'), + 'method' => 'drop_array', + 'default' => 'day', + 'array' => [ + 'day' => __('Daily', 'syslog'), + 'hour' => __('Hourly', 'syslog') + ] + ], 'syslog_partition_ahead_days' => [ 'friendly_name' => __('Partition Pre-create Window', 'syslog'), - 'description' => __('This is the number of future daily partitions to maintain for partitioned Syslog tables.', 'syslog'), + 'description' => __('This is the number of days of future partitions to maintain for partitioned Syslog tables, so that new partitions are always split from an empty dMaxValue partition. Hourly partitioning always keeps at least 2 days.', 'syslog'), 'method' => 'drop_array', 'default' => '3', 'array' => [ @@ -389,6 +399,79 @@ function syslog_config_settings(): void { } else { $settings['syslog'] = $temp; } + + if (get_nfilter_request_var('tab') === 'syslog' && basename((string) ($_SERVER['SCRIPT_NAME'] ?? '')) === 'settings.php' && syslog_config_safe()) { + if (get_nfilter_request_var('action') === 'save') { + if (syslog_settings_partition_gate() !== '') { + header('Location: settings.php?tab=syslog'); + exit; + } + } else { + syslog_settings_partition_pending($settings['syslog']); + } + } +} + +/** + * Refuse a Syslog Settings save whose partitioning level, retention and + * pre-create window exceed the database server's partition limit. Cacti runs + * the config_settings hook before settings.php writes anything, on 1.2 and 1.3. + * + * @param string|null $version The database VERSION(), or null to query it. + * + * @return string The reported error, or '' when the save may proceed. + */ +function syslog_settings_partition_gate(?string $version = null): string { + if (($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST') { + return ''; + } + + // syslog_connect() loads the helpers even when the Syslog database is down. + if ($version === null) { + $version = (string) (syslog_connect() ? syslog_db_fetch_cell('SELECT VERSION()') : db_fetch_cell('SELECT VERSION()')); + } + + $error = syslog_partition_settings_error( + get_nfilter_request_var('syslog_partition_granularity') === 'hour' ? 'hour' : 'day', + get_nfilter_request_var('syslog_retention'), + get_nfilter_request_var('syslog_partition_ahead_days'), + syslog_partition_server_limit($version) + ); + + if ($error === '') { + return ''; + } + + foreach (['syslog_retention', 'syslog_partition_granularity', 'syslog_partition_ahead_days'] as $field) { + $_SESSION['sess_error_fields'][$field] = $field; + } + + raise_message('syslog_partition_limit', $error . ' ' . __('The Syslog settings were not saved.', 'syslog'), MESSAGE_LEVEL_ERROR); + + return $error; +} + +/** + * Note on the Partitioning Level setting when the tables still use the other + * level, because only cli/upgrade_partitioning.php applies a change. + * + * @param array> $fields The Syslog settings definitions. + * + * @return void + */ +function syslog_settings_partition_pending(array &$fields): void { + if (!isset($fields['syslog_partition_granularity']) || !syslog_connect() || !syslog_db_table_exists('syslog', false)) { + return; + } + + $active = syslog_partition_layout_granularity('syslog'); + + if ($active === syslog_partition_requested_granularity()) { + return; + } + + $fields['syslog_partition_granularity']['description'] .= ' ' . __('Pending: the tables still use %s partitions. Run cli/upgrade_partitioning.php to apply the selected level.', + $active === 'hour' ? __('Hourly', 'syslog') : __('Daily', 'syslog'), 'syslog'); } /** diff --git a/locales/po/cacti.pot b/locales/po/cacti.pot index 6f155ee0..8cddaf0d 100644 --- a/locales/po/cacti.pot +++ b/locales/po/cacti.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: Cacti \n" "Report-Msgid-Bugs-To: developers@cacti.net\n" -"POT-Creation-Date: 2026-11-19 16:30+0000\n" +"POT-Creation-Date: 2026-11-20 10:08-0500\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: Cacti Developers >\n" "Language-Team: Cacti Developers \n" @@ -260,7 +260,7 @@ msgstr "" #: includes/functions.php #, php-format -msgid "remaining partition gap: %d day(s)" +msgid "remaining partition gap: %d partition(s)" msgstr "" #: includes/functions.php @@ -283,6 +283,33 @@ msgstr "" msgid "partition creation failed" msgstr "" +#: includes/functions.php +msgid "Unknown partitioning level; choose Hourly or Daily." +msgstr "" + +#: includes/functions.php +msgid "The Syslog retention is invalid." +msgstr "" + +#: includes/functions.php +msgid "Hourly partitioning cannot be used with Indefinite retention, which would need an unbounded number of partitions. Choose a retention period or Daily partitioning." +msgstr "" + +#: includes/functions.php +#, php-format +msgid "%s partitioning with %s days of retention and a %s day pre-create window needs %s partitions per table, which exceeds this database server's limit of %s partitions. Reduce the retention, shorten the pre-create window, or choose Daily partitioning." +msgstr "" + +#: includes/functions.php includes/installer.php includes/settings.php +#: syslog.php +msgid "Daily" +msgstr "" + +#: includes/functions.php includes/installer.php includes/settings.php +#: syslog.php +msgid "Hourly" +msgstr "" + #: includes/functions.php msgid "Automatic partition recovery deferred: dMaxValue size is unavailable; operator intervention required." msgstr "" @@ -625,17 +652,12 @@ msgstr "" msgid "Choose how many days of Syslog values you wish to maintain in the database." msgstr "" -#: includes/installer.php -msgid "Partitions per Day" -msgstr "" - -#: includes/installer.php -msgid "Select the number of partitions per day that you wish to create." +#: includes/installer.php includes/settings.php +msgid "Partitioning Level" msgstr "" #: includes/installer.php -#, php-format -msgid "%d Per Day" +msgid "The size of each partition of the syslog and syslog_removed tables. Daily is recommended. Hourly requires a finite retention, and the retention and partitioning level together must fit the database server's partition limit. To change the level later, change it in Settings and run cli/upgrade_partitioning.php." msgstr "" #: includes/installer.php @@ -974,7 +996,7 @@ msgid "Choose the custom html wrapper and CSS file to use. This file contains b msgstr "" #: includes/settings.php -msgid "Data Retention Settings" +msgid "Data Retention and Partitioning" msgstr "" #: includes/settings.php @@ -985,12 +1007,16 @@ msgstr "" msgid "This is the number of days to keep events." msgstr "" +#: includes/settings.php +msgid "The size of each partition of the syslog and syslog_removed tables. A change is saved here but takes effect only when cli/upgrade_partitioning.php rebuilds both tables; until then, partition maintenance keeps the existing size. Hourly partitioning requires a finite retention. The retention, pre-create window and partitioning level together must fit the database server's partition limit, or the settings are not saved." +msgstr "" + #: includes/settings.php msgid "Partition Pre-create Window" msgstr "" #: includes/settings.php -msgid "This is the number of future daily partitions to maintain for partitioned Syslog tables." +msgid "This is the number of days of future partitions to maintain for partitioned Syslog tables, so that new partitions are always split from an empty dMaxValue partition. Hourly partitioning always keeps at least 2 days." msgstr "" #: includes/settings.php syslog_alerts.php @@ -1185,6 +1211,15 @@ msgstr "" msgid "%d second" msgstr "" +#: includes/settings.php +msgid "The Syslog settings were not saved." +msgstr "" + +#: includes/settings.php +#, php-format +msgid "Pending: the tables still use %s partitions. Run cli/upgrade_partitioning.php to apply the selected level." +msgstr "" + #: includes/settings.php lib/syslog_dashboard.php syslog.php #: syslog_dashboards.php syslog_device_rules.php syslog_recommendations.php #: syslog_removal.php syslog_saved_searches.php @@ -1199,9 +1234,8 @@ msgstr "" msgid "Enable" msgstr "" -#: includes/settings.php syslog.php syslog_dashboards.php -#: syslog_device_rules.php syslog_recommendations.php syslog_removal.php -#: syslog_saved_searches.php +#: includes/settings.php syslog_dashboards.php syslog_device_rules.php +#: syslog_recommendations.php syslog_removal.php syslog_saved_searches.php msgid "Export" msgstr "" @@ -1280,10 +1314,6 @@ msgstr "" msgid "SQL Expression" msgstr "" -#: includes/settings.php -msgid "Daily" -msgstr "" - #: includes/settings.php msgid "Weekly" msgstr "" @@ -1317,12 +1347,12 @@ msgid "Recommendations" msgstr "" #: includes/utilities.php -#, php-format -msgid "There were %s Device records removed from the Syslog database" +msgid "Unable to connect to the Syslog database. No Device records were removed." msgstr "" #: includes/utilities.php -msgid "Unable to connect to the Syslog database. No Device records were removed." +#, php-format +msgid "There were %s Device records removed from the Syslog database" msgstr "" #: includes/utilities.php @@ -2039,6 +2069,15 @@ msgstr "" msgid "dMaxValue size" msgstr "" +#: syslog.php +msgid "Partitioning level" +msgstr "" + +#: syslog.php +#, php-format +msgid "%s is selected in Settings and takes effect after cli/upgrade_partitioning.php rebuilds the tables." +msgstr "" + #: syslog.php msgid "Last attempt" msgstr "" @@ -2463,14 +2502,6 @@ msgstr "" msgid "Delete the selected saved search?" msgstr "" -#: syslog.php -msgid "Save Search As" -msgstr "" - -#: syslog.php -msgid "Apply this search when you open this page and no search is active yet" -msgstr "" - #: syslog.php syslog_alerts.php syslog_dashboards.php syslog_device_rules.php #: syslog_recommendations.php syslog_removal.php syslog_reports.php #: syslog_saved_searches.php @@ -2503,6 +2534,14 @@ msgstr "" msgid "Save view defaults" msgstr "" +#: syslog.php +msgid "Save Search As" +msgstr "" + +#: syslog.php +msgid "Apply this search when you open this page and no search is active yet" +msgstr "" + #: syslog.php msgid "Breach" msgstr "" diff --git a/setup.php b/setup.php index b32f8a0a..1582b9b2 100644 --- a/setup.php +++ b/setup.php @@ -131,6 +131,7 @@ function syslog_execute_update(int $syslog_exists, array $options): void { require_once(__DIR__ . '/includes/installer.php'); require_once(__DIR__ . '/includes/schema.php'); + require_once(__DIR__ . '/includes/functions.php'); if (isset($options['cancel'])) { header('Location:' . $config['url_path'] . 'plugins.php?mode=uninstall&id=syslog&uninstall&uninstall_method=all'); @@ -155,6 +156,26 @@ function syslog_execute_update(int $syslog_exists, array $options): void { $requested_engine = isset($options['engine']) ? $options['engine'] : 'InnoDB'; $compatibility_options = $options; + if (isset($options['partitioning']) || isset($options['days'])) { + $partition_error = syslog_partition_settings_error( + isset($options['partitioning']) ? (string) $options['partitioning'] : syslog_partition_requested_granularity(), + $options['days'] ?? read_config_option('syslog_retention'), + read_config_option('syslog_partition_ahead_days'), + syslog_partition_server_limit() + ); + + if ($partition_error !== '') { + raise_message('syslog_error', $partition_error, MESSAGE_LEVEL_ERROR); + cacti_log('SYSLOG ERROR: Install rejected: ' . $partition_error, false, 'SYSLOG'); + + if (isset($options['install'])) { + syslog_install_advisor($syslog_exists, $options); + } + + return; + } + } + if (isset($options['install'])) { foreach ($remote_options as $option) { $compatibility_options[$option] = isset($options[$option]) && $options[$option] === 'on' ? 'on' : ''; diff --git a/syslog.php b/syslog.php index c4aeb2ed..6d23002f 100644 --- a/syslog.php +++ b/syslog.php @@ -743,13 +743,13 @@ function syslog_status_format_bytes($bytes): string { return number_format((float) $bytes, $unit === 0 ? 0 : 2) . ' ' . $units[$unit]; } -/** Format a dYYYYMMDD partition label as an ISO date. */ +/** Format a dYYYYMMDD or hYYYYMMDDHH partition label as an ISO date or hour. */ function syslog_status_format_partition_date(string $date): string { - if (preg_match('/^\d{8}$/', $date) !== 1) { + if (preg_match('/^\d{8}(\d{2})?$/', $date, $match) !== 1) { return __('Unavailable', 'syslog'); } - return substr($date, 0, 4) . '-' . substr($date, 4, 2) . '-' . substr($date, 6, 2); + return substr($date, 0, 4) . '-' . substr($date, 4, 2) . '-' . substr($date, 6, 2) . (isset($match[1]) ? ' ' . $match[1] . ':00' : ''); } /** Return a readable summary of the persisted per-table recovery state. */ @@ -978,6 +978,16 @@ function syslog_status_card_body(string $key): string { print ''; } print '
' . html_escape($table) . '' . html_escape($coverage) . '' . html_escape(isset($health['dmax_rows']) && $health['dmax_rows'] !== null ? number_format((float) $health['dmax_rows']) : __('Unavailable', 'syslog')) . '' . html_escape(syslog_status_format_bytes($health['dmax_bytes'] ?? null)) . '
'; + $levels = ['hour' => __('Hourly', 'syslog'), 'day' => __('Daily', 'syslog')]; + $active = $partition_health['syslog']['granularity'] ?? 'day'; + $requested = syslog_partition_requested_granularity(); + print '

' . __esc('Partitioning level', 'syslog') . ': ' . html_escape($levels[$active]); + + if ($requested !== $active) { + print ' — ' . html_escape(__('%s is selected in Settings and takes effect after cli/upgrade_partitioning.php rebuilds the tables.', $levels[$requested], 'syslog')); + } + + print '

'; break; case 'maintenance': @@ -2122,6 +2132,10 @@ function get_syslog_messages(string &$sql_where, $rows, string $tab): array { $sql_where .= ($sql_where == '' ? 'WHERE ' : ' AND ') . 'syslog.priority_id ' . $priorities; } + if ($tab == 'syslog') { + $sql_where .= ($sql_where == '' ? 'WHERE ' : ' AND ') . syslog_logtime_upper_bound_sql(); + } + $sql_where = api_plugin_hook_function('syslog_sqlwhere', $sql_where); $sql_order = get_order_string(); diff --git a/tests/Security/LogicalMessageSearchTest.php b/tests/Security/LogicalMessageSearchTest.php index 5ac568c8..b1108134 100644 --- a/tests/Security/LogicalMessageSearchTest.php +++ b/tests/Security/LogicalMessageSearchTest.php @@ -237,7 +237,8 @@ get_syslog_messages($sql_where, 20, 'syslog'); - expect(str_contains($sql_where, 'logtime'))->toBeFalse('Removing date conditions removes time restrictions'); + // Only the automatic upper bound remains, so future partitions stay pruned. + expect(preg_match('/^WHERE logtime < FROM_UNIXTIME\(\d+\)$/', $sql_where))->toBe(1, 'Removing date conditions leaves only the upper bound'); $GLOBALS['syslog_search_error'] = 'Invalid logical search'; diff --git a/tests/Security/PartitionTableLockingTest.php b/tests/Security/PartitionTableLockingTest.php index 82b9a829..4c6959fe 100644 --- a/tests/Security/PartitionTableLockingTest.php +++ b/tests/Security/PartitionTableLockingTest.php @@ -213,10 +213,10 @@ } // Boundary math may be expressed in SQL (strtotime()/UNIX_TIMESTAMP()) or, - // as of the UTC epoch rewrite, in PHP via intdiv() against 86400 seconds. - // Match the actual operands so an unrelated intdiv() elsewhere in the - // function body can't satisfy this check. - if (!preg_match('/(?:strtotime\s*\(|UNIX_TIMESTAMP\s*\(|intdiv\s*\(\s*\$time\s*,\s*86400\s*\))/', $create_body)) { + // as of the UTC epoch rewrite, in PHP via intdiv() against the hourly or + // daily period. Match the actual operands so an unrelated intdiv() + // elsewhere in the function body can't satisfy this check. + if (!preg_match('/(?:strtotime\s*\(|UNIX_TIMESTAMP\s*\(|intdiv\s*\(\s*\$time\s*,\s*(?:86400|\$period)\s*\))/', $create_body)) { throw new RuntimeException('syslog_partition_create is missing partition boundary computation logic.'); } diff --git a/tests/Unit/PartitionGranularityTest.php b/tests/Unit/PartitionGranularityTest.php new file mode 100644 index 00000000..d8562806 --- /dev/null +++ b/tests/Unit/PartitionGranularityTest.php @@ -0,0 +1,153 @@ + 8192, + '5.5.5-10.0.3-MariaDB-log' => 1024, + '10.0.4-MariaDB' => 8192, + '8.0.36' => 8192, + '5.7.44-log' => 8192, + '5.6.51' => 1024, + 'unknown' => 1024 + ] as $version => $limit) { + expect(syslog_partition_server_limit($version))->toBe($limit, $version); + } +}); + +it('rejects partitioning combinations above the partition limit or unbounded hourly growth', function () { + syslog_load_plugin_source('includes/functions.php'); + + expect(syslog_partition_name('hour', gmmktime(13, 5, 0, 11, 20, 2026)))->toBe('h2026112013') + ->and(syslog_partition_name('day', gmmktime(13, 5, 0, 11, 20, 2026)))->toBe('d20261120') + ->and(syslog_partition_ahead_days_for('hour', 1))->toBe(2) + ->and(syslog_partition_ahead_days_for('day', 1))->toBe(1) + ->and(syslog_partition_projected_count('day', 30, 3))->toBe(35) + ->and(syslog_partition_projected_count('hour', 30, 1))->toBe(24 * 33 + 1) + ->and(syslog_partition_projected_count('hour', 0, 3))->toBeNull() + ->and(syslog_partition_settings_error('day', '30', '3', 1024))->toBe('') + ->and(syslog_partition_settings_error('day', '0', '3', 1024))->toBe('') + ->and(syslog_partition_settings_error('hour', '183', '3', 8192))->toBe('') + ->and(syslog_partition_settings_error('hour', '365', '3', 8192))->toContain('8,857 partitions')->toContain('limit of 8,192') + ->and(syslog_partition_settings_error('hour', '60', '3', 1024))->toContain('1,537 partitions') + ->and(syslog_partition_settings_error('hour', '0', '3', 8192))->toContain('Indefinite') + ->and(syslog_partition_settings_error('day', '', '3', 8192))->toContain('retention is invalid') + ->and(syslog_partition_settings_error('week', '30', '3', 8192))->not->toBe(''); +}); + +it('refuses to save Syslog settings that exceed the partition limit', function () { + syslog_load_plugin_source('includes/settings.php'); + + $method = $_SERVER['REQUEST_METHOD'] ?? null; + $messages = []; + $_SERVER['REQUEST_METHOD'] = 'POST'; + $_SESSION['sess_error_fields'] = []; + test_override('raise_message', function ($id, $text, $level) use (&$messages) { + $messages[] = [$id, $text, $level]; + }); + + try { + $GLOBALS['request'] = ['syslog_partition_granularity' => 'hour', 'syslog_retention' => '365', 'syslog_partition_ahead_days' => '3']; + + expect(syslog_settings_partition_gate('10.11.6-MariaDB'))->toContain('8,857 partitions') + ->and($messages)->toHaveCount(1) + ->and($messages[0][0])->toBe('syslog_partition_limit') + ->and($messages[0][1])->toContain('The Syslog settings were not saved.') + ->and($messages[0][2])->toBe(MESSAGE_LEVEL_ERROR) + ->and($_SESSION['sess_error_fields'])->toHaveKeys(['syslog_retention', 'syslog_partition_granularity']); + + $GLOBALS['request']['syslog_retention'] = '60'; + expect(syslog_settings_partition_gate('10.11.6-MariaDB'))->toBe('') + ->and(syslog_settings_partition_gate('10.0.3-MariaDB'))->toContain('limit of 1,024'); + + $_SERVER['REQUEST_METHOD'] = 'GET'; + expect(syslog_settings_partition_gate('10.0.3-MariaDB'))->toBe(''); + } finally { + if ($method === null) { + unset($_SERVER['REQUEST_METHOD']); + } else { + $_SERVER['REQUEST_METHOD'] = $method; + } + + $GLOBALS['request'] = []; + $_SESSION['sess_error_fields'] = []; + } +}); + +it('recovers hourly partitions in bounded runs and checks only the horizon once it exists', function () { + syslog_load_plugin_source('includes/functions.php'); + + $GLOBALS['syslogdb_default'] = 'syslog'; + $state = (object) ['max' => gmmktime(12, 0, 0, 11, 20, 2026), 'created' => [], 'max_reads' => 0, 'limit' => '3']; + + test_override('read_config_option', fn($name) => $name === 'syslog_partition_recover_limit' ? $state->limit : ''); + test_override('syslog_db_fetch_cell_prepared', function ($sql) use ($state) { + if (str_contains($sql, 'GET_LOCK') || str_contains($sql, 'RELEASE_LOCK')) { + return 1; + } + + if (str_contains($sql, 'MAX(CAST(partition_description')) { + $state->max_reads++; + + return $state->max; + } + + return str_contains($sql, 'SELECT partition_expression') ? 'unix_timestamp(`logtime`)' : ''; + }); + test_override('syslog_db_fetch_row_prepared', function ($sql) use ($state) { + return match (true) { + str_contains($sql, 'AS max_boundary') => ['max_boundary' => $state->max, 'partition_expression' => 'unix_timestamp(`logtime`)'], + str_contains($sql, 'AS overflow_rows') => ['overflow_rows' => 0, 'overflow_bytes' => 0], + str_contains($sql, 'SHOW CREATE TABLE') => ['Create Table' => 'PARTITION BY RANGE (UNIX_TIMESTAMP(logtime))'], + default => [] + }; + }); + test_override('syslog_db_execute_prepared', function ($sql) use ($state) { + if (preg_match('/PARTITION (h\d{10}) VALUES LESS THAN \((\d+)\)/', $sql, $match)) { + $state->created[] = $match[1]; + $state->max = (int) $match[2]; + } + + return true; + }); + + $base = gmmktime(10, 30, 0, 11, 20, 2026); + $run = syslog_partition_recover('syslog', $base, 2, 'hour'); + + // Hours through 11:00 exist; the two-day horizon ends at 2026-11-22 10:00. + expect($state->created)->toBe(['h2026112012', 'h2026112013', 'h2026112014']) + ->and($run['created'])->toBe(3) + ->and($run['missing'])->toBe(44) + ->and($run['retention_deferred'])->toBeTrue(); + + $state->limit = '31'; + $state->created = []; + + while (($run = syslog_partition_recover('syslog', $base, 2, 'hour'))['missing'] > 0) { + expect($run['created'])->toBe(31); + } + + expect(end($state->created))->toBe('h2026112210') + ->and($run['stop_reason'])->toBe(''); + + $state->created = []; + $state->max_reads = 0; + $run = syslog_partition_recover('syslog', $base, 2, 'hour'); + + expect($run['created'])->toBe(0) + ->and($state->created)->toBe([]) + ->and($state->max_reads)->toBe(1); +}); diff --git a/tests/Unit/PartitionObservabilityDetailTest.php b/tests/Unit/PartitionObservabilityDetailTest.php index 970c9c25..ebe35dbb 100644 --- a/tests/Unit/PartitionObservabilityDetailTest.php +++ b/tests/Unit/PartitionObservabilityDetailTest.php @@ -63,4 +63,23 @@ expect($result['syslog']['dmax_rows'])->toBeNull() ->and($result['syslog']['dmax_bytes'])->toBeNull(); +}); + +it('reports hourly coverage and the partitioning level from hourly partition names', function () { + syslog_load_plugin_source('includes/functions.php'); + + $GLOBALS['syslogdb_default'] = 'status_test'; + + test_override('syslog_db_fetch_assoc_prepared', fn() => [ + ['PARTITION_NAME' => 'h2026092022'], + ['PARTITION_NAME' => 'h2026092023'], + ['PARTITION_NAME' => 'dMaxValue', 'TABLE_ROWS' => '0', 'DATA_LENGTH' => '0', 'INDEX_LENGTH' => '0'], + ]); + + $result = syslog_partition_observability(); + + expect($result['syslog']['coverage_start'])->toBe('2026092022') + ->and($result['syslog']['coverage_end'])->toBe('2026092023') + ->and($result['syslog']['partitions'])->toBe(2) + ->and($result['syslog']['granularity'])->toBe('hour'); }); \ No newline at end of file diff --git a/tests/Unit/PartitionPrecreateBoundaryTest.php b/tests/Unit/PartitionPrecreateBoundaryTest.php index cbb17226..a973eba2 100644 --- a/tests/Unit/PartitionPrecreateBoundaryTest.php +++ b/tests/Unit/PartitionPrecreateBoundaryTest.php @@ -85,3 +85,22 @@ function partition_precreate_dates($ddl) { expect($dates)->toContain('d' . $oldest); }); + +it('pre-creates hourly partitions from the retained days through at least two days ahead', function () { + partition_precreate_extract_function(); + + $GLOBALS['syslogdb_default'] = 'syslog'; + $GLOBALS['__test_partition_precreate_base_time'] = gmmktime(13, 20, 0, 11, 20, 2026); + + $ddl = test_create_partitioned_syslog_table('InnoDB', 2, 1, 'sec', 'hour'); + + preg_match_all('/PARTITION (h\d{10}) VALUES LESS THAN \((\d+)\)/', $ddl, $matches); + + // Two retained UTC days start at 2026-11-19 00:00; a 1-day window is raised to 2 days. + expect($matches[1][0])->toBe('h2026111900') + ->and(end($matches[1]))->toBe('h2026112213') + ->and($matches[1])->toHaveCount(86) + ->and((int) $matches[2][0])->toBe(gmmktime(1, 0, 0, 11, 19, 2026)) + ->and(partition_precreate_dates($ddl))->toBe([]) + ->and($ddl)->toContain('PARTITION dMaxValue VALUES LESS THAN MAXVALUE'); +}); diff --git a/tests/Unit/PartitionRebuildTest.php b/tests/Unit/PartitionRebuildTest.php index 737b7af5..9b412207 100644 --- a/tests/Unit/PartitionRebuildTest.php +++ b/tests/Unit/PartitionRebuildTest.php @@ -43,12 +43,15 @@ function partition_rebuild_setup(): stdClass { 'enabled' => '', 'slot' => true, 'slots' => [], - 'legacy' => [] + 'legacy' => [], + 'retention' => '30', + 'granularity' => '' ]; $GLOBALS['config']['poller_id'] = 1; test_override('read_config_option', static fn($key) => [ - 'syslog_install_engine' => 'InnoDB', 'syslog_retention' => '30', - 'syslog_partition_ahead_days' => '3', 'syslog_enabled' => $state->enabled + 'syslog_install_engine' => 'InnoDB', 'syslog_retention' => $state->retention, + 'syslog_partition_ahead_days' => '3', 'syslog_enabled' => $state->enabled, + 'syslog_partition_granularity' => $state->granularity ][$key] ?? ''); test_override('register_process_start', static function (...$args) use ($state) { $state->slots[] = array_merge(['start'], $args); @@ -78,6 +81,7 @@ function partition_rebuild_setup(): stdClass { }); test_override('rebuild_mariadb', static fn() => stripos($state->version, 'mariadb') !== false); test_override('syslog_db_fetch_cell', static fn($sql) => match (true) { + str_contains($sql, 'VERSION()') => $state->version, str_contains($sql, 'time_zone') => '+05:30', str_contains($sql, 'lock_wait_timeout') => '86400', default => 'STRICT_TRANS_TABLES,NO_ZERO_DATE' @@ -119,12 +123,47 @@ function partition_rebuild_setup(): stdClass { ->toContain('does NOT pause')->toContain('10,000-row copies') ->toContain('receive new sequence numbers') ->not->toContain('Type "yes"') - ->and($state->rebuilds)->toBe([['syslog', false, true, 'InnoDB', 30, 3]]) + ->and($state->rebuilds)->toBe([['syslog', false, true, 'InnoDB', 30, 3, 'day', 8192]]) ->and($state->slots)->toBe([['start', 'syslog', 'master', 1, 604800], ['end', 'syslog', 'master', 1]]); expect($state->settings)->toBe([]); expect(array_column($state->queries, 0))->toContain('SELECT RELEASE_LOCK(?)'); }); +it('saves --partition=hourly once confirmed and rebuilds with at least two days ahead', function () { + $state = partition_rebuild_setup(); + test_override('syslog_upart_rebuild', static function (...$args) use ($state) { + $state->rebuilds[] = $args; + expect($state->settings)->toBe(['syslog_partition_granularity' => 'hour']); + + return true; + }); + test_override('read_config_option', static fn($key) => [ + 'syslog_install_engine' => 'InnoDB', 'syslog_retention' => '30', 'syslog_partition_ahead_days' => '1' + ][$key] ?? ''); + $run = partition_rebuild_runner(); + ob_start(); + $ok = $run(true, false, true, 'hour'); + $output = ob_get_clean(); + + expect($ok)->toBeTrue() + ->and($output)->toContain('Rebuilding hourly partitions') + ->and($state->rebuilds)->toBe([['syslog', false, true, 'InnoDB', 30, 2, 'hour', 8192]]); +}); + +it('refuses hourly partitioning beyond the server partition limit or with indefinite retention', function () { + foreach ([['365', '10.11.6-MariaDB'], ['60', '10.0.3-MariaDB'], ['60', '5.6.51'], ['0', '11.4.2-MariaDB']] as [$retention, $version]) { + $state = partition_rebuild_setup(); + $state->retention = $retention; + $state->version = $version; + $run = partition_rebuild_runner(); + + expect($run(true, false, true, 'hour'))->toBeFalse() + ->and($state->slots)->toBe([]) + ->and($state->settings)->toBe([]) + ->and($state->rebuilds)->toBe([]); + } +}); + it('refuses to rebuild while Syslog processing is enabled', function () { $state = partition_rebuild_setup(); $state->enabled = 'on'; @@ -254,7 +293,8 @@ function partition_rebuild_setup(): stdClass { foreach ([ ['upgrade_partitioning.php', ['--help'], 0, [ 'Preserve existing messages (the default)', - 'future-partition horizon.' . PHP_EOL . PHP_EOL . 'Existing precision', + 'utility rebuilds the tables.' . PHP_EOL . PHP_EOL . 'Existing precision', + '--partition - Save and apply this Partitioning Level', '--convert-ms is selected.' . PHP_EOL . PHP_EOL . 'With preservation', 'Syslog Partition Upgrade Utility, Version 5.0, Copyright (C)', PHP_EOL . PHP_EOL . 'usage: upgrade_partitioning.php', @@ -266,7 +306,8 @@ function partition_rebuild_setup(): stdClass { ['upgrade_partitioning.php', ['--version'], 0, 'Syslog Partition Upgrade Utility, Version 5.0, Copyright (C)'], ['upgrade_partitioning.php', ['-V'], 0, 'Syslog Partition Upgrade Utility, Version 5.0, Copyright (C)'], ['upgrade_partitioning.php', ['-v'], 0, 'Syslog Partition Upgrade Utility, Version 5.0, Copyright (C)'], - ['upgrade_partitioning.php', ['--discard-data', '--preserve-data'], 1, 'cannot be combined'] + ['upgrade_partitioning.php', ['--discard-data', '--preserve-data'], 1, 'cannot be combined'], + ['upgrade_partitioning.php', ['--partition=weekly'], 1, '--partition must be hourly or daily'] ] as [$entry, $args, $expected_status, $expected_output]) { $process = proc_open(array_merge([PHP_BINARY, '-d', 'xdebug.mode=off', $base . '/plugins/syslog/cli/' . $entry], $args), [0 => ['pipe', 'r'], 1 => ['pipe', 'w'], 2 => ['pipe', 'w']], $pipes); diff --git a/tests/Unit/PartitionReorganizeCoverageTest.php b/tests/Unit/PartitionReorganizeCoverageTest.php index 56696305..ca199948 100644 --- a/tests/Unit/PartitionReorganizeCoverageTest.php +++ b/tests/Unit/PartitionReorganizeCoverageTest.php @@ -34,6 +34,11 @@ function partition_reorg_guard_setup($expression, $max_boundary, $to_days_value, return $to_days_value; } + // No hYYYYMMDDHH partitions: the table uses the daily layout. + if (str_contains($sql, "LIKE 'h%'")) { + return 0; + } + return 1; }); diff --git a/tests/regression/partition_rebuild_database_test.php b/tests/regression/partition_rebuild_database_test.php index 8cdd5643..4e8da219 100644 --- a/tests/regression/partition_rebuild_database_test.php +++ b/tests/regression/partition_rebuild_database_test.php @@ -39,6 +39,7 @@ function rebuild_check(bool $ok, string $message): void { } function syslog_connect() { return true; } +function __($text, ...$args) { array_pop($args); return $args ? vsprintf($text, $args) : $text; } function read_config_option($name, $force = false) { return $GLOBALS['options'][$name] ?? ''; } function register_process_start($type, $name, $id = 0, $timeout = 300) { $GLOBALS['slot_calls'][] = ['start', $type, $name, $id]; @@ -104,6 +105,20 @@ function syslog_db_table_exists($table, ...$args) { function syslog_partition_ahead_days() { return (int) read_config_option('syslog_partition_ahead_days'); } require dirname(__DIR__, 2) . '/includes/schema.php'; + +// The partitioning-level helpers come from the real functions.php source. +$helpers = file_get_contents(dirname(__DIR__, 2) . '/includes/functions.php'); +$helper_code = ''; + +foreach (['syslog_partition_periods', 'syslog_partition_requested_granularity', 'syslog_partition_ahead_days_for', + 'syslog_partition_server_limit', 'syslog_partition_projected_count', 'syslog_partition_settings_error'] as $helper) { + if (preg_match('/^function ' . $helper . '\(.*?^\}/ms', $helpers, $match) !== 1) { + throw new RuntimeException("Cannot find $helper"); + } + + $helper_code .= $match[0] . "\n"; +} + $source = file_get_contents(dirname(__DIR__, 2) . '/cli/upgrade_partitioning.php'); $offset = strpos($source, "\nfunction syslog_upart_run("); @@ -114,7 +129,7 @@ function syslog_partition_ahead_days() { return (int) read_config_option('syslog $fixture = tempnam(sys_get_temp_dir(), 'syslog-rebuild-'); try { - file_put_contents($fixture, "query('SELECT PARTITION_NAME FROM information_schema.PARTITIONS WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = \'syslog\' AND PARTITION_DESCRIPTION <> \'MAXVALUE\' AND CAST(PARTITION_DESCRIPTION AS UNSIGNED) > ' . ($now + 7200))->fetchAll(PDO::FETCH_COLUMN); + rebuild_check(count($future) >= 46, 'Hourly partitions exist at least two days ahead'); + $plan = $pdo->query('EXPLAIN PARTITIONS SELECT COUNT(*) FROM syslog WHERE logtime >= FROM_UNIXTIME(' . ($now - 86400) . ') AND logtime < FROM_UNIXTIME(' . ($now + 1) . ')')->fetch(); + $scanned = explode(',', (string) $plan['partitions']); + rebuild_check($scanned !== [''] && !array_intersect($scanned, array_merge($future, ['dMaxValue'])), 'The automatic upper bound prunes every empty future partition'); + ob_start(); + rebuild_check(syslog_upart_rebuild('syslog', false, true, 'InnoDB', 2, 2, 'day', 8192), 'A preserving rebuild converts hourly partitions back to daily'); + ob_end_clean(); + rebuild_check((int) syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog') === (int) $total && syslog_upart_layout_ok('syslog', 'InnoDB', 'day'), 'Every row survives the daily rebuild'); + rebuild_check(syslog_upart_partition_sql(400, 2, null, null, null, 'hour', 8192) === null, 'Hourly plans above the server partition limit are refused'); + print "$checks partition rebuild database checks passed.\n"; } finally { $pdo->exec('DROP DATABASE `syslog_rebuild_test`'); diff --git a/tests/regression/review_fixes_database_test.php b/tests/regression/review_fixes_database_test.php index 83f9c092..f33606cb 100644 --- a/tests/regression/review_fixes_database_test.php +++ b/tests/regression/review_fixes_database_test.php @@ -157,6 +157,34 @@ $result = syslog_process_alerts($max); check($result['syslog_alarms'] === 1, 'A host-level rule that uses an asterisk must fire'); + + // ---- Hourly partitioning: install, then poller maintenance --------------- + $options['syslog_partition_ahead_days'] = '1'; + $options['syslog_partition_recover_limit'] = '31'; + $pdo->exec('DROP TABLE syslog_removed'); + syslog_setup_table_new(['upgrade_type' => 'truncate', 'engine' => 'InnoDB', 'days' => 2, 'db_type' => 'part', 'partitioning' => 'hour'], false); + + $hourly = static fn() => $pdo->query("SELECT PARTITION_NAME FROM information_schema.PARTITIONS + WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'syslog' ORDER BY PARTITION_ORDINAL_POSITION")->fetchAll(PDO::FETCH_COLUMN); + $names = $hourly(); + $now = (int) $pdo->query('SELECT UNIX_TIMESTAMP()')->fetchColumn(); + + check($options['syslog_partition_granularity'] === 'hour', 'An hourly install records the partitioning level'); + check(end($names) === 'dMaxValue' && preg_grep('/^h\d{10}$/', $names) === array_slice($names, 0, -1), 'An hourly install creates only hourly partitions before dMaxValue'); + check(in_array(syslog_partition_name('hour', $now + 2 * 86400), $names, true), 'An hourly install keeps at least two days ahead, even with a 1-day window'); + check(syslog_partition_report_state('syslog') && syslog_partition_report_state('syslog_removed'), 'Hourly layouts pass the partition state check'); + check(syslog_partition_layout_granularity('syslog_removed') === 'hour', 'syslog_removed follows the hourly layout'); + + // Merge the last six future hours back into dMaxValue, then let maintenance + // heal the gap while Settings already asks for Daily: the layout must win. + $tail = array_slice($names, -7, 6); + $pdo->exec('ALTER TABLE syslog REORGANIZE PARTITION ' . implode(', ', $tail) . ', dMaxValue INTO (PARTITION dMaxValue VALUES LESS THAN MAXVALUE)'); + $options['syslog_partition_granularity'] = 'day'; + syslog_partition_manage(); + $healed = $hourly(); + + check(array_intersect($tail, $healed) === $tail && preg_grep('/^d\d{8}$/', $healed) === [], 'Maintenance heals the hourly gap and ignores the pending Daily setting'); + check(syslog_status_get()['partition_maintenance_outcome'] === 'success', 'Hourly maintenance completes'); } catch (Throwable $error) { $failures[] = $error->getMessage() . ' (' . basename($error->getFile()) . ':' . $error->getLine() . ')'; } finally { From 69ca4100809258b5068d38c18d22a97036be276b Mon Sep 17 00:00:00 2001 From: TheWitness Date: Fri, 20 Nov 2026 10:14:47 -0500 Subject: [PATCH 03/12] syslog: check partition limits against the tables' current layout too Maintenance keeps the tables' existing partitioning level until cli/upgrade_partitioning.php rebuilds them, so a settings save or upgrade is also checked against every level the syslog and syslog_removed tables currently use. This refuses, for example, Indefinite retention while the tables are still hourly, even when Daily is selected. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- includes/functions.php | 51 +++++++++++++++++++++++++ includes/settings.php | 16 +++++--- locales/po/cacti.pot | 7 +++- setup.php | 8 ++-- tests/Unit/PartitionGranularityTest.php | 28 ++++++++++++-- 5 files changed, 96 insertions(+), 14 deletions(-) diff --git a/includes/functions.php b/includes/functions.php index e19d9e80..891209b0 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -2938,6 +2938,57 @@ function syslog_partition_settings_error(string $granularity, $retention_days, $ return ''; } +/** + * The partitioning levels existing message tables use. + * + * @param string[] $tables The tables to inspect. + * + * @return string[] Distinct levels, empty when none of the tables exists. + */ +function syslog_partition_existing_layouts(array $tables = ['syslog', 'syslog_removed']): array { + $layouts = []; + + foreach ($tables as $table) { + if (syslog_db_table_exists($table, false)) { + $layouts[] = syslog_partition_layout_granularity($table); + } + } + + return array_values(array_unique($layouts)); +} + +/** + * Check retention and the pre-create window against the requested level and + * every level the tables still use. Maintenance follows the existing layout + * until cli/upgrade_partitioning.php rebuilds it, so both must fit. + * + * @param string $requested The requested level, 'hour' or 'day'. + * @param int|string $retention_days Days of retention; 0 is Indefinite. + * @param int|string $ahead_days The pre-create window in days. + * @param int $limit The server partition limit. + * @param string[] $layouts The levels the existing tables use. + * + * @return string The error, or '' when every level fits. + */ +function syslog_partition_levels_error(string $requested, $retention_days, $ahead_days, int $limit, array $layouts): string { + $error = syslog_partition_settings_error($requested, $retention_days, $ahead_days, $limit); + + foreach (array_diff($layouts, [$requested]) as $layout) { + if ($error !== '') { + break; + } + + $error = syslog_partition_settings_error($layout, $retention_days, $ahead_days, $limit); + + if ($error !== '') { + $error = __('The tables still use %s partitions until cli/upgrade_partitioning.php rebuilds them.', + $layout === 'hour' ? __('Hourly', 'syslog') : __('Daily', 'syslog'), 'syslog') . ' ' . $error; + } + } + + return $error; +} + /** * Upper logtime bound added to every message query. Messages cannot be newer * than now, so the empty future partitions are always pruned, whatever the diff --git a/includes/settings.php b/includes/settings.php index 49857b6a..84f446fd 100644 --- a/includes/settings.php +++ b/includes/settings.php @@ -417,25 +417,29 @@ function syslog_config_settings(): void { * pre-create window exceed the database server's partition limit. Cacti runs * the config_settings hook before settings.php writes anything, on 1.2 and 1.3. * - * @param string|null $version The database VERSION(), or null to query it. + * @param string|null $version The database VERSION(), or null to query it. + * @param string[]|null $layouts The levels the tables use, or null to read them. * * @return string The reported error, or '' when the save may proceed. */ -function syslog_settings_partition_gate(?string $version = null): string { +function syslog_settings_partition_gate(?string $version = null, ?array $layouts = null): string { if (($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST') { return ''; } // syslog_connect() loads the helpers even when the Syslog database is down. - if ($version === null) { - $version = (string) (syslog_connect() ? syslog_db_fetch_cell('SELECT VERSION()') : db_fetch_cell('SELECT VERSION()')); + if ($version === null || $layouts === null) { + $connected = syslog_connect(); + $version = $version ?? (string) ($connected ? syslog_db_fetch_cell('SELECT VERSION()') : db_fetch_cell('SELECT VERSION()')); + $layouts = $layouts ?? ($connected ? syslog_partition_existing_layouts() : []); } - $error = syslog_partition_settings_error( + $error = syslog_partition_levels_error( get_nfilter_request_var('syslog_partition_granularity') === 'hour' ? 'hour' : 'day', get_nfilter_request_var('syslog_retention'), get_nfilter_request_var('syslog_partition_ahead_days'), - syslog_partition_server_limit($version) + syslog_partition_server_limit($version), + $layouts ); if ($error === '') { diff --git a/locales/po/cacti.pot b/locales/po/cacti.pot index 8cddaf0d..cebb6c03 100644 --- a/locales/po/cacti.pot +++ b/locales/po/cacti.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: Cacti \n" "Report-Msgid-Bugs-To: developers@cacti.net\n" -"POT-Creation-Date: 2026-11-20 10:08-0500\n" +"POT-Creation-Date: 2026-11-20 10:14-0500\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: Cacti Developers >\n" "Language-Team: Cacti Developers \n" @@ -310,6 +310,11 @@ msgstr "" msgid "Hourly" msgstr "" +#: includes/functions.php +#, php-format +msgid "The tables still use %s partitions until cli/upgrade_partitioning.php rebuilds them." +msgstr "" + #: includes/functions.php msgid "Automatic partition recovery deferred: dMaxValue size is unavailable; operator intervention required." msgstr "" diff --git a/setup.php b/setup.php index 1582b9b2..bdec77d5 100644 --- a/setup.php +++ b/setup.php @@ -157,11 +157,13 @@ function syslog_execute_update(int $syslog_exists, array $options): void { $compatibility_options = $options; if (isset($options['partitioning']) || isset($options['days'])) { - $partition_error = syslog_partition_settings_error( - isset($options['partitioning']) ? (string) $options['partitioning'] : syslog_partition_requested_granularity(), + $partition_error = syslog_partition_levels_error( + isset($options['partitioning']) && $options['partitioning'] === 'hour' ? 'hour' : syslog_partition_requested_granularity(), $options['days'] ?? read_config_option('syslog_retention'), read_config_option('syslog_partition_ahead_days'), - syslog_partition_server_limit() + syslog_partition_server_limit(), + // A truncate recreates syslog only; syslog_removed keeps its layout. + syslog_partition_existing_layouts(($options['upgrade_type'] ?? '') === 'truncate' ? ['syslog_removed'] : ['syslog', 'syslog_removed']) ); if ($partition_error !== '') { diff --git a/tests/Unit/PartitionGranularityTest.php b/tests/Unit/PartitionGranularityTest.php index d8562806..8d0d614b 100644 --- a/tests/Unit/PartitionGranularityTest.php +++ b/tests/Unit/PartitionGranularityTest.php @@ -62,7 +62,7 @@ try { $GLOBALS['request'] = ['syslog_partition_granularity' => 'hour', 'syslog_retention' => '365', 'syslog_partition_ahead_days' => '3']; - expect(syslog_settings_partition_gate('10.11.6-MariaDB'))->toContain('8,857 partitions') + expect(syslog_settings_partition_gate('10.11.6-MariaDB', ['day']))->toContain('8,857 partitions') ->and($messages)->toHaveCount(1) ->and($messages[0][0])->toBe('syslog_partition_limit') ->and($messages[0][1])->toContain('The Syslog settings were not saved.') @@ -70,11 +70,16 @@ ->and($_SESSION['sess_error_fields'])->toHaveKeys(['syslog_retention', 'syslog_partition_granularity']); $GLOBALS['request']['syslog_retention'] = '60'; - expect(syslog_settings_partition_gate('10.11.6-MariaDB'))->toBe('') - ->and(syslog_settings_partition_gate('10.0.3-MariaDB'))->toContain('limit of 1,024'); + expect(syslog_settings_partition_gate('10.11.6-MariaDB', ['day']))->toBe('') + ->and(syslog_settings_partition_gate('10.0.3-MariaDB', ['day']))->toContain('limit of 1,024'); + + // Hourly tables keep growing hourly until the rebuild, whatever is selected. + $GLOBALS['request'] = ['syslog_partition_granularity' => 'day', 'syslog_retention' => '0', 'syslog_partition_ahead_days' => '3']; + expect(syslog_settings_partition_gate('10.11.6-MariaDB', ['hour']))->toStartWith('The tables still use Hourly partitions')->toContain('Indefinite') + ->and(syslog_settings_partition_gate('10.11.6-MariaDB', ['day']))->toBe(''); $_SERVER['REQUEST_METHOD'] = 'GET'; - expect(syslog_settings_partition_gate('10.0.3-MariaDB'))->toBe(''); + expect(syslog_settings_partition_gate('10.0.3-MariaDB', ['hour']))->toBe(''); } finally { if ($method === null) { unset($_SERVER['REQUEST_METHOD']); @@ -87,6 +92,21 @@ } }); +it('reads the partitioning level of each existing message table', function () { + syslog_load_plugin_source('includes/functions.php'); + + $GLOBALS['syslogdb_default'] = 'syslog'; + test_override('syslog_db_table_exists', fn($table) => $table !== 'syslog_removed'); + test_override('syslog_db_fetch_cell_prepared', fn($sql, $params) => str_contains($sql, "LIKE 'h%'") && $params[1] === 'syslog' ? '97' : '0'); + + expect(syslog_partition_existing_layouts())->toBe(['hour']) + ->and(syslog_partition_existing_layouts(['syslog_removed']))->toBe([]); + + test_override('syslog_db_table_exists', fn() => true); + + expect(syslog_partition_existing_layouts())->toBe(['hour', 'day']); +}); + it('recovers hourly partitions in bounded runs and checks only the horizon once it exists', function () { syslog_load_plugin_source('includes/functions.php'); From 46ac9b4ef57e12c2d102cb231775b1b72f42517c Mon Sep 17 00:00:00 2001 From: TheWitness Date: Fri, 20 Nov 2026 10:19:57 -0500 Subject: [PATCH 04/12] tests: bind parameters in the hourly pruning checks Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../regression/partition_rebuild_database_test.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/tests/regression/partition_rebuild_database_test.php b/tests/regression/partition_rebuild_database_test.php index 4e8da219..448f0b1e 100644 --- a/tests/regression/partition_rebuild_database_test.php +++ b/tests/regression/partition_rebuild_database_test.php @@ -235,13 +235,13 @@ function rebuild_table(string $table, bool $convert = true, string $engine = 'In rebuild_check(syslog_detect_logtime_precision('syslog_removed') === 'sec' && syslog_db_fetch_cell("SELECT DATA_TYPE FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='syslog_removed' AND COLUMN_NAME='logtime'") === 'timestamp', 'A discarded DATETIME table is recreated as TIMESTAMP'); - $options['syslog_enabled'] = 'on'; + set_config_option('syslog_enabled', 'on'); $slot_calls = []; ob_start(); rebuild_check(!syslog_upart_run(true, false), 'The command refuses to run while Syslog processing is enabled'); ob_end_clean(); rebuild_check($slot_calls === [], 'Enabled processing is refused before taking the process slot'); - $options['syslog_enabled'] = ''; + set_config_option('syslog_enabled', ''); $slot_free = false; ob_start(); rebuild_check(!syslog_upart_run(true, false), 'The command refuses to run while a processing run holds the master slot'); @@ -300,11 +300,13 @@ function rebuild_table(string $table, bool $convert = true, string $engine = 'In rebuild_check((int) syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog') === (int) $total, 'Every row survives the hourly rebuild'); rebuild_check(syslog_upart_layout_ok('syslog', 'InnoDB', 'hour') && !syslog_upart_layout_ok('syslog', 'InnoDB', 'day'), 'The replacement has contiguous UTC hourly partitions'); $now = (int) syslog_db_fetch_cell('SELECT UNIX_TIMESTAMP()'); - $future = $pdo->query('SELECT PARTITION_NAME FROM information_schema.PARTITIONS WHERE TABLE_SCHEMA = DATABASE() - AND TABLE_NAME = \'syslog\' AND PARTITION_DESCRIPTION <> \'MAXVALUE\' AND CAST(PARTITION_DESCRIPTION AS UNSIGNED) > ' . ($now + 7200))->fetchAll(PDO::FETCH_COLUMN); + $future = syslog_db_fetch_assoc_prepared('SELECT PARTITION_NAME FROM information_schema.PARTITIONS WHERE TABLE_SCHEMA = DATABASE() + AND TABLE_NAME = \'syslog\' AND PARTITION_DESCRIPTION <> \'MAXVALUE\' AND CAST(PARTITION_DESCRIPTION AS UNSIGNED) > ?', [$now + 7200]); + $future = array_column($future, 'PARTITION_NAME'); rebuild_check(count($future) >= 46, 'Hourly partitions exist at least two days ahead'); - $plan = $pdo->query('EXPLAIN PARTITIONS SELECT COUNT(*) FROM syslog WHERE logtime >= FROM_UNIXTIME(' . ($now - 86400) . ') AND logtime < FROM_UNIXTIME(' . ($now + 1) . ')')->fetch(); - $scanned = explode(',', (string) $plan['partitions']); + $plan = syslog_db_fetch_assoc_prepared('EXPLAIN PARTITIONS SELECT COUNT(*) FROM syslog WHERE logtime >= FROM_UNIXTIME(?) AND logtime < FROM_UNIXTIME(?)', + [$now - 86400, $now + 1]); + $scanned = explode(',', (string) ($plan[0]['partitions'] ?? '')); rebuild_check($scanned !== [''] && !array_intersect($scanned, array_merge($future, ['dMaxValue'])), 'The automatic upper bound prunes every empty future partition'); ob_start(); rebuild_check(syslog_upart_rebuild('syslog', false, true, 'InnoDB', 2, 2, 'day', 8192), 'A preserving rebuild converts hourly partitions back to daily'); From 623736b6e8f9770d50305692d51beca1337dc885 Mon Sep 17 00:00:00 2001 From: TheWitness Date: Fri, 20 Nov 2026 12:41:34 -0500 Subject: [PATCH 05/12] syslog: refresh Status cards through callbacks and fix Add card Cacti develop wraps every plain select in select2, which reports a change through jQuery's trigger. The card controller listens with a native addEventListener, so choosing a card under "Add card" did nothing. Bind the add select with jQuery in the Status adapter, append and bind the card, save the layout, and keep the select2 display in sync. Changing the Refresh interval assigned window.location, a full page load, and the Status tab also registered Cacti's page refresh. The interval is now saved per user through a CSRF-protected status_refresh callback, and a client timer refreshes the cards in place, through the same path as the Refresh button. The timer stops once the grid leaves the page. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- CHANGELOG.md | 1 + js/syslog_dashboard.js | 137 +++++++++++++++++------ locales/po/cacti.pot | 6 +- syslog.php | 45 +++++++- tests/Unit/StatusRefreshCallbackTest.php | 71 ++++++++++++ tests/browser/README.md | 9 ++ tests/browser/status_cards.cjs | 114 +++++++++++++++++++ 7 files changed, 347 insertions(+), 36 deletions(-) create mode 100644 tests/Unit/StatusRefreshCallbackTest.php create mode 100644 tests/browser/status_cards.cjs diff --git a/CHANGELOG.md b/CHANGELOG.md index 03c436b7..c57c8a3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ --- develop --- +* bug: Syslog Status: choosing a card in Add card now appends it (Cacti's select2 dropdowns report changes through jQuery, which the card controller's native listener missed), and the Refresh interval is saved per user through a callback and refreshes the cards in place instead of reloading the page * feature: Add an Hourly or Daily (default) Partitioning Level under the renamed Data Retention and Partitioning settings, the install advisor (replacing the unused Partitions per Day option) and silent install options. A changed level takes effect only when cli/upgrade_partitioning.php (or its new --partition=hourly|daily option) rebuilds the tables; until then maintenance follows the existing layout and Settings and Status show the change as pending. Hourly tables keep at least two days of partitions ahead so dMaxValue reorganizations stay instant * feature: Refuse to save Syslog settings, install, or rebuild when retention, pre-create window and partitioning level need more partitions than the database server supports (8192, or 1024 before MariaDB 10.0.4 and MySQL 5.7), or when Hourly is combined with Indefinite retention * bug: Bound every log query at the current time so empty future partitions are always pruned, whatever the filter's own date range diff --git a/js/syslog_dashboard.js b/js/syslog_dashboard.js index 13aeed16..36205c08 100644 --- a/js/syslog_dashboard.js +++ b/js/syslog_dashboard.js @@ -19,8 +19,9 @@ /* Syslog Status adapter for the shared card chrome (js/cards.js). The * CactiCards controller owns drag/keyboard reorder, the per-card tools and the * debounced layout save; this adapter maps those hooks to syslog.php's status - * endpoints (status_card / status_layout) and wires syslog's extras: the - * "Add card" catalogue, an auto-refresh interval and a manual refresh-all. Card + * endpoints (status_card / status_layout / status_refresh) and wires syslog's + * extras: the "Add card" catalogue, an auto-refresh interval and a manual + * refresh-all, all through callbacks without reloading the page. Card * markup uses the shared .cc* chrome contract; the card bodies keep syslog's * own prefixed content classes (tables, key/value rows, notes). */ @@ -64,6 +65,8 @@ function initSyslogStatus() { add.appendChild(option); } }); + + $(add).trigger('change.select2'); } // A per-card-id request generation shared by the per-card refresh (routed @@ -164,7 +167,9 @@ function initSyslogStatus() { } var adapter = { - addControl: 'syslog_status_add', + // The "Add card" select is bound below with jQuery, not through the + // controller's addControl: Cacti wraps selects in select2, which + // reports changes with a jQuery trigger that native listeners miss. // Keep syslog's content palette (the note accent) in sync with the // live Cacti theme; the shared chrome paints from the --cc-* custom @@ -219,44 +224,112 @@ function initSyslogStatus() { CactiCards.init(grid, adapter); syncAddOptions(); - // Changing the interval reloads the page so Cacti's page refresh picks up - // the new value; it persists in the session for subsequent auto-reloads. - var interval = document.getElementById('syslog_status_refresh'); - if (interval) { - $(interval).off('change.syslogstatus').on('change.syslogstatus', function() { - window.location = 'syslog.php?tab=status&refresh=' + encodeURIComponent(this.value); - }); - } + var add = document.getElementById('syslog_status_add'); + if (add) { + $(add).off('change.syslogstatus').on('change.syslogstatus', function() { + var id = $(this).val(); - var refreshNow = document.getElementById('syslog_status_refresh_now'); - if (refreshNow) { - $(refreshNow).off('click.syslogstatus').on('click.syslogstatus', function() { - var icon = this.querySelector('.fa, .fas'); - var cards = Array.prototype.slice.call(grid.querySelectorAll('.ccCard')); - var pending = cards.length; + if (!id) { + return; + } + + // Reset the visible choice; the select2-only event cannot re-enter here. + $(this).val('').trigger('change.select2'); + + var request = adapter.onAddCard(id); - if (!pending) { + if (!request) { return; } + request.then(function(html) { + if (!html || liveCard(id)) { + return; + } + + var tmp = document.createElement('div'); + tmp.innerHTML = String(html).trim(); + var fresh = tmp.querySelector('.ccCard') || tmp.firstElementChild; + + if (!fresh) { + return; + } + + grid.appendChild(fresh); + CactiCards.bindCard(grid, adapter, fresh); + adapter.applyTheme(grid); + adapter.onLayoutChange(CactiCards.serialize(grid, adapter)); + }); + }); + } + + function refreshAll() { + var icon = refreshNow ? refreshNow.querySelector('.fa, .fas') : null; + var cards = Array.prototype.slice.call(grid.querySelectorAll('.ccCard')); + + if (!cards.length) { + return $.Deferred().resolve().promise(); + } + + if (icon) { + icon.classList.add('fa-spin'); + } + + return $.when.apply($, cards.map(function(card) { + var wasExpanded = card.classList.contains('ccCardExpanded'); + + return fetchCard(card.dataset.card, wasExpanded).then(function(html) { + if (html && card.parentNode && window.CactiCards) { + CactiCards.replaceCard(grid, adapter, card, html, wasExpanded); + } + }, function() { + // A failed card keeps its last content; the others still refresh. + return $.Deferred().resolve().promise(); + }); + })).always(function() { if (icon) { - icon.classList.add('fa-spin'); + icon.classList.remove('fa-spin'); } + }); + } - cards.forEach(function(card) { - var wasExpanded = card.classList.contains('ccCardExpanded'); - fetchCard(card.dataset.card, wasExpanded).then(function(html) { - if (html && card.parentNode && window.CactiCards) { - CactiCards.replaceCard(grid, adapter, card, html, wasExpanded); - } - }).always(function() { - pending--; - if (pending <= 0 && icon) { - icon.classList.remove('fa-spin'); - } - }); - }); + // Auto-refresh replaces the cards in place through callbacks. One timer is + // shared across ajax page loads and stops once the grid leaves the page. + var interval = document.getElementById('syslog_status_refresh'); + var refreshNow = document.getElementById('syslog_status_refresh_now'); + + function schedule() { + clearTimeout(window.syslogStatusRefreshTimer); + + var seconds = interval ? Number.parseInt(interval.value, 10) : 0; + + if (seconds > 0) { + window.syslogStatusRefreshTimer = setTimeout(function() { + if (document.body.contains(grid)) { + refreshAll().always(schedule); + } + }, seconds * 1000); + } + } + + if (interval) { + $(interval).off('change.syslogstatus').on('change.syslogstatus', function() { + $.post('syslog.php', { + action: 'status_refresh', + tab: 'status', + refresh: this.value, + __csrf_magic: csrfMagicToken + }, null, 'json'); + schedule(); }); } + + if (refreshNow) { + $(refreshNow).off('click.syslogstatus').on('click.syslogstatus', function() { + refreshAll().always(schedule); + }); + } + + schedule(); }); } diff --git a/locales/po/cacti.pot b/locales/po/cacti.pot index cebb6c03..a4d9edf6 100644 --- a/locales/po/cacti.pot +++ b/locales/po/cacti.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: Cacti \n" "Report-Msgid-Bugs-To: developers@cacti.net\n" -"POT-Creation-Date: 2026-11-20 10:14-0500\n" +"POT-Creation-Date: 2026-11-20 12:40-0500\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: Cacti Developers >\n" "Language-Team: Cacti Developers \n" @@ -2324,6 +2324,10 @@ msgstr "" msgid "Remove from page" msgstr "" +#: syslog.php +msgid "Invalid refresh interval." +msgstr "" + #: syslog.php msgid "Unknown card." msgstr "" diff --git a/syslog.php b/syslog.php index 6d23002f..57556efd 100644 --- a/syslog.php +++ b/syslog.php @@ -129,6 +129,12 @@ exit; } +if (get_request_var('action') == 'status_refresh') { + header('Content-Type: application/json; charset=UTF-8'); + print syslog_status_refresh_save(); + exit; +} + if (get_request_var('action') == 'recommendation') { header('Content-Type: application/json; charset=UTF-8'); print syslog_recommendations_lookup_json((string) get_nfilter_request_var('message')); @@ -186,7 +192,8 @@ // validate the syslog post/get/request information syslog_request_validation($current_tab); -if (isset_request_var('refresh')) { +// The Status tab refreshes its cards through callbacks, not Cacti's page reload. +if (isset_request_var('refresh') && $current_tab !== 'status') { $refresh['seconds'] = get_request_var('refresh'); $refresh['page'] = $config['url_path'] . 'plugins/syslog/syslog.php?header=false&tab=' . $current_tab; $refresh['logout'] = 'false'; @@ -1257,6 +1264,38 @@ function syslog_status_layout_save(): string { return json_encode(['ok' => true]); } +/** + * The Status tab's auto-refresh interval in seconds for the current user. + * + * @return int The saved interval, or the log viewer's refresh default. + */ +function syslog_status_refresh_interval(): int { + return (int) read_user_setting('syslog_status_refresh', read_user_setting('syslog_refresh', read_config_option('syslog_refresh'))); +} + +/** + * Persist the posted Status auto-refresh interval for the current user. + * + * @return string A JSON status document. + */ +function syslog_status_refresh_save(): string { + global $page_refresh_interval; + + if ($_SERVER['REQUEST_METHOD'] !== 'POST' || !function_exists('csrf_check') || !csrf_check(false)) { + return (string) json_encode(['error' => __('Invalid request. Please try again.', 'syslog')]); + } + + $seconds = (string) get_nfilter_request_var('refresh'); + + if (!ctype_digit($seconds) || !isset($page_refresh_interval[(int) $seconds])) { + return (string) json_encode(['error' => __('Invalid refresh interval.', 'syslog')]); + } + + set_user_setting('syslog_status_refresh', (int) $seconds); + + return (string) json_encode(['ok' => true]); +} + /** * AJAX: render a single Status card, used when adding one from the catalogue or * refreshing one in place. @@ -1313,8 +1352,8 @@ function syslog_status(): void { print ''; // Auto-refresh interval picker plus a manual refresh, mirroring the log view's - // header refresh. The interval persists through the page's auto-reload. - $current_refresh = get_request_var('refresh'); + // header refresh. Both refresh the cards through callbacks. + $current_refresh = syslog_status_refresh_interval(); print '
'; print ''; print ' +
+
+
${card('latest_run', 'Latest run')}${card('workers', 'Workers')}
+ + + +`; + +(async () => { + const browser = await chromium.launch({headless: true, executablePath: process.env.CHROMIUM_PATH, args: ['--no-sandbox']}); + const page = await browser.newPage(); + const errors = []; + const posts = []; + let navigations = 0; + let rendered = 0; + + page.on('pageerror', error => errors.push(error.message)); + page.on('framenavigated', frame => { if (frame === page.mainFrame()) navigations++; }); + + const routes = { + '/jquery.js': path.join(cacti, 'include/js/jquery.js'), + '/select2.js': path.join(cacti, 'include/js/select2.js'), + '/select2.css': path.join(cacti, 'include/themes/cacti/select2.css'), + '/cards.js': path.join(root, 'js/cards.js'), + '/syslog_dashboard.js': path.join(root, 'js/syslog_dashboard.js') + }; + + await page.route('http://fixture/**', route => { + const url = new URL(route.request().url()); + + if (url.pathname === '/') { + return route.fulfill({body: page_html, contentType: 'text/html; charset=utf-8'}); + } + + if (url.pathname === '/syslog.php' && route.request().method() === 'POST') { + const data = Object.fromEntries(new URLSearchParams(route.request().postData() || '')); + assert.equal(data.__csrf_magic, 'fixture', 'Status callbacks carry the CSRF token'); + posts.push(data); + + if (data.action === 'status_card') { + rendered++; + + return route.fulfill({contentType: 'application/json', body: JSON.stringify({card: data.card, html: card(data.card, 'Rendered ' + data.card + ' ' + rendered)})}); + } + + return route.fulfill({contentType: 'application/json', body: JSON.stringify({ok: true})}); + } + + return routes[url.pathname] ? route.fulfill({path: routes[url.pathname], contentType: 'text/javascript'}) : route.fulfill({status: 404, body: ''}); + }); + + await page.goto('http://fixture/'); + await page.waitForTimeout(200); + assert.deepEqual(errors, []); + assert.equal(await page.locator('#syslog_status_add').evaluate(select => select.classList.contains('select2-hidden-accessible')), true, 'The fixture uses select2 like Cacti'); + const loaded = navigations; + + // Adding a card through the select2 widget appends it and saves the layout. + await page.locator('#syslog_status_add + .select2 .select2-selection').click(); + await page.locator('.select2-results__option', {hasText: 'Storage'}).click(); + await page.waitForFunction(() => document.querySelectorAll('#syslog_status_grid .ccCard').length === 3); + assert.deepEqual(await page.locator('#syslog_status_grid .ccCard').evaluateAll(cards => cards.map(item => item.dataset.card)), ['latest_run', 'workers', 'storage'], 'The added card goes to the end'); + await page.waitForTimeout(100); + const layout = posts.filter(post => post.action === 'status_layout').pop(); + assert.ok(layout, 'Adding a card saves the layout'); + assert.deepEqual(JSON.parse(layout.layout).order, ['latest_run', 'workers', 'storage']); + assert.equal(await page.locator('#syslog_status_add').inputValue(), '', 'The add control resets'); + assert.equal(await page.locator('#syslog_status_add option[value="storage"]').count(), 0, 'An added card leaves the catalogue'); + + // Changing the interval saves it through a callback and refreshes cards in place. + posts.length = 0; + await page.locator('#syslog_status_refresh + .select2 .select2-selection').click(); + await page.locator('.select2-results__option', {hasText: '1 Second'}).click(); + await page.waitForFunction(() => document.querySelectorAll('#syslog_status_grid .ccCard .ccCardBody').length === 3 + && [...document.querySelectorAll('#syslog_status_grid .ccCardBody')].every(body => body.textContent.startsWith('Rendered')), null, {timeout: 5000}); + assert.deepEqual(posts.find(post => post.action === 'status_refresh'), {action: 'status_refresh', tab: 'status', refresh: '1', __csrf_magic: 'fixture'}); + assert.deepEqual(posts.filter(post => post.action === 'status_card').map(post => post.card).sort(), ['latest_run', 'storage', 'workers']); + + // Never stops the timer; the manual refresh still uses callbacks. + await page.locator('#syslog_status_refresh + .select2 .select2-selection').click(); + await page.locator('.select2-results__option', {hasText: 'Never'}).click(); + await page.waitForTimeout(1500); + posts.length = 0; + await page.waitForTimeout(1500); + assert.equal(posts.filter(post => post.action === 'status_card').length, 0, 'Never stops automatic refreshes'); + await page.locator('#syslog_status_refresh_now').click(); + await page.waitForFunction(() => document.querySelectorAll('#syslog_status_refresh_now .fa-spin').length === 0); + await page.waitForTimeout(100); + assert.equal(posts.filter(post => post.action === 'status_card').length, 3, 'Refresh now re-renders every card'); + + assert.equal(navigations, loaded, 'No status interaction reloads the page'); + assert.deepEqual(errors, []); + await browser.close(); + console.log('Status card add and callback refresh passed (browser fixture)'); +})().catch(error => { + console.error(error); + process.exit(1); +}); From a1d4f393a1c3c39a98de0ca5ca4e9986b3614d2c Mon Sep 17 00:00:00 2001 From: TheWitness Date: Fri, 20 Nov 2026 12:46:53 -0500 Subject: [PATCH 06/12] tests: use https fixture URLs and an explicit sort in the Status card fixture Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- tests/browser/status_cards.cjs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/browser/status_cards.cjs b/tests/browser/status_cards.cjs index a942cb4b..7c4bcc4a 100644 --- a/tests/browser/status_cards.cjs +++ b/tests/browser/status_cards.cjs @@ -41,7 +41,7 @@ $(function() { $('select').each(function() { $(this).select2({width: 'auto', min '/syslog_dashboard.js': path.join(root, 'js/syslog_dashboard.js') }; - await page.route('http://fixture/**', route => { + await page.route('https://fixture/**', route => { const url = new URL(route.request().url()); if (url.pathname === '/') { @@ -65,7 +65,7 @@ $(function() { $('select').each(function() { $(this).select2({width: 'auto', min return routes[url.pathname] ? route.fulfill({path: routes[url.pathname], contentType: 'text/javascript'}) : route.fulfill({status: 404, body: ''}); }); - await page.goto('http://fixture/'); + await page.goto('https://fixture/'); await page.waitForTimeout(200); assert.deepEqual(errors, []); assert.equal(await page.locator('#syslog_status_add').evaluate(select => select.classList.contains('select2-hidden-accessible')), true, 'The fixture uses select2 like Cacti'); @@ -90,7 +90,7 @@ $(function() { $('select').each(function() { $(this).select2({width: 'auto', min await page.waitForFunction(() => document.querySelectorAll('#syslog_status_grid .ccCard .ccCardBody').length === 3 && [...document.querySelectorAll('#syslog_status_grid .ccCardBody')].every(body => body.textContent.startsWith('Rendered')), null, {timeout: 5000}); assert.deepEqual(posts.find(post => post.action === 'status_refresh'), {action: 'status_refresh', tab: 'status', refresh: '1', __csrf_magic: 'fixture'}); - assert.deepEqual(posts.filter(post => post.action === 'status_card').map(post => post.card).sort(), ['latest_run', 'storage', 'workers']); + assert.deepEqual(posts.filter(post => post.action === 'status_card').map(post => post.card).sort((a, b) => a.localeCompare(b)), ['latest_run', 'storage', 'workers']); // Never stops the timer; the manual refresh still uses callbacks. await page.locator('#syslog_status_refresh + .select2 .select2-selection').click(); From abdd9dc2d54ef0df23ffe1cc66497f8f312aa823 Mon Sep 17 00:00:00 2001 From: TheWitness Date: Fri, 20 Nov 2026 12:58:58 -0500 Subject: [PATCH 07/12] syslog: copy partition rebuilds in 50,000-row batches A 10M-row sweep on a 2 GiB buffer pool found batches of 10,000 to 65,536 rows within measurement noise of each other, with 50,000 fastest. The batch size is now a parameter so the regression test can keep exercising multi-batch resume on a small table. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- CHANGELOG.md | 2 +- README.md | 2 +- cli/upgrade_partitioning.php | 18 +++++++++++------- tests/Unit/PartitionRebuildTest.php | 2 +- .../partition_rebuild_database_test.php | 5 +++-- 5 files changed, 17 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c57c8a3a..08181f3e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +11,7 @@ * feature: Refuse to save Syslog settings, install, or rebuild when retention, pre-create window and partitioning level need more partitions than the database server supports (8192, or 1024 before MariaDB 10.0.4 and MySQL 5.7), or when Hourly is combined with Indefinite retention * bug: Bound every log query at the current time so empty future partitions are always pruned, whatever the filter's own date range * bug: Purge Syslog Devices (Utilities) crashed with "Call to undefined function syslog_db_execute()" because the hook never connected to the Syslog database; it now connects first, reports connection failures, qualifies the Syslog database, and uses NOT EXISTS so a NULL host_id no longer prevents purging (#472) -* bug: The partition rebuild CLI now refuses to start while Syslog processing is enabled and holds the master process slot; copies stored and removed messages without seq into a replacement whose AUTO_INCREMENT starts above the original high-water mark; verifies each 10,000-row batch by checksum and checkpoints it for resumable retries; widens the incoming queue in place to keep its sequence identities; and refuses legacy DATETIME history before changing anything +* bug: The partition rebuild CLI now refuses to start while Syslog processing is enabled and holds the master process slot; copies stored and removed messages without seq into a replacement whose AUTO_INCREMENT starts above the original high-water mark; verifies each 50,000-row batch by checksum and checkpoints it for resumable retries; widens the incoming queue in place to keep its sequence identities; and refuses legacy DATETIME history before changing anything * docs: Refresh README release/source compatibility, installation guidance and source-checked behavior; add published release notes and 5.0 upgrade guidance * bug: Keep log column resizing within the results area on both log tabs, preserve message ellipsis, and use an accessible Font Awesome export button * feature: Standardize both schema and partition CLI help with Cacti-style version banners and options, separate maintenance warnings, and replace the unreleased millisecond converter with --convert-ms diff --git a/README.md b/README.md index fb383912..eecaa8b4 100644 --- a/README.md +++ b/README.md @@ -170,7 +170,7 @@ php plugins/syslog/cli/upgrade_partitioning.php --convert-ms The `--convert-ms` option is **MariaDB only**. It rebuilds the stored/removed message tables to `TIMESTAMP(3)`, preserving data in verified batches of at -most **10,000 rows**. The incoming queue (`syslog_incoming`) and replication +most **50,000 rows**. The incoming queue (`syslog_incoming`) and replication outbox (`syslog_replication_output`) are widened in place with `ALTER TABLE`, so their sequence numbers, which archival and replication use as identities, are unchanged and undelivered rows are never discarded. Already-converted diff --git a/cli/upgrade_partitioning.php b/cli/upgrade_partitioning.php index b11fe191..9c305c89 100644 --- a/cli/upgrade_partitioning.php +++ b/cli/upgrade_partitioning.php @@ -271,7 +271,7 @@ function syslog_upart_run_locked(bool $confirm, bool $convert_ms, bool $preserve print 'Rebuilding ' . ($granularity === 'hour' ? 'hourly' : 'daily') . " partitions: engine=$engine, retention=$days days, ahead=$ahead days." . PHP_EOL; print 'Tables: ' . implode(', ', $tables) . ($widen ? '; widened in place: ' . implode(', ', $widen) : '') . PHP_EOL; print $preserve_data - ? 'Messages are preserved in verified 10,000-row copies and receive new sequence numbers; originals are dropped only after verification.' . PHP_EOL + ? 'Messages are preserved in verified 50,000-row copies and receive new sequence numbers; originals are dropped only after verification.' . PHP_EOL : 'DESTRUCTIVE: stored messages in the listed rebuild tables will be DELETED.' . PHP_EOL; print $convert_ms ? 'Converting the complete message pipeline to TIMESTAMP(3).' . PHP_EOL : 'Existing timestamp precision is preserved.' . PHP_EOL; @@ -370,14 +370,18 @@ function syslog_upart_run_locked(bool $confirm, bool $convert_ms, bool $preserve * * Rows are copied without seq, in original order, so the replacement numbers * them from its own AUTO_INCREMENT, which starts above the original high-water - * mark: a sequence value is never reused. Each 10,000-row batch is checksummed - * against its source range before a checkpoint records it. A retry discards - * replacement rows above the last checkpoint and resumes after it. + * mark: a sequence value is never reused. Each batch, 50,000 rows by default, + * is checksummed against its source range before a checkpoint records it. A + * retry discards replacement rows above the last checkpoint and resumes after it. + * At 10M rows on a 2 GiB buffer pool, batches of 10,000 to 65,536 rows ran + * within measurement noise of each other; 50,000 was fastest. */ function syslog_upart_rebuild(string $table, bool $convert_ms, bool $preserve_data, string $engine, int $days, int $ahead, - string $granularity = 'day', int $limit = 8192): bool { + string $granularity = 'day', int $limit = 8192, int $batch = 50000): bool { global $syslogdb_default; + $batch = max(1, $batch); + $backup = $table . '_rebuild'; $resume = syslog_db_table_exists($backup, false); $source = $resume ? $backup : $table; @@ -605,7 +609,7 @@ function syslog_upart_rebuild(string $table, bool $convert_ms, bool $preserve_da } $last = syslog_db_fetch_row_prepared("SELECT source.seq, source.logtime FROM $saved AS source - WHERE $where ORDER BY $order LIMIT 1 OFFSET 9999", $params); + WHERE $where ORDER BY $order LIMIT 1 OFFSET " . ($batch - 1), $params); $final = $last === []; if ($final) { @@ -785,7 +789,7 @@ function syslog_upart_display_help(): void { print 'Options:' . PHP_EOL; print ' --partition - Save and apply this Partitioning Level instead of the one in Settings.' . PHP_EOL; print ' --convert-ms - Also convert pipeline timestamps to TIMESTAMP(3); MariaDB only.' . PHP_EOL; - print ' --preserve-data - Preserve existing messages (the default), copying 10,000 rows at a time.' . PHP_EOL; + print ' --preserve-data - Preserve existing messages (the default), copying 50,000 rows at a time.' . PHP_EOL; print ' --discard-data - Delete messages in rebuilt tables; never discard the outbox.' . PHP_EOL; print ' --confirm - Skip confirmation, not the size and maintenance warnings.' . PHP_EOL; print ' --version - Display version information.' . PHP_EOL; diff --git a/tests/Unit/PartitionRebuildTest.php b/tests/Unit/PartitionRebuildTest.php index 9b412207..721dbbb3 100644 --- a/tests/Unit/PartitionRebuildTest.php +++ b/tests/Unit/PartitionRebuildTest.php @@ -120,7 +120,7 @@ function partition_rebuild_setup(): stdClass { ->and($output)->toContain('10.00 GiB (10,737,418,240 bytes)') ->toContain('AT LEAST another 10.00 GiB (10,737,418,240 bytes)') ->toContain('DATABASE SERVER')->toContain('SEVERAL HOURS') - ->toContain('does NOT pause')->toContain('10,000-row copies') + ->toContain('does NOT pause')->toContain('50,000-row copies') ->toContain('receive new sequence numbers') ->not->toContain('Type "yes"') ->and($state->rebuilds)->toBe([['syslog', false, true, 'InnoDB', 30, 3, 'day', 8192]]) diff --git a/tests/regression/partition_rebuild_database_test.php b/tests/regression/partition_rebuild_database_test.php index 448f0b1e..8f37144f 100644 --- a/tests/regression/partition_rebuild_database_test.php +++ b/tests/regression/partition_rebuild_database_test.php @@ -139,7 +139,8 @@ function rebuild_table(string $table, bool $convert = true, string $engine = 'In ob_start(); try { - return syslog_upart_rebuild($table, $convert, true, $engine, 2, 2); + // 10,000-row batches keep the multi-batch resume paths covered on a small table. + return syslog_upart_rebuild($table, $convert, true, $engine, 2, 2, 'day', 8192, 10000); } finally { ob_end_clean(); } @@ -292,7 +293,7 @@ function rebuild_table(string $table, bool $convert = true, string $engine = 'In rebuild_check((int) syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog_removed') === 0, 'Only explicit discard mode empties a rebuilt table'); rebuild_check(!syslog_db_table_exists('syslog_removed_rebuild'), 'Explicit discard removes the original after verification'); rebuild_check((int) syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog_replication_output') === 1, 'Discard mode does not delete the outbox'); - rebuild_check(max($batch_sizes) <= 10000, 'No stored INSERT SELECT may copy more than 10000 rows'); + rebuild_check(max($batch_sizes) <= 50000, 'No stored INSERT SELECT may copy more than the 50000-row default batch'); ob_start(); rebuild_check(syslog_upart_rebuild('syslog', false, true, 'InnoDB', 2, 2, 'hour', 8192), 'A preserving rebuild converts daily partitions to hourly'); From 177415deb116ab0bfef8d0ba7ba2c79e2ceafef6 Mon Sep 17 00:00:00 2001 From: TheWitness Date: Fri, 20 Nov 2026 13:13:24 -0500 Subject: [PATCH 08/12] syslog: address Copilot review of the rebuild, installer and Status timer - Rebuild locking: cli/upgrade_partitioning.php and syslog_process.php now share one database lock (syslog_processing_lock_name()) instead of the Cacti process registry, which on 1.2.x can treat another PHP script as stale and takes over expired slots. GET_LOCK never waits for or terminates the other side, and is released when its connection closes. A single lock also avoids old servers dropping a first GET_LOCK when a second is taken. - --discard-data with no incoming/outbox widening no longer issues "TABLE_NAME IN ()". - README rollback restores only tables that still have a _rebuild original and leaves completed tables untouched. - Status auto-refresh: a late callback from a grid replaced by ajax navigation no longer resets the current grid's shared timer. - Installer limit check (syslog_partition_install_error()): a submitted Daily level wins over the saved one, and truncate ignores the old layouts of both tables, which it drops and recreates. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- README.md | 30 +- cli/upgrade_partitioning.php | 277 +++++++++--------- includes/functions.php | 39 +++ js/syslog_dashboard.js | 6 + locales/po/cacti.pot | 2 +- setup.php | 9 +- syslog_process.php | 8 + tests/Unit/PartitionGranularityTest.php | 14 + tests/Unit/PartitionRebuildTest.php | 45 +-- .../partition_rebuild_database_test.php | 42 ++- 10 files changed, 267 insertions(+), 205 deletions(-) diff --git a/README.md b/README.md index eecaa8b4..5e5fec61 100644 --- a/README.md +++ b/README.md @@ -279,8 +279,10 @@ changes, not routine rotation. **Maintenance requirements for both commands:** * Back up the database and uncheck **Settings > Syslog > Syslog Enabled**. The - utility refuses to start while processing is enabled, and holds the Syslog - master process slot so no processing run can start during the rebuild. It + utility refuses to start while processing is enabled or a processing run is + active, and holds a database lock that `syslog_process.php` also takes, so no + processing run can start during the rebuild. Neither side ever terminates the + other. It does **not** stop remote collectors, replication or external database writers: stop them and buffer new logs upstream. Keep everything stopped until the rebuild or recovery finishes. Live log tables are temporarily absent @@ -306,11 +308,25 @@ changes, not routine rotation. after the last recorded batch and resumes from there. Do not change settings or manually delete `_rebuild` or `syslog_rebuild_progress` tables during recovery. -* To abandon a rebuild instead, with writers still stopped: - `DROP TABLE syslog; RENAME TABLE syslog_rebuild TO syslog;` (likewise for - `syslog_removed`), then `DROP TABLE syslog_rebuild_progress;`. A - `syslog_incoming_rebuild` table left by an earlier development build is - restored the same way. +* To abandon a rebuild instead, keep writers stopped and restore **only** the + tables whose original still exists. A table that already finished has no + `
_rebuild` and must be left alone: its original has been dropped, and + the live table is the only copy. First list what remains: + + ```sql + SHOW TABLES LIKE '%\_rebuild'; + ``` + + For each `
_rebuild` listed (`syslog`, `syslog_removed`, or a + `syslog_incoming_rebuild` left by an earlier development build), and only + then, replace the partial copy with the original: + + ```sql + DROP TABLE IF EXISTS syslog; -- the partial replacement + RENAME TABLE syslog_rebuild TO syslog; -- the untouched original + ``` + + When no `_rebuild` table remains, `DROP TABLE syslog_rebuild_progress;`. ### Delivering sub-second timestamps from your log forwarders diff --git a/cli/upgrade_partitioning.php b/cli/upgrade_partitioning.php index 9c305c89..d5a19b32 100644 --- a/cli/upgrade_partitioning.php +++ b/cli/upgrade_partitioning.php @@ -108,7 +108,7 @@ * Settings once confirmed; null uses Settings. */ function syslog_upart_run(bool $confirm, bool $convert_ms, bool $preserve_data = true, ?string $partition = null): bool { - global $config, $syslogdb_default; + global $syslogdb_default; $mariadb = syslog_db_is_mariadb(); @@ -147,17 +147,19 @@ function syslog_upart_run(bool $confirm, bool $convert_ms, bool $preserve_data = } // Writers would interleave with copied rows and break verification, so - // processing must be disabled and its master slot held for the duration. + // processing must be disabled, and the processing lock that + // syslog_process.php also takes is held for the duration. Unlike Cacti's + // process registry, the lock never takes over or terminates a live run. if ((string) read_config_option('syslog_enabled', true) !== '') { fwrite(STDERR, 'ERROR: Syslog processing is enabled. Uncheck Settings > Syslog > Syslog Enabled, stop remote collectors and any other Syslog database writers, then retry. No changes were made.' . PHP_EOL); return false; } - $poller_id = (int) ($config['poller_id'] ?? 1); + $lock = syslog_processing_lock_name(); - if (!register_process_start('syslog', 'master', $poller_id, 7 * 86400)) { - fwrite(STDERR, 'ERROR: A Syslog processing run is still active; wait for it to finish, then retry. No changes were made.' . PHP_EOL); + if ((int) syslog_db_fetch_cell_prepared('SELECT GET_LOCK(?, 0)', [$lock]) !== 1) { + fwrite(STDERR, 'ERROR: A Syslog processing run or another rebuild is still active; wait for it to finish, then retry. No changes were made.' . PHP_EOL); return false; } @@ -165,204 +167,193 @@ function syslog_upart_run(bool $confirm, bool $convert_ms, bool $preserve_data = try { return syslog_upart_run_locked($confirm, $convert_ms, $preserve_data, $engines[strtolower($engine)], (int) $days, $ahead, $granularity, $limit, $partition !== null); } finally { - unregister_process('syslog', 'master', $poller_id); + syslog_db_fetch_cell_prepared('SELECT RELEASE_LOCK(?)', [$lock]); } } /** - * Rebuild while holding the Syslog master process slot. + * Rebuild while holding the Syslog processing lock. */ function syslog_upart_run_locked(bool $confirm, bool $convert_ms, bool $preserve_data, string $engine, int $days, int $ahead, string $granularity = 'day', int $limit = 8192, bool $save_level = false): bool { global $syslogdb_default; - $lock = hash('sha256', $syslogdb_default . '.syslog_upgrade_partitioning'); - - if ((int) syslog_db_fetch_cell_prepared('SELECT GET_LOCK(?, 0)', [$lock]) !== 1) { - fwrite(STDERR, 'ERROR: Cannot acquire the rebuild lock; another rebuild may be running.' . PHP_EOL); + if (syslog_db_table_exists('syslog_incoming_rebuild', false)) { + fwrite(STDERR, 'ERROR: syslog_incoming_rebuild was left by an earlier development build. Restore it as described in the README, then retry. No changes were made.' . PHP_EOL); return false; } - try { - if (syslog_db_table_exists('syslog_incoming_rebuild', false)) { - fwrite(STDERR, 'ERROR: syslog_incoming_rebuild was left by an earlier development build. Restore it as described in the README, then retry. No changes were made.' . PHP_EOL); + $tables = []; - return false; - } + foreach (['syslog', 'syslog_removed'] as $table) { + $backup_exists = syslog_db_table_exists($table . '_rebuild', false); - $tables = []; + if ($backup_exists && !$preserve_data) { + fwrite(STDERR, "ERROR: {$table}_rebuild exists from an unfinished rebuild. Retry with --preserve-data and the original --convert-ms option, if used. No data was discarded." . PHP_EOL); - foreach (['syslog', 'syslog_removed'] as $table) { - $backup_exists = syslog_db_table_exists($table . '_rebuild', false); + return false; + } - if ($backup_exists && !$preserve_data) { - fwrite(STDERR, "ERROR: {$table}_rebuild exists from an unfinished rebuild. Retry with --preserve-data and the original --convert-ms option, if used. No data was discarded." . PHP_EOL); + if ($backup_exists || syslog_db_table_exists($table, false)) { + $tables[] = $table; + } + } - return false; - } + // The incoming queue and replication outbox keep their sequence identities + // (archival and delivery deduplicate on them), so they are widened in place. + $widen = []; - if ($backup_exists || syslog_db_table_exists($table, false)) { - $tables[] = $table; - } + foreach (['syslog_incoming', 'syslog_replication_output'] as $table) { + if ($convert_ms && syslog_db_table_exists($table, false) && syslog_detect_logtime_precision($table) !== 'ms') { + $widen[] = $table; } + } - // The incoming queue and replication outbox keep their sequence identities - // (archival and delivery deduplicate on them), so they are widened in place. - $widen = []; + if (!$tables && !$widen) { + print 'No tables require rebuilding.' . PHP_EOL; - foreach (['syslog_incoming', 'syslog_replication_output'] as $table) { - if ($convert_ms && syslog_db_table_exists($table, false) && syslog_detect_logtime_precision($table) !== 'ms') { - $widen[] = $table; - } - } + return true; + } - if (!$tables && !$widen) { - print 'No tables require rebuilding.' . PHP_EOL; + // Legacy DATETIME history is wall-clock time in an unknown zone; it is + // not converted. Only an explicit discard may replace such tables. + $checked = $widen; - return true; + foreach ($tables as $table) { + if ($preserve_data) { + $checked[] = $table; + $checked[] = $table . '_rebuild'; } + } - // Legacy DATETIME history is wall-clock time in an unknown zone; it is - // not converted. Only an explicit discard may replace such tables. - $checked = $widen; + // A discard with nothing to widen checks no tables; IN () is invalid SQL. + $legacy = !$checked ? [] : syslog_db_fetch_assoc_prepared('SELECT TABLE_NAME FROM information_schema.COLUMNS + WHERE TABLE_SCHEMA = ? AND COLUMN_NAME = \'logtime\' AND DATA_TYPE = \'datetime\' + AND TABLE_NAME IN (' . implode(', ', array_fill(0, count($checked), '?')) . ')', + array_merge([$syslogdb_default], $checked)); - foreach ($tables as $table) { - if ($preserve_data) { - $checked[] = $table; - $checked[] = $table . '_rebuild'; - } - } + if (!is_array($legacy)) { + fwrite(STDERR, 'ERROR: Cannot read the logtime column types; no tables were changed.' . PHP_EOL); - $legacy = syslog_db_fetch_assoc_prepared('SELECT TABLE_NAME FROM information_schema.COLUMNS - WHERE TABLE_SCHEMA = ? AND COLUMN_NAME = \'logtime\' AND DATA_TYPE = \'datetime\' - AND TABLE_NAME IN (' . implode(', ', array_fill(0, count($checked), '?')) . ')', - array_merge([$syslogdb_default], $checked)); + return false; + } - if (!is_array($legacy)) { - fwrite(STDERR, 'ERROR: Cannot read the logtime column types; no tables were changed.' . PHP_EOL); + if ($legacy) { + fwrite(STDERR, 'ERROR: Legacy DATETIME logtime columns are not supported: ' . implode(', ', array_column($legacy, 'TABLE_NAME')) + . '. Rebuild stored and removed history with --discard-data, or convert it manually as described in the README. No changes were made.' . PHP_EOL); - return false; - } + return false; + } - if ($legacy) { - fwrite(STDERR, 'ERROR: Legacy DATETIME logtime columns are not supported: ' . implode(', ', array_column($legacy, 'TABLE_NAME')) - . '. Rebuild stored and removed history with --discard-data, or convert it manually as described in the README. No changes were made.' . PHP_EOL); + $bytes = syslog_db_fetch_cell_prepared('SELECT SUM(DATA_LENGTH + INDEX_LENGTH) + FROM information_schema.TABLES WHERE TABLE_SCHEMA = ?', [$syslogdb_default]); - return false; - } + if (!is_numeric($bytes)) { + fwrite(STDERR, 'ERROR: Cannot determine the current database size; no tables were changed.' . PHP_EOL); - $bytes = syslog_db_fetch_cell_prepared('SELECT SUM(DATA_LENGTH + INDEX_LENGTH) - FROM information_schema.TABLES WHERE TABLE_SCHEMA = ?', [$syslogdb_default]); + return false; + } - if (!is_numeric($bytes)) { - fwrite(STDERR, 'ERROR: Cannot determine the current database size; no tables were changed.' . PHP_EOL); + $size = number_format((float) $bytes / 1073741824, 2) . ' GiB (' . number_format((float) $bytes, 0) . ' bytes)'; + print '================================ WARNING ================================' . PHP_EOL; + print "Database $syslogdb_default size (estimated data + indexes): $size" . PHP_EOL; + print "Have AT LEAST another $size free on the DATABASE SERVER during transfer." . PHP_EOL; + print 'Allow additional headroom for temporary files, indexes and binary logs.' . PHP_EOL; + print 'Depending on the size of the Syslog tables and storage speed, this can' . PHP_EOL; + print 'take SEVERAL HOURS. Database metadata is an estimate, not a disk-space check.' . PHP_EOL; + print 'Rebuilding ' . ($granularity === 'hour' ? 'hourly' : 'daily') . " partitions: engine=$engine, retention=$days days, ahead=$ahead days." . PHP_EOL; + print 'Tables: ' . implode(', ', $tables) . ($widen ? '; widened in place: ' . implode(', ', $widen) : '') . PHP_EOL; + print $preserve_data + ? 'Messages are preserved in verified 50,000-row copies and receive new sequence numbers; originals are dropped only after verification.' . PHP_EOL + : 'DESTRUCTIVE: stored messages in the listed rebuild tables will be DELETED.' . PHP_EOL; + print $convert_ms ? 'Converting the complete message pipeline to TIMESTAMP(3).' . PHP_EOL + : 'Existing timestamp precision is preserved.' . PHP_EOL; + print 'Take a database backup. Syslog processing is disabled and its lock' . PHP_EOL; + print 'is held, but this utility does NOT pause remote collectors, replication or' . PHP_EOL; + print 'external database writers; stop them and buffer new logs upstream.' . PHP_EOL; + print 'Keep them stopped until completion or recovery.' . PHP_EOL; + print 'Live log tables are temporarily absent or incomplete during rebuilding.' . PHP_EOL; + print '========================================================================' . PHP_EOL; + + if (!$confirm) { + print 'Type "yes" to proceed, anything else to abort: '; + + if (strtolower(trim((string) fgets(STDIN))) !== 'yes') { + print 'Aborted; no tables were changed.' . PHP_EOL; return false; } + } - $size = number_format((float) $bytes / 1073741824, 2) . ' GiB (' . number_format((float) $bytes, 0) . ' bytes)'; - print '================================ WARNING ================================' . PHP_EOL; - print "Database $syslogdb_default size (estimated data + indexes): $size" . PHP_EOL; - print "Have AT LEAST another $size free on the DATABASE SERVER during transfer." . PHP_EOL; - print 'Allow additional headroom for temporary files, indexes and binary logs.' . PHP_EOL; - print 'Depending on the size of the Syslog tables and storage speed, this can' . PHP_EOL; - print 'take SEVERAL HOURS. Database metadata is an estimate, not a disk-space check.' . PHP_EOL; - print 'Rebuilding ' . ($granularity === 'hour' ? 'hourly' : 'daily') . " partitions: engine=$engine, retention=$days days, ahead=$ahead days." . PHP_EOL; - print 'Tables: ' . implode(', ', $tables) . ($widen ? '; widened in place: ' . implode(', ', $widen) : '') . PHP_EOL; - print $preserve_data - ? 'Messages are preserved in verified 50,000-row copies and receive new sequence numbers; originals are dropped only after verification.' . PHP_EOL - : 'DESTRUCTIVE: stored messages in the listed rebuild tables will be DELETED.' . PHP_EOL; - print $convert_ms ? 'Converting the complete message pipeline to TIMESTAMP(3).' . PHP_EOL - : 'Existing timestamp precision is preserved.' . PHP_EOL; - print 'Take a database backup. Syslog processing is disabled and its process slot' . PHP_EOL; - print 'is held, but this utility does NOT pause remote collectors, replication or' . PHP_EOL; - print 'external database writers; stop them and buffer new logs upstream.' . PHP_EOL; - print 'Keep them stopped until completion or recovery.' . PHP_EOL; - print 'Live log tables are temporarily absent or incomplete during rebuilding.' . PHP_EOL; - print '========================================================================' . PHP_EOL; - - if (!$confirm) { - print 'Type "yes" to proceed, anything else to abort: '; - - if (strtolower(trim((string) fgets(STDIN))) !== 'yes') { - print 'Aborted; no tables were changed.' . PHP_EOL; + // A retry must rebuild to the same level, so --partition is saved first. + if ($save_level) { + set_config_option('syslog_partition_granularity', $granularity); + } - return false; - } - } + $zone = syslog_db_fetch_cell('SELECT @@SESSION.time_zone'); + $mode = syslog_db_fetch_cell('SELECT @@SESSION.sql_mode'); + $lock_wait = syslog_db_fetch_cell('SELECT @@SESSION.lock_wait_timeout'); - // A retry must rebuild to the same level, so --partition is saved first. - if ($save_level) { - set_config_option('syslog_partition_granularity', $granularity); - } + if (!is_string($zone) || !is_string($mode) || !ctype_digit((string) $lock_wait)) { + fwrite(STDERR, 'ERROR: Cannot read the database session settings; no tables were changed.' . PHP_EOL); - $zone = syslog_db_fetch_cell('SELECT @@SESSION.time_zone'); - $mode = syslog_db_fetch_cell('SELECT @@SESSION.sql_mode'); - $lock_wait = syslog_db_fetch_cell('SELECT @@SESSION.lock_wait_timeout'); + return false; + } - if (!is_string($zone) || !is_string($mode) || !ctype_digit((string) $lock_wait)) { - fwrite(STDERR, 'ERROR: Cannot read the database session settings; no tables were changed.' . PHP_EOL); + $lock_wait = (int) $lock_wait; + $copy_mode = implode(',', array_diff(explode(',', $mode), ['NO_ZERO_DATE', 'NO_ZERO_IN_DATE', ''])); + + try { + if (!syslog_db_execute("SET SESSION time_zone = '+00:00'") + || !syslog_db_execute_prepared('SET SESSION sql_mode = ?', [$copy_mode]) + || !syslog_db_execute('SET SESSION lock_wait_timeout = 30')) { + fwrite(STDERR, 'ERROR: Cannot configure the copy session; no tables were changed.' . PHP_EOL); return false; } - $lock_wait = (int) $lock_wait; - $copy_mode = implode(',', array_diff(explode(',', $mode), ['NO_ZERO_DATE', 'NO_ZERO_IN_DATE', ''])); - - try { - if (!syslog_db_execute("SET SESSION time_zone = '+00:00'") - || !syslog_db_execute_prepared('SET SESSION sql_mode = ?', [$copy_mode]) - || !syslog_db_execute('SET SESSION lock_wait_timeout = 30')) { - fwrite(STDERR, 'ERROR: Cannot configure the copy session; no tables were changed.' . PHP_EOL); - + foreach ($tables as $table) { + if (!syslog_upart_rebuild($table, $convert_ms, $preserve_data, $engine, $days, $ahead, $granularity, $limit)) { return false; } + } - foreach ($tables as $table) { - if (!syslog_upart_rebuild($table, $convert_ms, $preserve_data, $engine, $days, $ahead, $granularity, $limit)) { - return false; - } - } - - $column = syslog_logtime_sql_type('ms'); + $column = syslog_logtime_sql_type('ms'); - foreach ($widen as $table) { - if (!syslog_db_execute("ALTER TABLE `$syslogdb_default`.`$table` MODIFY COLUMN logtime $column")) { - fwrite(STDERR, "ERROR: Cannot widen $table; the precision setting was not changed." . PHP_EOL); + foreach ($widen as $table) { + if (!syslog_db_execute("ALTER TABLE `$syslogdb_default`.`$table` MODIFY COLUMN logtime $column")) { + fwrite(STDERR, "ERROR: Cannot widen $table; the precision setting was not changed." . PHP_EOL); - return false; - } + return false; } + } - if ($convert_ms) { - foreach (['syslog', 'syslog_removed', 'syslog_incoming', 'syslog_replication_output'] as $table) { - if (syslog_db_table_exists($table, false) && syslog_detect_logtime_precision($table) !== 'ms') { - fwrite(STDERR, "ERROR: $table did not convert; the precision setting was not changed." . PHP_EOL); + if ($convert_ms) { + foreach (['syslog', 'syslog_removed', 'syslog_incoming', 'syslog_replication_output'] as $table) { + if (syslog_db_table_exists($table, false) && syslog_detect_logtime_precision($table) !== 'ms') { + fwrite(STDERR, "ERROR: $table did not convert; the precision setting was not changed." . PHP_EOL); - return false; - } + return false; } - - set_config_option('syslog_install_logtime_precision', 'ms'); } - } finally { - $restored_zone = syslog_db_execute_prepared('SET SESSION time_zone = ?', [$zone]); - $restored_mode = syslog_db_execute_prepared('SET SESSION sql_mode = ?', [$mode]); - $restored_wait = syslog_db_execute("SET SESSION lock_wait_timeout = $lock_wait"); - if (!$restored_zone || !$restored_mode || !$restored_wait) { - cacti_log('SYSLOG ERROR: Could not restore the rebuild session settings', false, 'SYSLOG'); - } + set_config_option('syslog_install_logtime_precision', 'ms'); } - - cacti_log('SYSLOG: Completed cli/upgrade_partitioning.php' . ($convert_ms ? ' with --convert-ms' : ''), false, 'SYSLOG'); - print 'Rebuild complete. Re-enable Syslog processing.' . ($convert_ms ? ' Syslog now stores millisecond timestamps; configure forwarders accordingly.' : '') . PHP_EOL; - - return true; } finally { - syslog_db_fetch_cell_prepared('SELECT RELEASE_LOCK(?)', [$lock]); + $restored_zone = syslog_db_execute_prepared('SET SESSION time_zone = ?', [$zone]); + $restored_mode = syslog_db_execute_prepared('SET SESSION sql_mode = ?', [$mode]); + $restored_wait = syslog_db_execute("SET SESSION lock_wait_timeout = $lock_wait"); + + if (!$restored_zone || !$restored_mode || !$restored_wait) { + cacti_log('SYSLOG ERROR: Could not restore the rebuild session settings', false, 'SYSLOG'); + } } + + cacti_log('SYSLOG: Completed cli/upgrade_partitioning.php' . ($convert_ms ? ' with --convert-ms' : ''), false, 'SYSLOG'); + print 'Rebuild complete. Re-enable Syslog processing.' . ($convert_ms ? ' Syslog now stores millisecond timestamps; configure forwarders accordingly.' : '') . PHP_EOL; + + return true; } /** diff --git a/includes/functions.php b/includes/functions.php index 891209b0..3be85ba6 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -2796,6 +2796,20 @@ function syslog_partition_ahead_days() { return $ahead_days; } +/** + * Name of the database lock shared by syslog_process.php and + * cli/upgrade_partitioning.php, so a rebuild never runs beside processing. + * GET_LOCK waits for nobody and terminates nothing; it is released when its + * connection closes, even after a crash. + * + * @return string A lock name within MySQL's 64-character limit. + */ +function syslog_processing_lock_name(): string { + global $syslogdb_default; + + return substr(hash('sha256', $syslogdb_default . '.syslog_processing'), 0, 60); +} + /** * Supported partition sizes, in seconds. * @@ -2989,6 +3003,31 @@ function syslog_partition_levels_error(string $requested, $retention_days, $ahea return $error; } +/** + * Check install or upgrade options against the server partition limit. + * + * @param array $options The install options. + * @param int|null $limit The server partition limit, or null to query it. + * @param string[]|null $layouts The existing tables' levels, or null to read them. + * + * @return string The error, or '' when the options fit. + */ +function syslog_partition_install_error(array $options, ?int $limit = null, ?array $layouts = null): string { + // A truncate drops and recreates both message tables, so no old layout remains. + if (($options['upgrade_type'] ?? '') === 'truncate') { + $layouts = []; + } + + return syslog_partition_levels_error( + // Match syslog_setup_table_new(): a submitted level wins over the saved one. + isset($options['partitioning']) ? ($options['partitioning'] === 'hour' ? 'hour' : 'day') : syslog_partition_requested_granularity(), + $options['days'] ?? read_config_option('syslog_retention'), + read_config_option('syslog_partition_ahead_days'), + $limit ?? syslog_partition_server_limit(), + $layouts ?? syslog_partition_existing_layouts() + ); +} + /** * Upper logtime bound added to every message query. Messages cannot be newer * than now, so the empty future partitions are always pruned, whatever the diff --git a/js/syslog_dashboard.js b/js/syslog_dashboard.js index 36205c08..8402d39b 100644 --- a/js/syslog_dashboard.js +++ b/js/syslog_dashboard.js @@ -299,6 +299,12 @@ function initSyslogStatus() { var refreshNow = document.getElementById('syslog_status_refresh_now'); function schedule() { + // A late callback from a grid that ajax navigation replaced must not + // touch the shared timer the current Status grid now owns. + if (!document.body.contains(grid)) { + return; + } + clearTimeout(window.syslogStatusRefreshTimer); var seconds = interval ? Number.parseInt(interval.value, 10) : 0; diff --git a/locales/po/cacti.pot b/locales/po/cacti.pot index a4d9edf6..ff009143 100644 --- a/locales/po/cacti.pot +++ b/locales/po/cacti.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: Cacti \n" "Report-Msgid-Bugs-To: developers@cacti.net\n" -"POT-Creation-Date: 2026-11-20 12:40-0500\n" +"POT-Creation-Date: 2026-11-20 13:12-0500\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: Cacti Developers >\n" "Language-Team: Cacti Developers \n" diff --git a/setup.php b/setup.php index bdec77d5..63d5c177 100644 --- a/setup.php +++ b/setup.php @@ -157,14 +157,7 @@ function syslog_execute_update(int $syslog_exists, array $options): void { $compatibility_options = $options; if (isset($options['partitioning']) || isset($options['days'])) { - $partition_error = syslog_partition_levels_error( - isset($options['partitioning']) && $options['partitioning'] === 'hour' ? 'hour' : syslog_partition_requested_granularity(), - $options['days'] ?? read_config_option('syslog_retention'), - read_config_option('syslog_partition_ahead_days'), - syslog_partition_server_limit(), - // A truncate recreates syslog only; syslog_removed keeps its layout. - syslog_partition_existing_layouts(($options['upgrade_type'] ?? '') === 'truncate' ? ['syslog_removed'] : ['syslog', 'syslog_removed']) - ); + $partition_error = syslog_partition_install_error($options); if ($partition_error !== '') { raise_message('syslog_error', $partition_error, MESSAGE_LEVEL_ERROR); diff --git a/syslog_process.php b/syslog_process.php index 78f60d2a..292a738b 100644 --- a/syslog_process.php +++ b/syslog_process.php @@ -230,6 +230,14 @@ exit(0); } +// cli/upgrade_partitioning.php holds this lock while it rebuilds the message +// tables; it is held until this process exits and its connection closes. +if ((int) syslog_db_fetch_cell_prepared('SELECT GET_LOCK(?, 0)', [syslog_processing_lock_name()]) !== 1) { + cacti_log('NOTE: Syslog processing skipped while cli/upgrade_partitioning.php rebuilds the message tables.', false, 'SYSLOG'); + unregister_process('syslog', 'master', $config['poller_id']); + exit(0); +} + // Only the registered master may migrate history or replace synchronized rules. if ($config['poller_id'] > 1) { require_once(__DIR__ . '/includes/processing.php'); diff --git a/tests/Unit/PartitionGranularityTest.php b/tests/Unit/PartitionGranularityTest.php index 8d0d614b..d3962a81 100644 --- a/tests/Unit/PartitionGranularityTest.php +++ b/tests/Unit/PartitionGranularityTest.php @@ -92,6 +92,20 @@ } }); +it('checks install options against the submitted level and only the layouts that survive', function () { + syslog_load_plugin_source('includes/functions.php'); + + // The saved setting asks for Hourly; the submitted Daily level wins. + test_override('read_config_option', fn($name) => ['syslog_partition_granularity' => 'hour', 'syslog_partition_ahead_days' => '3'][$name] ?? ''); + + expect(syslog_partition_install_error(['partitioning' => 'day', 'days' => '365'], 8192, ['day']))->toBe('') + ->and(syslog_partition_install_error(['days' => '365'], 8192, ['day']))->toContain('8,857 partitions') + // An upgrade keeps hourly tables, so Indefinite is refused even with Daily submitted. + ->and(syslog_partition_install_error(['partitioning' => 'day', 'days' => '0', 'upgrade_type' => 'upgrade'], 8192, ['hour']))->toContain('still use Hourly') + // A truncate recreates both tables, so their old Hourly layout no longer matters. + ->and(syslog_partition_install_error(['partitioning' => 'day', 'days' => '0', 'upgrade_type' => 'truncate'], 8192, ['hour']))->toBe(''); +}); + it('reads the partitioning level of each existing message table', function () { syslog_load_plugin_source('includes/functions.php'); diff --git a/tests/Unit/PartitionRebuildTest.php b/tests/Unit/PartitionRebuildTest.php index 721dbbb3..8a348fdd 100644 --- a/tests/Unit/PartitionRebuildTest.php +++ b/tests/Unit/PartitionRebuildTest.php @@ -11,8 +11,6 @@ function partition_rebuild_runner(): string { file_put_contents($path, ' [], 'precision' => 'sec', 'enabled' => '', - 'slot' => true, - 'slots' => [], + 'assoc' => [], 'legacy' => [], 'retention' => '30', 'granularity' => '' @@ -53,17 +50,11 @@ function partition_rebuild_setup(): stdClass { 'syslog_partition_ahead_days' => '3', 'syslog_enabled' => $state->enabled, 'syslog_partition_granularity' => $state->granularity ][$key] ?? ''); - test_override('register_process_start', static function (...$args) use ($state) { - $state->slots[] = array_merge(['start'], $args); + test_override('syslog_db_fetch_assoc_prepared', static function ($sql) use ($state) { + $state->assoc[] = $sql; - return $state->slot; + return str_contains($sql, "DATA_TYPE = 'datetime'") ? $state->legacy : []; }); - test_override('unregister_process', static function (...$args) use ($state) { - $state->slots[] = array_merge(['end'], $args); - - return true; - }); - test_override('syslog_db_fetch_assoc_prepared', static fn($sql) => str_contains($sql, "DATA_TYPE = 'datetime'") ? $state->legacy : []); test_override('syslog_db_execute', static function ($sql) use ($state) { $state->statements[] = $sql; @@ -124,7 +115,7 @@ function partition_rebuild_setup(): stdClass { ->toContain('receive new sequence numbers') ->not->toContain('Type "yes"') ->and($state->rebuilds)->toBe([['syslog', false, true, 'InnoDB', 30, 3, 'day', 8192]]) - ->and($state->slots)->toBe([['start', 'syslog', 'master', 1, 604800], ['end', 'syslog', 'master', 1]]); + ->and($state->queries[0])->toBe(['SELECT GET_LOCK(?, 0)', [syslog_processing_lock_name()]]); expect($state->settings)->toBe([]); expect(array_column($state->queries, 0))->toContain('SELECT RELEASE_LOCK(?)'); }); @@ -158,7 +149,7 @@ function partition_rebuild_setup(): stdClass { $run = partition_rebuild_runner(); expect($run(true, false, true, 'hour'))->toBeFalse() - ->and($state->slots)->toBe([]) + ->and($state->queries)->toBe([]) ->and($state->settings)->toBe([]) ->and($state->rebuilds)->toBe([]); } @@ -170,23 +161,21 @@ function partition_rebuild_setup(): stdClass { $run = partition_rebuild_runner(); expect($run(true, false))->toBeFalse() - ->and($state->slots)->toBe([]) ->and($state->queries)->toBe([]) ->and($state->rebuilds)->toBe([]); }); -it('refuses to rebuild while a processing run holds the master slot', function () { +it('refuses to rebuild while processing or another rebuild holds the processing lock', function () { $state = partition_rebuild_setup(); - $state->slot = false; + $state->lock = 0; $run = partition_rebuild_runner(); expect($run(true, false))->toBeFalse() - ->and($state->slots)->toBe([['start', 'syslog', 'master', 1, 604800]]) - ->and($state->queries)->toBe([]) + ->and($state->queries)->toBe([['SELECT GET_LOCK(?, 0)', [syslog_processing_lock_name()]]]) ->and($state->rebuilds)->toBe([]); }); -it('refuses legacy DATETIME history before any change and releases the slot', function () { +it('refuses legacy DATETIME history before any change and releases the lock', function () { $state = partition_rebuild_setup(); $state->legacy = [['TABLE_NAME' => 'syslog']]; $run = partition_rebuild_runner(); @@ -194,7 +183,19 @@ function partition_rebuild_setup(): stdClass { expect($run(true, false))->toBeFalse() ->and($state->rebuilds)->toBe([]) ->and($state->statements)->toBe([]) - ->and(end($state->slots))->toBe(['end', 'syslog', 'master', 1]); + ->and(end($state->queries))->toBe(['SELECT RELEASE_LOCK(?)', [syslog_processing_lock_name()]]); +}); + +it('discards without a column-type query when no table needs checking', function () { + $state = partition_rebuild_setup(); + $run = partition_rebuild_runner(); + ob_start(); + $ok = $run(true, false, false); + ob_end_clean(); + + expect($ok)->toBeTrue() + ->and(array_filter($state->assoc, static fn($sql) => str_contains($sql, 'information_schema.COLUMNS')))->toBe([]) + ->and($state->rebuilds)->toBe([['syslog', false, false, 'InnoDB', 30, 3, 'day', 8192]]); }); it('widens the incoming queue in place so its sequence identities are kept', function () { diff --git a/tests/regression/partition_rebuild_database_test.php b/tests/regression/partition_rebuild_database_test.php index 8f37144f..d4101427 100644 --- a/tests/regression/partition_rebuild_database_test.php +++ b/tests/regression/partition_rebuild_database_test.php @@ -26,9 +26,6 @@ $corrupt_after_copy = ''; $batch_sizes = []; $checks = 0; -$config = ['poller_id' => 1]; -$slot_free = true; -$slot_calls = []; function rebuild_check(bool $ok, string $message): void { $GLOBALS['checks']++; @@ -41,16 +38,6 @@ function rebuild_check(bool $ok, string $message): void { function syslog_connect() { return true; } function __($text, ...$args) { array_pop($args); return $args ? vsprintf($text, $args) : $text; } function read_config_option($name, $force = false) { return $GLOBALS['options'][$name] ?? ''; } -function register_process_start($type, $name, $id = 0, $timeout = 300) { - $GLOBALS['slot_calls'][] = ['start', $type, $name, $id]; - - return $GLOBALS['slot_free']; -} -function unregister_process($type, $name, $id = 0, $pid = -1) { - $GLOBALS['slot_calls'][] = ['end', $type, $name, $id]; - - return true; -} function set_config_option($name, $value) { $GLOBALS['options'][$name] = $value; } function cacti_log(...$args) {} function syslog_db_execute_prepared($sql, $params = [], ...$args) { @@ -111,7 +98,8 @@ function syslog_partition_ahead_days() { return (int) read_config_option('syslog $helper_code = ''; foreach (['syslog_partition_periods', 'syslog_partition_requested_granularity', 'syslog_partition_ahead_days_for', - 'syslog_partition_server_limit', 'syslog_partition_projected_count', 'syslog_partition_settings_error'] as $helper) { + 'syslog_partition_server_limit', 'syslog_partition_projected_count', 'syslog_partition_settings_error', + 'syslog_processing_lock_name'] as $helper) { if (preg_match('/^function ' . $helper . '\(.*?^\}/ms', $helpers, $match) !== 1) { throw new RuntimeException("Cannot find $helper"); } @@ -135,6 +123,10 @@ function syslog_partition_ahead_days() { return (int) read_config_option('syslog unlink($fixture); } +function is_free_processing_lock(): bool { + return (int) syslog_db_fetch_cell_prepared('SELECT IS_FREE_LOCK(?)', [syslog_processing_lock_name()]) === 1; +} + function rebuild_table(string $table, bool $convert = true, string $engine = 'InnoDB'): bool { ob_start(); @@ -223,13 +215,12 @@ function rebuild_table(string $table, bool $convert = true, string $engine = 'In rebuild_check(!rebuild_table('syslog_removed', false, 'Aria'), 'Preserving a legacy DATETIME table is refused'); rebuild_check(!syslog_db_table_exists('syslog_removed_rebuild'), 'The DATETIME refusal happens before any change'); $options['syslog_enabled'] = ''; - $slot_calls = []; ob_start(); rebuild_check(!syslog_upart_run(true, false), 'The command refuses legacy DATETIME history before any change'); ob_end_clean(); rebuild_check(syslog_db_fetch_cell("SELECT DATA_TYPE FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='syslog_removed' AND COLUMN_NAME='logtime'") === 'datetime' && !syslog_db_table_exists('syslog_removed_rebuild'), 'A refused command leaves DATETIME tables untouched'); - rebuild_check($slot_calls === [['start', 'syslog', 'master', 1], ['end', 'syslog', 'master', 1]], 'The processing slot is released after a refusal'); + rebuild_check(is_free_processing_lock(), 'The processing lock is released after a refusal'); ob_start(); rebuild_check(syslog_upart_rebuild('syslog_removed', false, false, 'InnoDB', 2, 2), 'Explicit discard mode replaces a legacy DATETIME table'); ob_end_clean(); @@ -237,18 +228,19 @@ function rebuild_table(string $table, bool $convert = true, string $engine = 'In WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='syslog_removed' AND COLUMN_NAME='logtime'") === 'timestamp', 'A discarded DATETIME table is recreated as TIMESTAMP'); set_config_option('syslog_enabled', 'on'); - $slot_calls = []; ob_start(); rebuild_check(!syslog_upart_run(true, false), 'The command refuses to run while Syslog processing is enabled'); ob_end_clean(); - rebuild_check($slot_calls === [], 'Enabled processing is refused before taking the process slot'); + rebuild_check(is_free_processing_lock(), 'Enabled processing is refused before taking the processing lock'); set_config_option('syslog_enabled', ''); - $slot_free = false; + // A second connection stands in for a running syslog_process.php. + $holder = new PDO($dsn, 'root', getenv('SYSLOG_TEST_MYSQL_PASSWORD') ?: '', [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]); + $holder->prepare('SELECT GET_LOCK(?, 0)')->execute([syslog_processing_lock_name()]); ob_start(); - rebuild_check(!syslog_upart_run(true, false), 'The command refuses to run while a processing run holds the master slot'); + rebuild_check(!syslog_upart_run(true, false), 'The command refuses to run while a processing run holds the processing lock'); ob_end_clean(); rebuild_check(!syslog_db_table_exists('syslog_rebuild') && !syslog_db_table_exists('syslog_removed_rebuild'), 'Refusals leave the message tables untouched'); - $slot_free = true; + $holder = null; $pdo->exec('CREATE TABLE syslog_incoming (seq BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, logtime TIMESTAMP NOT NULL, status INT, message TEXT) ENGINE=InnoDB'); $pdo->exec("INSERT INTO syslog_incoming VALUES(30,UTC_DATE(),1,'queued'),(40,UTC_DATE(),0,'new')"); @@ -262,7 +254,6 @@ function rebuild_table(string $table, bool $convert = true, string $engine = 'In $stored_epochs = syslog_db_fetch_cell("SELECT SUM(UNIX_TIMESTAMP(logtime)) FROM syslog WHERE logtime > '1970-01-01'"); $options['syslog_install_logtime_precision'] = 'sec'; $fail_prefix = "ALTER TABLE `$syslogdb_default`.`syslog_replication_output`"; - $slot_calls = []; ob_start(); rebuild_check(!syslog_upart_run(true, true), 'Outbox conversion failure must fail the overall operation'); $output = ob_get_clean(); @@ -270,8 +261,7 @@ function rebuild_table(string $table, bool $convert = true, string $engine = 'In && str_contains($output, 'SEVERAL HOURS'), 'Size, extra-space and duration warnings are mandatory with --confirm'); rebuild_check($options['syslog_install_logtime_precision'] === 'sec', 'Partial conversion must not update the precision setting'); rebuild_check(syslog_db_fetch_cell('SELECT @@SESSION.time_zone') === '+05:30', 'The original non-UTC session zone is restored after failure'); - rebuild_check((int) syslog_db_fetch_cell("SELECT IS_FREE_LOCK('" . hash('sha256', $syslogdb_default . '.syslog_upgrade_partitioning') . "')") === 1, 'The named lock is released after failure'); - rebuild_check(end($slot_calls) === ['end', 'syslog', 'master', 1], 'The processing slot is released after failure'); + rebuild_check(is_free_processing_lock(), 'The processing lock is released after failure'); $fail_prefix = ''; ob_start(); rebuild_check(syslog_upart_run(true, true), 'The complete pipeline can finish after the outbox failure is fixed'); @@ -314,6 +304,10 @@ function rebuild_table(string $table, bool $convert = true, string $engine = 'In ob_end_clean(); rebuild_check((int) syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog') === (int) $total && syslog_upart_layout_ok('syslog', 'InnoDB', 'day'), 'Every row survives the daily rebuild'); rebuild_check(syslog_upart_partition_sql(400, 2, null, null, null, 'hour', 8192) === null, 'Hourly plans above the server partition limit are refused'); + ob_start(); + rebuild_check(syslog_upart_run(true, false, false), 'The discard command runs when no table needs a column-type check'); + ob_end_clean(); + rebuild_check((int) syslog_db_fetch_cell('SELECT COUNT(*) FROM syslog') === 0 && is_free_processing_lock(), 'The discard command empties the rebuilt tables and releases the lock'); print "$checks partition rebuild database checks passed.\n"; } finally { From d92a8e474aabd1b6e20f055f20d063e36a90796b Mon Sep 17 00:00:00 2001 From: TheWitness Date: Fri, 20 Nov 2026 13:31:29 -0500 Subject: [PATCH 09/12] syslog: hold the processing lock on its own connection; repair and settings checks - syslog_process.php takes the processing lock through the new syslog_processing_lock_acquire(), on a separate connection to the same server as $syslog_cnn, opened from the details Cacti recorded for it (bypassing the cached session and persistent handles). MySQL before 5.7.5 keeps one named lock per session, so the GET_LOCK calls made by partition maintenance and alert delivery on the Syslog connection could otherwise release it mid-run and let a rebuild overlap. - Repair (automatic and cli/syslog_schema.php) recreates a missing syslog table from the current retention rather than the install-time days, and checks the plan against the server partition limit before any DDL. - The Syslog settings save is refused when the Syslog server's version or table layouts cannot be read, rather than validating against the Cacti server's version and no layouts. The installer check refuses likewise when the existing layouts cannot be read. - README: the DATETIME round-trip check counts values UNIX_TIMESTAMP() cannot convert (NULL-safe comparison). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- README.md | 5 +- includes/functions.php | 105 +++++++++++++- includes/schema.php | 25 +++- includes/settings.php | 24 ++-- locales/po/cacti.pot | 10 +- syslog_process.php | 16 ++- tests/Unit/PartitionGranularityTest.php | 13 ++ tests/Unit/ProcessingLockTest.php | 135 ++++++++++++++++++ tests/Unit/UpgradeTruncateTest.php | 2 + tests/regression/cli_schema_upgrade_test.php | 2 + .../regression/review_fixes_database_test.php | 20 +++ 11 files changed, 331 insertions(+), 26 deletions(-) create mode 100644 tests/Unit/ProcessingLockTest.php diff --git a/README.md b/README.md index 5e5fec61..dd50ed03 100644 --- a/README.md +++ b/README.md @@ -252,7 +252,7 @@ that zone, then convert and repartition in one statement: ```sql SET SESSION time_zone = 'America/New_York'; -- the zone the values were written in SELECT COUNT(*) FROM syslog - WHERE FROM_UNIXTIME(UNIX_TIMESTAMP(logtime)) <> logtime; -- must be 0 + WHERE NOT (FROM_UNIXTIME(UNIX_TIMESTAMP(logtime)) <=> logtime); -- must be 0 ALTER TABLE syslog MODIFY COLUMN logtime timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' PARTITION BY RANGE (UNIX_TIMESTAMP(logtime)) (/* daily partitions */ PARTITION dMaxValue VALUES LESS THAN MAXVALUE); @@ -282,8 +282,7 @@ changes, not routine rotation. utility refuses to start while processing is enabled or a processing run is active, and holds a database lock that `syslog_process.php` also takes, so no processing run can start during the rebuild. Neither side ever terminates the - other. It - does **not** stop remote collectors, replication or external database + other. It does **not** stop remote collectors, replication or external database writers: stop them and buffer new logs upstream. Keep everything stopped until the rebuild or recovery finishes. Live log tables are temporarily absent or incomplete; avoid using log views during the rebuild. DDL waits at most diff --git a/includes/functions.php b/includes/functions.php index 3be85ba6..cf89f892 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -2810,6 +2810,81 @@ function syslog_processing_lock_name(): string { return substr(hash('sha256', $syslogdb_default . '.syslog_processing'), 0, 60); } +/** + * Take the processing lock for syslog_process.php on a connection of its own. + * MySQL before 5.7.5 holds one named lock per session, so the GET_LOCK calls + * partition maintenance and alert delivery make on the Syslog connection + * would otherwise release it while processing continues. The new connection + * reaches the same server as $syslog_cnn and stays open until the process + * exits. + * + * @return bool|null True when held, false when another session holds it, and + * null when the lock connection could not be opened. + */ +function syslog_processing_lock_acquire(): ?bool { + global $syslog_cnn, $database_sessions, $database_details, $database_persist; + + $details = null; + + foreach ((array) $database_details as $entry) { + if (is_array($entry) && is_object($syslog_cnn) && ($entry['database_conn'] ?? null) === $syslog_cnn) { + $details = $entry; + + break; + } + } + + if ($details === null) { + return null; + } + + // db_connect_real() returns a cached session for the same server and + // database, and a persistent PDO handle would share this session too. + $key = $details['database_hostname'] . ':' . $details['database_port'] . ':' . $details['database_default']; + $cached = $database_sessions[$key] ?? null; + $persist = $database_persist ?? false; + + unset($database_sessions[$key]); + $database_persist = false; + + $args = [ + (string) $details['database_hostname'], (string) $details['database_username'], (string) $details['database_password'], + (string) $details['database_default'], (string) ($details['database_type'] ?? 'mysql'), (int) $details['database_port'], + (int) ($details['database_retries'] ?? 5), (bool) ($details['database_ssl'] ?? false), (string) ($details['database_ssl_key'] ?? ''), + (string) ($details['database_ssl_cert'] ?? ''), (string) ($details['database_ssl_ca'] ?? ''), + ]; + + // Cacti 1.3 also records the CA path and server certificate verification. + if (array_key_exists('database_ssl_capath', $details)) { + $args[] = (string) $details['database_ssl_capath']; + $args[] = (bool) ($details['database_ssl_verify_server_cert'] ?? true); + } + + try { + $cnn = db_connect_real(...$args); + } finally { + if ($cached !== null) { + $database_sessions[$key] = $cached; + } else { + unset($database_sessions[$key]); + } + + $database_persist = $persist; + } + + if (!is_object($cnn)) { + return null; + } + + if ((int) db_fetch_cell_prepared('SELECT GET_LOCK(?, 0)', [syslog_processing_lock_name()], '', false, $cnn) !== 1) { + return false; + } + + $GLOBALS['syslog_processing_lock_cnn'] = $cnn; + + return true; +} + /** * Supported partition sizes, in seconds. * @@ -2832,17 +2907,22 @@ function syslog_partition_requested_granularity(): string { /** * The partitioning level a table actually uses, read from its partition names. * - * @param string $table The partitioned table. + * @param string $table The partitioned table. + * @param bool $strict Return '' rather than 'day' when the lookup fails. * * @return string 'hour' when the table has hYYYYMMDDHH partitions, otherwise 'day'. */ -function syslog_partition_layout_granularity(string $table): string { +function syslog_partition_layout_granularity(string $table, bool $strict = false): string { global $syslogdb_default; $hourly = syslog_db_fetch_cell_prepared("SELECT COUNT(*) FROM information_schema.PARTITIONS WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? AND PARTITION_NAME LIKE 'h%'", [$syslogdb_default, $table]); + if (!is_numeric($hourly) && $strict) { + return ''; + } + return is_numeric($hourly) && (int) $hourly > 0 ? 'hour' : 'day'; } @@ -2957,14 +3037,21 @@ function syslog_partition_settings_error(string $granularity, $retention_days, $ * * @param string[] $tables The tables to inspect. * - * @return string[] Distinct levels, empty when none of the tables exists. + * @return string[]|null Distinct levels, empty when none of the tables exists, + * or null when a table's level could not be read. */ -function syslog_partition_existing_layouts(array $tables = ['syslog', 'syslog_removed']): array { +function syslog_partition_existing_layouts(array $tables = ['syslog', 'syslog_removed']): ?array { $layouts = []; foreach ($tables as $table) { if (syslog_db_table_exists($table, false)) { - $layouts[] = syslog_partition_layout_granularity($table); + $layout = syslog_partition_layout_granularity($table, true); + + if ($layout === '') { + return null; + } + + $layouts[] = $layout; } } @@ -3018,13 +3105,19 @@ function syslog_partition_install_error(array $options, ?int $limit = null, ?arr $layouts = []; } + $layouts = $layouts ?? syslog_partition_existing_layouts(); + + if ($layouts === null) { + return __('The partitioning level of the existing Syslog tables could not be read.', 'syslog'); + } + return syslog_partition_levels_error( // Match syslog_setup_table_new(): a submitted level wins over the saved one. isset($options['partitioning']) ? ($options['partitioning'] === 'hour' ? 'hour' : 'day') : syslog_partition_requested_granularity(), $options['days'] ?? read_config_option('syslog_retention'), read_config_option('syslog_partition_ahead_days'), $limit ?? syslog_partition_server_limit(), - $layouts ?? syslog_partition_existing_layouts() + $layouts ); } diff --git a/includes/schema.php b/includes/schema.php index 53bbc178..53060435 100644 --- a/includes/schema.php +++ b/includes/schema.php @@ -771,6 +771,16 @@ function syslog_setup_table_new(array $options, bool $repair = false): void { // set table construction settings for the remote pollers $granularity = ($options['partitioning'] ?? read_config_option('syslog_partition_granularity')) === 'hour' ? 'hour' : 'day'; + // Repair recreates missing tables under the current settings, so their + // history follows the effective retention, not the install-time value. + if ($repair) { + $retention = read_config_option('syslog_retention'); + + if (is_numeric($retention) && (int) $retention >= 0) { + $options['days'] = (int) $retention; + } + } + if (!$repair) { set_config_option('syslog_install_upgrade_type', empty($options['upgrade_type']) ? '' : $options['upgrade_type'], true); set_config_option('syslog_install_engine', $engine, true); @@ -785,8 +795,19 @@ function syslog_setup_table_new(array $options, bool $repair = false): void { } // The syslog table is created partitioned; the helper also selects the - // matching ROW_FORMAT for the chosen engine. - syslog_create_partitioned_syslog_table($engine, $options['days'], read_config_option('syslog_partition_ahead_days'), $precision, $granularity); + // matching ROW_FORMAT for the chosen engine. Check the plan against the + // server's partition limit before creating a missing table. + $plan_error = ''; + + if (!$syslogexists || $truncate) { + $plan_error = syslog_partition_settings_error($granularity, $options['days'], read_config_option('syslog_partition_ahead_days'), syslog_partition_server_limit()); + } + + if ($plan_error !== '') { + cacti_log('SYSLOG ERROR: The syslog table was not created. ' . $plan_error, false, 'SYSLOG'); + } else { + syslog_create_partitioned_syslog_table($engine, $options['days'], read_config_option('syslog_partition_ahead_days'), $precision, $granularity); + } syslog_db_execute("CREATE TABLE IF NOT EXISTS `$syslogdb_default`.`syslog_alert` ( `id` int(10) NOT NULL auto_increment, diff --git a/includes/settings.php b/includes/settings.php index 84f446fd..7ca4020b 100644 --- a/includes/settings.php +++ b/includes/settings.php @@ -427,20 +427,24 @@ function syslog_settings_partition_gate(?string $version = null, ?array $layouts return ''; } - // syslog_connect() loads the helpers even when the Syslog database is down. + // Validate only against the Syslog server's own version and table layouts. if ($version === null || $layouts === null) { $connected = syslog_connect(); - $version = $version ?? (string) ($connected ? syslog_db_fetch_cell('SELECT VERSION()') : db_fetch_cell('SELECT VERSION()')); - $layouts = $layouts ?? ($connected ? syslog_partition_existing_layouts() : []); + $version = $version ?? ($connected ? (string) syslog_db_fetch_cell('SELECT VERSION()') : ''); + $layouts = $layouts ?? ($connected ? syslog_partition_existing_layouts() : null); } - $error = syslog_partition_levels_error( - get_nfilter_request_var('syslog_partition_granularity') === 'hour' ? 'hour' : 'day', - get_nfilter_request_var('syslog_retention'), - get_nfilter_request_var('syslog_partition_ahead_days'), - syslog_partition_server_limit($version), - $layouts - ); + if ($version === '' || $layouts === null) { + $error = __('The Syslog database is unavailable, so the partitioning settings cannot be checked against its partition limit and tables.', 'syslog'); + } else { + $error = syslog_partition_levels_error( + get_nfilter_request_var('syslog_partition_granularity') === 'hour' ? 'hour' : 'day', + get_nfilter_request_var('syslog_retention'), + get_nfilter_request_var('syslog_partition_ahead_days'), + syslog_partition_server_limit($version), + $layouts + ); + } if ($error === '') { return ''; diff --git a/locales/po/cacti.pot b/locales/po/cacti.pot index ff009143..7e7aab2e 100644 --- a/locales/po/cacti.pot +++ b/locales/po/cacti.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: Cacti \n" "Report-Msgid-Bugs-To: developers@cacti.net\n" -"POT-Creation-Date: 2026-11-20 13:12-0500\n" +"POT-Creation-Date: 2026-11-20 13:31-0500\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: Cacti Developers >\n" "Language-Team: Cacti Developers \n" @@ -315,6 +315,10 @@ msgstr "" msgid "The tables still use %s partitions until cli/upgrade_partitioning.php rebuilds them." msgstr "" +#: includes/functions.php +msgid "The partitioning level of the existing Syslog tables could not be read." +msgstr "" + #: includes/functions.php msgid "Automatic partition recovery deferred: dMaxValue size is unavailable; operator intervention required." msgstr "" @@ -1216,6 +1220,10 @@ msgstr "" msgid "%d second" msgstr "" +#: includes/settings.php +msgid "The Syslog database is unavailable, so the partitioning settings cannot be checked against its partition limit and tables." +msgstr "" + #: includes/settings.php msgid "The Syslog settings were not saved." msgstr "" diff --git a/syslog_process.php b/syslog_process.php index 292a738b..7671240f 100644 --- a/syslog_process.php +++ b/syslog_process.php @@ -231,11 +231,19 @@ } // cli/upgrade_partitioning.php holds this lock while it rebuilds the message -// tables; it is held until this process exits and its connection closes. -if ((int) syslog_db_fetch_cell_prepared('SELECT GET_LOCK(?, 0)', [syslog_processing_lock_name()]) !== 1) { - cacti_log('NOTE: Syslog processing skipped while cli/upgrade_partitioning.php rebuilds the message tables.', false, 'SYSLOG'); +// tables. It is taken on a connection of its own and held until this process +// exits and that connection closes. +$processing_lock = syslog_processing_lock_acquire(); + +if ($processing_lock !== true) { + if ($processing_lock === false) { + cacti_log('NOTE: Syslog processing skipped while cli/upgrade_partitioning.php rebuilds the message tables.', false, 'SYSLOG'); + } else { + cacti_log('ERROR: Syslog processing skipped; the processing lock connection could not be opened.', false, 'SYSLOG'); + } + unregister_process('syslog', 'master', $config['poller_id']); - exit(0); + exit($processing_lock === false ? 0 : 1); } // Only the registered master may migrate history or replace synchronized rules. diff --git a/tests/Unit/PartitionGranularityTest.php b/tests/Unit/PartitionGranularityTest.php index d3962a81..5ddced8b 100644 --- a/tests/Unit/PartitionGranularityTest.php +++ b/tests/Unit/PartitionGranularityTest.php @@ -78,6 +78,11 @@ expect(syslog_settings_partition_gate('10.11.6-MariaDB', ['hour']))->toStartWith('The tables still use Hourly partitions')->toContain('Indefinite') ->and(syslog_settings_partition_gate('10.11.6-MariaDB', ['day']))->toBe(''); + // Without the Syslog server's version the limit cannot be checked. + $messages = []; + expect(syslog_settings_partition_gate('', ['day']))->toContain('Syslog database is unavailable') + ->and($messages[0][1])->toContain('The Syslog settings were not saved.'); + $_SERVER['REQUEST_METHOD'] = 'GET'; expect(syslog_settings_partition_gate('10.0.3-MariaDB', ['hour']))->toBe(''); } finally { @@ -119,6 +124,14 @@ test_override('syslog_db_table_exists', fn() => true); expect(syslog_partition_existing_layouts())->toBe(['hour', 'day']); + + // A failed lookup must not pass for Daily tables. + test_override('syslog_db_fetch_cell_prepared', fn() => false); + + expect(syslog_partition_layout_granularity('syslog'))->toBe('day') + ->and(syslog_partition_existing_layouts())->toBeNull() + ->and(syslog_partition_install_error(['partitioning' => 'day', 'days' => '30'], 8192))->toContain('could not be read') + ->and(syslog_partition_install_error(['partitioning' => 'day', 'days' => '30', 'upgrade_type' => 'truncate'], 8192))->toBe(''); }); it('recovers hourly partitions in bounded runs and checks only the horizon once it exists', function () { diff --git a/tests/Unit/ProcessingLockTest.php b/tests/Unit/ProcessingLockTest.php new file mode 100644 index 00000000..e43ec8b2 --- /dev/null +++ b/tests/Unit/ProcessingLockTest.php @@ -0,0 +1,135 @@ + + */ +function syslog_test_lock_details(object $cnn, bool $cacti13): array { + $details = [ + 'database_conn' => $cnn, + 'database_hostname' => 'syslog-db', + 'database_username' => 'syslog', + 'database_password' => 'secret', + 'database_default' => 'syslog', + 'database_type' => 'mysql', + 'database_port' => 3307, + 'database_retries' => 2, + 'database_ssl' => true, + 'database_ssl_key' => 'key.pem', + 'database_ssl_cert' => 'cert.pem', + 'database_ssl_ca' => 'ca.pem', + 'database_persist' => false, + ]; + + if ($cacti13) { + $details['database_ssl_capath'] = '/etc/ssl/certs'; + $details['database_ssl_verify_server_cert'] = false; + } + + return $details; +} + +beforeEach(function () { + syslog_load_plugin_source('includes/functions.php'); + + $this->saved = []; + + foreach (['syslog_cnn', 'syslogdb_default', 'database_sessions', 'database_details', 'database_persist', 'syslog_processing_lock_cnn'] as $name) { + $this->saved[$name] = $GLOBALS[$name] ?? null; + } + + $GLOBALS['syslogdb_default'] = 'syslog'; + $GLOBALS['syslog_cnn'] = new stdClass(); + unset($GLOBALS['syslog_processing_lock_cnn']); +}); + +afterEach(function () { + foreach ($this->saved as $name => $value) { + if ($value === null) { + unset($GLOBALS[$name]); + } else { + $GLOBALS[$name] = $value; + } + } +}); + +it('takes the processing lock on a new connection to the Syslog server', function () { + $lock_cnn = new stdClass(); + $seen = []; + + $GLOBALS['database_details'] = ['other' => ['database_conn' => new stdClass()], 'syslog' => syslog_test_lock_details($GLOBALS['syslog_cnn'], true)]; + $GLOBALS['database_sessions'] = ['syslog-db:3307:syslog' => $GLOBALS['syslog_cnn']]; + $GLOBALS['database_persist'] = true; + + test_override('db_connect_real', function (...$args) use (&$seen, $lock_cnn) { + // The cached session and persistent handles would reuse $syslog_cnn. + $seen = [$args, isset($GLOBALS['database_sessions']['syslog-db:3307:syslog']), $GLOBALS['database_persist']]; + + return $lock_cnn; + }); + test_override('db_fetch_cell_prepared', fn($sql, $params, $col, $log, $cnn) => $sql === 'SELECT GET_LOCK(?, 0)' && $params === [syslog_processing_lock_name()] && $cnn === $lock_cnn ? '1' : '0'); + + expect(syslog_processing_lock_acquire())->toBeTrue() + ->and($seen)->toBe([['syslog-db', 'syslog', 'secret', 'syslog', 'mysql', 3307, 2, true, 'key.pem', 'cert.pem', 'ca.pem', '/etc/ssl/certs', false], false, false]) + ->and($GLOBALS['syslog_processing_lock_cnn'])->toBe($lock_cnn) + ->and($GLOBALS['database_sessions']['syslog-db:3307:syslog'])->toBe($GLOBALS['syslog_cnn']) + ->and($GLOBALS['database_persist'])->toBeTrue(); +}); + +it('reports a lock held elsewhere and a lock connection that cannot be opened', function () { + $GLOBALS['database_details'] = [syslog_test_lock_details($GLOBALS['syslog_cnn'], false)]; + $GLOBALS['database_sessions'] = []; + $args = []; + + test_override('db_connect_real', function (...$connect) use (&$args) { + $args = $connect; + + return new stdClass(); + }); + test_override('db_fetch_cell_prepared', fn() => '0'); + + // Cacti 1.2 records no CA path, so only its eleven connection arguments are passed. + expect(syslog_processing_lock_acquire())->toBeFalse() + ->and($args)->toHaveCount(11) + ->and($GLOBALS)->not->toHaveKey('syslog_processing_lock_cnn') + ->and($GLOBALS['database_sessions'])->toBe([]); + + test_override('db_connect_real', fn() => false); + + expect(syslog_processing_lock_acquire())->toBeNull(); + + // Without recorded details the Syslog server cannot be reached again. + $GLOBALS['database_details'] = []; + + expect(syslog_processing_lock_acquire())->toBeNull(); +}); diff --git a/tests/Unit/UpgradeTruncateTest.php b/tests/Unit/UpgradeTruncateTest.php index 96bc6b94..4a2776a0 100644 --- a/tests/Unit/UpgradeTruncateTest.php +++ b/tests/Unit/UpgradeTruncateTest.php @@ -30,6 +30,8 @@ function syslog_db_table_exists($table, $log = true) { return true; } function syslog_install_storage_engine($engine, $options = []) { return "InnoDB"; } function syslog_dashboard_seed_default() {} function syslog_validate_storage_engine($engine) { return "InnoDB"; } + function syslog_partition_settings_error(...$args) { return ""; } + function syslog_partition_server_limit(...$args) { return 8192; } function read_config_option($name) { return ""; } function set_config_option($name, $value, $force = false) {} function db_update_table(...$args) { return true; } diff --git a/tests/regression/cli_schema_upgrade_test.php b/tests/regression/cli_schema_upgrade_test.php index 78f25aad..5cd054ab 100644 --- a/tests/regression/cli_schema_upgrade_test.php +++ b/tests/regression/cli_schema_upgrade_test.php @@ -36,6 +36,8 @@ function set_config_option($name, $value, $force = false) { $options_saved[$name] = $value; } function syslog_install_storage_engine($engine, $options) { return $engine; } +function syslog_partition_settings_error(...$args) { return ''; } +function syslog_partition_server_limit(...$args) { return 8192; } function syslog_db_fetch_row($sql) { return []; } function syslog_db_execute($sql) { global $queries; diff --git a/tests/regression/review_fixes_database_test.php b/tests/regression/review_fixes_database_test.php index f33606cb..2b9b3216 100644 --- a/tests/regression/review_fixes_database_test.php +++ b/tests/regression/review_fixes_database_test.php @@ -185,6 +185,26 @@ check(array_intersect($tail, $healed) === $tail && preg_grep('/^d\d{8}$/', $healed) === [], 'Maintenance heals the hourly gap and ignores the pending Daily setting'); check(syslog_status_get()['partition_maintenance_outcome'] === 'success', 'Hourly maintenance completes'); + + // ---- Repair sizes a missing table by the current retention ------------- + // 365 days of hourly history would exceed 8,192 partitions; repair must use + // the 3-day retention now configured, not the install-time 365 days. + $options['syslog_install_days'] = '365'; + $options['syslog_retention'] = '3'; + $options['syslog_partition_granularity'] = 'hour'; + $pdo->exec('DROP TABLE syslog'); + syslog_setup_table_new([], true); + $names = $hourly(); + + check($names !== [] && count($names) < 24 * 8, 'Repair recreates a missing hourly table from the current retention'); + check(strcmp($names[0], syslog_partition_name('hour', $now - 4 * 86400)) >= 0, 'Repair keeps no history beyond the current retention'); + + // Hourly partitioning cannot keep Indefinite history: repair refuses before any DDL. + $options['syslog_retention'] = '0'; + $pdo->exec('DROP TABLE syslog'); + syslog_setup_table_new([], true); + + check(!syslog_db_table_exists('syslog', false), 'Repair creates no table whose partition plan is refused'); } catch (Throwable $error) { $failures[] = $error->getMessage() . ' (' . basename($error->getFile()) . ':' . $error->getLine() . ')'; } finally { From aad966c9801caf149db5ee1302815db3c5c59eab Mon Sep 17 00:00:00 2001 From: TheWitness Date: Fri, 20 Nov 2026 13:35:09 -0500 Subject: [PATCH 10/12] changelog: keep only security, issue and feature entries, in a fixed order Every release section now lists security, then issue, then feature entries. Numbered entries come first in ascending issue or pull number, then unnumbered entries alphabetically. bug: and compat: entries become issue:, and issue#N - / issue#N separators become issue#N:. A single trailing "(issue #N)" or "(#N)" reference moves into the prefix. dev:, docs: and test: entries are dropped. This PR's entries are tagged #473, and the rebuild entries now describe the shared processing lock and 50,000-row batches. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- CHANGELOG.md | 265 +++++++++++++++++++++++++-------------------------- 1 file changed, 130 insertions(+), 135 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 08181f3e..e96db578 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,61 +6,59 @@ --- develop --- -* bug: Syslog Status: choosing a card in Add card now appends it (Cacti's select2 dropdowns report changes through jQuery, which the card controller's native listener missed), and the Refresh interval is saved per user through a callback and refreshes the cards in place instead of reloading the page -* feature: Add an Hourly or Daily (default) Partitioning Level under the renamed Data Retention and Partitioning settings, the install advisor (replacing the unused Partitions per Day option) and silent install options. A changed level takes effect only when cli/upgrade_partitioning.php (or its new --partition=hourly|daily option) rebuilds the tables; until then maintenance follows the existing layout and Settings and Status show the change as pending. Hourly tables keep at least two days of partitions ahead so dMaxValue reorganizations stay instant -* feature: Refuse to save Syslog settings, install, or rebuild when retention, pre-create window and partitioning level need more partitions than the database server supports (8192, or 1024 before MariaDB 10.0.4 and MySQL 5.7), or when Hourly is combined with Indefinite retention -* bug: Bound every log query at the current time so empty future partitions are always pruned, whatever the filter's own date range -* bug: Purge Syslog Devices (Utilities) crashed with "Call to undefined function syslog_db_execute()" because the hook never connected to the Syslog database; it now connects first, reports connection failures, qualifies the Syslog database, and uses NOT EXISTS so a NULL host_id no longer prevents purging (#472) -* bug: The partition rebuild CLI now refuses to start while Syslog processing is enabled and holds the master process slot; copies stored and removed messages without seq into a replacement whose AUTO_INCREMENT starts above the original high-water mark; verifies each 50,000-row batch by checksum and checkpoints it for resumable retries; widens the incoming queue in place to keep its sequence identities; and refuses legacy DATETIME history before changing anything -* docs: Refresh README release/source compatibility, installation guidance and source-checked behavior; add published release notes and 5.0 upgrade guidance -* bug: Keep log column resizing within the results area on both log tabs, preserve message ellipsis, and use an accessible Font Awesome export button +* issue#472: Purge Syslog Devices (Utilities) crashed with "Call to undefined function syslog_db_execute()" because the hook never connected to the Syslog database; it now connects first, reports connection failures, qualifies the Syslog database, and uses NOT EXISTS so a NULL host_id no longer prevents purging +* issue#473: Syslog Status: choosing a card in Add card now appends it (Cacti's select2 dropdowns report changes through jQuery, which the card controller's native listener missed), and the Refresh interval is saved per user through a callback and refreshes the cards in place instead of reloading the page +* issue#473: Bound every log query at the current time so empty future partitions are always pruned, whatever the filter's own date range +* issue#473: The partition rebuild CLI now refuses to start while Syslog processing is enabled and holds a database lock that syslog_process.php also takes (on a connection of its own, so nested locks cannot release it); copies stored and removed messages without seq into a replacement whose AUTO_INCREMENT starts above the original high-water mark; verifies each 50,000-row batch by checksum and checkpoints it for resumable retries; widens the incoming queue in place to keep its sequence identities; and refuses legacy DATETIME history before changing anything +* issue: Keep log column resizing within the results area on both log tabs, preserve message ellipsis, and use an accessible Font Awesome export button +* issue: Load grouped message details only on expansion, 50 at a time, preserving the displayed query's date range and filters instead of rendering every occurrence into hidden rows +* issue: Place Cancel before an active submit-type Save button in Save Search As, support Enter through a separate dialog form and the existing callback, and show a Font Awesome refresh glyph that spins only while results callbacks are running +* issue: Remove message-row and lightbulb hover tooltips, keep details in the click-open flyout, and fix the recommendation column at 20 pixels with a centered header +* issue: Remove recommendation callbacks on message hover, show a theme-aware lightbulb before messages with matching recommendations, and include the CSRF token in the details flyout's fallback POST +* issue: Trim leading and trailing parentheses and square brackets from incoming program names before reference lookup and rule processing +* feature#473: Add an Hourly or Daily (default) Partitioning Level under the renamed Data Retention and Partitioning settings, the install advisor (replacing the unused Partitions per Day option) and silent install options. A changed level takes effect only when cli/upgrade_partitioning.php (or its new --partition=hourly|daily option) rebuilds the tables; until then maintenance follows the existing layout and Settings and Status show the change as pending. Hourly tables keep at least two days of partitions ahead so dMaxValue reorganizations stay instant +* feature#473: Refuse to save Syslog settings, install, or rebuild when retention, pre-create window and partitioning level need more partitions than the database server supports (8192, or 1024 before MariaDB 10.0.4 and MySQL 5.7), or when Hourly is combined with Indefinite retention +* feature: Add a data-preserving partition rebuild CLI with optional --convert-ms, verified 50,000-row batch copies, interrupted-copy recovery, and database-size, extra-disk-space and several-hour maintenance warnings * feature: Standardize both schema and partition CLI help with Cacti-style version banners and options, separate maintenance warnings, and replace the unreleased millisecond converter with --convert-ms -* feature: Add a data-preserving partition rebuild CLI with optional --convert-ms, verified 10,000-row copies, interrupted-copy recovery, and database-size, extra-disk-space and several-hour maintenance warnings -* bug: Trim leading and trailing parentheses and square brackets from incoming program names before reference lookup and rule processing -* bug: Load grouped message details only on expansion, 50 at a time, preserving the displayed query's date range and filters instead of rendering every occurrence into hidden rows -* bug: Place Cancel before an active submit-type Save button in Save Search As, support Enter through a separate dialog form and the existing callback, and show a Font Awesome refresh glyph that spins only while results callbacks are running -* bug: Remove message-row and lightbulb hover tooltips, keep details in the click-open flyout, and fix the recommendation column at 20 pixels with a centered header -* bug: Remove recommendation callbacks on message hover, show a theme-aware lightbulb before messages with matching recommendations, and include the CSRF token in the details flyout's fallback POST --- 5.0 --- -* bug: Exercise the grouped-message detail query directly in regression tests without dynamic code evaluation or interpolated fixture table names, and cover unavailable or failing gzip operations -* bug: Cache populated Syslog realm ID lookups per display name for the current request while continuing to check the current user's permissions on each call -* bug: Align the saved-search Default label beneath Name and use Cacti's toggle switch, restore full-message hover tooltips alongside the details flyout, and retain the active search and date range when fetching grouped message details -* bug: Re-importing a full Recommendations export rejected the 860 bundled records whose identifiers contain characters such as `|`, `#` or brackets (an over-strict identifier check). Imports now accept any identifier without spaces, write each rejected record to the Cacti log with its position, identifier and reason plus a one-line summary, and warn about identifiers that can never match a message -* bug: Keep a leading `%` as a literal part of a Cisco recommendation identifier; it must match the message word as written instead of being silently added or removed -* feature: The Recommendations Import button, filters and import submission now run as callbacks instead of full page loads, like the other Syslog Settings pages; the list explains how recommendations are matched -* bug: The Alert and Removal rule filter panel no longer shows the Dark theme's tiled jQuery UI background in Dark and Deepness, and Deepness gets its own Syslog surface colors instead of Dark's (Deepness reuses Dark's jQuery UI styles, which the Syslog views sample) +* issue: Align the saved-search Default label beneath Name and use Cacti's toggle switch, restore full-message hover tooltips alongside the details flyout, and retain the active search and date range when fetching grouped message details +* issue: Cache populated Syslog realm ID lookups per display name for the current request while continuing to check the current user's permissions on each call +* issue: Creating an Alert or Removal rule with the SQL Expression match type failed in the browser with "An invalid form control is not focusable" because the hidden Filter Builder's required fields still took part in form validation; the hidden builder is now disabled +* issue: Exercise the grouped-message detail query directly in regression tests without dynamic code evaluation or interpolated fixture table names, and cover unavailable or failing gzip operations +* issue: Keep a leading `%` as a literal part of a Cisco recommendation identifier; it must match the message word as written instead of being silently added or removed +* issue: Re-importing a full Recommendations export rejected the 860 bundled records whose identifiers contain characters such as `|`, `#` or brackets (an over-strict identifier check). Imports now accept any identifier without spaces, write each rejected record to the Cacti log with its position, identifier and reason plus a one-line summary, and warn about identifiers that can never match a message +* issue: SQL Expression report emails showed an empty Host column and logged PHP "Undefined array key" warnings; the report query now resolves host and program names +* issue: The Alert and Removal rule filter panel no longer shows the Dark theme's tiled jQuery UI background in Dark and Deepness, and Deepness gets its own Syslog surface colors instead of Dark's (Deepness reuses Dark's jQuery UI styles, which the Syslog views sample) +* issue: The rule Test preview dialogs on the Alert, Removal and Report Rules pages are no longer modal, so the rule can be edited while the preview stays open +* issue: Theme fixes: in Midwinter the rule Filter Builder text and buttons inherited the theme's mid-grey text at about 3.5:1 contrast and now use Midwinter's own palette (including its light color mode); in Paper Plane, whose buttons are light text on a dark fill, the Syslog search builder's add buttons were white on white and now fall back to the body text color; the rule Filter Builder no longer overflows the form column on the rule edit pages * feature: Report Rules gain the Filter Builder (previously listed but never shown) and a Test button that previews matches against the most recent stored messages; Filter Builder reports are compiled to bound SQL and processed like the other match types -* bug: Creating an Alert or Removal rule with the SQL Expression match type failed in the browser with "An invalid form control is not focusable" because the hidden Filter Builder's required fields still took part in form validation; the hidden builder is now disabled -* bug: The rule Test preview dialogs on the Alert, Removal and Report Rules pages are no longer modal, so the rule can be edited while the preview stays open +* feature: The Recommendations Import button, filters and import submission now run as callbacks instead of full page loads, like the other Syslog Settings pages; the list explains how recommendations are matched * feature: The Report Rules Message Match String is a resizable text area, taller for SQL Expressions -* bug: SQL Expression report emails showed an empty Host column and logged PHP "Undefined array key" warnings; the report query now resolves host and program names -* bug: Theme fixes: in Midwinter the rule Filter Builder text and buttons inherited the theme's mid-grey text at about 3.5:1 contrast and now use Midwinter's own palette (including its light color mode); in Paper Plane, whose buttons are light text on a dark fill, the Syslog search builder's add buttons were white on white and now fall back to the body text color; the rule Filter Builder no longer overflows the form column on the rule edit pages -* docs: State plainly in the README that replacing the plugin files does not upgrade the plugin. Upgrades never start automatically during ordinary page or poller requests; start one with `cli/syslog_schema.php --upgrade` (recommended, runs outside the web server) or by disabling and enabling the plugin to open the Upgrade Advisor (runs inside that web request). Until then Plugin Management shows the old version and new pages return Permission Denied; the version shown there, not `--audit`, confirms that an upgrade completed -* feature: Add Recommendations (issue #46). A vendor-neutral `syslog_recommendations` table maps a message identifier (a Cisco `%FACILITY-SEV-MNEMONIC`, a Juniper `RPD_OSPF_NBRDOWN`, and so on) to an explanation and a recommended action. Hovering over a row in the Syslog tab now asks the server for a matching recommendation and shows its explanation and recommended action in the tooltip. Console > Syslog Settings > Recommendations (Syslog Administration realm) adds, edits and deletes records and imports and exports them as JSON, using field names drawn from Sigma and the Elastic Common Schema so catalogues from other suppliers can be converted. docs/cisco.json.gz, converted from the Cisco Error Message Database (CEMDB) shipped with php-syslog-ng, is loaded once on install or upgrade without overwriting existing records +* feature#46: Add Recommendations. A vendor-neutral `syslog_recommendations` table maps a message identifier (a Cisco `%FACILITY-SEV-MNEMONIC`, a Juniper `RPD_OSPF_NBRDOWN`, and so on) to an explanation and a recommended action. Hovering over a row in the Syslog tab now asks the server for a matching recommendation and shows its explanation and recommended action in the tooltip. Console > Syslog Settings > Recommendations (Syslog Administration realm) adds, edits and deletes records and imports and exports them as JSON, using field names drawn from Sigma and the Elastic Common Schema so catalogues from other suppliers can be converted. docs/cisco.json.gz, converted from the Cisco Error Message Database (CEMDB) shipped with php-syslog-ng, is loaded once on install or upgrade without overwriting existing records * feature: Add an optional Juniper EX Series recommendation catalogue in docs/juniper.json.gz, convert the Cisco seed to docs/cisco.json.gz, accept .json and .json.gz recommendation imports, and export gzip-compressed JSON by default while retaining one-time Cisco seeding * issue#458: Initialize and validate the active tab before saving view defaults, avoiding a TypeError that disables Syslog and stops processing * issue#459: Explain that SQL Expression rule previews search only queued incoming messages, so an empty preview is not mistaken for an alert-processing failure * issue#459: Legacy "Facility is" removal and alert rules compared the facility name (for example `local7`) with the numeric `facility_id` column of the incoming table, which MySQL evaluates as facility 0 (kern). Removal rules therefore discarded or transferred kern messages instead of their own facility, and facility alerts never fired. The name is now resolved through the facility reference table, as the rule preview, rule editor and stored-table removal already did. `syslog_process.php --debug` now names each removal rule that matched and how many messages it took, and notes host-level alerts skipped for having no matches, so a removal rule that takes messages before an alert sees them is visible -* bug: Filter Builder conditions such as `Host != router01` or `Program != sshd` on Report Rules and retroactive removal against stored messages selected only the excluded host or program, because mapping names to ids dropped the negation -* bug: Testing a Filter Builder report after visiting the Alert or Removal Rules page could send the previous page's conditions; Test now uses the builder that belongs to the form being tested -* feature: Clicking a System Logs or Alert Logs row opens a Message details flyout on the right, in the style of the active theme. It requests the record from the server (`syslog.php?action=message_details`, which returns JSON for stored, removed and alert log records) and shows its details, the full message, the matching recommendations and the rule and filter actions. The flyout survives the page's auto refresh, and Copy link produces a `details=` address that reopens the record -* bug: The System Logs and Alert Logs tables always showed a horizontal scroll bar: the column-resize handle of the last header cell sits 5px outside the table and overflowed the results area. In Midwinter the results area also collapsed to zero width beside the theme's floated tab bar, pushing the whole table off the page +* issue#461: Fit message previews to the available column width instead of cutting them to 100 characters +* issue: Clicking a host or program on the log view, or Filter by device or program in the message details, reloaded the whole page, and clicking the same host again added a duplicate condition. Filters now apply as a callback into the page, values already limiting the search are marked and are not added twice, and the search, clear and refresh actions also reload as callbacks (exports still download through a hidden frame) +* issue: Dragging a column edge on the log view did nothing and then sorted the column. The message column claimed all spare width under the automatic table layout, which ignored the dragged widths, and releasing the drag over the header text counted as a sort click. The log tables now use the fixed layout so resized widths hold, the message column absorbs the change instead of the neighbouring column, and the click that ends a resize no longer sorts +* issue: Filter Builder conditions such as `Host != router01` or `Program != sshd` on Report Rules and retroactive removal against stored messages selected only the excluded host or program, because mapping names to ids dropped the negation +* issue: Opening one of the log view's menus (Manage, View options) or dropdowns left any others open, so several could be open at once; opening one now closes the rest, and clicking elsewhere on the page closes them all +* issue: Testing a Filter Builder report after visiting the Alert or Removal Rules page could send the previous page's conditions; Test now uses the builder that belongs to the form being tested +* issue: The grouped Count column now shows thousands separators for the current language +* issue: The log view's action and menu controls (Search, Clear, Refresh, Export, Save and the Manage menu) are now real `
'; $levels = ['hour' => __('Hourly', 'syslog'), 'day' => __('Daily', 'syslog')]; - $active = $partition_health['syslog']['granularity'] ?? 'day'; $requested = syslog_partition_requested_granularity(); - print '

' . __esc('Partitioning level', 'syslog') . ': ' . html_escape($levels[$active]); + $actives = []; - if ($requested !== $active) { + foreach (['syslog', 'syslog_removed'] as $table) { + $actives[$table] = ($partition_health[$table]['granularity'] ?? 'day') === 'hour' ? 'hour' : 'day'; + } + + // After a partly failed rebuild the two tables can differ; show both. + $label = count(array_unique($actives)) === 1 ? $levels[$actives['syslog']] + : implode(', ', array_map(fn($table) => $table . ': ' . $levels[$actives[$table]], array_keys($actives))); + + print '

' . __esc('Partitioning level', 'syslog') . ': ' . html_escape($label); + + if (array_diff($actives, [$requested])) { print ' — ' . html_escape(__('%s is selected in Settings and takes effect after cli/upgrade_partitioning.php rebuilds the tables.', $levels[$requested], 'syslog')); } diff --git a/syslog_process.php b/syslog_process.php index 9a35feb6..d6e260e9 100644 --- a/syslog_process.php +++ b/syslog_process.php @@ -35,8 +35,11 @@ exit(0); } +// Schema repair waits until this process holds the processing lock. +define('SYSLOG_DEFER_SCHEMA_REPAIR', true); + if (!syslog_connect()) { - cacti_log('ERROR: Syslog database or schema is unavailable; skipping processing.', false, 'SYSLOG'); + cacti_log('ERROR: Syslog database is unavailable; skipping processing.', false, 'SYSLOG'); exit(1); } @@ -59,15 +62,16 @@ */ global $config; -global $child, $run_id, $phase, $seq_start, $seq_end; +global $child, $run_id, $phase, $seq_start, $seq_end, $lock_owner; -$debug = false; -$forcer = false; -$child = 0; -$run_id = ''; -$phase = ''; -$seq_start = 0; -$seq_end = 0; +$debug = false; +$forcer = false; +$child = 0; +$run_id = ''; +$phase = ''; +$seq_start = 0; +$seq_end = 0; +$lock_owner = 0; // process calling arguments $parms = $_SERVER['argv']; @@ -142,6 +146,16 @@ $seq_end = (int) $value; + break; + case '--lock-owner': + if (preg_match('/^[1-9][0-9]*$/', $value) !== 1) { + print "ERROR: Lock owner must be a positive integer.\n\n"; + display_help(); + exit(1); + } + + $lock_owner = (int) $value; + break; case '--version': case '-V': @@ -246,6 +260,14 @@ exit($processing_lock === false ? 0 : 1); } +require_once(__DIR__ . '/includes/schema.php'); + +if (!syslog_ensure_table_structures()) { + cacti_log('ERROR: Syslog schema is unavailable; skipping processing.', false, 'SYSLOG'); + unregister_process('syslog', 'master', $config['poller_id']); + exit(1); +} + // Only the registered master may migrate history or replace synchronized rules. if ($config['poller_id'] > 1) { require_once(__DIR__ . '/includes/processing.php'); @@ -559,7 +581,7 @@ function sig_handler($signo) { * @return (void) This function always exits. */ function syslog_worker_main($debug = false) { - global $child, $run_id, $phase, $seq_start, $seq_end; + global $child, $run_id, $phase, $seq_start, $seq_end, $lock_owner; // The command line is the only source of these values, revalidate if (!syslog_validate_worker_args($child, $run_id, $phase, $seq_start, $seq_end)) { @@ -580,6 +602,19 @@ function syslog_worker_main($debug = false) { exit(1); } + /* + * Write only while the master that launched this worker still holds the + * processing lock. Registration comes first, so a partition rebuild that + * takes the lock afterwards sees this worker and refuses to start. + */ + if (!syslog_processing_lock_held_by((int) $lock_owner)) { + cacti_log("WARNING: Syslog child $child exited; the processing run that launched it no longer holds the processing lock", false, 'SYSLOG'); + + unregister_process('syslog', 'child', $child); + + exit(1); + } + syslog_status_set('last_worker_start', time()); $child_start = microtime(true); @@ -703,6 +738,7 @@ function syslog_launch_workers($phase, $slices, $run_id, $debug = false) { ' --phase=' . $phase . ' --seq-start=' . $slice['start'] . ' --seq-end=' . $slice['end'] . + ' --lock-owner=' . (int) ($GLOBALS['syslog_processing_lock_id'] ?? 0) . ($debug ? ' --debug' : ''); exec_background($php_binary, $extra_args); @@ -771,7 +807,7 @@ function display_help() { print 'The main Syslog poller process script for Cacti Syslogging.' . PHP_EOL . PHP_EOL; print 'usage: syslog_process.php [--debug] [--force-report]' . PHP_EOL . PHP_EOL; - print ' syslog_process.php --child=N --run-id=<32hex> --phase=references|transfer --seq-start=N --seq-end=N' . PHP_EOL . PHP_EOL; + print ' syslog_process.php --child=N --run-id=<32hex> --phase=references|transfer --seq-start=N --seq-end=N --lock-owner=N' . PHP_EOL . PHP_EOL; print 'options:' . PHP_EOL; print ' --force-report Send email reports now.' . PHP_EOL; print ' --debug Provide more verbose debug output.' . PHP_EOL . PHP_EOL; @@ -780,5 +816,6 @@ function display_help() { print ' --run-id The 32 character hexadecimal run identifier.' . PHP_EOL; print ' --phase The phase to process, references or transfer.' . PHP_EOL; print ' --seq-start The first seq of the slice to process.' . PHP_EOL; - print ' --seq-end The last seq of the slice to process.' . PHP_EOL . PHP_EOL; + print ' --seq-end The last seq of the slice to process.' . PHP_EOL; + print ' --lock-owner The database session holding the master\'s processing lock.' . PHP_EOL . PHP_EOL; } diff --git a/tests/Unit/PartitionRebuildTest.php b/tests/Unit/PartitionRebuildTest.php index 8a348fdd..699211bb 100644 --- a/tests/Unit/PartitionRebuildTest.php +++ b/tests/Unit/PartitionRebuildTest.php @@ -7,11 +7,14 @@ function partition_rebuild_runner(): string { $source = plugin_test_read_source('cli/upgrade_partitioning.php'); preg_match('/^function syslog_upart_run\(.*?^\}/ms', $source, $run); preg_match('/^function syslog_upart_run_locked\(.*?^\}/ms', $source, $locked); + preg_match('/^function syslog_upart_live_workers\(.*?^\}/ms', $source, $workers); $path = tempnam(sys_get_temp_dir(), 'syslog-rebuild-unit-'); file_put_contents($path, 'and($state->rebuilds)->toBe([]); }); +it('refuses to rebuild while transfer workers launched before the lock are still alive', function () { + $state = partition_rebuild_setup(); + $run = partition_rebuild_runner(); + $errno = 3; + + test_override('db_fetch_assoc', static fn($sql) => str_contains($sql, "FROM processes WHERE tasktype = 'syslog' AND taskname = 'child'") + ? [['pid' => '101'], ['pid' => '102'], ['pid' => '103']] : []); + // 101 is alive, 102 is a stale row left by a crashed worker, 103 runs as another user. + test_override('posix_kill', static function ($pid) use (&$errno) { + $errno = $pid === 103 ? 1 : 3; + + return $pid === 101; + }); + test_override('posix_get_last_error', static function () use (&$errno) { + return $errno; + }); + + expect($run(true, false))->toBeFalse() + ->and($state->rebuilds)->toBe([]) + ->and($state->statements)->toBe([]) + ->and(end($state->queries))->toBe(['SELECT RELEASE_LOCK(?)', [syslog_processing_lock_name()]]); + + // Only stale rows remain: the rebuild proceeds. + test_override('posix_kill', static fn() => false); + $errno = 3; + ob_start(); + $ok = $run(true, false); + ob_end_clean(); + + expect($ok)->toBeTrue(); +}); + it('refuses legacy DATETIME history before any change and releases the lock', function () { $state = partition_rebuild_setup(); $state->legacy = [['TABLE_NAME' => 'syslog']]; diff --git a/tests/Unit/ProcessingLockTest.php b/tests/Unit/ProcessingLockTest.php index 64136d7c..6410010b 100644 --- a/tests/Unit/ProcessingLockTest.php +++ b/tests/Unit/ProcessingLockTest.php @@ -64,7 +64,7 @@ function syslog_test_lock_details(object $cnn, bool $cacti13): array { $this->saved = []; - foreach (['syslog_cnn', 'syslogdb_default', 'database_sessions', 'database_details', 'database_persist', 'syslog_processing_lock_cnn'] as $name) { + foreach (['syslog_cnn', 'syslogdb_default', 'database_sessions', 'database_details', 'database_persist', 'syslog_processing_lock_cnn', 'syslog_processing_lock_id'] as $name) { $this->saved[$name] = $GLOBALS[$name] ?? null; } @@ -98,15 +98,41 @@ function syslog_test_lock_details(object $cnn, bool $cacti13): array { return $lock_cnn; }); test_override('db_fetch_cell_prepared', fn($sql, $params, $col, $log, $cnn) => $sql === 'SELECT GET_LOCK(?, 0)' && $params === [syslog_processing_lock_name()] && $cnn === $lock_cnn ? '1' : '0'); - test_override('db_fetch_cell', fn($sql, $col, $log, $cnn) => $sql === 'SELECT @@SESSION.wait_timeout' && $cnn === $lock_cnn ? '86400' : '0'); + test_override('db_fetch_cell', fn($sql, $col, $log, $cnn) => $cnn !== $lock_cnn ? '0' : ['SELECT @@SESSION.wait_timeout' => '86400', 'SELECT CONNECTION_ID()' => '42'][$sql] ?? '0'); $GLOBALS['__test_db_calls'] = []; expect(syslog_processing_lock_acquire())->toBeTrue() ->and(array_column($GLOBALS['__test_db_calls'], 'sql'))->toContain('SET SESSION wait_timeout = GREATEST(@@SESSION.wait_timeout, 86400)') ->and($seen)->toBe([['syslog-db', 'syslog', 'secret', 'syslog', 'mysql', 3307, 2, true, 'key.pem', 'cert.pem', 'ca.pem', '/etc/ssl/certs', false], false, false]) ->and($GLOBALS['syslog_processing_lock_cnn'])->toBe($lock_cnn) + ->and($GLOBALS['syslog_processing_lock_id'])->toBe(42) ->and($GLOBALS['database_sessions']['syslog-db:3307:syslog'])->toBe($GLOBALS['syslog_cnn']) ->and($GLOBALS['database_persist'])->toBeTrue(); + + $released = []; + test_override('db_fetch_cell_prepared', function ($sql, $params, $col, $log, $cnn) use (&$released) { + $released[] = [$sql, $cnn]; + + return '1'; + }); + syslog_processing_lock_release(); + + expect($released)->toBe([['SELECT RELEASE_LOCK(?)', $lock_cnn]]) + ->and($GLOBALS)->not->toHaveKey('syslog_processing_lock_cnn') + ->and($GLOBALS)->not->toHaveKey('syslog_processing_lock_id'); +}); + +it('lets a worker write only while its master session holds the lock', function () { + test_override('syslog_db_fetch_cell_prepared', fn($sql, $params) => $sql === 'SELECT IS_USED_LOCK(?)' && $params === [syslog_processing_lock_name()] ? '42' : null); + + expect(syslog_processing_lock_held_by(42))->toBeTrue() + ->and(syslog_processing_lock_held_by(43))->toBeFalse() + ->and(syslog_processing_lock_held_by(0))->toBeFalse(); + + // A released lock reports NULL. + test_override('syslog_db_fetch_cell_prepared', fn() => null); + + expect(syslog_processing_lock_held_by(42))->toBeFalse(); }); it('reports a lock held elsewhere and a lock connection that cannot be opened', function () { diff --git a/tests/regression/cli_schema_upgrade_test.php b/tests/regression/cli_schema_upgrade_test.php index 5cd054ab..a2f692fb 100644 --- a/tests/regression/cli_schema_upgrade_test.php +++ b/tests/regression/cli_schema_upgrade_test.php @@ -21,7 +21,7 @@ function check($condition, $message) { $syslogdb_default = 'syslog'; function syslog_connect() { return true; } function cacti_sizeof($value) { return count($value); } -function read_config_option($name) { return ''; } +function read_config_option($name) { return $name === 'syslog_retention' ? '0' : ''; } // The precision helpers live above syslog_setup_table_new() in schema.php and // are not part of the evaluated slice, so stub them. Repair runs assert plain // second-precision table construction. @@ -64,6 +64,12 @@ function syslog_ensure_message_digest_columns() {} function syslog_db_add_column($table, $column, $log = true) { return true; } function syslog_db_fetch_cell_prepared($sql, $params = [], $col_name = '', $log = true) { return '50'; } function syslog_status_set($name, $value) { return true; } +// Repair of a missing table runs under the processing lock. +$lock_calls = []; +$lock_free = true; +function syslog_processing_lock_acquire() { $GLOBALS['lock_calls'][] = 'acquire'; return $GLOBALS['lock_free']; } +function syslog_processing_lock_release() { $GLOBALS['lock_calls'][] = 'release'; } +function cacti_log(...$args) {} function db_update_table($table, $definition, $create, $upgrade, $connection) { return true; } // syslog_setup_table_new() routes table creation through this wrapper; forward // to the stubbed db_update_table() so the real builder runs without a database. @@ -85,6 +91,14 @@ function syslog_db_table_exists($table, $log = true) { } check(!isset($options_saved['syslog_rows']), 'Repair reset an existing preference'); check(substr_count(implode("\n", $queries), 'INSERT INTO') === 2, 'Lookup tables must only be seeded when missing'); + +// The stubbed builder never records `syslog` as created, so it stays missing. +$lock_free = false; +check(!syslog_ensure_table_structures() && $lock_calls === ['acquire'], 'Repair must not recreate a missing table while another session holds the processing lock'); +$lock_free = true; +$lock_calls = []; +syslog_ensure_table_structures(); +check($lock_calls === ['acquire', 'release'], 'Repair of a missing table must run under the processing lock'); $config['poller_id'] = 2; $all_tables_exist = true; check(syslog_ensure_table_structures(), 'Remote schema repair failed'); diff --git a/tests/regression/partition_rebuild_database_test.php b/tests/regression/partition_rebuild_database_test.php index d4101427..3c984dea 100644 --- a/tests/regression/partition_rebuild_database_test.php +++ b/tests/regression/partition_rebuild_database_test.php @@ -90,6 +90,8 @@ function syslog_db_table_exists($table, ...$args) { WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ?', [$GLOBALS['syslogdb_default'], $table]); } function syslog_partition_ahead_days() { return (int) read_config_option('syslog_partition_ahead_days'); } +// No Cacti process table here, so no transfer workers are registered. +function db_fetch_assoc($sql, ...$args) { return []; } require dirname(__DIR__, 2) . '/includes/schema.php';