2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * CLI tool for system checks
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([
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
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'
60 -v, --verbose Show details of all checks, not just failed checks
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']) {
75 $filter = $options['filter'];
77 $checks = array_filter($checks, function($check, $key) use ($filter) {
78 $ref = $check->get_ref();
79 return (strpos($ref, $filter) !== false);
83 // These shell exit codes and labels align with the NRPE standard.
91 result
::CRITICAL
=> 2,
97 result
::UNKNOWN
=> 'UNKNOWN',
98 result
::WARNING
=> 'WARNING',
99 result
::ERROR
=> 'CRITICAL',
100 result
::CRITICAL
=> 'CRITICAL',
103 $format = "% 10s| % -60s\n";
104 $spacer = "----------+--------------------------------------------------------------------\n";
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
) {
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();
150 $output .= sprintf( $format, '', ' ' . $actionlink->url
);
152 $output .= sprintf( $format, '', '');
157 // Print NRPE header.
160 // Only show the table header if there is anything to show.
162 print sprintf($format,
163 get_string('status'). ' ',
169 // NRPE shell exit code.