performance_nagios

Versions
mediamosa-21
performance_nagios()

Implements hook_nagios()

Code

sites/all/modules/devel/performance/performance.module, line 570

<?php
function performance_nagios() {
  $info = performance_nagios_info();
  $id = $info['id'];

  // Find out if we have what we need enabled
  $sum = array();

  $sum[] = variable_get('performance_summary_db', 0);
  $sum[] = variable_get('performance_summary_apc', 0);
  $go = array_sum($sum);

  if (!$go) {
    return array(
      $id => array(
        'status' => NAGIOS_STATUS_UNKNOWN,
        'type'   => 'perf',
        'text'   => t('Performance logging is not enabled'),
      ),
    );
  }

  // Initialize variables
  $total_rows = $total_bytes = $total_millisecs = $total_accesses = $total_query_time = $total_query_count = 0;

  // Check which data store to use
  if (variable_get('performance_summary_apc', 0) && drupal_function_exists('apc_cache_info')) {
    // Get the data from the APC cache
    foreach (performance_apc_list_all() as $key) {
      $data_list[] = apc_fetch($key);
    }
  }
  else {
    // Get the data form the database table for URLs that have been accessed in the last 15 minutes
    $result = db_query("SELECT * FROM {performance_summary} WHERE last_access >= %d", time() - 15*60);
    while ($row = db_fetch_array($result)) {
      $data_list[] = $row;
    }
  }

  foreach ($data_list as $data) {
    $total_rows++;

    // Calculate running averages
    $total_bytes += $data['bytes_avg'];
    $total_millisecs += $data['millisecs_avg'];
    $total_accesses += $data['num_accesses'];
    $total_query_time += $data['query_timer_avg'];
    $total_query_count += $data['query_count_avg'];
  }

  // Protect against divide by zero
  if ($total_rows > 0) {
    $ms_avg      = number_format($total_millisecs   / $total_rows, 1, '.', '');
    $ms_query    = number_format($total_query_time  / $total_rows, 1, '.', '');
    $query_count = number_format($total_query_count / $total_rows, 2, '.', '');
    $mb_avg      = number_format($total_bytes       / $total_rows/1024/1024, 1);
  }
  else {
    $mb_avg = '';
    $ms_avg = '';
    $ms_query = '';
    $query_count = '';
  }

  $status = NAGIOS_STATUS_OK;

  return array(
    'ACC' => array(
      'status' => $status,
      'type'   => 'perf',
      'text'   => $total_accesses,
    ),
    'MS' => array(
      'status' => $status,
      'type'   => 'perf',
      'text'   => $ms_avg,
    ),
    'MMB' => array(
      'status' => $status,
      'type'   => 'perf',
      'text'   => $mb_avg,
    ),
    'QRC' => array(
      'status' => $status,
      'type'   => 'perf',
      'text'   => $query_count,
    ),
    'QRT' => array(
      'status' => $status,
      'type'   => 'perf',
      'text'   => $ms_query,
    ),
  );
}
?>