Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.
   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.
  13  //
  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/>.
  16  
  17  /**
  18   * CLI tool for system checks
  19   *
  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
  24   */
  25  
  26  define('CLI_SCRIPT', true);
  27  
  28  require(__DIR__ . '/../../config.php');
  29  require_once($CFG->libdir.'/clilib.php');
  30  
  31  use core\check\result;
  32  
  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  ]);
  44  
  45  if ($unrecognized) {
  46      $unrecognized = implode("\n  ", $unrecognized);
  47      cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
  48  }
  49  
  50  $checks = \core\check\manager::get_checks($options['type']);
  51  $types = join(', ', \core\check\manager::TYPES);
  52  
  53  $help = "Run Moodle system checks
  54  
  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
  61  
  62  Example:
  63  
  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
  67  
  68  ";
  69  
  70  if ($options['help']) {
  71      echo $help;
  72      die();
  73  }
  74  
  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);
  81  }
  82  
  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,
  92  ];
  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',
 101  ];
 102  
 103  $format = "%      10s| % -60s\n";
 104  $spacer = "----------+--------------------------------------------------------------------\n";
 105  $prefix = '          |';
 106  
 107  $output = '';
 108  $header = $exitlabel[result::OK] . ': ' . get_string('checksok', '', $options['type']) . "\n";
 109  $exitcode = $exitcodes[result::OK];
 110  
 111  foreach ($checks as $check) {
 112      $ref = $check->get_ref();
 113      $result = $check->get_result();
 114  
 115      $status = $result->get_status();
 116      $checkexitcode = $exitcodes[$status];
 117  
 118      // Summary is treated as html.
 119      $summary = $result->get_summary();
 120      $summary = html_to_text($summary, 60, false);
 121  
 122      if ($checkexitcode > $exitcode) {
 123          $exitcode = $checkexitcode;
 124          $header = $exitlabel[$status] . ': ' . $check->get_name() . " (" . $check->get_ref() . ")\n";
 125      }
 126  
 127      if (empty($messages[$status])) {
 128          $messages[$status] = $result;
 129      }
 130  
 131      $len = strlen(get_string('status' . $status));
 132  
 133      if ($options['verbose'] ||
 134          $status == result::WARNING ||
 135          $status == result::CRITICAL ||
 136          $status == result::ERROR) {
 137  
 138          $output .= sprintf(
 139              $format,
 140              $OUTPUT->check_result($result),
 141              sprintf('%s (%s)', $check->get_name(), $ref)
 142          );
 143  
 144          $summary = str_replace("\n", "\n" . $prefix . '     ', $summary);
 145          $output .= sprintf( $format, '', '    ' . $summary);
 146  
 147          if ($options['verbose']) {
 148              $actionlink = $check->get_action_link();
 149              if ($actionlink) {
 150                  $output .= sprintf( $format, '', '    ' . $actionlink->url);
 151              }
 152              $output .= sprintf( $format, '', '');
 153          }
 154      }
 155  }
 156  
 157  // Print NRPE header.
 158  print $header;
 159  
 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;
 167  }
 168  
 169  // NRPE shell exit code.
 170  exit($exitcode);
 171