You are here

function drush_nagios_updates in Nagios Monitoring 7

Same name and namespace in other branches
  1. 8 nagios.drush.inc \drush_nagios_updates()
  2. 6 nagios.drush.inc \drush_nagios_updates()

Drush command callback: nagios-updates.

Parameters

string $update_type: A string containing:

  • 'all' to list all updates.
  • 'security' to list security updates only.

integer $nagios_flag: Send 'nagios' to prefix the output with a Nagios string.

File

./nagios.drush.inc, line 123
Provides drush integration for the Nagios module.

Code

function drush_nagios_updates($update_type = NULL, $nagios_flag = NULL) {
  $flag_insecure = FALSE;
  $exit_status = NULL;
  $message = '';

  // Need to ensure the Update module was installed before continuing.
  if (db_table_exists('cache_update')) {
    if ($available = _nagios_update_get_available(TRUE)) {
      module_load_include('inc', 'update', 'update.compare');
      $data = update_calculate_project_data($available);

      // Unset any data referencing projects we're not interested in.
      $nagios_ignored_modules = variable_get('nagios_ignored_modules', []);
      $nagios_ignored_themes = variable_get('nagios_ignored_themes', []);
      $nagios_ignored_projects = $nagios_ignored_modules + $nagios_ignored_themes;
      foreach ($nagios_ignored_projects as $key => $value) {
        if ($value == TRUE && isset($data[$key])) {
          unset($data[$key]);
        }
      }
      $module_list = [];
      $updates = nagios_updates($data);
      if (count($updates)) {
        foreach ($updates as $project_name => $update_info) {

          // In any case we need to flag security updates.
          if ($update_info['status'] == UPDATE_NOT_SECURE) {
            $flag_insecure = TRUE;
            $module_list[] = $project_name;
          }
          elseif ($update_type && $update_type == 'all') {
            $module_list[] = $project_name;
          }
        }
        if ($nagios_flag == 'nagios') {
          if ($flag_insecure) {

            // Returning the value didn't work in drush 5.x, so we print it instead.
            $message = 'ADMIN CRITICAL - Updates required for: ' . implode(' ', $module_list);
            $exit_status = 2;
          }
          else {
            if (empty($module_list)) {
              $message = 'ADMIN WARNING - Updates required, but no security updates.';
            }
            else {
              $message = 'ADMIN WARNING - Updates required for: ' . implode(' ', $module_list);
            }
            $exit_status = 1;
          }
        }
        else {
          $message = implode(' ', $module_list);

          // Set $exit_status to zero so any dependent scripts do not get a false failure.
          $exit_status = 0;
        }
      }
    }
    if ($nagios_flag == 'nagios' && $exit_status === NULL) {
      $message = 'OK - No updates';
    }
  }
  else {
    $message = 'The core update module was never installed so we cannot use update check features.';

    // Set a warning, because someone is clearly explicitly trying to use this feature and cannot.
    $exit_status = 1;
  }
  drush_set_context('DRUSH_EXECUTION_COMPLETED', TRUE);
  if ($exit_status !== NULL || $nagios_flag == 'nagios') {
    drush_print($message);
  }
  exit($exit_status);
}