mirror of
https://github.com/Cacti/cacti.git
synced 2026-10-26 04:58:21 +09:00
* Add one-click RRDfile repair to Data Source Info mode The Data Source Info mode already compares the RRDfile against the Cacti definition and prints the rrdtool tune recommendations (add a missing Data Source with DS:name:type:heartbeat:min:max, remove an extra one with DEL:name, and correct the type, heartbeat, minimum and maximum). It did not, however, offer a way to apply them from the page the way data_debug.php does. - Add an "Apply Suggested Fixes" link to the "RRDtool Tune Info" box header when the web server can write the RRDfile; when it cannot, the header falls back to "Run Fix from Command Line" and the commands remain listed for manual use. - Add a ds_repair action that runs the same tune recommendations server-side (gated on data_source_authorized() and on the RRDfile being writable, and skipping any recommendation carrying control characters), logs each command to DSDEBUG, then reloads the Data Source so the result is visible. This mirrors data_debug.php's run_repair / "Apply Suggested Fixes". - Harden the Info-mode guard so a missing/unreadable RRDfile (rrdtool info returning false) no longer triggers an array-offset warning. * Address review: POST-guard ds_repair and harden repair result check - Route the "Apply Suggested Fixes" action through a CSRF-protected POST: the link now uses the cactiPostAction handler (href="#", data-url) instead of a GET navigation, and ds_repair is added to the $bad_actions guard in include/global.php so a tokenless request fails closed with 405. ds_repair is added to the curated CSRF action test for regression coverage. - Use a strict comparison on the rrdtool_execute() result: it can return null on a failed exec/proxy connection, and null == '' would have reported a failed repair as "All RRDfile repairs succeeded." - Refresh the sink inventory baseline for the new header redirects in ds_repair. * Add request-coverage probe for the ds_repair RRDfile repair handler The Data Source Info mode repair handler ds_repair() is a POST action that the page-level e2e suites do not drive (it needs a Data Source whose RRDfile mismatches its Cacti definition). Add a request-coverage fixture that loads the handler in isolation via cacti_fixture_source_function(), stubs its I/O boundaries, and drives every branch (unauthorized, unreadable RRDfile, no RRA, nothing to tune, not writable, a control-character recommendation that is skipped, and both tune success and failure). Register it in run_request_coverage.sh for data_sources.php changes so the changed-line coverage gate sees the handler exercised. * Address review: use defined helpers and boolean repair result - Replace the undefined cacti_has_control_chars() check with a comparison against rrd_strip_control_chars() (loaded from lib/rrd.php) to skip any tune recommendation that carries control characters. - Execute each tune with RRDTOOL_OUTPUT_BOOLEAN and treat a strict true as success so proxy storage (which returns the raw 'OK u:...' status under RRDTOOL_OUTPUT_RETURN_STDERR) is not misreported as a failure. - Add the new translatable strings to locales/po/cacti.pot and reuse the existing failure msgid. - Update the request-coverage fixture to the new boundary contract. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --------- Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
139 lines
4.5 KiB
PHP
139 lines
4.5 KiB
PHP
<?php
|
|
// Copyright (C) 2026 The Cacti Group. Licensed under GPL-2.0-or-later.
|
|
namespace CactiDataSourceRrdRepairFixture;
|
|
|
|
use RuntimeException;
|
|
|
|
/*
|
|
* Request-coverage probe for the Data Source Info mode RRDfile repair handler.
|
|
*
|
|
* ds_repair() (data_sources.php) backs the "Apply Suggested Fixes" POST action.
|
|
* It runs the rrdtool tune recommendations (add/remove Data Sources, correct
|
|
* min/max/type/heartbeat) when the web server can write the RRDfile. The page
|
|
* level e2e suites do not drive a Data Source whose RRDfile mismatches its Cacti
|
|
* definition, so the handler is loaded in isolation with its I/O boundaries
|
|
* stubbed and driven through every branch; RequestCoverage maps the hits back to
|
|
* the source.
|
|
*/
|
|
|
|
require dirname(__DIR__, 2) . '/Helpers/RequestCoverage.php';
|
|
require dirname(__DIR__, 2) . '/Helpers/SourceSlice.php';
|
|
|
|
if (!defined('MESSAGE_LEVEL_INFO')) {
|
|
define('MESSAGE_LEVEL_INFO', 3);
|
|
}
|
|
if (!defined('MESSAGE_LEVEL_ERROR')) {
|
|
define('MESSAGE_LEVEL_ERROR', 1);
|
|
}
|
|
if (!defined('RRDTOOL_OUTPUT_BOOLEAN')) {
|
|
define('RRDTOOL_OUTPUT_BOOLEAN', 4);
|
|
}
|
|
|
|
/* Mutable boundary state the stubs read, reset per scenario below. */
|
|
$GLOBALS['ds_repair_state'] = [
|
|
'authorized' => true,
|
|
'rrd_info' => ['rra' => [0 => []], 'filename' => '/tmp/fixture.rrd', 'ds' => []],
|
|
'diff' => ['tune' => ['/tmp/fixture.rrd DS:traffic_in:COUNTER:600:0:U']],
|
|
'writable' => true,
|
|
'exec_result' => true,
|
|
];
|
|
|
|
$GLOBALS['ds_repair_messages'] = [];
|
|
|
|
function gfrv($name, $filter = FILTER_VALIDATE_INT, $options = []) {
|
|
return 5;
|
|
}
|
|
function grv($name, $default = '') {
|
|
return 5;
|
|
}
|
|
function data_source_authorized($id) {
|
|
return $GLOBALS['ds_repair_state']['authorized'];
|
|
}
|
|
function raise_message($id, $message = '', $level = 0) {
|
|
$GLOBALS['ds_repair_messages'][] = $id;
|
|
}
|
|
function header($location, $replace = true, $response_code = 0) {
|
|
// swallow the redirect; this probe runs on the CLI
|
|
}
|
|
function rrdtool_function_info($id) {
|
|
return $GLOBALS['ds_repair_state']['rrd_info'];
|
|
}
|
|
function cacti_sizeof($value) {
|
|
return is_countable($value) ? count($value) : 0;
|
|
}
|
|
function rrdtool_cacti_compare($id, &$info) {
|
|
return $GLOBALS['ds_repair_state']['diff'];
|
|
}
|
|
function get_data_source_path($id, $expand) {
|
|
return '/tmp/fixture.rrd';
|
|
}
|
|
function is_writeable($path) {
|
|
return $GLOBALS['ds_repair_state']['writable'];
|
|
}
|
|
function read_config_option($name, $force = false) {
|
|
return 'rrdtool';
|
|
}
|
|
function rrd_strip_control_chars($command_line) {
|
|
return preg_replace('/[\x00-\x1f\x7f]/', '', $command_line);
|
|
}
|
|
function rrdtool_execute($command, $log_to_stdout, $output_flag, $rrdtool_pipe = false, $logopt = 'WEBLOG') {
|
|
return $GLOBALS['ds_repair_state']['exec_result'];
|
|
}
|
|
function cacti_log($message, $print = false, $type = '', $level = 0) {
|
|
// no-op
|
|
}
|
|
function __($text, ...$args) {
|
|
return $args !== [] ? @vsprintf($text, $args) : $text;
|
|
}
|
|
|
|
cacti_fixture_source_function(dirname(__DIR__, 3) . '/data_sources.php', 'ds_repair', __NAMESPACE__);
|
|
|
|
/**
|
|
* Reset the boundary state, overriding individual keys, then run the handler.
|
|
*/
|
|
function run_scenario(array $overrides) : void {
|
|
$GLOBALS['ds_repair_state'] = array_merge([
|
|
'authorized' => true,
|
|
'rrd_info' => ['rra' => [0 => []], 'filename' => '/tmp/fixture.rrd', 'ds' => []],
|
|
'diff' => ['tune' => ['/tmp/fixture.rrd DS:traffic_in:COUNTER:600:0:U']],
|
|
'writable' => true,
|
|
'exec_result' => true,
|
|
], $overrides);
|
|
|
|
ds_repair();
|
|
}
|
|
|
|
/* unauthorized -> permission_denied, no repair */
|
|
run_scenario(['authorized' => false]);
|
|
|
|
/* RRDfile unreadable (rrdtool info returns false) */
|
|
run_scenario(['rrd_info' => false]);
|
|
|
|
/* RRDfile present but no RRA data */
|
|
run_scenario(['rrd_info' => ['rra' => [], 'filename' => '/tmp/fixture.rrd']]);
|
|
|
|
/* nothing to tune */
|
|
run_scenario(['diff' => []]);
|
|
|
|
/* tune needed but the RRDfile is not writable by the web server */
|
|
run_scenario(['writable' => false]);
|
|
|
|
/* writable: one control-character recommendation is skipped, one succeeds */
|
|
run_scenario([
|
|
'diff' => ['tune' => ["/tmp/fixture.rrd DEL:control\x01", '/tmp/fixture.rrd DS:traffic_in:COUNTER:600:0:U']],
|
|
'exec_result' => true,
|
|
]);
|
|
|
|
/* writable: the tune command fails, so the repair is reported as failed */
|
|
run_scenario(['exec_result' => false]);
|
|
|
|
$messages = $GLOBALS['ds_repair_messages'];
|
|
|
|
foreach (['permission_denied', 'ds_repair'] as $expected) {
|
|
if (!in_array($expected, $messages, true)) {
|
|
throw new RuntimeException('ds_repair did not raise the expected "' . $expected . '" message');
|
|
}
|
|
}
|
|
|
|
print json_encode(['ds_repair_scenarios' => count($messages)], JSON_THROW_ON_ERROR) . "\n";
|