You are here

public function StatuspageController::getStringFromNagiosData in Nagios Monitoring 8

Parameters

array $nagios_data:

Return value

array{ 0: string, 1: int }

1 call to StatuspageController::getStringFromNagiosData()
StatuspageController::content in src/Controller/StatuspageController.php
Main function building the string to show via HTTP.

File

src/Controller/StatuspageController.php, line 203

Class

StatuspageController
Class StatuspageController produces the HTTP output that the bash script in the nagios-plugin directory understands.

Namespace

Drupal\nagios\Controller

Code

public function getStringFromNagiosData(array $nagios_data) {

  // Find the highest level to be the overall status:
  $severity = NAGIOS_STATUS_OK;
  $min_severity = $this->config
    ->get('nagios.min_report_severity');
  $output_state = [];
  $output_perf = [];
  $codes = nagios_status();
  foreach ($nagios_data as $module_name => $module_data) {
    foreach ($module_data as $key => $value) {

      // Check status and set global severity:
      if (is_array($value) && array_key_exists('status', $value) && $value['status'] >= $min_severity) {
        $severity = max($severity, $value['status']);
      }
      switch ($value['type']) {
        case 'state':

          // Complain only if status is larger than minimum severity:
          if ($value['status'] >= $min_severity) {
            $tmp_state = $key . ':' . $codes[$value['status']];
          }
          else {
            $tmp_state = $key . ':' . $codes[NAGIOS_STATUS_OK];
          }
          if (!empty($value['text'])) {
            $tmp_state .= '=' . $value['text'];
          }

          // TODO: The $value['text'] condition is very likely wrong.
          // Check with automated tests.
          if ($key == 'ADMIN' && (string) $value['text'] == (string) $this
            ->t('Module and theme update status') && $this->config
            ->get('nagios.show_outdated_names')) {
            $this
              ->calculateOutdatedModuleAndThemeNames($tmp_state, $this->config);
          }
          $output_state[] = $tmp_state;
          break;
        case 'perf':
          $output_perf[] = $key . '=' . $value['text'];
          break;
      }
    }
  }

  // Identifier that we check on the bash side:
  $output = "\n" . 'nagios=' . $codes[$severity] . ', ';
  $output .= implode(', ', $output_state) . ' | ' . implode('; ', $output_perf) . "\n";
  return [
    $output,
    $severity,
  ];
}