Monitoring your web site. Checking if the host server is running

PHP code to monitor your web site

By Sergey Skudaev

+++++

Here I show you how to monitor your web site and check how many times per day your host is getting down.

All error codes and description will be stored in the MySql database on your host control panel. First, we have to create a table to store error information. I called the table "mylog"

If you do not know how to create table in MySql database using phpMyAdmin read my tutorial about phpMyAdmin.

Here is script for our table:

CREATE TABLE IF NOT EXISTS mylog (
id int(11) NOT NULL auto_increment,
cerror varchar(100) NOT NULL,
errtime timestamp NOT NULL default CURRENT_TIMESTAMP,
PRIMARY KEY (id)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

We are going to use few PHP functions. The first one is curl_init() that initializes a new session and return a cURL handle. This cURL handle will be used with the curl_setopt(), curl_exec(), and curl_close() functions.

The bool curl_setopt ( resource $ch , int $option , mixed $value ) function sets URL options.

We will use this fuction to set two options. Detailed description of these options is located on www.PHP.net web site

CURLOPT_RETURNTRANSFER option is used to return only true or false from the curl_exec() function.

CURLOPT_CONNECTTIMEOUT option sets the number of seconds to wait while trying to connect.

The curl_exec(curl_handle) function executes the given cURL session.

The curl_error(curl_handle) function returns error text

The curl_getinfo(curl_handle) will be used to get http_code for errors

And curl_close(curl_handle) fucntion will close the cURL resurce.

Here is a list of http codes from www.php.net

The whole code goes below:

<?

//get today date and time in "2009-03-19 17:15:10" format
$amp;today=date("Y-m-d H:m:s");

$amp;url='http://www.mydomain.com'; // the URL of your web site
$amp;timeout = 60; // the number of seconds to wait for request.

$amp;curl_handle = curl_init($amp;url);
curl_setopt($amp;curl_handle, CURLOPT_RETURNTRANSFER, 0); // return TRUE or FALSE
curl_setopt($amp;curl_handle, CURLOPT_CONNECTTIMEOUT, $amp;timeout);

ob_start(); //turn on buffer
$amp;result = curl_exec($amp;curl_handle);
ob_end_clean(); //clear buffer
$amp;cinfo = curl_getinfo($amp;curl_handle);
curl_close($amp;curl_handle);

if ( !$amp;result || $amp;cinfo['http_code'] != 200 ) {
$cerror =" HTTP Code: [". $cinfo['http_code']. "]";
//Returns a text error message.
if ( curl_error($curl_handle) )
$cerror .= " ". curl_error($curl_handle);

       $amp;hostname = "localhost";
       $amp;dbuser = "your_user";
       $amp;dbpassword = "your_password";
       $amp;dbname = "your_db";

$amp;db_link=mysql_connect($amp;host, $amp;dbuser, $amp;dbpassword) or die("Unable to connect to the server!");

mysql_select_db($amp;dbname) or die("Unable to connect to the database");

$amp;isql="insert into mylog(id, cerror, errtime) values(0, '".$amp;cerror."','".$amp;today."')";

           if(!mysql_query($amp;isql))
           {
           echo mysql_errno() . ":error ";
           echo mysql_error() . "<br/>";
           }

}

?>

Now to monitor your web site you have to run that sript periodically, for example, every hour. Your host control panel has cron job cron job icon in control panel that allows you to set schedule and run the script.

Double click this icon and on the next window select the Advance (Unix Style) button.

setup choice

On the next window set up time to run the script. On the table time set to run script once a day.

00***

On the image below time set to run script every hour.

set time on cron job

In the command field enter path to PHP. Usually, it is '/usr/local/bin/php'

Then you have to enter the path to your script. You can fint it out on control panel. It is called "home directory.

home directory

If you cannot find home directory go to file manager home directoryand double click the icon. You will see your home directory

home directory again

If your script name is myscript.php and you put in the root directory, then your whole command will be like that:

"/usr/local/bin/php home/user/www/myscript.php"

It is better, however, to put the script in protected folder.

After entering command, click Commit Changes button. Your cron job is saved.

First time you may try to run this script manually to see if you did not mistype something.

All error in cript can be found in Error Log home directory again on your control panel

Then you may set time to nearest minute for example if now is 12:30 you can set cron table to 12:32 and save.

That is it

Web hosting links:

http://www.hostmonster.com Free Domain, Free Setup, Host Unlimited Domains $6.95 I am using it for this site.

http://www.justhost.com Professional Hosting from Just Host $4.95/month - Unlimited disk space and transfers and host unlimited domains I am using it for my another web site www.best-your-trip.com

http://www.Hostgator.com
Looking for Web Hosting With Quality Support? 24/7 Support Via Phone, Live Chat, and Email!

http://www.bluehost.com
Reliable, Affordable Business Hosting from Bluehost.com.

http://www.ImHosted.com
ImHosted.com - MEGA Web Hosting Deal. Click for details!

http://www.ixwebhosting.com
IXWebHosting.Com: $3.95/mo - Superior Service with Excellent 24/7 Toll Free Support, Live Chat, and Email. Linux AND Windows packages!!!

http://www.iPower.com
iPower - The Leader in Web Hosting

http://www.ipowerweb.com
Web Hosting - $7.95 - Free Setup - Rebates

http://www.fatcow.com/fatcow/index.bml
FatCow Web Hosting. Free Trial , Free Setup, Free Support – All risk free!

Did you find information useful? Send to your friend a link to this page

Please rate the tutorial

1 2 3 4 5 6 7 8 9 10




Comments
 
Register to add comments ( 1000 char ) for check_server.php.
Are you human? Please select two the same numbers