Commit 15e9308b authored by kbahey's avatar kbahey

Synching with Drupal 6.x:

- Remove the 'title' field.
- Add the nagios hooks and all its baggage.
parent 90ce0587
...@@ -15,7 +15,6 @@ function performance_install() { ...@@ -15,7 +15,6 @@ function performance_install() {
query_count INT NOT NULL DEFAULT '0', query_count INT NOT NULL DEFAULT '0',
query_timer INT NOT NULL DEFAULT '0', query_timer INT NOT NULL DEFAULT '0',
anon INT(1) DEFAULT '1', anon INT(1) DEFAULT '1',
title VARCHAR(255) DEFAULT NULL,
path VARCHAR(255) DEFAULT NULL, path VARCHAR(255) DEFAULT NULL,
PRIMARY KEY (pid), PRIMARY KEY (pid),
KEY (timestamp) KEY (timestamp)
...@@ -33,7 +32,6 @@ function performance_install() { ...@@ -33,7 +32,6 @@ function performance_install() {
query_timer_max INT NOT NULL DEFAULT '0', query_timer_max INT NOT NULL DEFAULT '0',
query_timer_avg INT NOT NULL DEFAULT '0', query_timer_avg INT NOT NULL DEFAULT '0',
num_accesses INT NOT NULL DEFAULT '0', num_accesses INT NOT NULL DEFAULT '0',
title VARCHAR(255) DEFAULT NULL,
PRIMARY KEY (path), PRIMARY KEY (path),
KEY (last_access) KEY (last_access)
) /*!40100 DEFAULT CHARACTER SET UTF8 */ "); ) /*!40100 DEFAULT CHARACTER SET UTF8 */ ");
...@@ -48,7 +46,6 @@ function performance_install() { ...@@ -48,7 +46,6 @@ function performance_install() {
query_count INT NOT NULL DEFAULT '0', query_count INT NOT NULL DEFAULT '0',
query_timer INT NOT NULL DEFAULT '0', query_timer INT NOT NULL DEFAULT '0',
anon SMALLINT DEFAULT '1', anon SMALLINT DEFAULT '1',
title VARCHAR(255) DEFAULT NULL,
path VARCHAR(255) DEFAULT NULL, path VARCHAR(255) DEFAULT NULL,
PRIMARY KEY (pid) PRIMARY KEY (pid)
)"); )");
...@@ -66,7 +63,6 @@ function performance_install() { ...@@ -66,7 +63,6 @@ function performance_install() {
query_timer_max INT NOT NULL DEFAULT '0', query_timer_max INT NOT NULL DEFAULT '0',
query_timer_avg INT NOT NULL DEFAULT '0', query_timer_avg INT NOT NULL DEFAULT '0',
num_accesses INT NOT NULL DEFAULT '0', num_accesses INT NOT NULL DEFAULT '0',
title VARCHAR(255) DEFAULT NULL,
PRIMARY KEY (path) PRIMARY KEY (path)
)"); )");
db_query("CREATE INDEX {performance_summary}_last_access_idx ON {performance_summary} (last_access)"); db_query("CREATE INDEX {performance_summary}_last_access_idx ON {performance_summary} (last_access)");
......
...@@ -130,26 +130,24 @@ function performance_settings() { ...@@ -130,26 +130,24 @@ function performance_settings() {
} }
function performance_init() { function performance_init() {
if (!variable_get('cache', 0)) { register_shutdown_function('performance_shutdown');
// When page caching is off, we don't hook_exit(), instead
// we register a function that runs as late as possible.
register_shutdown_function('performance_shutdown');
}
}
function performance_exit() {
if (variable_get('cache', 0)) {
// Page caching is on, so we run the shutdown function directly
performance_shutdown();
}
} }
function performance_shutdown() { function performance_shutdown() {
global $queries; global $queries;
if ($_GET['q']) {
// q= has a value, use that for the path
$path = $_GET['q'];
}
else {
// q= is empty, use whatever the site_frontpage is set to
$path = variable_get('site_frontpage', 'node');
}
$params = array( $params = array(
'timer' => timer_read('page'), 'timer' => timer_read('page'),
'path' => $_GET['q'], 'path' => $path,
); );
// Memory // Memory
...@@ -205,12 +203,10 @@ function performance_log_summary_apc($params = array()) { ...@@ -205,12 +203,10 @@ function performance_log_summary_apc($params = array()) {
'query_count_max' => max($params['query_count'], $data['query_count_max']), 'query_count_max' => max($params['query_count'], $data['query_count_max']),
'query_count_avg' => ($data['query_count_avg'] + $params['query_count']) / 2, 'query_count_avg' => ($data['query_count_avg'] + $params['query_count']) / 2,
'num_accesses' => $data['num_accesses'] + 1, 'num_accesses' => $data['num_accesses'] + 1,
'title' => $data['title'],
); );
} }
else { else {
$data = array( $data = array(
'title' => strip_tags(drupal_get_title()),
'path' => $params['path'], 'path' => $params['path'],
'bytes_max' => $params['mem'], 'bytes_max' => $params['mem'],
'bytes_avg' => $params['mem'], 'bytes_avg' => $params['mem'],
...@@ -257,12 +253,11 @@ function performance_log_summary_db($params = array()) { ...@@ -257,12 +253,11 @@ function performance_log_summary_db($params = array()) {
else { else {
// First time we log this path, write fresh values // First time we log this path, write fresh values
db_query("INSERT INTO {performance_summary} db_query("INSERT INTO {performance_summary}
(title, path, last_access, num_accesses, (path, last_access, num_accesses,
bytes_max, bytes_avg, millisecs_max, millisecs_avg, bytes_max, bytes_avg, millisecs_max, millisecs_avg,
query_count_max, query_count_avg, query_timer_max, query_timer_avg) query_count_max, query_count_avg, query_timer_max, query_timer_avg)
VALUES VALUES
('%s', '%s', %d, %d, %d, %d, %d, %d, %d, %d, %d, %d)", ('%s', %d, %d, %d, %d, %d, %d, %d, %d, %d, %d)",
strip_tags(drupal_get_title()),
$params['path'], $params['path'],
time(), time(),
1, 1,
...@@ -281,8 +276,7 @@ function performance_log_summary_db($params = array()) { ...@@ -281,8 +276,7 @@ function performance_log_summary_db($params = array()) {
function performance_log_details($params = array()) { function performance_log_details($params = array()) {
global $user; global $user;
$anon = ($user->uid) ? FALSE : TRUE; $anon = ($user->uid) ? FALSE : TRUE;
db_query("INSERT INTO {performance_detail} (title, path, anon, bytes, millisecs, timestamp, query_timer, query_count) VALUES ('%s', '%s', %d, %d, %d, %d, %d, %d)", db_query("INSERT INTO {performance_detail} (path, anon, bytes, millisecs, timestamp, query_timer, query_count) VALUES ('%s', %d, %d, %d, %d, %d, %d)",
strip_tags(drupal_get_title()),
$params['path'], $params['path'],
$anon, $anon,
$params['mem'], $params['mem'],
...@@ -364,7 +358,7 @@ function performance_view_summary() { ...@@ -364,7 +358,7 @@ function performance_view_summary() {
$shown++; $shown++;
$rows[] = array('data' => $rows[] = array('data' =>
array( array(
performance_url($data['path'], $data['title']), $data['path'],
format_date($data['last_access'], 'small'), format_date($data['last_access'], 'small'),
$data['num_accesses'], $data['num_accesses'],
number_format($data['bytes_max']/1024/1024, 2), number_format($data['bytes_max']/1024/1024, 2),
...@@ -439,7 +433,7 @@ function performance_view_details() { ...@@ -439,7 +433,7 @@ function performance_view_details() {
array( array(
$data['pid'], $data['pid'],
format_date($data['timestamp'], 'small'), format_date($data['timestamp'], 'small'),
performance_url($data['path'], $data['title']), $data['path'],
number_format($data['bytes']/1024/1024, 2), number_format($data['bytes']/1024/1024, 2),
$data['millisecs'], $data['millisecs'],
($data['anon']) ? t('Yes') : t('No'), ($data['anon']) ? t('Yes') : t('No'),
...@@ -517,14 +511,132 @@ function performance_clear_apc_confirm_submit($form_id, &$form) { ...@@ -517,14 +511,132 @@ function performance_clear_apc_confirm_submit($form_id, &$form) {
drupal_goto('admin/settings/performance'); drupal_goto('admin/settings/performance');
} }
function performance_url($path = '', $title = '') { /**
if (variable_get('performance_titles', 0)) { * Implementation of hook_nagios_info()
// Show titles */
$ret = ($title) ? l($title, $path) : l($path, $path); function performance_nagios_info() {
return $ret; return array(
'name' => 'Performance logging',
'id' => 'PERF',
);
}
/**
* Implementation of hook_nagios()
*/
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,
'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) && 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 { else {
// Show URLs only // Get the data form the database table
return $path; $result = db_query("SELECT * FROM {performance_summary}");
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) {
$mb_avg = number_format($total_bytes/$total_rows/1024/1024, 1);
$ms_avg = number_format($total_millisecs/$total_rows, 2);
$ms_query = number_format($total_query_time/$total_rows, 2);
$query_count = number_format($total_query_count/$total_rows, 2);
}
else {
$mb_avg = '-';
$ms_avg = '-';
$ms_query = '-';
$query_count = '-';
}
$status = NAGIOS_STATUS_OK;
$mb_thresh = variable_get('performance_nagios_mb', 8);
$ms_thresh = variable_get('performance_nagios_ms', 500);
$query_count_thresh = variable_get('performance_nagios_qc', 400);
// Check if any of the following conditions is met:
//
// - page execution average milliseconds is greater than threshold
// - page memory usage average megabytes is greater than threshold
// - query count average is greater than threshold
// - query time is more than half the total page execution time
if ($ms_avg > $ms_thresh ||
$mb_avg > $mb_thresh ||
$query_count > $query_count_thresh ||
$ms_query > ($ms_avg/2)) {
// Raise a critical alert
$status = NAGIOS_STATUS_CRITICAL;
}
return array(
$id => array(
'status' => $status,
'text' => $total_accesses . '/' . $ms_avg . 'ms/' . $mb_avg . 'MB/' . $query_count . '/' . $ms_query . 'ms',
),
);
}
/**
* Implementation of hook_nagios_settings()
*/
function performance_nagios_settings() {
$form['performance_nagios_ms'] = array(
'#type' => 'textfield',
'#title' => t('Critical page generation time (milliseconds)'),
'#default_value' => variable_get('performance_nagios_ms', 500),
'#description' => t('Number of milliseconds (average) considered critical.')
);
$form['performance_nagios_qc'] = array(
'#type' => 'textfield',
'#title' => t('Critical number of queries'),
'#default_value' => variable_get('performance_nagios_qc', 400),
'#description' => t('Number of queries per page load that is considered critical.')
);
$form['performance_nagios_mb'] = array(
'#type' => 'textfield',
'#title' => t('Critical memory (MB)'),
'#default_value' => variable_get('performance_nagios_mb', 8),
'#description' => t('Number of megabytes (average) considered critical.')
);
return $form;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment