You have probably seen some of those nifty websites that ‘monitor your blog’. All they are really doing is checking up on your domain or IP in an automated fashion. I needed a solution that would let me and/or some folks I work with know if a server is up and running or not. (Some dedicated server hosts give you a notification option)
Figuring that someone has to have already made a script that does this, I did some searching and found some code on blog.a2b.cc. The idea here is that you setup the script with a cron job on some inexpensive and fairly reliable shared hosting account. This way you have a third party monitoring your precious servers.
In the following I have taken what was posted on blog.a2b.cc and made some improvements.
- Configuration section for clarity
- Option to execute a ping command or use fsockopen()
- Option to use mail() or smtp (via phpmailer)
Not many shared hosts let you use the exec() function. So executing a ping command is often out. Using fsockopen() to create a socket connection is a decent way around this.
Using mail() to send your notification emails these days seems to be worthless. More hosts are implementing throttling and other means that either block or delay sending. SMTP is really the better way to send.
First you need to create a table in a database that is something like this:
CREATE TABLE mon_host_ping (
mon_host_ping_id int(5) NOT NULL auto_increment,
mon_host_ping_hostname varchar(128) NOT NULL default ‘’,
mon_host_ping_factor bigint(16) default NULL,
mon_host_ping_lastupdt timestamp(14) NOT NULL,
PRIMARY KEY (mon_host_ping_id),
UNIQUE KEY mon_host_ping_hostname (mon_host_ping_hostname)
Insert the address of whatever website(s) or server(s) you plan to monitor:
insert into mon_host_ping values(NULL, “target.example.com”, 0, NOW());
You will need go through the configuration options at the top of the file. Upload php-status-monitor.php to your account (preferably not somewhere that is open to the web). Then create a cron job that will run every minute. It would look something like this:
php -q /your-absolute-path-or-document-root/php-status-monitor.php -h target.example.com -l pinger.example.com -e firstname.lastname@example.org,email@example.com
-h is the “host” you are pinging, -l is the name of the “localhost”, i.e. the machine you are pinging from, and -e is the email address (or multiple email addresses) you want to send any alarm mails to. If you want it the message sent as a text message to your cell phone you will need to figure out what your cell phone email address is.
Feel free to share and edit the script. Any comments or suggestions submit below.