View source
<?php
function prod_check_status() {
drupal_set_title(t('Production check status'));
$output = '';
$functions = _prod_check_functions();
unset($functions['prod_mon']);
unset($functions['perf_data']);
foreach ($functions as $set => $data) {
$result = array();
foreach ($data['functions'] as $function => $title) {
$check = call_user_func($function);
if (is_array($check) && !empty($check)) {
$result = array_merge($result, $check);
}
}
$output .= '<h2>' . t($data['title']) . '</h2>' . "\n";
$output .= '<div class="description"><p><em>' . t($data['description']) . '</em></p></div>' . "\n";
$output .= theme('prod_check_status_report', $result);
}
return $output;
}
function prod_check_settings_form(&$form_state) {
drupal_set_title(t('Production check settings'));
$base = drupal_get_path('module', 'prod_check');
drupal_add_css($base . '/css/prod-check.css');
drupal_add_js($base . '/js/jquery.equalheights.js', 'module', 'header');
drupal_add_js($base . '/js/jquery.maskedinput.min.js', 'module', 'header');
drupal_add_js($base . '/js/prod-check.js', 'module', 'header');
$form = array();
$form['prod_check_general'] = array(
'#type' => 'fieldset',
'#title' => t('General settings'),
'#description' => t('Settings to allow certain checks to function properly.'),
'#collapsible' => FALSE,
);
$form['prod_check_general']['prod_check_sitemail'] = array(
'#type' => 'textfield',
'#title' => t('Mail check'),
'#default_value' => variable_get('prod_check_sitemail', ''),
'#size' => 60,
'#description' => t('Enter (part of) the e-mail address you always <strong>use when developing</strong> a website. This is used in a regular expression in the "Site e-mail", Contact and Webform modules check.'),
'#required' => FALSE,
);
if (module_exists('dblog')) {
$form['prod_check_general']['prod_check_dblog_php'] = array(
'#type' => 'select',
'#title' => t('Minimal watchdog severity for PHP errors'),
'#default_value' => variable_get('prod_check_dblog_php', WATCHDOG_WARNING),
'#options' => watchdog_severity_levels(),
'#description' => t('Select the severity level from which to start reporting PHP errors being logged to the watchdog table.'),
'#required' => TRUE,
);
$form['prod_check_general']['prod_check_dblog_php_threshold'] = array(
'#type' => 'textfield',
'#title' => t('Threshold for PHP errors'),
'#size' => 2,
'#default_value' => variable_get('prod_check_dblog_php_threshold', 1),
'#description' => t('Enter the number of times a PHP error needs to occur before reporting a problem. E.g. entering 3 here will allow 2 occurences of the exact same PHP error without an error being reported.'),
'#required' => TRUE,
);
}
$form['prod_check_apc'] = array(
'#type' => 'fieldset',
'#title' => t('Advanced APC/OPcache settings'),
'#description' => t('These settings are used in the !link functionality.', prod_check_link_array('advanced APC', 'admin/reports/status/apc-opc')),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['prod_check_apc']['prod_check_apc_expunge'] = array(
'#type' => 'textfield',
'#title' => t('APC/OPcache cache full count threshold'),
'#default_value' => variable_get('prod_check_apc_expunge', 0),
'#size' => 2,
'#description' => t('Issue a critical error when the cache full count is greater than the number entered here.'),
'#required' => FALSE,
);
$form['prod_check_apc']['prod_check_apcuser'] = array(
'#type' => 'textfield',
'#title' => t('APC advanced features username'),
'#default_value' => variable_get('prod_check_apcuser', 'apc'),
'#size' => 60,
'#description' => t('The username for logging in to the APC settings page.'),
'#required' => FALSE,
);
$form['prod_check_apc']['prod_check_apcpass'] = array(
'#type' => 'password_confirm',
'#title' => t('APC advanced features password'),
'#size' => 60,
'#description' => t('The password for logging in to the APC settings page.'),
'#required' => FALSE,
);
$form['prod_check_xmlrpc'] = array(
'#type' => 'fieldset',
'#title' => t('Production monitor integration.'),
'#description' => t('You can set up integration with the Production monitor module here.'),
'#collapsible' => FALSE,
);
$form['prod_check_xmlrpc']['prod_check_enable_xmlrpc'] = array(
'#type' => 'checkbox',
'#title' => t('Enable XMLRPC API'),
'#default_value' => variable_get('prod_check_enable_xmlrpc', 0),
'#description' => t('Tick this box if you would like to the module to open up the XMLRPC api so that it can be queried externally to supply information to a base site for monitoring purposes.'),
'#ahah' => array(
'path' => 'admin/settings/prod-check/xmlrpc',
'wrapper' => 'prod-check-xmlrpc',
'effect' => 'fade',
),
'#required' => FALSE,
);
$form['prod_check_xmlrpc']['xmlrpc'] = array(
'#type' => 'markup',
'#prefix' => '<div id="prod-check-xmlrpc">',
'#value' => ' ',
'#suffix' => '</div>',
);
if (!isset($form_state['values']['prod_check_enable_xmlrpc'])) {
$form_state['values']['prod_check_enable_xmlrpc'] = variable_get('prod_check_enable_xmlrpc', 0);
}
if ($form_state['values']['prod_check_enable_xmlrpc']) {
$form['prod_check_xmlrpc']['xmlrpc']['prod_check_xmlrpc_key'] = array(
'#type' => 'textfield',
'#title' => t('API key'),
'#default_value' => variable_get('prod_check_xmlrpc_key', prod_check_generate_key()),
'#maxlength' => 128,
'#size' => 60,
'#description' => t('Enter a key here to ensure secure transfer of data over the API. Use a mixture of alphanumeric and special characters for increased security.'),
'#required' => FALSE,
);
$form['prod_check_xmlrpc']['xmlrpc']['prod_check_module_list_day'] = array(
'#type' => 'select',
'#title' => t('Report module list every'),
'#options' => array(
t('Sunday'),
t('Monday'),
t('Tuesday'),
t('Wednesday'),
t('Thursday'),
t('Friday'),
t('Saturday'),
),
'#default_value' => variable_get('prod_check_module_list_day', 0),
'#description' => t('Defines which day the module list will be fetchable by Production monitor for an update status check.'),
'#required' => FALSE,
);
$form['prod_check_xmlrpc']['xmlrpc']['prod_check_module_list_time'] = array(
'#type' => 'textfield',
'#title' => t('at this time'),
'#default_value' => variable_get('prod_check_module_list_time', '03:00'),
'#maxlength' => 5,
'#size' => 5,
'#description' => t('Defines what time (HH:MM) the module list will be fetchable by Production monitor for an update status check.'),
'#required' => FALSE,
);
if (variable_get('prod_check_module_list_lastrun', 0) != -1) {
$form['prod_check_xmlrpc']['xmlrpc']['reset'] = array(
'#prefix' => '<div class="clear">',
'#type' => 'submit',
'#value' => t('Force immediate module list reporting'),
'#postfix' => '</div>',
);
}
else {
$form['prod_check_xmlrpc']['xmlrpc']['reset'] = array(
'#type' => 'markup',
'#value' => '<div class="description clear">' . t('Production monitor will fetch the module list on next cron run or when manually invoked for this site!') . '</div>',
);
}
}
if (module_exists('nagios')) {
$form['prod_check_nagios'] = array(
'#type' => 'fieldset',
'#title' => t('Nagios integration.'),
'#description' => t('You can set up integration with the !link module here.', prod_check_link_array('Nagios', 'http://drupal.org/project/nagios')),
'#collapsible' => FALSE,
);
$form['prod_check_nagios']['prod_check_enable_nagios'] = array(
'#type' => 'checkbox',
'#title' => t('Enable Nagios integration'),
'#description' => t('Tick this box if you want to enable integration with Nagios. The !link module is required for this to function.', array(
'!link' => l(t('Nagios'), 'http://drupal.org/project/nagios', array(
'attributes' => array(
'title' => t('Nagios'),
),
)),
)),
'#default_value' => variable_get('prod_check_enable_nagios', 0),
'#ahah' => array(
'path' => 'admin/settings/prod-check/nagios',
'wrapper' => 'prod-check-nagios',
'effect' => 'fade',
),
'#required' => FALSE,
);
$form['prod_check_nagios']['nagios'] = array(
'#type' => 'markup',
'#prefix' => '<div id="prod-check-nagios">',
'#value' => ' ',
'#suffix' => '</div>',
);
if (!isset($form_state['values']['prod_check_enable_nagios'])) {
$form_state['values']['prod_check_enable_nagios'] = variable_get('prod_check_enable_nagios', 0);
}
if ($form_state['values']['prod_check_enable_nagios']) {
$form['prod_check_nagios']['nagios']['settings'] = _prod_check_functions_as_form();
$options = variable_get('prod_check_nagios_checks', array());
if (!empty($options)) {
$monitor_settings =& $form['prod_check_nagios']['nagios']['settings']['prod_check_settings']['monitor_settings'];
foreach (element_children($monitor_settings) as $set) {
if (isset($options[$set])) {
$monitor_settings[$set]['#default_value'] = $options[$set];
}
else {
$monitor_settings[$set]['#default_value'] = array();
}
}
}
$form['prod_check_nagios']['nagios']['settings']['prod_check_nagios_takeover'] = array(
'#type' => 'textfield',
'#title' => t('Custom Nagios path'),
'#description' => t('If you wish to use a different path than the default /nagios path enabled by the Nagios module, then enter the desired path here. Leave blank to do disable the takover.'),
'#default_value' => variable_get('prod_check_nagios_takeover', ''),
'#required' => FALSE,
);
$form['prod_check_nagios']['nagios']['settings']['prod_check_nagios_unique'] = array(
'#type' => 'select',
'#title' => t('When Nagios unique ID not recieved'),
'#description' => t('Select what should happen when the Nagios unique ID is not recieved by the nagios page.'),
'#options' => array(
'default' => t('default Nagios module behavior'),
'404' => t('throw a 404 error'),
'home' => t('redirect to homepege'),
),
'#default_value' => variable_get('prod_check_nagios_unique', 'default'),
'#required' => FALSE,
);
$form['prod_check_nagios']['nagios']['settings']['prod_check_nagios_verbose'] = array(
'#type' => 'checkbox',
'#title' => t('Show verbose status info'),
'#description' => t('Tick this box if you want to see detailed information about every check. Useful for debugging or first setup, but <strong>not recommended for production use!</strong>'),
'#default_value' => variable_get('prod_check_nagios_verbose', 0),
'#required' => FALSE,
);
}
}
$form['buttons'] = array(
'#type' => 'markup',
'#prefix' => '<div class="buttons">',
'#value' => ' ',
'#suffix' => '</div>',
);
$form['buttons']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save configuration'),
);
$form['buttons']['reset'] = array(
'#type' => 'submit',
'#value' => t('Reset to defaults'),
);
return $form;
}
function _prod_check_functions_as_form($compatibility = 'all') {
$form = array();
$form['prod_check_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Configure what data you wish to monitor with <strong>Nagios</strong> for this site.'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['prod_check_settings']['monitor_settings'] = array(
'#type' => 'markup',
'#prefix' => '<div id="prod-check-settings">',
'#suffix' => '</div>',
'#tree' => TRUE,
);
$i = 1;
$functions = _prod_check_functions();
if ($compatibility == 'all') {
unset($functions['prod_mon']);
unset($functions['perf_data']);
}
foreach ($functions as $set => $data) {
$rest = $i % 2;
$form['prod_check_settings']['monitor_settings'][$set] = array(
'#type' => 'checkboxes',
'#title' => t($data['title']),
'#description' => t($data['description']),
'#options' => $data['functions'],
'#default_value' => array_keys($data['functions']),
'#prefix' => '<div class="prod-check-settings ' . ($rest ? 'odd' : 'even') . '">',
'#suffix' => '</div>',
);
$i++;
}
return $form;
}
function prod_check_enable_xmlrpc() {
$form = _prod_check_ahah_helper();
$xmlrpc = $form['prod_check_xmlrpc']['xmlrpc'];
unset($xmlrpc['#prefix'], $xmlrpc['#suffix']);
$output = drupal_render($xmlrpc);
print drupal_json(array(
'status' => TRUE,
'data' => $output,
));
exit;
}
function prod_check_enable_nagios() {
$form = _prod_check_ahah_helper();
$nagios = $form['prod_check_nagios']['nagios'];
unset($nagios['#prefix'], $nagios['#suffix']);
$output = drupal_render($nagios);
print drupal_json(array(
'status' => TRUE,
'data' => $output,
));
exit;
}
function _prod_check_ahah_helper() {
$form_state = array(
'storage' => NULL,
'submitted' => FALSE,
);
$form_build_id = $_POST['form_build_id'];
$form = form_get_cache($form_build_id, $form_state);
$args = $form['#parameters'];
$form_id = array_shift($args);
$form_state['post'] = $form['#post'] = $_POST;
$form_state['ahah_submission'] = TRUE;
$form['#programmed'] = $form['#redirect'] = FALSE;
drupal_process_form($form_id, $form, $form_state);
$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
return $form;
}
function prod_check_settings_form_validate($form, &$form_state) {
if (!empty($form_state['ahah_submission'])) {
return;
}
$base = drupal_get_path('module', 'prod_check');
drupal_add_css($base . '/css/prod-check.css');
drupal_add_js($base . '/js/jquery.equalheights.js', 'module', 'header');
drupal_add_js($base . '/js/jquery.maskedinput.min.js', 'module', 'header');
drupal_add_js($base . '/js/prod-check.js', 'module', 'header');
if (module_exists('dblog')) {
if (!is_numeric($form_state['values']['prod_check_dblog_php_threshold'])) {
form_set_error('prod_check_dblog_php_threshold', t('The PHP error threshold should be numeric!'));
}
}
if ($form_state['values']['prod_check_enable_xmlrpc']) {
if (empty($form_state['values']['prod_check_xmlrpc_key'])) {
form_set_error('prod_check_xmlrpc_key', t('When enabling the XPLRPC API, you <strong>must</strong> enter an API key!'));
}
}
if (!empty($form_state['values']['prod_check_module_list_time'])) {
if (strpos($form_state['values']['prod_check_module_list_time'], ':') != 2) {
form_set_error('prod_check_module_list_time', t('Time must be input in 24 hour format: HH:MM!'));
}
else {
$time = explode(':', $form_state['values']['prod_check_module_list_time']);
if (intval($time[0]) > 23) {
form_set_error('prod_check_module_list_time', t('Hours must range from 00 (midnight) to 23!'));
}
if (intval($time[1]) > 59) {
form_set_error('prod_check_module_list_time', t('Minutes must range from 00 to 59!'));
}
}
}
if (!is_numeric($form_state['values']['prod_check_apc_expunge'])) {
form_set_error('prod_check_apc_expunge', t('APC/OPcache Cache full count threshold should be numeric!'));
}
if (isset($form_state['values']['prod_check_enable_nagios']) && $form_state['values']['prod_check_enable_nagios']) {
$checks = array();
foreach ($form_state['values']['monitor_settings'] as $set => $data) {
foreach ($data as $check => $value) {
if ($value) {
$checks[$set][] = $value;
}
}
}
if (empty($checks)) {
form_set_error('monitor_settings', t('When enabling Nagios support, you <strong>must</strong> tick at least one of the checkboxes!'));
}
if (!empty($form_state['values']['prod_check_nagios_takeover'])) {
if (!preg_match('/^[a-zA-Z0-9\\-_]*$/', $form_state['values']['prod_check_nagios_takeover'])) {
form_set_error('prod_check_nagios_takeover', t('The takover path can only contain alphanumeric characters, dash and underscore.'));
}
elseif (strtolower($form_state['values']['prod_check_nagios_takeover']) == 'nagios') {
form_set_error('prod_check_nagios_takeover', t('The path should be different than the default Nagios module path!'));
}
}
}
}
function prod_check_settings_form_submit($form, &$form_state) {
if (!empty($form_state['ahah_submission'])) {
return;
}
switch ($form_state['values']['op']) {
case t('Force immediate module list reporting'):
variable_set('prod_check_module_list_lastrun', -1);
break;
case t('Save configuration'):
variable_set('prod_check_sitemail', $form_state['values']['prod_check_sitemail']);
if (module_exists('dblog')) {
variable_set('prod_check_dblog_php', $form_state['values']['prod_check_dblog_php']);
variable_set('prod_check_dblog_php_threshold', $form_state['values']['prod_check_dblog_php_threshold']);
}
variable_set('prod_check_apc_expunge', $form_state['values']['prod_check_apc_expunge']);
variable_set('prod_check_apcuser', $form_state['values']['prod_check_apcuser']);
if (!empty($form_state['values']['prod_check_apcpass'])) {
variable_set('prod_check_apcpass', $form_state['values']['prod_check_apcpass']);
}
else {
variable_set('prod_check_apcpass', 'password');
}
if ($form_state['values']['prod_check_enable_xmlrpc']) {
variable_set('prod_check_enable_xmlrpc', $form_state['values']['prod_check_enable_xmlrpc']);
variable_set('prod_check_xmlrpc_key', $form_state['values']['prod_check_xmlrpc_key']);
variable_set('prod_check_module_list_day', $form_state['values']['prod_check_module_list_day']);
variable_set('prod_check_module_list_time', $form_state['values']['prod_check_module_list_time']);
}
else {
variable_set('prod_check_enable_xmlrpc', 0);
}
if (isset($form_state['values']['prod_check_enable_nagios']) && $form_state['values']['prod_check_enable_nagios']) {
$checks = array();
foreach ($form_state['values']['monitor_settings'] as $set => $data) {
foreach ($data as $check => $value) {
if ($value) {
$checks[$set][] = $value;
}
}
}
variable_set('prod_check_enable_nagios', $form_state['values']['prod_check_enable_nagios']);
variable_set('prod_check_nagios_checks', $checks);
if (variable_get('prod_check_nagios_takeover', '') != $form_state['values']['prod_check_nagios_takeover'] || variable_get('prod_check_nagios_unique', 'default') != $form_state['values']['prod_check_nagios_unique']) {
variable_set('prod_check_nagios_takeover', $form_state['values']['prod_check_nagios_takeover']);
variable_set('prod_check_nagios_unique', $form_state['values']['prod_check_nagios_unique']);
menu_rebuild();
}
variable_set('prod_check_nagios_verbose', $form_state['values']['prod_check_nagios_verbose']);
}
else {
variable_set('prod_check_enable_nagios', 0);
}
drupal_set_message(t('The configuration options have been saved.'));
break;
case t('Reset to defaults'):
db_query('DELETE FROM {variable} WHERE name LIKE "prod_check\\_%" AND name <> "prod_check_module_list_lastrun"');
cache_clear_all('variables', 'cache');
drupal_set_message(t('The configuration options have been reset to their default values.'));
break;
}
}
function prod_check_prod_mode_form() {
drupal_set_title(t('Switch to production mode'));
$form = array();
$form['help'] = array(
'#markup' => '<p>' . t('Submitting this form will switch this website to <em>production mode</em>. This means that various settings will be adjusted in order to fix most of the problems reported on the status page. <strong>Some modules will be disabled as well!</strong>') . '</p>',
);
$form['site_mail'] = array(
'#type' => 'textfield',
'#title' => t('Site e-mail address'),
'#description' => t('This field is optional and will be used to setup the default e-mail address of this site. <strong>Currently set to %mail.</strong>', array(
'%mail' => variable_get('site_mail', '[not set]'),
)),
);
if (module_exists('webform')) {
$form['webform_default_from_address'] = array(
'#type' => 'textfield',
'#title' => t('Webform default from e-mail address'),
'#description' => t('This field is optional and will be used to setup the default from e-mail address for <em>Webform</em>. <strong>Currently set to %mail.</strong>', array(
'%mail' => variable_get('webform_default_from_address', '[not set]'),
)),
);
}
if (module_exists('googleanalytics')) {
$form['googleanalytics_account'] = array(
'#type' => 'textfield',
'#title' => t('Google Analytics Web Property ID'),
'#description' => t('This field is optional and will be used to setup the <em>Google Analytics</em> account. <strong>Currently set to %account.</strong>', array(
'%account' => variable_get('googleanalytics_account', '[not set]'),
)),
);
}
$form['block_cache'] = array(
'#type' => 'checkbox',
'#title' => t('Enable <em>Block cache</em>'),
'#description' => t("If ticked, this will enable block caching. <strong>This can cause unwanted results if custom blocks don't handle caching well!</strong>"),
);
if (module_exists('dblog')) {
$form['dblog'] = array(
'#type' => 'checkbox',
'#title' => t('Disable <em>Database logging</em>'),
'#description' => t('If ticked, this will disable the <em>dblog</em> module wich could generate too much overhead on high traffic sites.'),
);
}
$form['nagios'] = array(
'#type' => 'checkbox',
'#title' => t('Enable <em>Nagios</em>'),
'#description' => t('If ticked, this will enable the <em>Nagios</em> monitoring module if it is present.'),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Enable production mode'),
);
return $form;
}
function prod_check_prod_mode_form_validate($form, &$form_state) {
$checks = array(
'site_mail',
'webform_default_from_address',
);
foreach ($checks as $field) {
if (!empty($form_state['values'][$field])) {
if (!valid_email_address($form_state['values'][$field])) {
form_set_error($field, t('The e-mail address %mail is not valid.', array(
'%mail' => $form_state['values'][$field],
)));
}
}
}
if (!empty($form_state['values']['googleanalytics_account'])) {
if (!preg_match('/^UA-\\d{4,}-\\d+$/', $form_state['values']['googleanalytics_account'])) {
form_set_error('googleanalytics_account', t('A valid Google Analytics Web Property ID is case sensitive and formatted like UA-xxxxxxx-yy.'));
}
}
}
function prod_check_prod_mode_form_submit($form, &$form_state) {
$variables = prod_check_prod_mode_settings($form_state['values']);
drupal_set_message(t('The following settings have been changed: %variables.', array(
'%variables' => implode(', ', array_keys($variables)),
)));
$modules = prod_check_prod_mode_modules($form_state['values']);
if (!empty($modules['disable'])) {
drupal_set_message(t('The following modules have been <strong>disabled</strong>: %modules.', array(
'%modules' => implode(', ', $modules['disable']),
)));
}
if (!empty($modules['enable'])) {
drupal_set_message(t('The following modules have been <strong>enabled</strong>: %modules.', array(
'%modules' => implode(', ', $modules['enable']),
)));
}
$form_state['redirect'] = 'admin/reports/prod-check';
}
function prod_check_prod_mode_settings($options) {
$variables = array(
'error_level' => ERROR_REPORTING_HIDE,
'cache' => 1,
'preprocess_css' => 1,
'preprocess_js' => 1,
);
if (!module_exists('varnish') && !module_exists('steroids')) {
$variables['page_compression'] = 1;
}
if (isset($options['site_mail']) && !empty($options['site_mail'])) {
$variables['site_mail'] = $options['site_mail'];
}
if (isset($options['webform_default_from_address']) && !empty($options['webform_default_from_address'])) {
$variables['webform_default_from_address'] = $options['webform_default_from_address'];
}
if (isset($options['googleanalytics_account']) && !empty($options['googleanalytics_account'])) {
$variables['googleanalytics_account'] = $options['googleanalytics_account'];
}
if (isset($options['block_cache']) && !empty($options['block_cache'])) {
$variables['block_cache'] = 1;
}
foreach ($variables as $variable => $value) {
variable_set($variable, $value);
}
drupal_clear_css_cache();
drupal_clear_js_cache();
cache_clear_all('*', 'cache_page', TRUE);
return $variables;
}
function prod_check_prod_mode_modules($options) {
$modules['disable'] = array(
'devel',
'devel_generate',
'devel_node_access',
'devel_themer',
'update',
);
if (isset($options['dblog']) && $options['dblog']) {
$modules['disable'][] = 'dblog';
}
foreach ($modules['disable'] as $id => $module) {
if (!module_exists($module)) {
unset($modules['disable'][$id]);
}
}
if (!empty($modules['disable'])) {
module_disable($modules['disable']);
}
$modules['enable'] = array();
if ($options['nagios']) {
$modules['enable'][] = 'nagios';
}
if (!empty($modules['enable'])) {
module_enable($modules['enable']);
foreach ($modules['enable'] as $id => $module) {
if (!module_exists($module)) {
unset($modules['enable'][$id]);
}
}
}
return $modules;
}
function prod_check_generate_key($length = 25) {
$chars = 'abcdefghijklmnopqrstuxyvwzABCDEFGHIJKLMNOPQRSTUXYVWZ+-*#&@!?';
$size = strlen($chars);
$key = '';
for ($i = 0; $i < $length; $i++) {
$key .= $chars[rand(0, $size - 1)];
}
return $key;
}
function prod_check_dbstatus() {
global $db_type, $db_url;
$db_name = trim(parse_url($db_url, PHP_URL_PATH), '/');
$details = array(
'status' => array(),
'tables' => array(),
'databases' => array(),
);
switch ($db_type) {
case 'mysql':
case 'mysqli':
$output = _prod_check_dbstatus_mysql($db_type, $db_name, $details);
break;
case 'pgsql':
$output = _prod_check_dbstatus_pgsql($db_name, $details);
break;
default:
$title = t('Unsupported database type.');
$output = theme('prod_check_dbstatus', $title, NULL, $details);
break;
}
return $output;
}
function _prod_check_dbstatus_mysql($db_type, $db_name, $details) {
$server = call_user_func($db_type . '_get_server_info');
$title = t('Running @db @version', array(
'@db' => 'MySQL',
'@version' => $server,
));
$status = call_user_func($db_type . '_stat');
if (is_array($status)) {
$status = implode("<br />\n", $status);
}
else {
$status = str_replace(' ', "<br />\n", $status);
}
$client_info = call_user_func($db_type . '_get_client_info');
$host_info = call_user_func($db_type . '_get_host_info');
$protocol_version = call_user_func($db_type . '_get_proto_info');
$additional = "Client version: {$client_info}<br />\n";
$additional .= "Host info: {$host_info}<br />\n";
$additional .= "Protocol version: {$protocol_version}<br />\n";
$additional .= t('For a summary, check the Drupal core <a href="!link">sql status page</a>.', array(
'!link' => url('admin/reports/status/sql'),
)) . "<br /><br />\n";
$status = $additional . $status;
$rows = array();
try {
$result = db_query('SHOW STATUS');
while ($row = db_fetch_object($result)) {
$rows[] = array(
$row->Variable_name,
$row->Value,
);
}
} catch (Exception $e) {
}
if ($rows) {
$details['status'] = array(
'title' => t('Detailed status'),
'header' => array(
t('Variable'),
t('Value'),
),
'rows' => $rows,
);
}
$rows = array();
try {
$result = db_query('SHOW TABLES FROM %s', $db_name);
$property = 'Tables_in_' . $db_name;
while ($row = db_fetch_object($result)) {
$rows[] = array(
$row->{$property},
);
}
} catch (Exception $e) {
}
if ($rows) {
$details['tables'] = array(
'title' => t('Tables for active database %name', array(
'%name' => $db_name,
)),
'header' => array(
t('Table'),
),
'rows' => $rows,
);
}
$rows = array();
try {
$result = db_query('SHOW DATABASES');
while ($row = db_fetch_object($result)) {
$rows[] = array(
$row->Database,
);
}
} catch (Exception $e) {
}
if ($rows) {
$details['databases'] = array(
'title' => t('Available databases'),
'header' => array(
t('Database'),
),
'rows' => $rows,
);
}
return theme('prod_check_dbstatus', $title, $status, $details);
}
function _prod_check_dbstatus_pgsql($db_name, $details) {
global $active_db;
$server = db_result(db_query('SELECT version()'));
$title = t('Running @version', array(
'@version' => $server,
));
$variables = array(
'server_version',
'server_encoding',
'client_encoding',
'is_superuser',
'session_authorization',
'DateStyle',
'TimeZone',
'integer_datetimes',
);
$status = '';
foreach ($variables as $variable) {
$status .= $variable . ': ' . pg_parameter_status($active_db, $variable) . "<br />\n";
}
$rows = array();
try {
$result = db_query('SELECT * FROM pg_settings');
while ($row = db_fetch_object($result)) {
$rows[] = array(
$row->name,
$row->setting,
);
}
} catch (Exception $e) {
}
if ($rows) {
$details['status'] = array(
'title' => t('Detailed status'),
'header' => array(
t('Name'),
t('Setting'),
),
'rows' => $rows,
);
}
$rows = array();
try {
$result = db_query("SELECT relname FROM pg_class WHERE relname !~ '^(pg_|sql_)' AND relkind = 'r'");
while ($row = db_fetch_object($result)) {
$rows[] = array(
$row->relname,
);
}
} catch (Exception $e) {
}
if ($rows) {
$details['tables'] = array(
'title' => t('Tables for active database %name', array(
'%name' => $db_name,
)),
'header' => array(
t('Table'),
),
'rows' => $rows,
);
}
$rows = array();
try {
$result = db_query('SELECT datname FROM pg_database');
while ($row = db_fetch_object($result)) {
$rows[] = array(
$row->datname,
);
}
} catch (Exception $e) {
}
if ($rows) {
$details['databases'] = array(
'title' => t('Available databases'),
'header' => array(
t('Database'),
),
'rows' => $rows,
);
}
return theme('prod_check_dbstatus', $title, $status, $details);
}
function prod_check_apc_opc() {
if (function_exists('opcache_get_status')) {
include drupal_get_path('module', 'prod_check') . '/includes/prod_check.opcache.inc';
exit;
}
elseif (function_exists('apc_cache_info')) {
define('ADMIN_USERNAME', variable_get('prod_check_apcuser', 'apc'));
define('ADMIN_PASSWORD', variable_get('prod_check_apcpass', 'password'));
include drupal_get_path('module', 'prod_check') . '/includes/prod_check.apc.inc';
exit;
}
else {
return t('APC nor OPcache is installed on this webserver!');
}
}
function prod_check_memcache() {
$memcache_servers = variable_get('memcache_servers', array(
'127.0.0.1:11211' => 'default',
));
global $MEMCACHE_SERVERS;
$MEMCACHE_SERVERS = array_keys($memcache_servers);
include drupal_get_path('module', 'prod_check') . '/includes/prod_check.memcache.inc';
exit;
}