You are here

function monitoring_drush_result_output_sensu in Monitoring 7

Same name and namespace in other branches
  1. 8 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 291
Drush support for monitoring.

Code

function monitoring_drush_result_output_sensu($results, $source, $ttl, $handlers, $metric_handlers, $metrics) {
  $status_codes = array(
    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 = array();
    $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
      ->getSensorInfo()
      ->getCachingTime();
    $sensu_output['duration'] = $result
      ->getExecutionTime();
    $sensu_output['source'] = $source;
    drush_print(drupal_json_encode($sensu_output));

    // Also print numeric sensors as metrics, if enabled.
    if ($result
      ->getSensorInfo()
      ->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(drupal_json_encode($sensu_metric_output));
    }
  }
}