diff --git a/classes/checker.php b/classes/checker.php index ca7b52f..c61f1a0 100644 --- a/classes/checker.php +++ b/classes/checker.php @@ -66,9 +66,11 @@ class checker { * If exceptions are thrown, they are caught and returned as result messages as well. * Note - OK results are not returned. * + * @param array $filters array of check ref strings to filter by + * * @return array array of resultmessage objects */ - public static function get_check_messages(): array { + public static function get_check_messages(array $filters = []): array { // First try to get the checks, if this fails return a critical message (code is very broken). $checks = []; @@ -86,12 +88,24 @@ public static function get_check_messages(): array { foreach ($checks as $check) { try { + if (!empty($filters) && !isset($filters[$check->get_ref()])) { + continue; + } $messages[] = self::process_check_and_get_result($check); } catch (Throwable $e) { $messages[] = self::exception_to_message("Error processing check " . $check->get_ref() . ": ", $e); } } + // Nothing executed, return a warning message. + if (empty($messages) && !empty($filters)) { + $res = new resultmessage(); + $res->level = resultmessage::LEVEL_WARN; + $res->title = "Invalid filter"; + $res->message = "No checks were executed. Check the filter names."; + $messages[] = $res; + } + // Add any output buffer message. $messages[] = self::get_ob_message(); diff --git a/croncheck.php b/croncheck.php index 394d824..301b72b 100644 --- a/croncheck.php +++ b/croncheck.php @@ -43,11 +43,22 @@ $dirroot = __DIR__ . '/../../../'; require_once($dirroot . 'config.php'); +$filterids = []; if ($isweb) { // If run from the web. // Add requirement for IP validation. tool_heartbeat\lib::validate_ip_against_config(); + $filterraw = optional_param('filter', '', PARAM_RAW_TRIMMED); + if (!empty($filterraw)) { + foreach (explode(',', $filterraw) as $id) { + $id = trim($id); + if ($id !== '') { + $filterids[$id] = true; + } + } + } + header("Content-Type: text/plain"); // Ensure its not cached. @@ -72,7 +83,7 @@ lib::process_error_log_ping(); -$messages = checker::get_check_messages(); +$messages = checker::get_check_messages($filterids); // Construct the output message. $PAGE->set_context(\context_system::instance()); @@ -80,7 +91,7 @@ // Indent the messages. $msg = array_map(function($message) { global $OUTPUT; - + $spacer = " "; // Add the spacer to the start of each message line. @@ -88,7 +99,7 @@ $indentedlines = array_map(function($line) use ($spacer) { return $spacer . $line; }, $indentedlines); - + $indentedmessage = implode("\n", $indentedlines); return $OUTPUT->render_from_template('tool_heartbeat/resultmessage', [ diff --git a/tests/checker_test.php b/tests/checker_test.php index 3710461..613ff4b 100644 --- a/tests/checker_test.php +++ b/tests/checker_test.php @@ -23,6 +23,7 @@ * @author Matthew Hilton * @copyright 2023, Catalyst IT * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + * @covers \tool_heartbeat\checker */ final class checker_test extends \advanced_testcase { /** @@ -40,6 +41,32 @@ public function test_get_check_messages(): void { $this->assertNotEmpty($checks); } + /** + * Tests get_check_messages function with filter + * @return void + */ + public function test_get_check_messages_with_filter(): void { + // Check API modifies DB state. + $this->resetAfterTest(true); + + // Filter which has a result. + ob_start(); + $checks = checker::get_check_messages(['tool_task_cronrunning' => true]); + $this->assertNotEmpty($checks); + + // Filter which doesn't have result. + ob_start(); + $checks = checker::get_check_messages(['tool_task_adhocqueue' => true]); + $this->assertEmpty($checks); + + // Filter by invalid value. + ob_start(); + $checks = checker::get_check_messages(['tool_invalid_name' => true]); + $this->assertCount(1, $checks); + $check = reset($checks); + $this->assertEquals('Invalid filter', $check->title); + } + /** * Provides values to determine_nagios_level test * @return array diff --git a/version.php b/version.php index ea6adf0..65cbb0f 100644 --- a/version.php +++ b/version.php @@ -24,8 +24,8 @@ defined('MOODLE_INTERNAL') || die(); -$plugin->version = 2024111803; -$plugin->release = 2024111803; // Match release exactly to version. +$plugin->version = 2025121600; +$plugin->release = 2025121600; // Match release exactly to version. $plugin->requires = 2020061500; // Support for 3.9 and above, due to the Check API. $plugin->supported = [39, 405]; $plugin->component = 'tool_heartbeat';