You are here

function monitoring_drush_run in Monitoring 8

Same name and namespace in other branches
  1. 7 monitoring.drush.inc \monitoring_drush_run()

Drush callback to get sensor results.

Parameters

string $sensor_name: Sensor name to run.

Return value

int The most escalated sensor status. 0 - OK 1 - WARNING 2 - UNKNOWN 3 - CRITICAL

1 string reference to 'monitoring_drush_run'
monitoring_drush_command in ./monitoring.drush.inc
Implements hook_drush_command().

File

./monitoring.drush.inc, line 199
Drush support for monitoring.

Code

function monitoring_drush_run($sensor_name = NULL) {
  $force_run = (bool) drush_get_option('force');
  $verbose = (bool) drush_get_option('verbose');
  $output = drush_get_option('output', 'table');
  $expand = drush_get_option('expand');
  $show_exec_time = drush_get_option('show-exec-time');
  try {
    $sensor_names = array();
    if (!empty($sensor_name)) {
      $sensor_names = array(
        $sensor_name,
      );
    }
    $results = monitoring_sensor_run_multiple($sensor_names, $force_run, $verbose);
  } catch (NonExistingSensorException $e) {
    drush_set_error('MONITORING_SENSOR_INVALID_NAME', dt('Sensor "@name" does not exist.', array(
      '@name' => $sensor_name,
    )));
    return MONITORING_DRUSH_SENSOR_STATUS_UNKNOWN;
  } catch (DisabledSensorException $e) {
    drush_set_error('MONITORING_SENSOR_DISABLED', dt('Sensor "@name" is not enabled.', array(
      '@name' => $sensor_name,
    )));
    return MONITORING_DRUSH_SENSOR_STATUS_UNKNOWN;
  }
  if ($output == 'table') {
    monitoring_drush_result_output_table($results, $show_exec_time);
  }
  elseif ($output == 'json') {
    monitoring_drush_result_output_json($results, $expand);
  }
  elseif ($output == 'sensu') {
    $source = drush_get_option('sensu-source', \Drupal::request()
      ->getHost());
    $ttl = (int) drush_get_option('sensu-ttl');
    $handlers = [];
    if (drush_get_option('sensu-handlers')) {
      $handlers = explode(',', drush_get_option('sensu-handlers'));
    }
    $metric_handlers = [];
    if (drush_get_option('sensu-metric-handlers')) {
      $metric_handlers = explode(',', drush_get_option('sensu-metric-handlers'));
    }
    $metrics = drush_get_option('sensu-metrics', 1);
    monitoring_drush_result_output_sensu($results, $source, $ttl, $handlers, $metric_handlers, $metrics);
  }
  else {
    drush_set_error('MONITORING_UNKNOWN_OUTPUT', dt('Unknown output @output.', array(
      '@output' => $output,
    )));
    return MONITORING_DRUSH_SENSOR_STATUS_UNKNOWN;
  }
  $status = MONITORING_DRUSH_SENSOR_STATUS_OK;
  foreach ($results as $result) {
    if ($result
      ->isCritical() && in_array($status, array(
      MONITORING_DRUSH_SENSOR_STATUS_UNKNOWN,
      MONITORING_DRUSH_SENSOR_STATUS_WARNING,
      MONITORING_DRUSH_SENSOR_STATUS_OK,
    ))) {
      $status = MONITORING_DRUSH_SENSOR_STATUS_CRITICAL;
    }
    elseif ($result
      ->isUnknown() && in_array($status, array(
      MONITORING_DRUSH_SENSOR_STATUS_WARNING,
      MONITORING_DRUSH_SENSOR_STATUS_OK,
    ))) {
      $status = MONITORING_DRUSH_SENSOR_STATUS_UNKNOWN;
    }
    elseif ($result
      ->isWarning() && $status == MONITORING_DRUSH_SENSOR_STATUS_OK) {
      $status = MONITORING_DRUSH_SENSOR_STATUS_WARNING;
    }
  }

  // Set the exit status based on the sensor status.
  return $status;
}