A Simple Homemade Server Alarm

Note: This post is over a year and a half old. You may want to check later in this blog to see if there is new information.

So, you’ve got your server tweaked and you’re pretty sure you’re prepared for an onslaught of traffic should you write a brilliant and useful article. But you’re still a little nervous, right? You’d at least like to know if your site went down, but you’re fully aware that these things always happen while you’re out driving or at work or doing something that requires you to not be sitting in front of your computer constantly refreshing your page like a paranoid dork. So build a simple server alarm! Your server probably already runs PHP, and if you’re using any of the major open source blogging packages, you’re hooked into a mySQL database. Those, together with the UNIX commands “cron” and “sendmail”, are going to be our alarm.

Preparation

Download mysqlcheck.txt and save it as mysqlcheck.php. Open it up in a text editor. Edit your mysql server’s ip address, login and password, and enter an email address to send the alert message to. I have mine set up to send to my cell phone, which happens to have an email address for text messaging. I think most carriers provide that, don’t they? If you have a Blackberry or the like, I’m sure you’re all set.

Installation

Upload the php file to your home directory on your blog server (or any Unix server that’s always on, really). Next we’re going to edit the crontab file. Telnet or SSH to your server and type ‘crontab -e’ at the command line. You’ll be in vi now, so press the letter “I” to enter insert mode. Now add the line:

*/20    *       *       *       *       php ~/mysqlcheck.php

Those are tabs between each asterisk. Not mandatory, but that’s how it’s usually formatted. This will run a check to see if your database is accessible every 20 minutes. You can alter the interval by editing the */20 part to suit your tastes (and level of paranoia). Here’s more information on cron, if you need it.

The basic idea is that I’ve noticed when my Wordpress site gets too much traffic, it’s the mySQL database that is the first to lock up. Then I get an error page saying that the database is unreachable. So anything that’s just checking to see if the port 80 is running will be fine, I think (I haven’t checked the response headers on the error page, which might provide another easy means of checking the site status). This script ensures that the most sensitive part of my server is checked regularly for accessibility.

Now, if your sendmail parameters are properly set up in your php.ini file, you’re all set! The script will use the PHP mail command to send you a message if it encounters any error accessing your database. It won’t do anything to your database, just establish a connection and then die.

Other Options

Other options for the less paranoid: If you run Mint by Shaun Inman there’s a great Pepper available called Mint Notification. You can set a number of hits or unique hits at which point it will send you an email notifying you that your defined point has been reached for the day. This allows you to set a higher-than-normal level of hits that would be an indication of unusual activity at which point you would be notified and could check on the health of your server.

If you use a Mac, Server Siren is a handy little toolbar item that lets you define servers and regularly checks their reachability. It won’t determine whether the database is accessible, but it will let you know if the server goes down for any reason. It can sound an audible alarm or notify you via email. It can also run an applescript or send an SMS (North America only).

Chill

So relax, you can walk away from your terminal now. You’ll know if things go wrong. Of course, you’ll still be checking your Mint stats to see your hits who’s linking to you, but you could always set up Mint Jr. and an RSS Feed on the referrers and check it at the end of the day ;-).

» » » » »

Have your say

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>




Safari hates me