You are here

public function NagiosCommands::nagios in Nagios Monitoring 8

Allows querying Drupal's health. Useful for NRPE.

@command nagios

Parameters

string $check: Optionally specify which check to run, e.g. cron

Return value

int Defaults: NAGIOS_STATUS_OK: 0 NAGIOS_STATUS_WARNING: 1 NAGIOS_STATUS_CRITICAL: 2 NAGIOS_STATUS_UNKNOWN: 3

File

src/Commands/NagiosCommands.php, line 29

Class

NagiosCommands
Drush command file for Nagios

Namespace

Drupal\nagios\Commands

Code

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;
}