Skip to content

Commit 711e2b3

Browse files
committed
[#232] Add filter for croncheck
1 parent 6ad07bc commit 711e2b3

File tree

4 files changed

+58
-6
lines changed

4 files changed

+58
-6
lines changed

classes/checker.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,11 @@ class checker {
6666
* If exceptions are thrown, they are caught and returned as result messages as well.
6767
* Note - OK results are not returned.
6868
*
69+
* @param array $filters array of check ref strings to filter by
70+
*
6971
* @return array array of resultmessage objects
7072
*/
71-
public static function get_check_messages(): array {
73+
public static function get_check_messages(array $filters = []): array {
7274
// First try to get the checks, if this fails return a critical message (code is very broken).
7375
$checks = [];
7476

@@ -86,12 +88,24 @@ public static function get_check_messages(): array {
8688

8789
foreach ($checks as $check) {
8890
try {
91+
if (!empty($filters) && !isset($filters[$check->get_ref()])) {
92+
continue;
93+
}
8994
$messages[] = self::process_check_and_get_result($check);
9095
} catch (Throwable $e) {
9196
$messages[] = self::exception_to_message("Error processing check " . $check->get_ref() . ": ", $e);
9297
}
9398
}
9499

100+
// Nothing executed, return a warning message.
101+
if (empty($messages) && !empty($filters)) {
102+
$res = new resultmessage();
103+
$res->level = resultmessage::LEVEL_WARN;
104+
$res->title = "Invalid filter";
105+
$res->message = "No checks were executed. Check the filter names.";
106+
$messages[] = $res;
107+
}
108+
95109
// Add any output buffer message.
96110
$messages[] = self::get_ob_message();
97111

croncheck.php

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,22 @@
4343
$dirroot = __DIR__ . '/../../../';
4444
require_once($dirroot . 'config.php');
4545

46+
$filterids = [];
4647
if ($isweb) {
4748
// If run from the web.
4849
// Add requirement for IP validation.
4950
tool_heartbeat\lib::validate_ip_against_config();
5051

52+
$filterraw = optional_param('filter', '', PARAM_RAW_TRIMMED);
53+
if (!empty($filterraw)) {
54+
foreach (explode(',', $filterraw) as $id) {
55+
$id = trim($id);
56+
if ($id !== '') {
57+
$filterids[$id] = true;
58+
}
59+
}
60+
}
61+
5162
header("Content-Type: text/plain");
5263

5364
// Ensure its not cached.
@@ -72,23 +83,23 @@
7283

7384
lib::process_error_log_ping();
7485

75-
$messages = checker::get_check_messages();
86+
$messages = checker::get_check_messages($filterids);
7687

7788
// Construct the output message.
7889
$PAGE->set_context(\context_system::instance());
7990

8091
// Indent the messages.
8192
$msg = array_map(function($message) {
8293
global $OUTPUT;
83-
94+
8495
$spacer = " ";
8596

8697
// Add the spacer to the start of each message line.
8798
$indentedlines = explode("\n", $message->message);
8899
$indentedlines = array_map(function($line) use ($spacer) {
89100
return $spacer . $line;
90101
}, $indentedlines);
91-
102+
92103
$indentedmessage = implode("\n", $indentedlines);
93104

94105
return $OUTPUT->render_from_template('tool_heartbeat/resultmessage', [

tests/checker_test.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* @author Matthew Hilton <matthewhilton@catalyst-au.net>
2424
* @copyright 2023, Catalyst IT
2525
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26+
* @covers \tool_heartbeat\checker
2627
*/
2728
final class checker_test extends \advanced_testcase {
2829
/**
@@ -40,6 +41,32 @@ public function test_get_check_messages(): void {
4041
$this->assertNotEmpty($checks);
4142
}
4243

44+
/**
45+
* Tests get_check_messages function with filter
46+
* @return void
47+
*/
48+
public function test_get_check_messages_with_filter(): void {
49+
// Check API modifies DB state.
50+
$this->resetAfterTest(true);
51+
52+
// Filter which has a result.
53+
ob_start();
54+
$checks = checker::get_check_messages(['tool_task_cronrunning' => true]);
55+
$this->assertNotEmpty($checks);
56+
57+
// Filter which doesn't have result.
58+
ob_start();
59+
$checks = checker::get_check_messages(['tool_task_adhocqueue' => true]);
60+
$this->assertEmpty($checks);
61+
62+
// Filter by invalid value.
63+
ob_start();
64+
$checks = checker::get_check_messages(['tool_invalid_name' => true]);
65+
$this->assertCount(1, $checks);
66+
$check = reset($checks);
67+
$this->assertEquals('Invalid filter', $check->title);
68+
}
69+
4370
/**
4471
* Provides values to determine_nagios_level test
4572
* @return array

version.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424

2525
defined('MOODLE_INTERNAL') || die();
2626

27-
$plugin->version = 2024111803;
28-
$plugin->release = 2024111803; // Match release exactly to version.
27+
$plugin->version = 2025121600;
28+
$plugin->release = 2025121600; // Match release exactly to version.
2929
$plugin->requires = 2020061500; // Support for 3.9 and above, due to the Check API.
3030
$plugin->supported = [39, 405];
3131
$plugin->component = 'tool_heartbeat';

0 commit comments

Comments
 (0)