You are here

class NagiosCommands in Nagios Monitoring 8

Drush command file for Nagios

Hierarchy

  • class \Drupal\nagios\Commands\NagiosCommands extends \Drush\Commands\DrushCommands

Expanded class hierarchy of NagiosCommands

1 string reference to 'NagiosCommands'
drush.services.yml in ./drush.services.yml
drush.services.yml
1 service uses NagiosCommands
nagios.commands in ./drush.services.yml
\Drupal\nagios\Commands\NagiosCommands

File

src/Commands/NagiosCommands.php, line 13

Namespace

Drupal\nagios\Commands
View source
class NagiosCommands extends DrushCommands {

  /**
   * Allows querying Drupal's health. Useful for NRPE.
   *
   * @param string $check
   *   Optionally specify which check to run, e.g. cron
   *
   * @command nagios
   * @return int
   *   Defaults:
   *   NAGIOS_STATUS_OK: 0
   *   NAGIOS_STATUS_WARNING: 1
   *   NAGIOS_STATUS_CRITICAL: 2
   *   NAGIOS_STATUS_UNKNOWN: 3
   */
  public function nagios($check = '') {
    if ($check) {
      $moduleHandler = \Drupal::moduleHandler();
      if (array_key_exists($check, nagios_functions())) {

        // A specific module has been requested.
        $func = 'nagios_check_' . $check;
        $result = $func();
        $nagios_data['nagios'][$result['key']] = $result['data'];
      }
      elseif ($moduleHandler
        ->moduleExists($check)) {
        $result = $moduleHandler
          ->invoke($check, 'nagios');
        $nagios_data[$check] = $result;
      }
      else {
        $logger = $this
          ->logger();
        $logger
          ->error($check . ' is not a valid nagios check.');
        $logger
          ->error(dt('Run `drush nagios-list` for valid checks.'));
        return 1;
      }
    }
    else {
      $nagios_data = nagios_invoke_all('nagios');
    }
    [
      $output,
      $severity,
    ] = (new StatuspageController())
      ->getStringFromNagiosData($nagios_data);
    $users = array_unique(_nagios_update_os_user());
    if (count($users) > 1 && $severity != NAGIOS_STATUS_OK) {
      $warning = dt('Warning') . ': ';
      $warning .= dt('All nagios checks should be executed as the same user as the web page.') . "\n";
      $warning .= dt('This is important when modules confirm file system permissions are correct.') . "\n";
      $warning .= dt('You can use `sudo -u` to run drush under a different user.') . "\n";
      $warning .= dt('Use `:command` to delete the state.', [
        ':command' => 'drush state:delete nagios.os_user',
      ]) . "\n";
      $warning .= 'User' . print_r($users, TRUE);
      $this
        ->logger()
        ->warning($warning);
    }
    $this->output
      ->writeln($output);
    return $severity;
  }

  /**
   * Prints valid checks for `drush nagios`.
   *
   * @command nagios-list
   * @table-style default
   * @field-labels
   *   check: Check
   *   description: Description
   *   module: Module
   * @default-fields check,description
   * @filter-output
   * @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields
   */
  public function nagios_list() {
    $valid_checks = nagios_functions();
    $rows = [];
    foreach ($valid_checks as $check => $description) {
      $rows[$check] = [
        'check' => $check,
        'description' => $description,
        'module' => 'nagios',
      ];
    }
    $moduleHandler = \Drupal::moduleHandler();
    $module_names = $moduleHandler
      ->getImplementations('nagios');
    foreach ($module_names as $name) {
      $info = $moduleHandler
        ->invoke($name, 'nagios_info');
      $description = !empty($info['name']) && is_string($info['name']) ? $info['name'] : '';
      $rows[$name] = [
        'check' => $name,
        'description' => $description,
        'module' => $name,
      ];
    }
    ksort($rows);
    return new RowsOfFields($rows);
  }

  /**
   * Allows querying Drupal's update status. Useful for NRPE.
   *
   * It will respect the nagios.min_report_severity setting.
   *
   * @command nagios-updates
   * @return int
   *   Defaults:
   *   NAGIOS_STATUS_OK: 0
   *   NAGIOS_STATUS_WARNING: 1
   *   NAGIOS_STATUS_CRITICAL: 2
   *   NAGIOS_STATUS_UNKNOWN: 3
   */
  public function check_updates() {
    $logger = $this->logger;
    try {
      $update_data = nagios_check_requirements()['data'];
      $severity = $update_data['status'];
    } catch (ServiceNotFoundException $e) {
      $logger
        ->error(dt('This Drush command is only available if Core’s update module is enabled.'));
      $logger
        ->error(dt('Run `drush en update` to enable it.'));
      return NAGIOS_STATUS_UNKNOWN;
    }
    if ($severity == NAGIOS_STATUS_CRITICAL) {
      $logger
        ->error($update_data['text']);
    }
    elseif ($severity == NAGIOS_STATUS_WARNING) {
      $logger
        ->warning($update_data['text']);
    }
    else {
      $logger
        ->notice($update_data['text']);
    }
    return $severity;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
NagiosCommands::check_updates public function Allows querying Drupal's update status. Useful for NRPE.
NagiosCommands::nagios public function Allows querying Drupal's health. Useful for NRPE.
NagiosCommands::nagios_list public function Prints valid checks for `drush nagios`.