J. Cornelius

Website and MySQL Monitoring

March 15, 2006

siteWatcher is an easy way to check if your Websites and MySQL databases are up and running. It runs on a schedule you set and simply checks your websites for content you specify and notifies you by email if that content is not found. You can also use it to check connectivity to MySQL databases.

To setup siteWatcher all you need to do is download the PHP file, put it on your server (preferably not in a web accessible place), setup the Websites you want to watch, and add it to your crontab.

NOTE: It works best when run from a server that is not hosting the sites or databases you want to monitor. (duh!)

Get siteWatcher

Checking Websites

siteWatcher checks Websites for the text you specify. Using CURL it will actually go to the Website and read the text. If it cannot find the text you specify, it triggers an email notification.

Important: Be sure the text you enter will always be on the page you are checking. Do Not use any text that is dynamic or might be replaced over time, unless you want to get a lot of email about it.

Open siteWatcher.php in your favorite text editor and look for the following code around line 14 and bend it to your will:

$sites = array('http://www.yoursite.com/','string to find');

Once you have that you can just save the file and move on. Be sure to CHMOD the file to 755 so that you can execute it from the crontab we will setup in a bit.

Checking MySQL

You can also setup siteWatcher to check your MySQL database connections. This is also quite easy. Just look for the following code around line 32:

$mysql[0]['user'] = 'username'; $mysql[0]['pass'] = 'password'; $mysql[0]['db'] = 'mysql.server.name';

If you want to check more than one database just copy and paste the code above and change the number 0 to 1, 2, 3, etc.

Sending Email Notices

What's the point of a monitor if it never tells you when something is down? Next you need to setup your email messages to get to the right place.

Look around line 87 for this:

$TO = 'you@yoursite.com'; $FROM = 'siteWatcher@yoursite.com';

Just change the TO and FROM email addresses and you're all set.

Add it to a Crontab

This is where the action happens. You will need to add a command to your crontab to run siteWatcher every so often. I run it every 10 minutes, but you can change this as you see fit. Note that the more sites and databases you check the longer the script will take to run. So if you are checking a lot of things you may want to check less frequently.

You can easily edit your crontab by typing crontab -e on your servers command line. This uses VI commands so you'll want to be familiar with them. Just add the following to your crontab:

0,10,20,30,40,50 * * * * /path/to/siteWatcher.php
If you don’t have shell access, try using Webmin or see if your hosting provider or server administrator has a tool to edit crontabs.

That's it. Your siteWatcher should now run every 10 minutes to check on things for you. If there is an error, you will get an email. Enjoy!

Was it good for you?

Post to Digg Post to del.icio.us Post to ma.gnolia Post to Furl Post to Mixx

Tags

administration, development, mysql, operations, php

About

J Cornelius is a software developer, Web developer, and Formula 1 fan in Atlanta GA. He has a strange affinity for odd numbers, european sports cars, thoughtful analogies, and is hopelessly addicted to chips & salsa. Read more

Recent Articles