Matteo Scandolo | aca8665 | 2017-08-08 13:05:27 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
AyumuUeha | 76a01bc | 2017-05-18 13:34:13 +0900 | [diff] [blame] | 17 | #!/usr/bin/env bash |
| 18 | #************************************************************/ |
| 19 | #** File: vsg_vcpe_monitor.sh */ |
| 20 | #** Contents: Contains shell script to periodically */ |
| 21 | #** (every 30s) check VCPE status. If vcpe is */ |
| 22 | #** up , install packages and report the */ |
| 23 | #** status via NETCFG consolidator to ONOS. */ |
| 24 | #** Note: This scripts runs inside VSG */ |
| 25 | #************************************************************/ |
| 26 | |
| 27 | echo "vsg_vcpe_monitor.sh Script Execution: Begin" |
| 28 | |
| 29 | function is_vcpe_active() { |
| 30 | |
| 31 | echo "Checking vCPE status of docker instance : $1" |
| 32 | vcpe=0 |
| 33 | ping=0 |
| 34 | active=0 |
| 35 | if sudo docker ps -a|grep $1|grep Up >/dev/null ; then |
| 36 | active=1 |
| 37 | fi |
| 38 | if [[ $active -eq 0 ]]; then |
| 39 | return 0 |
| 40 | fi |
| 41 | # check if ping is ok |
| 42 | if sudo docker exec -t $1 ping -c 3 8.8.8.8 > /dev/null; then |
| 43 | return 1 |
| 44 | else |
| 45 | return 0 |
| 46 | fi |
| 47 | } |
| 48 | |
| 49 | # Read the temp_id.txt file and fill the array named "array" |
| 50 | getArray() { |
| 51 | array=() # Create array |
| 52 | while IFS= read -r line # Read a line |
| 53 | do |
| 54 | array+=("$line") # Append line to the array |
| 55 | done < "$1" |
| 56 | } |
| 57 | |
| 58 | function check_vcpe_status_and_setup_vcpeproxy() { |
| 59 | |
| 60 | echo "Checking for new vcpe..." |
| 61 | |
| 62 | temp_vcpe_file="${HOME_DIR}/temp_vcpe.txt" |
| 63 | active_vcpe_file="${HOME_DIR}/$file_vcpe_names" |
| 64 | |
| 65 | sudo docker ps -a |grep vcpe-|grep Up|awk '{print $NF}' > $temp_vcpe_file |
| 66 | |
| 67 | getArray $temp_vcpe_file |
| 68 | |
| 69 | for name in "${array[@]}" |
| 70 | do |
| 71 | # if vcpename does not exist, add it if vCPE is Up |
| 72 | if ! grep -q $name "$active_vcpe_file" > /dev/null; then |
| 73 | echo "Found new VCPE" |
| 74 | echo "VCPE NAME=$name" |
| 75 | |
| 76 | is_vcpe_active $name |
| 77 | is_active=$? |
| 78 | |
| 79 | if [[ $is_active -eq 1 ]]; then |
| 80 | echo "VCPE: $name is active" |
| 81 | # add vcpe name to the file |
| 82 | echo "$name" >> $active_vcpe_file |
| 83 | vcpe_id=`cat $active_vcpe_file|wc -l` |
| 84 | echo "Set up vcpe docker $name as VCPE APP proxy: [name=$name] [vsgId=$vsg_id] [vcpe_id=$vcpe_id]" |
| 85 | source $vsg_home_dir/$vsg_vcpe_proxy_setup_script $name $vsg_id $vcpe_id |
| 86 | else |
| 87 | echo "No new activei vcpe is found" |
| 88 | fi |
| 89 | fi |
| 90 | done |
| 91 | } |
| 92 | |
| 93 | if [ -z $VSG_ENV_FILE ]; then |
| 94 | echo "WARNING:******VSG_ENV_FILE is not set ..." |
| 95 | fi |
| 96 | if [ -z $HOME_DIR ]; then |
| 97 | echo "HOME_DIR is not set...." |
| 98 | HOME_DIR=`pwd`;export HOME_DIR |
| 99 | fi |
| 100 | |
| 101 | if [ -z $vsg_id ]; then |
| 102 | echo "WARNING:******* vsg_id is not set..." |
| 103 | vsg_id=1 |
| 104 | fi |
| 105 | VSG_ID=$vsg_id; export VSG_ID |
| 106 | while true |
| 107 | do |
| 108 | echo "Periodically checking for new VCPE Docker instance" |
| 109 | |
| 110 | check_vcpe_status_and_setup_vcpeproxy |
| 111 | date |
| 112 | printf "\n" |
| 113 | sleep 30 |
| 114 | done |
| 115 | echo "vsg_vpce_monitor.sh Execution : End" |