You are here

function monitoring_drush_result_output_sensu in Monitoring 8

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

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.

string $metric_handlers: Sensu metric handlers.

bool $metrics: Sensu metrics.

1 call to monitoring_drush_result_output_sensu()
monitoring_drush_run in ./monitoring.drush.inc
Drush callback to get sensor results.

File

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

Code

function monitoring_drush_result_output_sensu($results, $source, $ttl, $handlers, $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;
    drush_print(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;
      drush_print(Json::encode($sensu_metric_output));
    }
  }
}