You are here

function memcache_admin_stats in Memcache API and Integration 7

Same name and namespace in other branches
  1. 5.2 memcache_admin/memcache_admin.module \memcache_admin_stats()
  2. 5 memcache_admin/memcache_admin.module \memcache_admin_stats()
  3. 6 memcache_admin/memcache_admin.module \memcache_admin_stats()

Callback for the Memcache Stats page.

Return value

string The page output.

1 string reference to 'memcache_admin_stats'
memcache_admin_menu in memcache_admin/memcache_admin.module
Implements hook_menu().

File

memcache_admin/memcache_admin.module, line 261
For the collection and display of memcache stats.

Code

function memcache_admin_stats($bin = 'default') {
  if (!class_exists('MemCacheDrupal', FALSE)) {
    return t('There is a problem with your memcache configuration. Please review !readme for help resolving the following issue: %error.', array(
      '!readme' => l('README.txt', 'http://cgit.drupalcode.org/memcache/tree/README.txt?id=refs/heads;id2=7.x-1.x'),
      '%error' => t('cache_backends not properly configured in settings.php, failed to load required file memcache.inc'),
    ));
  }
  if ($memcache_debug_log = variable_get('memcache_debug_log', FALSE)) {
    if (variable_get('memcache_debug_verbose', FALSE)) {
      $verbose = t('verbose');
    }
    else {
      $verbose = '';
    }
    drupal_set_message(t('You are writing !verbose debug logs to !debug_log.', array(
      '!verbose' => $verbose,
      '!debug_log' => $memcache_debug_log,
    )), 'warning');
  }
  $bin = memcache_admin_bin_mapping($bin);
  $output = '';
  $server = array();
  $stats = dmemcache_stats($bin, 'default', TRUE);
  if (empty($stats[$bin])) {

    // Failed to load statistics. Provide a useful error about where to get
    // more information and help.
    drupal_set_message(t('Failed to retreive statistics. There may be a problem with your Memcache configuration. Please review %readme and !more for more information.', array(
      '%readme' => 'README.txt',
      'admin/reports/status',
      '!more' => module_exists('memcache') ? t('visit the Drupal admin !status page', array(
        '!status' => l(t('status report'), 'admin/reports/status'),
      )) : t('!enable the memcache module', array(
        '!enable' => l(t('enable'), 'admin/modules', array(
          'fragment' => 'edit-modules-performance-and-scalability',
        )),
      )),
    )), 'error');
  }
  else {
    $stats = $stats[$bin];
    $aggregate = array_pop($stats);
    $mc = dmemcache_object($bin);
    if ($mc instanceof Memcached) {
      $version = t('Memcached v!version', array(
        '!version' => phpversion('Memcached'),
      ));
    }
    elseif ($mc instanceof Memcache) {
      $version = t('Memcache v!version', array(
        '!version' => phpversion('Memcache'),
      ));
    }
    else {
      $version = t('Unknown');
      drupal_set_message(t('Failed to detect the memcache PECL extension.'), 'error');
    }
    $memcache_servers = variable_get('memcache_servers', array(
      '127.0.0.1:11211' => 'default',
    ));
    foreach ($stats as $server => $statistics) {
      if (empty($statistics['uptime'])) {
        drupal_set_message(t('Failed to connect to server at %address.', array(
          '%address' => $server,
        )), 'error');
      }
      else {
        $servers[] = $server;
        $data['server_overview'][$server] = t('v!version running !uptime', array(
          '!version' => check_plain($statistics['version']),
          '!uptime' => format_interval($statistics['uptime']),
        ));
        $data['server_pecl'][$server] = t('n/a');
        $data['server_serialize'][$server] = t('n/a');
        $data['server_time'][$server] = format_date($statistics['time']);
        $data['server_connections'][$server] = _memcache_admin_stats_connections($statistics);
        $data['cache_sets'][$server] = _memcache_admin_stats_sets($statistics);
        $data['cache_gets'][$server] = _memcache_admin_stats_gets($statistics);
        $data['cache_counters'][$server] = _memcache_admin_stats_counters($statistics);
        $data['cache_transfer'][$server] = _memcache_admin_stats_transfer($statistics);
        $data['cache_average'][$server] = _memcache_admin_stats_average($statistics);
        $data['memory_available'][$server] = _memcache_admin_stats_memory($statistics);
        $data['memory_evictions'][$server] = number_format($statistics['evictions']);
      }
    }

    // Don't display aggregate totals if there's only one server.
    if (count($servers) == 1) {
      $aggregate = array();
    }

    // Build a custom report array.
    $report = array();

    // Report server uptime.
    $item = array(
      'label' => t('Uptime'),
      'servers' => $data['server_overview'],
    );
    if (count($aggregate)) {
      $item['total'] = t('n/a');
    }
    $report['uptime'][] = $item;

    // Report server PECL extension.
    $item = array(
      'label' => t('PECL extension'),
    );
    if (count($aggregate)) {
      $item['servers'] = $data['server_pecl'];
      $item['total'] = $version;
    }
    else {
      $item['servers'] = array(
        $servers[0] => $version,
      );
    }
    $report['uptime'][] = $item;

    // Report which serialize function is being used.
    $item = array(
      'label' => t('Serialize function'),
    );
    $serialize_function = dmemcache_serialize();
    if ($serialize_function != 'serialize') {
      $serialize_function = t('!function v!version', array(
        '!function' => $serialize_function,
        '!version' => phpversion(dmemcache_serialize_extension()),
      ));
    }
    if (count($aggregate)) {
      $item['servers'] = $data['server_serialize'];
      $item['total'] = $serialize_function;
    }
    else {
      $item['servers'] = array(
        $servers[0] => $serialize_function,
      );
    }
    $report['uptime'][] = $item;

    // Report server time.
    $item = array(
      'label' => t('Time'),
      'servers' => $data['server_time'],
    );
    if (count($aggregate)) {
      $item['total'] = t('n/a');
    }
    $report['uptime'][] = $item;

    // Report number of connections.
    $item = array(
      'label' => t('Connections'),
      'servers' => $data['server_connections'],
    );
    if (count($aggregate)) {
      $item['total'] = _memcache_admin_stats_connections($aggregate);
    }
    $report['uptime'][] = $item;
    $stats = array(
      'sets' => t('Sets'),
      'gets' => t('Gets'),
      'counters' => t('Counters'),
      'transfer' => t('Transferred'),
      'average' => t('Per-connection average'),
    );
    foreach ($stats as $type => $label) {
      $item = array(
        'label' => $label,
        'servers' => $data["cache_{$type}"],
      );
      if (count($aggregate)) {
        $func = "_memcache_admin_stats_{$type}";
        $item['total'] = $func($aggregate);
      }
      $report['stats'][] = $item;
    }

    // Report on available memory.
    $item = array(
      'label' => t('Available memory'),
      'servers' => $data['memory_available'],
    );
    if (count($aggregate)) {
      $item['total'] = _memcache_admin_stats_memory($aggregate);
    }
    $report['memory'][] = $item;

    // Report on memory evictions.
    $item = array(
      'label' => t('Evictions'),
      'servers' => $data['memory_evictions'],
    );
    if (count($aggregate)) {
      $item['total'] = number_format($aggregate['evictions']);
    }
    $report['memory'][] = $item;
    $output = theme('memcache_admin_stats_table', array(
      'bin' => $bin,
      'servers' => $servers,
      'report' => $report,
    ));
  }
  return $output;
}