You are here

function prod_check_nagios in Production check & Production monitor 7

Same name and namespace in other branches
  1. 6 prod_check.module \prod_check_nagios()

Implementation of hook_nagios()

File

./prod_check.module, line 373

Code

function prod_check_nagios() {
  $status = array();
  if (variable_get('prod_check_enable_nagios', 0)) {
    $checks = variable_get('prod_check_nagios_checks', array());
    foreach ($checks as $set => $calls) {

      // TODO: add check on $set here. Single out 'perf_data' and treat differently.
      foreach ($calls as $key => $function) {
        if (function_exists($function)) {
          $check = call_user_func($function, 'nagios');
          if (is_array($check) && !empty($check)) {
            $status = array_merge($status, $check);
          }
        }
      }
    }

    // Not verbose? Then filter the output.
    if (variable_get('prod_check_nagios_verbose', 0) == 0) {
      $nagios = array(
        'OK' => array(
          'count' => 0,
        ),
        'Unknown' => array(
          'count' => 0,
          'items' => array(),
        ),
        'Warning' => array(
          'count' => 0,
          'items' => array(),
        ),
        'CRITICAL' => array(
          'count' => 0,
          'items' => array(),
        ),
      );
      $highest = 0;
      foreach ($status as $item => $check) {
        switch ($check['status']) {
          case NAGIOS_STATUS_OK:
            $nagios['OK']['count']++;
            break;
          case NAGIOS_STATUS_UNKNOWN:
            $nagios['Unknown']['count']++;
            $nagios['Unknown']['items'][] = $item;
            break;
          case NAGIOS_STATUS_WARNING:
            $nagios['Warning']['count']++;
            $nagios['Warning']['items'][] = $item;
            break;
          case NAGIOS_STATUS_CRITICAL:
            $nagios['CRITICAL']['count']++;
            $nagios['CRITICAL']['items'][] = $item;
            break;
        }
        if ($check['status'] > $highest) {
          $highest = $check['status'];
        }
      }

      // Build message.
      $message = '[';
      foreach ($nagios as $state => $value) {

        // Ignore 0 values.
        if (!$value['count']) {
          continue;
        }
        $message .= '@' . strtolower($state) . ' ' . $state;
        if (isset($nagios[$state]['items'])) {
          $message .= ': ' . implode('|', $nagios[$state]['items']);
        }
        $message .= ', ';
      }

      // Remove last comma and space.
      $message = rtrim($message, ', ');
      $message .= ']';

      // TODO: add | followed by performance data here if enabled.
      // Reset status array.
      $status = array();
      $status['PRODCHK'] = array(
        'status' => $highest,
        'type' => 'state',
        'text' => t($message, array(
          '@ok' => $nagios['OK']['count'],
          '@unknown' => $nagios['Unknown']['count'],
          '@warning' => $nagios['Warning']['count'],
          '@critical' => $nagios['CRITICAL']['count'],
        )),
      );
    }
  }

  // Keep this outside of the if to avoid PHP notices on the nagios status page.
  return $status;
}