CURL response from Shell Script

Curl response from shell script

I am running 2 nodes with a website that are maintained by a load balancer. The load balancer is virtual in the case that only the primary site is available at a time because the DB is not mirrored across websites. A sync process sync’s up data every from hours from primary to secondary. We’re using AWS EC2 instances along with their load balancer. The Load Balancer configuration checks for a file online.html to set the state of the server to active or inactive. Initially – we only had the online.html available on the primary site so that the LB always pointed to primary. But when the primary went down – the load balancer would not automatically switch over to the secondary since it didn’t have the online.html.

Solution: I create a page offline.html on the secondary site. I used shell script to get a ping response from primary. If primary was not available – meant that it went down and it was time to bring secondary online. I ssh’d into the secondary – renamed the offline.html to be called online.html so that the AWS LB updates the health check and sets the secondary site to being healthy. And therefore – having the LB automatically switch over to the secondary site. Here’s the shell script that I used:

primarySite=""   ### Could also be an IP Addresse
secondarySite="" ### Could also be an IP Address

### Get response from Primary Site
primaryResponse=$(curl --write-out %{http_code} --silent --output /dev/null "http://$primarySite/online.html")
echo $primaryResponse

### Get response from Secondary Site
secondaryResponse=$(curl --write-out %{http_code} --silent --output /dev/null "http://$secondarySite/online.html")
echo $secondaryResponse

if [ "$primaryResponse" = "200" ]
  echo "Primary Site is Up"
elif [ "$secondaryResponse" != "200" ]
  echo "Primary Site Down, Secondary Site is Down"

# ----------------------------------- Begin of else
ssh -T www-pub@"$secondarySite" <<EOM
cd /home/httpd/
mv offline.html online.html
php /home/path_to_email/sendNotification.php

  echo "Email already sent"


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s