Check AmazonEC2 WebSite status and if server is down send notification and restart instance


/ Published in: PHP
Save to your folder(s)

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


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. require_once('AWSSDKforPHP/sdk.class.php');
  4.  
  5. function checkOnline($domain) {
  6. $curlInit = curl_init($domain);
  7. curl_setopt($curlInit,CURLOPT_CONNECTTIMEOUT,10);
  8. curl_setopt($curlInit,CURLOPT_HEADER,true);
  9. curl_setopt($curlInit,CURLOPT_NOBODY,true);
  10. curl_setopt($curlInit,CURLOPT_RETURNTRANSFER,true);
  11.  
  12. //get answer
  13. $response = curl_exec($curlInit);
  14.  
  15. curl_close($curlInit);
  16. if ($response) return true;
  17. return false;
  18. }
  19.  
  20. function sendNotificationEmail($message){
  21. $email = new AmazonSES();
  22. $response = $email->send_email(
  23. '[email protected]', // Source (aka From) Authorized Amazon SES sender
  24. array( // Destination (aka To)
  25. 'ToAddresses' => array(
  26. "[email protected]" // Email to send notifications
  27.  
  28. ),
  29.  
  30. ),
  31. array( // Message (long form)
  32. 'Subject' => array(
  33. 'Data' => 'WebSite not available',
  34. 'Charset' => 'UTF-8'
  35. ),
  36. 'Body' => array(
  37. 'Text' => array(
  38. 'Data' => utf8_encode($message),
  39. 'Charset' => 'UTF-8'
  40. ),
  41. 'Html' => array(
  42. 'Data' => utf8_encode($message),
  43. 'Charset' => 'UTF-8'
  44. )
  45. )
  46. ),
  47. array( // Optional parameters
  48. //'ReplyToAddresses' => array('[email protected]', '[email protected]')
  49. )
  50. );
  51.  
  52. // Success?
  53. return $response->isOK();
  54.  
  55.  
  56. }
  57.  
  58. $Instances = array(
  59. 'WebSite1'=>array(
  60. 'instanceId'=>'i-1923cb7e',
  61. 'testUrl'=>'http://www.domain1.com',
  62.  
  63. ),
  64. 'WebSite2'=>array(
  65. 'instanceId'=>'i-32243d57',
  66. 'testUrl'=>'http://www.domain2.com',
  67.  
  68. ),
  69. 'WebSite3'=>array(
  70. 'instanceId'=>'i-ddcfc1bb',
  71. 'testUrl'=>'http://www.domain3.com',
  72.  
  73. )
  74.  
  75. );
  76. $ec2 = new AmazonEC2();
  77.  
  78. foreach ($Instances as $key => $val){
  79.  
  80. echo $key." => ". $val["instanceId"] ;
  81.  
  82. if(checkOnline($val["testUrl"])) {
  83. echo "OnLine";
  84.  
  85. }
  86. else{
  87. $response = $ec2->reboot_instances($val["instanceId"]);
  88. echo "OffLIne";
  89. if(!$response->isOK()){
  90. sendNotificationEmail("The WebSite ".$key." is not available and it couldn't be restarted \n please check status ".$val["testUrl"]);
  91.  
  92. }
  93. else{
  94. sendNotificationEmail("The WebSite ".$key." and it's been restarted \n check status here ".$val["testUrl"]);
  95.  
  96. }
  97. }
  98.  
  99. }
  100.  
  101.  
  102. //You may add crontab using : */3 * * * * wget --spider 'http://www.yourdomain.com/scriptJob.php'
  103.  
  104.  
  105.  
  106.  
  107.  
  108. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.