[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP MySQL - check if remote website is up and MySQL is running

Posted on 2005-05-05
8
Medium Priority
?
478 Views
Last Modified: 2008-02-01
Hi,
I have a website that displays ads - website A.
The ads are located on a MySQL database, which is located on a remote website B that I connect to via my connection script below.

Contents of conn_data.php

<?php session_start();
$hostip = "203.12.34.256";
$hostname_conn_data = $hostip;
$database_conn_data = "ads_data";
$username_conn_data = "ads_user";
$password_conn_data = "ads12345";
$link = $conn_data = @mysql_pconnect($hostname_conn_data, $username_conn_data, $password_conn_data);
?>

I would like to run a script on website A via cron say every 5 mins that will not only ping website B but also check if MySQL is running with no problems.
If it finds a problem with either the website not being available or MySQL being down then I need this script to be able to set a flag as down and also change the value of $hostip in my conn_data.php connection script to point to my backup server.

The next time the script runs it will check if the flag is down and if Website B is back, then reset IP address back to normal & flag to up, if its still down do nothing.
I guess the flag would just be a text file, which will contain the word UP or DOWN depending on the status of website B.

I would also have a sms message sent to my cell (mobile phone) when Website B is down and also when it comes back up.
I already have set up and tested my sms script supplied by sms gateway to handle the sms message, I just need the script for the above to trigger this.

Any comments or feedback would be appreciated.

Thanks.
0
Comment
Question by:sabecs
  • 5
  • 2
8 Comments
 
LVL 7

Expert Comment

by:Promethyl
ID: 13934809
<?php session_start();
$hostip = "203.12.34.256";
$hostname_conn_data = $hostip;
$database_conn_data = "ads_data";
$username_conn_data = "ads_user";
$password_conn_data = "ads12345";
$link = $conn_data = @mysql_pconnect($hostname_conn_data, $username_conn_data, $password_conn_data);

if (mysql_error( )) $sitedown.="SQL";
if (!strlen(file_get_contents("http://203.12..34.256"))) $sitedown.="Webpage.";

echo $sitedown;
if ($sitedown) mail('your@email.com','WARNING: SIte down '.$sitedown,$sitedown);
 
?>

crontab
*/5 * * * * /usr/bin/php -q /our/page/thispage.php
0
 
LVL 7

Expert Comment

by:Promethyl
ID: 13934813
<?php session_start();
$hostip = "203.12.34.256";
$hostname_conn_data = $hostip;
$database_conn_data = "ads_data";
$username_conn_data = "ads_user";
$password_conn_data = "ads12345";
$link = $conn_data = mysql_connect($hostname_conn_data, $username_conn_data, $password_conn_data);

if (mysql_error( )) $sitedown.="SQL";
if (!strlen(file_get_contents("http://203.12..34.256"))) $sitedown.="Webpage.";

echo $sitedown;
if ($sitedown) mail('your@email.com','WARNING: SIte down '.$sitedown,$sitedown);
 
?>

crontab
*/5 * * * * /usr/bin/php -q /our/page/thispage.php
0
 
LVL 7

Expert Comment

by:jdpipe
ID: 13942351
One thing to point out is that connecting to a MySQL server remotely involves sending your password in clear text over the net. It would be better if this test script were divided as follows. Haven't tested this but I did use something similar once before.

On the server you are testing
-----------------------------------
<?php
mysql_connect("localhost","ads_user","ads12345",");
if($s = mysql_error()){
   header("HTTP/1.1 501 Database conneciton error");
   die("Error: SQL conneciton error: $s");
}
print("OK: database connection was checked");
?>
----------------------------------

On the machine that is doing the test
----------------------------------
<?php
$f=join("",@file("http://remoteserver.com/test.php"));
if(!$f){
   print("Unable to contact remote server - check Apache!");
}else if(preg_match("#error#i",$f)){
   print("Remote server error: $f");
}else{
   print("OK");
}
----------------------------------
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:sabecs
ID: 13950308
Thanks for your comments, I will try both suggestions.

If the remote site is down won't there be a few second delay while the script determines that it cannot connect, before trying to connect to a backup site?
And would this delay occur with every visitor perforing a query on the database?
Is it possible to actually change the IP address in my connection script when remote site is unavailable to point to my backup site and then back again when it is up.

Thanks again for your help.
0
 
LVL 7

Accepted Solution

by:
Promethyl earned 2000 total points
ID: 13950696
That's what the crontab is for.

If you run the job in a cron, it won't affect the speed of your program.

<?php session_start();
$hostip = "203.12.34.256";
$hostname_conn_data = $hostip;
$database_conn_data = "ads_data";
$username_conn_data = "ads_user";
$password_conn_data = "ads12345";
$link = $conn_data = mysql_connect($hostname_conn_data, $username_conn_data, $password_conn_data);

if (mysql_error( )) $sitedown['SQL']="SQL";
if (!strlen(file_get_contents("http://203.12.34.256"))) $sitedown['WEB']="Webpage.";

echo implode(', ',$sitedown);

$fp = fopen ('sql','w');
if  ($sitedown['SQL']) {
  fwrite($fp,'down');
} else {
  fwrite($fp,'up');
}
fclose($fp);
if ($sitedown) mail('your@email.com','WARNING: SIte down '. implode(', ',$sitedown), implode(', ',$sitedown););
 
// at this level, all you have to do is check the sql file for 'up' or 'down', and make your sql server determination.

?>

crontab
*/5 * * * * /usr/bin/php -q /our/page/thispage.php
0
 
LVL 7

Expert Comment

by:Promethyl
ID: 13950703
if (file_get_contents('sql')=='down') {
        $server = 'altserver';
} else {
        $server ='primaryserver';
}

$conn = mysql_connect($server, $uname, $pwd);
0
 

Author Comment

by:sabecs
ID: 13968250
Thanks Promethyl for your help, code works perfectly.
0
 
LVL 7

Expert Comment

by:Promethyl
ID: 13972371
Well met! I'm pleased EE could help you.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month20 days, 1 hour left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question