You are here

function drush_nagios_updates in Nagios Monitoring 8

Same name and namespace in other branches
  1. 6 nagios.drush.inc \drush_nagios_updates()
  2. 7 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.

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

File

./nagios.drush.inc, line 133
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 = '';
  $config = \Drupal::config('nagios.settings');
  if ($available = 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 = $config
      ->get('nagios_ignored_modules') ?: [];
    $nagios_ignored_themes = $config
      ->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'] == UpdateManagerInterface::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: ';
          $message .= 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: ';
            $message .= 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';
  }
  drush_set_context('DRUSH_EXECUTION_COMPLETED', TRUE);
  if ($exit_status !== NULL || $nagios_flag == 'nagios') {
    drush_print($message);
  }
  exit($exit_status);
}