diff --git a/changelog.d/8586.issue b/changelog.d/8586.issue new file mode 100644 index 0000000000..b0e8498d2c --- /dev/null +++ b/changelog.d/8586.issue @@ -0,0 +1 @@ +Improve Script Server performance by only building the debug log strings when debug logging is enabled diff --git a/lib/poller.php b/lib/poller.php index 2ca4f9a696..997560dcf2 100644 --- a/lib/poller.php +++ b/lib/poller.php @@ -24,6 +24,71 @@ require_once __DIR__ . '/CactiProcessLock.php'; +/** + * Resolves the effective logging verbosity for the current request. + * + * This mirrors the level resolution that cacti_log() performs: a selective + * (per-file or per-plugin) debug override takes precedence, otherwise the + * configured log_verbosity setting is used. Callers can use the result to + * skip assembling expensive debug strings when debug logging is not enabled. + * + * @return mixed The effective POLLER_VERBOSITY_* level. + */ +function cacti_effective_log_level() : mixed { + $level = get_selective_log_level(); + + if ($level == -1) { + $level = read_config_option('log_verbosity'); + } + + return $level; +} + +/** + * Logs a Script Server request line at debug verbosity. + * + * The (potentially large) debug string is only assembled when DEBUG logging is + * actually enabled, which keeps the Script Server's hot path cheap on a normal + * production installation. + * + * @param int $pid The Script Server process id. + * @param int $ctr The request counter. + * @param string $include_file The script file named by the request. + * @param string $function The function named by the request. + * @param array $parameter_array The parsed function parameters. + * + * @return void + */ +function script_server_log_request(int $pid, int $ctr, string $include_file, string $function, array $parameter_array) : void { + if (cacti_effective_log_level() != POLLER_VERBOSITY_DEBUG) { + return; + } + + cacti_log("DEBUG: PID[$pid] CTR[$ctr] INC: '" . basename($include_file) . + "' FUNC: '$function' PARMS: '" . implode('\', \'', $parameter_array) . + "'", false, 'PHPSVR'); +} + +/** + * Logs a Script Server response line at debug verbosity. + * + * As with script_server_log_request(), the debug string is only assembled when + * DEBUG logging is actually enabled. + * + * @param int $pid The Script Server process id. + * @param int $ctr The request counter. + * @param string $result The function result returned to the poller. + * + * @return void + */ +function script_server_log_response(int $pid, int $ctr, string $result) : void { + if (cacti_effective_log_level() != POLLER_VERBOSITY_DEBUG) { + return; + } + + cacti_log("DEBUG: PID[$pid] CTR[$ctr] RESPONSE:'$result'", false, 'PHPSVR'); +} + /** * exec_poll - executes a command and returns its output * diff --git a/script_server.php b/script_server.php index 73427c2fd3..c2aea6dae9 100644 --- a/script_server.php +++ b/script_server.php @@ -279,9 +279,7 @@ while (1) { continue; } - cacti_log("DEBUG: PID[$pid] CTR[$ctr] INC: '" . basename($include_file) . - "' FUNC: '$function' PARMS: '" . implode('\', \'',$parameter_array) . - "'", false, 'PHPSVR', POLLER_VERBOSITY_DEBUG); + script_server_log_request((int) $pid, $ctr, $include_file, $function, $parameter_array); // validate the existence of the function, and include if applicable if (!function_exists($function)) { @@ -323,7 +321,7 @@ while (1) { fputs(STDOUT, trim($result) . "\n"); fflush(STDOUT); - cacti_log("DEBUG: PID[$pid] CTR[$ctr] RESPONSE:'$result'", false, 'PHPSVR', POLLER_VERBOSITY_DEBUG); + script_server_log_response((int) $pid, $ctr, $result); $ctr++; } else { diff --git a/tests/Unit/DataCollection/Poller/ScriptServerLogHelpersTest.php b/tests/Unit/DataCollection/Poller/ScriptServerLogHelpersTest.php new file mode 100644 index 0000000000..d2c5b18f36 --- /dev/null +++ b/tests/Unit/DataCollection/Poller/ScriptServerLogHelpersTest.php @@ -0,0 +1,80 @@ +logfile = tempnam(sys_get_temp_dir(), 'ss-log-'); + + $config[OPTIONS_CLI]['path_cactilog'] = $this->logfile; + $config[OPTIONS_CLI]['log_destination'] = 1; + $config[OPTIONS_CLI]['selective_debug'] = ''; + $config[OPTIONS_CLI]['selective_plugin_debug'] = ''; + $config[OPTIONS_CLI]['log_verbosity'] = POLLER_VERBOSITY_NONE; +}); + +afterEach(function () { + if (!empty($this->logfile) && file_exists($this->logfile)) { + unlink($this->logfile); + } +}); + +test('cacti_effective_log_level falls back to log_verbosity without a selective override', function () { + global $config; + + $config[OPTIONS_CLI]['log_verbosity'] = POLLER_VERBOSITY_MEDIUM; + expect(cacti_effective_log_level())->toBe(POLLER_VERBOSITY_MEDIUM); + + $config[OPTIONS_CLI]['log_verbosity'] = POLLER_VERBOSITY_DEBUG; + expect(cacti_effective_log_level())->toBe(POLLER_VERBOSITY_DEBUG); +}); + +test('script_server_log_request logs only at debug verbosity', function () { + global $config; + + // Debug disabled: the helper returns without touching the log. + $config[OPTIONS_CLI]['log_verbosity'] = POLLER_VERBOSITY_NONE; + script_server_log_request(1234, 1, '/scripts/ss_example.php', 'ss_example', ['one', 'two']); + expect(file_get_contents($this->logfile))->toBe(''); + + // Debug enabled: the helper assembles and logs the request line. + $config[OPTIONS_CLI]['log_verbosity'] = POLLER_VERBOSITY_DEBUG; + script_server_log_request(1234, 2, '/scripts/ss_example.php', 'ss_example', ['one', 'two']); + + $logged = file_get_contents($this->logfile); + expect($logged)->toContain("PID[1234] CTR[2] INC: 'ss_example.php'") + ->and($logged)->toContain("FUNC: 'ss_example'") + ->and($logged)->toContain("PARMS: 'one', 'two'"); +}); + +test('script_server_log_response logs only at debug verbosity', function () { + global $config; + + $config[OPTIONS_CLI]['log_verbosity'] = POLLER_VERBOSITY_NONE; + script_server_log_response(1234, 1, 'result-value'); + expect(file_get_contents($this->logfile))->toBe(''); + + $config[OPTIONS_CLI]['log_verbosity'] = POLLER_VERBOSITY_DEBUG; + script_server_log_response(1234, 2, 'result-value'); + + expect(file_get_contents($this->logfile))->toContain("PID[1234] CTR[2] RESPONSE:'result-value'"); +});