Revision: 61657
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at January 3, 2013 13:18 by cduquev
Initial Code
<?php
require_once('AWSSDKforPHP/sdk.class.php');
function checkOnline($domain) {
$curlInit = curl_init($domain);
curl_setopt($curlInit,CURLOPT_CONNECTTIMEOUT,10);
curl_setopt($curlInit,CURLOPT_HEADER,true);
curl_setopt($curlInit,CURLOPT_NOBODY,true);
curl_setopt($curlInit,CURLOPT_RETURNTRANSFER,true);
//get answer
$response = curl_exec($curlInit);
curl_close($curlInit);
if ($response) return true;
return false;
}
function sendNotificationEmail($message){
$email = new AmazonSES();
$response = $email->send_email(
'[email protected]', // Source (aka From) Authorized Amazon SES sender
array( // Destination (aka To)
'ToAddresses' => array(
"[email protected]" // Email to send notifications
),
),
array( // Message (long form)
'Subject' => array(
'Data' => 'WebSite not available',
'Charset' => 'UTF-8'
),
'Body' => array(
'Text' => array(
'Data' => utf8_encode($message),
'Charset' => 'UTF-8'
),
'Html' => array(
'Data' => utf8_encode($message),
'Charset' => 'UTF-8'
)
)
),
array( // Optional parameters
//'ReplyToAddresses' => array('[email protected]', '[email protected]')
)
);
// Success?
return $response->isOK();
}
$Instances = array(
'WebSite1'=>array(
'instanceId'=>'i-1923cb7e',
'testUrl'=>'http://www.domain1.com',
),
'WebSite2'=>array(
'instanceId'=>'i-32243d57',
'testUrl'=>'http://www.domain2.com',
),
'WebSite3'=>array(
'instanceId'=>'i-ddcfc1bb',
'testUrl'=>'http://www.domain3.com',
)
);
$ec2 = new AmazonEC2();
foreach ($Instances as $key => $val){
echo $key." => ". $val["instanceId"] ;
if(checkOnline($val["testUrl"])) {
echo "OnLine";
}
else{
$response = $ec2->reboot_instances($val["instanceId"]);
echo "OffLIne";
if(!$response->isOK()){
sendNotificationEmail("The WebSite ".$key." is not available and it couldn't be restarted \n please check status ".$val["testUrl"]);
}
else{
sendNotificationEmail("The WebSite ".$key." and it's been restarted \n check status here ".$val["testUrl"]);
}
}
}
//You may add crontab using : */3 * * * * wget --spider 'http://www.yourdomain.com/scriptJob.php'
?>
Initial URL
Initial Description
Script File launched every 3 minutes to check WebSite status. It uses AWSSDKforPHP library http://docs.amazonwebservices.com/AWSSdkDocsPHP/latest/DeveloperGuide/php-dg-setup.html
Initial Title
Check AmazonEC2 WebSite status and if server is down send notification and restart instance
Initial Tags
curl
Initial Language
PHP