You are here

function nagios_check_requirements in Nagios Monitoring 6

Same name and namespace in other branches
  1. 8 nagios.module \nagios_check_requirements()
  2. 5 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

File

./nagios.module, line 399

Code

function nagios_check_requirements() {

  // Load .install files
  include_once './includes/install.inc';
  module_load_include('inc', 'update', 'update.compare');
  drupal_load_updates();

  // Get the run-time requirements and status information.
  // module_invoke_all('requirements', 'runtime') returns an array that isn't
  // keyed by the module name, eg we might get a key 'ctools_css_cache'.
  // We have no way of knowing which module set this, and we can't guess based
  // on the name, as removing everything that begins with 'ctools_' might remove
  // data from other ctools sub-modules that we want to still monitor.
  // The only safe way is to use module_invoke, calling each module in turn.
  $project_data = update_get_projects();
  $nagios_ignored_modules = variable_get('nagios_ignored_modules', array());
  $nagios_ignored_themes = variable_get('nagios_ignored_themes', array());
  $nagios_ignored_projects = $nagios_ignored_modules + $nagios_ignored_themes;
  $enabled_modules = array();
  foreach ($project_data as $project) {
    foreach ($project['includes'] as $key => $val) {
      if (!isset($nagios_ignored_projects[$key])) {
        $enabled_modules[] = $key;
      }
    }
  }

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

  // 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');
    $data = update_calculate_project_data($available);
  }

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

  // Cycle through enabled modules for requirements checks.
  $reqs = array();
  $core_modules = array_keys($project_data['drupal']['includes']);
  foreach ($enabled_modules as $module_name) {
    $requirements_data = module_invoke($module_name, 'requirements', 'runtime');
    if (count($requirements_data)) {

      // Intercept the Update Status module to respect our Ignore behaviour.
      // Note, if $data is empty then there's no available update data and Update Status will handle that for us.
      if ($module_name == 'update' && !empty($data)) {

        // Don't want the 'update_contrib' section reported by 'update' module,
        // because that one contains *ALL* modules, even the ones ignored by
        // nagios module.
        unset($requirements_data['update_contrib']);

        // Now we need to loop through all modules again to reset 'update_contrib'.
        foreach ($enabled_modules as $module_name) {

          // Double check we're not processing a core module.
          if (!in_array($module_name, array_keys($project_data['drupal']['includes']))) {

            // If the module is a sub-module (eg views_ui) then we need to set the status key.
            // Without this,  _update_requirement_check() will return severity = 2.
            if (isset($data[$module_name]['status']) && is_numeric($data[$module_name]['status'])) {

              // Within this clause, only contrib modules are processed. Get update
              // status for the current one, and store data as it would be left
              // by update_requirements() function.
              $contrib_req = _update_requirement_check($data[$module_name], 'contrib');
              $contrib_req['name'] = $module_name;

              // If module up to date, set a severity of -1 for sorting purposes.
              if (!isset($contrib_req['severity'])) {
                $contrib_req['severity'] = -1;
              }

              // Build an array of required contrib updates.
              if ($contrib_req) {
                $module_data[] = $contrib_req;
              }
            }
          }
        }

        // Sort our finished array by severity so we can set Nagios status accordingly.
        usort($module_data, '_nagios_updates_sort_by_severity');

        // Add the 'worst case' to requirements.
        $requirements_data['update_contrib'] = array_pop($module_data);
      }
      $reqs += $requirements_data;
    }
  }

  // Check the requirements as to the most severe status
  $descriptions = array();
  $severity = REQUIREMENT_OK;
  $min_severity = variable_get('nagios_min_report_severity', NAGIOS_STATUS_WARNING);
  foreach ($reqs as $key => $requirement) {
    if (isset($requirement['severity'])) {

      // Ignore update_core warning if update check is pending
      if (($key == 'update_core' || $key == 'update_contrib') && $requirement['severity'] == REQUIREMENT_ERROR && $requirement['reason'] == UPDATE_FETCH_PENDING) {
        continue;
      }
      if ($requirement['severity'] >= $min_severity) {
        if ($requirement['severity'] > $severity) {
          $severity = $requirement['severity'];
        }
        $descriptions[] = $requirement['title'];
      }
    }
  }
  if (empty($descriptions)) {
    $desc = t('No information.');
  }
  else {
    $desc = join(', ', $descriptions);
  }

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