Created
September 20, 2014 22:37
-
-
Save adamcrosby/8b6e83a8aff51e638065 to your computer and use it in GitHub Desktop.
AWS VIP Monitor/takeover script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| # This script will monitor another HA node and take over a Virtual IP (VIP) | |
| # if communication with the other node fails | |
| # High Availability IP variables | |
| # Other node's IP to ping and VIP to swap if other node goes down | |
| HA_Node_IP= | |
| VIP= | |
| # Specify the EC2 region that this will be running in | |
| REGION= | |
| # Run aws-apitools-common.sh to set up default environment variables and to | |
| # leverage AWS security credentials provided by EC2 roles | |
| . /etc/profile.d/aws-apitools-common.sh | |
| # Determine the instance and ENI IDs so we can reassign the VIP to the | |
| # correct ENI. Requires EC2 describe-instances and assign-private-ip-address | |
| # permissions. The following example EC2 Roles policy will authorize these | |
| # commands: | |
| # { | |
| # "Statement": [ | |
| # { | |
| # "Action": [ | |
| # "ec2:AssignPrivateIpAddresses", | |
| # "ec2:DescribeInstances" | |
| # ], | |
| # "Effect": "Allow", | |
| # "Resource": "*" | |
| # } | |
| # ] | |
| # } | |
| Instance_ID=`/usr/bin/curl --silent http://169.254.169.254/latest/meta-data/instance-id` | |
| ENI_ID=`/opt/aws/bin/ec2-describe-instances $Instance_ID --region $REGION | grep eni -m 1 | awk '{print $2;}'` | |
| echo `date` "-- Starting HA monitor" | |
| while [ . ]; do | |
| pingresult=`ping -c 3 -W 1 $HA_Node_IP | grep time= | wc -l` | |
| if [ "$pingresult" == "0" ]; then | |
| echo `date` "-- HA heartbeat failed, taking over VIP" | |
| /opt/aws/bin/ec2-assign-private-ip-addresses -n $ENI_ID --secondary-private-ip-address $VIP --allow-reassignment --region $REGION | |
| pingresult=`ping -c 1 -W 1 $VIP | grep time= | wc -l` | |
| if [ "$pingresult" == "0" ]; then | |
| echo `date` "-- Restarting network" | |
| /sbin/service network restart > /dev/null 2>&1 | |
| fi | |
| sleep 60 | |
| fi | |
| sleep 2 | |
| done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment