You are here

function nagios_check_requirements in Nagios Monitoring 8

Same name and namespace in other branches
  1. 5 nagios.module \nagios_check_requirements()
  2. 6 nagios.module \nagios_check_requirements()
  3. 7 nagios.module \nagios_check_requirements()

Check is all Drupal requirenemts are satisfied.

Calls hook_requirements on all modules to gather info.

Return value

array Array containing state data

File

./nagios.module, line 327
Main file for Nagios service monitoring.

Code

function nagios_check_requirements() {
  $config = Drupal::config('nagios.settings');

  /** @noinspection PhpIncludeInspection */

  // Load .install files:
  include_once DRUPAL_ROOT . '/core/includes/install.inc';
  module_load_include('inc', 'update', 'update.compare');
  drupal_load_updates();
  $enabled_projects = Drupal::service('update.manager')
    ->getProjects();
  $nagios_ignored_modules = $config
    ->get('nagios.ignored_modules') ?: [];
  $nagios_ignored_themes = $config
    ->get('nagios.ignored_themes') ?: [];

  /** @var string[] $nagios_ignored_extensions */
  $nagios_ignored_extensions = $nagios_ignored_modules + $nagios_ignored_themes;
  $nagios_ignored_extensions['cron'] = 1;

  /** @var array<string, \Drupal\Core\Extension\Extension> $modules_to_check */
  $modules_to_check = Drupal::service('extension.list.module')
    ->reset()
    ->getList();
  $themes_to_check = Drupal::service('extension.list.theme')
    ->reset()
    ->getList();
  $installed_extensions = array_map(static function ($module) {
    if ($module instanceof Extension) {
      return $module
        ->getName();
    }
    return $module;
  }, $modules_to_check + $themes_to_check);

  /** @var string[] $extensions_to_check */
  $extensions_to_check = array_diff($installed_extensions, $nagios_ignored_extensions);

  // Copied from update_requirements(). Get available update data for projects.
  $project_update_status = [];

  // TODO: The TRUE param should be made configurable when writing a drush
  // command, so we don't rely on cached data.
  if ($available = update_get_available(TRUE)) {
    module_load_include('inc', 'update', 'update.compare');
    $project_update_status = update_calculate_project_data($available);
  }

  // Remove from the update data array the projects ignored.
  foreach ($nagios_ignored_extensions as $key => $value) {
    unset($project_update_status[$key]);
  }

  // Cycle through enabled modules for requirements checks.
  $req_controller = new RequirementsController($config);
  $req_controller
    ->collectRequirements($extensions_to_check, $project_update_status, $enabled_projects);

  // Check the requirements as to the most severe status:
  [
    $severity,
    $desc,
  ] = $req_controller
    ->findMostSevereProblem();

  // Create a status to pass back, and a text description too:
  switch ($severity) {
    case REQUIREMENT_OK:
    case REQUIREMENT_INFO:
      $data = [
        'status' => NAGIOS_STATUS_OK,
        'type' => 'state',
        'text' => t('No known issues at this time.'),
      ];
      break;
    case REQUIREMENT_WARNING:
      $data = [
        'status' => NAGIOS_STATUS_WARNING,
        'type' => 'state',
        'text' => t('@desc', [
          '@desc' => $desc,
        ]),
      ];
      break;
    case REQUIREMENT_ERROR:
      $data = [
        'status' => NAGIOS_STATUS_CRITICAL,
        'type' => 'state',
        'text' => t('@desc', [
          '@desc' => $desc,
        ]),
      ];
      break;
    default:
      $data = [
        'status' => NAGIOS_STATUS_UNKNOWN,
        'type' => 'state',
        'text' => t('severity is @severity', [
          '@severity' => $severity,
        ]),
      ];
      break;
  }
  return [
    'key' => 'ADMIN',
    'data' => $data,
  ];
}