You are here

statsd.module in StatsD 6

Same filename and directory in other branches
  1. 7.2 statsd.module
  2. 7 statsd.module

File

statsd.module
View source
<?php

/**
 * Implementation of hook_menu()
 *
 */
function statsd_menu() {
  $items['admin/settings/logging/statsd'] = array(
    'title' => 'StatsD',
    'description' => 'Settings for statsd logging. StatsD is a Node JS daemon that aggregates statistics for visibility in Graphite. Best suited for compiling arbitrary statistics on various developer implemented metrics, this module can also be used to send all system events (via watchdog calls) to Graphite.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'statsd_admin_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'statsd.admin.inc',
  );
  return $items;
}

/**
 * Implementation of hook_boot()
 *
 */
function statsd_boot() {
  if (variable_get('statsd_performance_events', TRUE)) {
    statsd_timer(microtime(TRUE));
  }
}

/**
 * Implementation of hook_user_login()
 *
 */
function statsd_user_login(&$edit, $account) {
  if (!variable_get('statsd_user_events', TRUE)) {
    return;
  }
  statsd_call('user_events.successful_login');
}

/**
 * Send failed login attempt
 *
 */
function statsd_user_login_failed($account) {
  if (!variable_get('statsd_user_events', TRUE)) {
    return;
  }
  statsd_call('user_events.failed_login');
}

/**
 * Implementation of hook_exit()
 *
 */
function statsd_exit($destination = NULL) {
  if (variable_get('statsd_user_events', TRUE)) {
    $active_sessions = db_result(db_query("SELECT count(*) as num FROM {sessions} WHERE timestamp > UNIX_TIMESTAMP() - 3600"));
    statsd_call('user_events.active_sessions', 'gauge', $active_sessions);
    statsd_call('user_events.page_view');
  }
  if (variable_get('statsd_performance_events', TRUE)) {
    $memory = round(memory_get_peak_usage() / 1024 / 1024, 2);
    statsd_call('performance_events.peak_memory', 'gauge', $memory);
    $start = statsd_timer();

    // hook_boot() may not be called in certain contexts.
    if ($start > 0) {
      $end = microtime(TRUE);
      $time = round(($end - $start) * 1000, 0);
      statsd_call('performance_events.execution_time', 'timing', $time);
    }
  }
}

/**
 * Implementation of hook_watchdog()
 *
 */
function statsd_watchdog($entry) {
  if (strstr($entry['message'], 'Login attempt failed for')) {
    statsd_user_login_failed($entry['user']);
  }
  $enabled = variable_get('statsd_watchdog', TRUE);
  $level = variable_get('statsd_watchdog_level', WATCHDOG_WARNING);
  if (!$enabled || $level < $entry['severity']) {
    return;
  }
  $levels = watchdog_severity_levels();
  $data = array(
    'watchdog.type.' . $entry['type'],
    'watchdog.severity.' . $levels[$entry['severity']],
  );
  statsd_call($data);
}

/* End of hooks, API functions follow.
---------------------------------------------------------------------- */

/**
 * The generic statsd wrapper. Used for convenience.
 *
 * @param $name
 *   Name of the value you want to track.
 * @param $type
 *   The type of action you want to take with the value.
 * @param $value
 *   The numeric value you wish to pass to statsd.
 *
 */
function statsd_call($name, $type = 'increment', $value = NULL) {
  require_once __DIR__ . '/includes/statsd.inc';
  switch ($type) {
    case 'increment':
      StatsD::updateStats($name, isset($value) ? $value : 1);
      break;
    case 'decrement':
      StatsD::updateStats($name, isset($value) ? $value : -1);
      break;
    case 'gauge':
      StatsD::gauge($name, $value);
      break;
    case 'timing':
      StatsD::timing($name, $value);
      break;
    default:
      watchdog('statsd', 'Unknown method called for statsd: %type', array(
        '%type' => $type,
      ), WATCHDOG_WARNING);
      break;
  }
}

/**
 * Helper function to record the timer in the absence of drupal_static()
 *
 * @param $time
 *   The time (in milliseconds) the record.
 * @return
 *   The current value of $time.
 *
 */
function statsd_timer($time = NULL) {
  static $value;
  if (!$value) {
    $value = $time;
  }
  return $value;
}

Functions

Namesort descending Description
statsd_boot Implementation of hook_boot()
statsd_call The generic statsd wrapper. Used for convenience.
statsd_exit Implementation of hook_exit()
statsd_menu Implementation of hook_menu()
statsd_timer Helper function to record the timer in the absence of drupal_static()
statsd_user_login Implementation of hook_user_login()
statsd_user_login_failed Send failed login attempt
statsd_watchdog Implementation of hook_watchdog()