You are here

public function MonitoringCommands::run in Monitoring 8

Runs all sensors or a specific sensor and provides verbose data.

@option force If the sensor execution should be forced in case cached result is available. @option output (deprecated) Use --format for standard outputs and --sensu-output for a format suitable for sensu. @option expand Relevant only for the json output. Currently "sensor" value supported. @option sensu-source Relevant only for sensu output. The sensu source. Defaults to the host name. @option sensu-ttl Relevant only for sensu output. Sensu TTL (Time to live) @option sensu-handlers Relevant only for sensu output. A comma separated list of Sensu handlers (names). @option sensu-metric-handlers Relevant only for sensu output. Defaults to the Sensu handlers. @option sensu-metrics Relevant only for sensu output. Expose numeric sensors additionally as metrics. Enabled by default. @option sensu-output Use this option to format the output for sensu. To get other formats use the drush option --format.

@usage drush monitoring-run monitoring_git_dirty_tree Runs sensor to monitor the git status. @usage drush monitoring-run --verbose monitoring_git_dirty_tree Runs sensor to monitor the git status and displays verbose information. @usage drush monitoring-run --output=json --watchdog=disable Will output the sensor results in json format. The option --watchdog=disable will suppress watchdog output to console. @usage drush monitoring-run --output=sensu --sensu-source=example.org --sensu-ttl=600 --sensu-handlers=email,pagerduty Will output the sensor results in sensu format. The option --sensu-source=example.org will provide the sensu source. The option --sensu-ttl=600 will provide the sensor 600 seconds time to live. @validate-module-enabled monitoring

@command monitoring:run @aliases monitoring-run @field-labels sensor_name: ID label: Label status: Status message: Message exec_time: Execution time result_age: Result age value: Value verbose: Verbose sensor: Sensor data

Parameters

string $sensor_name: Sensor name to invoke or keep empty to invoke all sensors.

array $options: An associative array of options whose values come from cli, aliases, config, etc.

Return value

\Consolidation\AnnotatedCommand\CommandResult The result list with exit code.

File

src/Commands/MonitoringCommands.php, line 221

Class

MonitoringCommands
A Drush commandfile for the Monitoring module.

Namespace

Drupal\monitoring\Commands

Code

public function run($sensor_name = NULL, array $options = [
  'force' => NULL,
  'output' => NULL,
  'expand' => NULL,
  'show-exec-time' => NULL,
  'sensu-source' => NULL,
  'sensu-ttl' => NULL,
  'sensu-handlers' => NULL,
  'sensu-metric-handlers' => NULL,
  'sensu-metrics' => true,
]) {
  $sensor_names = [];
  if (!empty($sensor_name)) {
    $sensor_names[] = $sensor_name;
  }
  $results = monitoring_sensor_run_multiple($sensor_names, $options['force'], $options['verbose']);
  $rows = [];
  foreach ($results as $key => $result) {
    $rows[$key] = [
      'sensor_name' => $result
        ->getSensorId(),
      'label' => $result
        ->getSensorConfig()
        ->getLabel(),
      'status' => $result
        ->getStatusLabel(),
      'message' => $result
        ->getMessage(),
      'exec_time' => $result
        ->getExecutionTime(),
      'result_age' => $this->dateFormatter
        ->formatInterval(time() - $result
        ->getTimestamp()),
    ];
    if ($options['format'] != 'table') {
      $rows[$key]['value'] = $result
        ->getValue();
      if ($options['expand'] == 'sensor') {
        $rows[$key]['sensor'] = $result
          ->getSensorConfig()
          ->toArray();
      }
    }

    // Add the verbose output if requested.
    if ($verbose_output = $result
      ->getVerboseOutput()) {

      // @todo Improve plaintext rendering for tables(view sensor).
      $rows[$key]['verbose'] = strip_tags(\Drupal::service('renderer')
        ->renderRoot($verbose_output));
    }
  }
  $status = self::SENSOR_STATUS_OK;
  foreach ($results as $result) {
    if ($status < self::SENSOR_STATUS_CRITICAL && $result
      ->isCritical()) {
      $status = self::SENSOR_STATUS_CRITICAL;

      // We already have the highest status so we can stop looping now.
      break;
    }
    if ($status < self::SENSOR_STATUS_UNKNOWN && $result
      ->isUnknown()) {
      $status = self::SENSOR_STATUS_UNKNOWN;
    }
    elseif ($status < self::SENSOR_STATUS_WARNING && $result
      ->isWarning()) {
      $status = self::SENSOR_STATUS_WARNING;
    }
  }
  if ($options['output'] == 'sensu' || $options['sensu-output']) {
    $source = $options['sensu-source'] ?: \Drupal::request()
      ->getHost();
    $ttl = (int) $options['sensu-ttl'];
    $handlers = explode(',', $options['sensu-handlers']);
    $metric_handlers = explode(',', $options['sensu-metric-handlers']);
    $this
      ->outputSensuFormat($results, $source, $ttl, $handlers, $metric_handlers, $options['sensu-metrics']);
    return CommandResult::exitCode($status);
  }
  elseif ($options['output'] && $options['output'] !== 'sensu') {
    $this
      ->logger()
      ->error(dt('Unsupported output option @output. Use the --format option to specify formatting options.', [
      '@output' => $options['output'],
    ]));
    return CommandResult::exitCode(MONITORING_DRUSH_SENSOR_STATUS_UNKNOWN);
  }
  return CommandResult::dataWithExitCode(new RowsOfFields($rows), $status);
}