You are here

statsd.module in StatsD 7

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

File

statsd.module
View source
<?php

/**
 * Defines regexp for the list of illegal characters in a Graphite series name.
 *
 */
define('STATSD_SERIES_INVALID_CHARACTERS', '/[^A-Za-z0-9-_.]/');

/**
 * Implements hook_menu().
 *
 */
function statsd_menu() {
  $items['admin/config/development/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;
}

/**
 * Implements hook_boot().
 *
 */
function statsd_boot() {
  if (variable_get('statsd_performance_events', TRUE)) {
    drupal_static('statsd_timer', microtime(TRUE));
  }
}

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

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

/**
 * Implements hook_exit().
 *
 */
function statsd_exit($destination = NULL) {
  if (variable_get('statsd_user_events', TRUE)) {
    $active_sessions = db_query("SELECT count(*) as num FROM {sessions} WHERE timestamp > UNIX_TIMESTAMP() - 3600")
      ->fetchField();
    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 =& drupal_static('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);
    }
  }
}

/**
 * Implements 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_events', 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);
}

/**
 * Implements hook_logger_event().
 *
 * @see https://drupal.org/project/logger
 *
 */
function statsd_logger_event($name, $type, $value) {
  statsd_call($name, $type, $value);
}

/* 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) {

  // In certain situations, hook_watchdog() is called early in the bootstrap
  // process before autoloaded classes are available.
  require_once __DIR__ . '/includes/statsd.inc';
  switch ($type) {
    case 'count':
      StatsD::updateStats($name, $value);
      break;
    case 'increment':
      StatsD::updateStats($name, $value ?: 1);
      break;
    case 'decrement':
      StatsD::updateStats($name, $value ?: -1);
      break;
    case 'gauge':
      StatsD::gauge($name, $value);
      break;
    case 'set':
      StatsD::set($name, $value);
      break;
    case 'time':
    case 'timing':
      StatsD::timing($name, $value);
      break;
    default:
      watchdog('statsd', 'Unknown method called for statsd: %type', array(
        '%type' => $type,
      ), WATCHDOG_WARNING);
      break;
  }
}

Functions

Namesort descending Description
statsd_boot Implements hook_boot().
statsd_call The generic statsd wrapper. Used for convenience.
statsd_exit Implements hook_exit().
statsd_logger_event Implements hook_logger_event().
statsd_menu Implements hook_menu().
statsd_user_login Implements hook_user_login().
statsd_user_login_failed Send a failed login attempt.
statsd_watchdog Implements hook_watchdog().

Constants

Namesort descending Description
STATSD_SERIES_INVALID_CHARACTERS Defines regexp for the list of illegal characters in a Graphite series name.