srikanth | 116e6e8 | 2014-08-19 07:22:37 -0700 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | # |
| 3 | # Copyright (c) 2013 Big Switch Networks, Inc. |
| 4 | # |
| 5 | # Licensed under the Eclipse Public License, Version 1.0 (the |
| 6 | # "License"); you may not use this file except in compliance with the |
| 7 | # License. You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.eclipse.org/legal/epl-v10.html |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
| 14 | # implied. See the License for the specific language governing |
| 15 | # permissions and limitations under the License. |
| 16 | |
| 17 | # |
| 18 | # This script is invoked as part of VRRP track_script function |
| 19 | # The goal is to do a health check of sdnplatform through a rest API |
| 20 | # invocation, and if health is not good, then return with error code |
| 21 | # |
| 22 | if [ -e "/opt/sdnplatform/health-check-error-message" ] |
| 23 | then |
| 24 | rm -f /opt/sdnplatform/health-check-error-message |
| 25 | fi |
| 26 | |
| 27 | FAILOVER=$3 |
| 28 | |
| 29 | if [ -e "/opt/sdnplatform/force-one-time-health-check-failure" ] |
| 30 | then |
| 31 | logger "$0: force-one-time-health-check-failure file exists -- returning error exit code" |
| 32 | rm -f /opt/sdnplatform/force-one-time-health-check-failure |
| 33 | echo "User requested failover." > /opt/sdnplatform/health-check-error-message |
| 34 | exit 1 |
| 35 | fi |
| 36 | |
| 37 | if [ "$FAILOVER" ] ; then |
| 38 | exit 0 |
| 39 | fi |
| 40 | |
| 41 | if [ -e "/var/run/sdnplatform-healthcheck-disabled" ]; then |
| 42 | OLD=`cat /var/run/sdnplatform-healthcheck-disabled` |
| 43 | NEW=`date +%s` |
| 44 | if [ $NEW -gt $OLD ]; then |
| 45 | # if new timestamp is greater, remove the file and do health check |
| 46 | rm -f /var/run/sdnplatform-healthcheck-disabled |
| 47 | else |
| 48 | # skip the health check and exit |
| 49 | exit 0 |
| 50 | fi |
| 51 | fi |
| 52 | |
| 53 | COUNT=$1 |
| 54 | STEP=$2 |
| 55 | TIMER=0 |
| 56 | start="$(date +%s%N)" |
| 57 | while [ $TIMER -le $COUNT ] ; do |
| 58 | curl -f -s "http://localhost:8080/wm/core/health/json" |
| 59 | ERROR=$? |
| 60 | if [ $ERROR -eq 0 ]; then |
| 61 | break |
| 62 | fi |
| 63 | sleep $STEP |
| 64 | TIMER=$((STEP+TIMER)) |
| 65 | done |
| 66 | duration="$(($(date +%s%N)-start))" |
| 67 | duration="$((duration/1000000))" |
| 68 | logger "ha-health checker took $duration milliseconds" |
| 69 | if [ $ERROR -ne 0 ]; then |
| 70 | logger "$0: sdnplatform health check failed with error code $ERROR" |
| 71 | echo "Controller health check failed." > /opt/sdnplatform/health-check-error-message |
| 72 | exit 1 |
| 73 | fi |
| 74 | |
| 75 | exit 0 |