AyumuUeha | 76a01bc | 2017-05-18 13:34:13 +0900 | [diff] [blame^] | 1 | #!/usr/bin/env bash |
| 2 | #************************************************************/ |
| 3 | #** File: vsg_vcpe_monitor.sh */ |
| 4 | #** Contents: Contains shell script to periodically */ |
| 5 | #** (every 30s) check VCPE status. If vcpe is */ |
| 6 | #** up , install packages and report the */ |
| 7 | #** status via NETCFG consolidator to ONOS. */ |
| 8 | #** Note: This scripts runs inside VSG */ |
| 9 | #************************************************************/ |
| 10 | |
| 11 | echo "vsg_vcpe_monitor.sh Script Execution: Begin" |
| 12 | |
| 13 | function is_vcpe_active() { |
| 14 | |
| 15 | echo "Checking vCPE status of docker instance : $1" |
| 16 | vcpe=0 |
| 17 | ping=0 |
| 18 | active=0 |
| 19 | if sudo docker ps -a|grep $1|grep Up >/dev/null ; then |
| 20 | active=1 |
| 21 | fi |
| 22 | if [[ $active -eq 0 ]]; then |
| 23 | return 0 |
| 24 | fi |
| 25 | # check if ping is ok |
| 26 | if sudo docker exec -t $1 ping -c 3 8.8.8.8 > /dev/null; then |
| 27 | return 1 |
| 28 | else |
| 29 | return 0 |
| 30 | fi |
| 31 | } |
| 32 | |
| 33 | # Read the temp_id.txt file and fill the array named "array" |
| 34 | getArray() { |
| 35 | array=() # Create array |
| 36 | while IFS= read -r line # Read a line |
| 37 | do |
| 38 | array+=("$line") # Append line to the array |
| 39 | done < "$1" |
| 40 | } |
| 41 | |
| 42 | function check_vcpe_status_and_setup_vcpeproxy() { |
| 43 | |
| 44 | echo "Checking for new vcpe..." |
| 45 | |
| 46 | temp_vcpe_file="${HOME_DIR}/temp_vcpe.txt" |
| 47 | active_vcpe_file="${HOME_DIR}/$file_vcpe_names" |
| 48 | |
| 49 | sudo docker ps -a |grep vcpe-|grep Up|awk '{print $NF}' > $temp_vcpe_file |
| 50 | |
| 51 | getArray $temp_vcpe_file |
| 52 | |
| 53 | for name in "${array[@]}" |
| 54 | do |
| 55 | # if vcpename does not exist, add it if vCPE is Up |
| 56 | if ! grep -q $name "$active_vcpe_file" > /dev/null; then |
| 57 | echo "Found new VCPE" |
| 58 | echo "VCPE NAME=$name" |
| 59 | |
| 60 | is_vcpe_active $name |
| 61 | is_active=$? |
| 62 | |
| 63 | if [[ $is_active -eq 1 ]]; then |
| 64 | echo "VCPE: $name is active" |
| 65 | # add vcpe name to the file |
| 66 | echo "$name" >> $active_vcpe_file |
| 67 | vcpe_id=`cat $active_vcpe_file|wc -l` |
| 68 | echo "Set up vcpe docker $name as VCPE APP proxy: [name=$name] [vsgId=$vsg_id] [vcpe_id=$vcpe_id]" |
| 69 | source $vsg_home_dir/$vsg_vcpe_proxy_setup_script $name $vsg_id $vcpe_id |
| 70 | else |
| 71 | echo "No new activei vcpe is found" |
| 72 | fi |
| 73 | fi |
| 74 | done |
| 75 | } |
| 76 | |
| 77 | if [ -z $VSG_ENV_FILE ]; then |
| 78 | echo "WARNING:******VSG_ENV_FILE is not set ..." |
| 79 | fi |
| 80 | if [ -z $HOME_DIR ]; then |
| 81 | echo "HOME_DIR is not set...." |
| 82 | HOME_DIR=`pwd`;export HOME_DIR |
| 83 | fi |
| 84 | |
| 85 | if [ -z $vsg_id ]; then |
| 86 | echo "WARNING:******* vsg_id is not set..." |
| 87 | vsg_id=1 |
| 88 | fi |
| 89 | VSG_ID=$vsg_id; export VSG_ID |
| 90 | while true |
| 91 | do |
| 92 | echo "Periodically checking for new VCPE Docker instance" |
| 93 | |
| 94 | check_vcpe_status_and_setup_vcpeproxy |
| 95 | date |
| 96 | printf "\n" |
| 97 | sleep 30 |
| 98 | done |
| 99 | echo "vsg_vpce_monitor.sh Execution : End" |