AyumuUeha | 76a01bc | 2017-05-18 13:34:13 +0900 | [diff] [blame] | 1 | #!/usr/bin/env bash |
| 2 | #************************************************************/ |
| 3 | #** File: nova_vsg_monitor.sh */ |
| 4 | #** Contents: Contains shell script to periodically */ |
| 5 | #** (every minute) check VSG status. If VSG is*/ |
| 6 | #** up calls, prepare vSG with the */ |
| 7 | #** necessary packages and start vCPE */ |
| 8 | #** program inside VSG. When vCPE comes up */ |
| 9 | #** execute vsg_vcep_setup.sh script */ |
| 10 | #************************************************************/ |
| 11 | function is_vsg_active() { |
| 12 | |
| 13 | echo "Checking vSG status..." |
| 14 | vcpe=0 |
| 15 | ping=0 |
| 16 | active=0 |
| 17 | |
| 18 | # check if vsg is active, if vsg is not active return |
| 19 | if [ 'nova list --all-tenants | grep $1 | grep ACTIVE' ]; then |
| 20 | active=1 |
| 21 | #echo "VSG is ACTIVE" |
| 22 | else |
| 23 | #echo "VSG is not active, exit" |
| 24 | return 0 |
| 25 | fi |
| 26 | |
| 27 | # check if ping is ok |
| 28 | if ssh ubuntu@$MGMTIP "ping -c 3 8.8.8.8" > /dev/null; then |
| 29 | ping=1 |
| 30 | #echo "PING OK" |
| 31 | fi |
| 32 | |
| 33 | # if all the above checks are ok then vsg is active |
| 34 | if [[ "$active" == 1 && "$ping" == 1 ]]; then |
| 35 | #echo "VSG is ACTIVE" |
| 36 | return 1 |
| 37 | else |
| 38 | return 0 |
| 39 | fi |
| 40 | |
| 41 | } |
| 42 | |
| 43 | # Read the temp_id.txt file and fill the array named "array" |
| 44 | getArray() { |
| 45 | array=() # Create array |
| 46 | while IFS= read -r line # Read a line |
| 47 | do |
| 48 | array+=("$line") # Append line to the array |
| 49 | done < "$1" |
| 50 | } |
| 51 | |
| 52 | function check_vsg_status() { |
| 53 | |
| 54 | source ${HOME_DIR}/admin-openrc.sh |
| 55 | |
| 56 | echo "Checking for new VSG..." |
| 57 | |
| 58 | file_temp="${HOME_DIR}/temp_id.txt" |
| 59 | file_id="${HOME_DIR}/$PPPOE_VSG_ID_FILE_NAME" |
| 60 | |
| 61 | nova list --all-tenants|grep mysite_vsg|awk '{print $2}' > $file_temp |
| 62 | |
| 63 | getArray $file_temp |
| 64 | |
| 65 | for id in "${array[@]}" |
| 66 | do |
| 67 | # if VSG Id does not exist, add it if VSG is active |
| 68 | if ! grep -q $id "$file_id" > /dev/null; then |
| 69 | echo "Found new VSG" |
| 70 | echo "VSG ID=$id" |
| 71 | |
| 72 | MGMTIP=$( nova interface-list $id|grep 172.27|awk '{print $8}' ) |
| 73 | |
| 74 | echo "MGMTIP: $MGMTIP" |
| 75 | if [ ! "$MGMTIP" ];then |
| 76 | echo "MGMTIP:$MGMTIP is null, continue" |
| 77 | continue |
| 78 | fi |
| 79 | |
| 80 | is_vsg_active $MGMTIP $id |
| 81 | is_active=$? |
| 82 | |
| 83 | if [[ $is_active -eq 1 ]]; then |
| 84 | echo "VSG is active" |
| 85 | # add vsg ID to the file |
| 86 | echo "$id" >> $file_id |
| 87 | |
| 88 | echo "Calling VSG Setup script" |
| 89 | vsg_id=`cat $file_id|wc -l` |
| 90 | echo "Setting up vsg_id:.....$vsg_id CP_PREFIX= $VCPEPROXY_CP_IP_PREFIX" |
| 91 | source ./nova_vsg_setup.sh $MGMTIP $vsg_id |
| 92 | echo "VSG Instance $vsg_id setup is complete" |
| 93 | else |
| 94 | echo "VSG is not active" |
| 95 | fi |
| 96 | fi |
| 97 | done |
| 98 | } |
| 99 | |
| 100 | echo "nova_vsg_monitor: Execution Begin" |
| 101 | |
| 102 | if [ -z $HOME_DIR ]; then |
| 103 | echo "HOME_DIR env variable is not...Using current directory as home" |
| 104 | HOME_DIR=`pwd` |
| 105 | fi |
| 106 | |
| 107 | while true |
| 108 | do |
| 109 | echo "Periodically checking for new VSG" |
| 110 | |
| 111 | MGMTIP="" |
| 112 | #echo "BEFORE: $MGMTIP" |
| 113 | check_vsg_status |
| 114 | |
| 115 | date |
| 116 | printf "\n" |
| 117 | sleep 60 |
| 118 | |
| 119 | done |
| 120 | echo "nova_vsg_monitor.sh: Execution End" |