You are here

protected function MonitoringCommands::outputSensuFormat in Monitoring 8

Results output in sensu format.

Parameters

\Drupal\monitoring\Result\SensorResultInterface[] $results: List of sensor result objects.

string $source: Sensu source.

int $ttl: Sensor time to live.

array $handlers: Sensu handlers.

array $metric_handlers: Sensu metric handlers.

bool $metrics: Sensu metrics.

1 call to MonitoringCommands::outputSensuFormat()
MonitoringCommands::run in src/Commands/MonitoringCommands.php
Runs all sensors or a specific sensor and provides verbose data.

File

src/Commands/MonitoringCommands.php, line 378

Class

MonitoringCommands
A Drush commandfile for the Monitoring module.

Namespace

Drupal\monitoring\Commands

Code

protected function outputSensuFormat(array $results, $source, $ttl, array $handlers, array $metric_handlers, $metrics) {
  $status_codes = [
    SensorResultInterface::STATUS_OK => 0,
    SensorResultInterface::STATUS_WARNING => 1,
    SensorResultInterface::STATUS_CRITICAL => 2,
    SensorResultInterface::STATUS_UNKNOWN => 3,
    SensorResultInterface::STATUS_INFO => 0,
  ];
  foreach ($results as $name => $result) {

    // Build sensu check result.
    $sensu_output = [];
    $sensu_output['name'] = $name;
    $sensu_output['status'] = $status_codes[$result
      ->getStatus()];
    if ($ttl) {
      $sensu_output['ttl'] = $ttl;
    }
    if ($handlers) {
      $sensu_output['handlers'] = $handlers;
    }
    $sensu_output['output'] = $result
      ->getMessage();
    $sensu_output['interval'] = $result
      ->getSensorConfig()
      ->getCachingTime();
    $sensu_output['duration'] = $result
      ->getExecutionTime() / 1000;
    $sensu_output['source'] = $source;
    $this
      ->output()
      ->writeln(Json::encode($sensu_output));

    // Also print numeric sensors as metrics, if enabled.
    if ($result
      ->getSensorConfig()
      ->isNumeric() && $metrics) {
      $sensu_metric_output = $sensu_output;
      $sensu_metric_output['name'] = $name . '_metric';
      $sensu_metric_output['type'] = 'metric';
      if ($metric_handlers) {
        $sensu_metric_output['handlers'] = $metric_handlers;
      }

      // Build the metrics data.
      $reversed_source = implode('.', array_reverse(explode('.', $sensu_output['source'])));
      $value = $result
        ->getValue();
      $executed = $result
        ->getTimestamp();
      $sensu_metric_output['output'] = $reversed_source . '.' . $name . ' ' . $value . ' ' . $executed;
      $this
        ->output()
        ->writeln(Json::encode($sensu_metric_output));
    }
  }
}