You are here

prod_check.dbconnect.php in Production check & Production monitor 6

Same filename and directory in other branches
  1. 7 prod_check.dbconnect.php

Simple database connection check that can be placed anywhere within a Drupal installation. Does NOT need to be in the root where index.php resides!

File

prod_check.dbconnect.php
View source
<?php

/**
 * @file
 * Simple database connection check that can be placed anywhere within a Drupal
 * installation. Does NOT need to be in the root where index.php resides!
 */

/**
 * Locate the actual Drupal root. Based on drush_locate_root().
 */
function locate_root() {
  $drupal_root = FALSE;
  $start_path = isset($_SERVER['PWD']) ? $_SERVER['PWD'] : '';
  if (empty($start_path)) {
    $start_path = getcwd();
  }
  foreach (array(
    TRUE,
    FALSE,
  ) as $follow_symlinks) {
    $path = $start_path;
    if ($follow_symlinks && is_link($path)) {
      $path = realpath($path);
    }

    // Check the start path.
    if (valid_root($path)) {
      $drupal_root = $path;
      break;
    }
    else {

      // Move up dir by dir and check each.
      while ($path = shift_path_up($path)) {
        if ($follow_symlinks && is_link($path)) {
          $path = realpath($path);
        }
        if (valid_root($path)) {
          $drupal_root = $path;
          break 2;
        }
      }
    }
  }
  return $drupal_root;
}

/**
 * Based on the DrupalBoot*::valid_root() from Drush.
 */
function valid_root($path) {
  if (!empty($path) && is_dir($path) && file_exists($path . '/index.php')) {
    $candidate = 'includes/common.inc';
    if (file_exists($path . '/' . $candidate) && file_exists($path . '/misc/drupal.js')) {
      return TRUE;
    }
  }
  return FALSE;
}

/**
 * Based on _drush_shift_path_up().
 */
function shift_path_up($path) {
  if (empty($path)) {
    return FALSE;
  }
  $path = explode('/', $path);

  // Move one directory up.
  array_pop($path);
  return implode('/', $path);
}

/**
 * Do the actual database connection check.
 */
define('DRUPAL_ROOT', locate_root());

// Drupal 6 does not use DRUPAL_ROOT internally so we have to change the dir
// manually in order for the bootstrap to find all necessary includes.
chdir(DRUPAL_ROOT);
require_once DRUPAL_ROOT . '/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);
$result = db_result(db_query('SELECT COUNT(filename) FROM {system}'));
if ($result) {
  $msg = 'OK';
  http_response_code(200);
}
else {
  http_response_code(500);
  $msg = 'NOK';
}

// Prevent caching of this page!
header("Cache-Control: no-cache, no-store, must-revalidate");

// HTTP 1.1.
header("Pragma: no-cache");

// HTTP 1.0.
header("Expires: 0");

// Proxies.
exit($msg);

Functions

Namesort descending Description
locate_root Locate the actual Drupal root. Based on drush_locate_root().
shift_path_up Based on _drush_shift_path_up().
valid_root Based on the DrupalBoot*::valid_root() from Drush.

Constants

Namesort descending Description
DRUPAL_ROOT Do the actual database connection check.