Note: This article was originally published in 2014. Some steps, commands, or software versions may have changed. Check the current Varnish documentation for the latest information.
In this step-by-step guide, you’ll learn create a php file to check on the status of a drupal server.
How to: Create a php file to check on the status of a Drupal Server
I was working on creating a php status file that would indicate if a server was able to handle client requests or not. The idea is that you can have a loadbalancer check periodically those status.php files to see if that server is online and able to handle traffic or not. In my case I was interested in creating a file for a WordPress which one day I hope I’ll get to it, but while looking around for something I found a good one for Drupal.
If you are looking into deploying a Highly Available Drupal server here is the source post that covers how to do so using Varnish as front end proxy servers: http://www.lullabot.com/blog/article/configuring-varnish-high-availability-multiple-web-servers.
I think there is lots of great content on the site but seeing that I already use (http://kx.cloudingenium.com/technologies/web/nginx/nginx/ “What is: NginX”)as a front end proxy server (I once used Varnish but then I figured why add another system to the stack and decided to stay with (http://kx.cloudingenium.com/technologies/web/nginx/nginx/ “What is: NginX”)as I was already using it as our web server) what I was really interested on was the status file. Below I am copying that file for anyone who is interested and you can refer to the original site for more information on how to setup a Highly Available Website.
<?php
// Register our shutdown function so that no other shutdown functions run before this one.
// This shutdown function calls exit(), immediately short-circuiting any other shutdown functions,
// such as those registered by the devel.module for statistics.
register_shutdown_function('status_shutdown');
function status_shutdown() {
exit();
}
// Drupal bootstrap.
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_DATABASE);
// Build up our list of errors.
$errors = array();
// Check that the main database is active.
$result = db_query('SELECT * FROM {users} WHERE uid = 1');
$account = db_fetch_object($result);
if (!$account->uid == 1) {
$errors[] = 'Master database not responding.';
}
// Check that the slave database is active.
if (function_exists('db_query_slave')) {
$result = db_query_slave('SELECT * FROM {users} WHERE uid = 1');
$account = db_fetch_object($result);
if (!$account->uid == 1) {
$errors[] = 'Slave database not responding.';
}
}
// Check that all memcache instances are running on this server.
if (isset($conf)) {
foreach ($conf as $address => $bin) {
list($ip, $port) = explode(':', $address);
if (!memcache_connect($ip, $port)) {
$errors[] = 'Memcache bin <em>' . $bin . '</em> at address ' . $address . ' is not available.';
}
}
}
// Check that the files directory is operating properly.
if ($test = tempnam(variable_get('file_directory_path', conf_path() .'/files'), 'status_check_')) {
// Uncomment to check if files are saved in the correct server directory.
//if (!strpos($test, '/mnt/nfs') === 0) {
// $errors[] = 'Files are not being saved in the NFS mount under /mnt/nfs.';
//}
if (!unlink($test)) {
$errors[] = 'Could not delete newly create files in the files directory.';
}
}
else {
$errors[] = 'Could not create temporary file in the files directory.';
}
// Print all errors.
if ($errors) {
$errors[] = 'Errors on this server will cause it to be removed from the load balancer.';
header('HTTP/1.1 500 Internal Server Error');
print implode("<br />\n", $errors);
}
else {
// Split up this message, to prevent the remote chance of monitoring software
// reading the source code if mod_php fails and then matching the string.
print 'CONGRATULATIONS' . ' 200';
}
// Exit immediately, note the shutdown function registered at the top of the file.
exit();
(http://img.zemanta.com/zemified_h.png?x-id=529e85d6-19ce-42aa-a8b1-2fe64083d82b)](http://www.zemanta.com/?px “Enhanced by Zemanta”)
Summary
You’ve successfully learned create a php file to check on the status of a drupal server. If you run into any issues, double-check the prerequisites and ensure your Varnish environment is properly configured.