/ Published in: PHP
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
Expand |
Embed | Plain Text
<?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]" // Email to send notifications ), ), 'Data' => 'WebSite not available', 'Charset' => 'UTF-8' ), 'Charset' => 'UTF-8' ), 'Charset' => 'UTF-8' ) ) ), //'ReplyToAddresses' => array('[email protected]', '[email protected]') ) ); // Success? return $response->isOK(); } '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){ 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' ?>
You need to login to post a comment.
