View source
<?php
define('STATSD_SERIES_INVALID_CHARACTERS', '/[^A-Za-z0-9-_.]/');
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;
}
function statsd_boot() {
if (variable_get('statsd_performance_events', TRUE)) {
drupal_static('statsd_timer', microtime(TRUE));
}
}
function statsd_user_login(&$edit, $account) {
if (!variable_get('statsd_user_events', TRUE)) {
return;
}
statsd_call('user_events.successful_login');
}
function statsd_user_login_failed($account) {
if (!variable_get('statsd_user_events', TRUE)) {
return;
}
statsd_call('user_events.failed_login');
}
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');
if ($start > 0) {
$end = microtime(TRUE);
$time = round(($end - $start) * 1000, 0);
statsd_call('performance_events.execution_time', 'timing', $time);
}
}
}
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);
}
function statsd_logger_event($name, $type, $value) {
statsd_call($name, $type, $value);
}
function statsd_call($name, $type = 'increment', $value = NULL) {
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;
}
}