MDL-73975 course: Remove course_search_form template
[moodle.git] / admin / cli / checks.php
blob40deb4d55353331f3f91ee8cd06d1853679afcc9
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * CLI tool for system checks
20 * @package core
21 * @category check
22 * @copyright 2020 Brendan Heywood (brendan@catalyst-au.net)
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 define('CLI_SCRIPT', true);
28 require(__DIR__ . '/../../config.php');
29 require_once($CFG->libdir.'/clilib.php');
31 use core\check\result;
33 list($options, $unrecognized) = cli_get_params([
34 'help' => false,
35 'filter' => '',
36 'type' => 'status',
37 'verbose' => false,
38 ], [
39 'h' => 'help',
40 'f' => 'filter',
41 'v' => 'verbose',
42 't' => 'type',
43 ]);
45 if ($unrecognized) {
46 $unrecognized = implode("\n ", $unrecognized);
47 cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
50 $checks = \core\check\manager::get_checks($options['type']);
51 $types = join(', ', \core\check\manager::TYPES);
53 $help = "Run Moodle system checks
55 Options:
56 -h, --help Print out this help
57 -f, --filter Filter to a subset of checks
58 -t, --type Which set of checks? Defaults to 'status'
59 One of $types
60 -v, --verbose Show details of all checks, not just failed checks
62 Example:
64 sudo -u www-data php admin/cli/checks.php
65 sudo -u www-data php admin/cli/checks.php -v
66 sudo -u www-data php admin/cli/checks.php -v --filter=environment
70 if ($options['help']) {
71 echo $help;
72 die();
75 $filter = $options['filter'];
76 if ($filter) {
77 $checks = array_filter($checks, function($check, $key) use ($filter) {
78 $ref = $check->get_ref();
79 return (strpos($ref, $filter) !== false);
80 }, 1);
83 // These shell exit codes and labels align with the NRPE standard.
84 $exitcodes = [
85 result::NA => 0,
86 result::OK => 0,
87 result::INFO => 0,
88 result::UNKNOWN => 3,
89 result::WARNING => 1,
90 result::ERROR => 2,
91 result::CRITICAL => 2,
93 $exitlabel = [
94 result::NA => 'OK',
95 result::OK => 'OK',
96 result::INFO => 'OK',
97 result::UNKNOWN => 'UNKNOWN',
98 result::WARNING => 'WARNING',
99 result::ERROR => 'CRITICAL',
100 result::CRITICAL => 'CRITICAL',
103 $format = "% 10s| % -60s\n";
104 $spacer = "----------+--------------------------------------------------------------------\n";
105 $prefix = ' |';
107 $output = '';
108 $header = $exitlabel[result::OK] . ': ' . get_string('checksok', '', $options['type']) . "\n";
109 $exitcode = $exitcodes[result::OK];
111 foreach ($checks as $check) {
112 $ref = $check->get_ref();
113 $result = $check->get_result();
115 $status = $result->get_status();
116 $checkexitcode = $exitcodes[$status];
118 // Summary is treated as html.
119 $summary = $result->get_summary();
120 $summary = html_to_text($summary, 60, false);
122 if ($checkexitcode > $exitcode) {
123 $exitcode = $checkexitcode;
124 $header = $exitlabel[$status] . ': ' . $check->get_name() . " (" . $check->get_ref() . ")\n";
127 if (empty($messages[$status])) {
128 $messages[$status] = $result;
131 $len = strlen(get_string('status' . $status));
133 if ($options['verbose'] ||
134 $status == result::WARNING ||
135 $status == result::CRITICAL ||
136 $status == result::ERROR) {
138 $output .= sprintf(
139 $format,
140 $OUTPUT->check_result($result),
141 sprintf('%s (%s)', $check->get_name(), $ref)
144 $summary = str_replace("\n", "\n" . $prefix . ' ', $summary);
145 $output .= sprintf( $format, '', ' ' . $summary);
147 if ($options['verbose']) {
148 $actionlink = $check->get_action_link();
149 if ($actionlink) {
150 $output .= sprintf( $format, '', ' ' . $actionlink->url);
152 $output .= sprintf( $format, '', '');
157 // Print NRPE header.
158 print $header;
160 // Only show the table header if there is anything to show.
161 if ($output) {
162 print sprintf($format,
163 get_string('status'). ' ',
164 get_string('check')
165 ) . $spacer;
166 print $output;
169 // NRPE shell exit code.
170 exit($exitcode);