Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion classes/checker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand All @@ -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();

Expand Down
17 changes: 14 additions & 3 deletions croncheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -72,23 +83,23 @@

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());

// Indent the messages.
$msg = array_map(function($message) {
global $OUTPUT;

$spacer = " ";

// Add the spacer to the start of each message line.
$indentedlines = explode("\n", $message->message);
$indentedlines = array_map(function($line) use ($spacer) {
return $spacer . $line;
}, $indentedlines);

$indentedmessage = implode("\n", $indentedlines);

return $OUTPUT->render_from_template('tool_heartbeat/resultmessage', [
Expand Down
27 changes: 27 additions & 0 deletions tests/checker_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
* @author Matthew Hilton <matthewhilton@catalyst-au.net>
* @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 {
/**
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Loading