Merge citrix branch into master.
[sliver-openvswitch.git] / utilities / ovs-monitor
1 #!/bin/sh
2
3 # Copyright (C) 2008, 2009 Nicira Networks, Inc.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at:
8 #
9 #     http://www.apache.org/licenses/LICENSE-2.0
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 implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
18
19 OPENFLOWD_PID=/var/run/ovs-openflowd.pid
20 OPENFLOWD_SOCK=/var/run/ovs-openflowd.mgmt
21 LOG_FILE=/var/log/openflow/monitor
22 INTERVAL=1
23 FAIL_THRESH=3
24
25 usage() {
26     echo usage: $0 options
27     echo
28     echo "OPTIONS:"
29     echo "  -h   Show this message"
30     echo "  -p   PID file for ovs-openflowd (default: $OPENFLOWD_PID)"
31     echo "  -s   Unix socket for ovs-openflowd (default: $OPENFLOWD_SOCK)"
32     echo "  -l   File to log messages (default: $LOG_FILE)"
33     echo "  -i   Interval to send probes in seconds (default: $INTERVAL)"
34     echo "  -c   Number of failed probes before reboot (default: $FAIL_THRESH)"
35 }
36
37 log() {
38     echo `date +"%b %d %X"`:$1 
39     echo `date +"%b %d %X"`:$1 >> $LOG_FILE
40 }
41
42
43 while getopts "hp:s:l:i:c:" OPTION; do
44     case $OPTION in
45         h)
46             usage
47             exit 1
48             ;;
49
50         p) 
51             OPENFLOWD_PID=$OPTARG
52             ;;
53
54         s) 
55             OPENFLOWD_SOCK=$OPTARG
56             ;;
57
58         l) 
59             LOG_FILE=$OPTARG
60             ;;
61
62         i) 
63             INTERVAL=$OPTARG
64             ;;
65
66         c) 
67             FAIL_THRESH=$OPTARG
68             ;;
69
70         *)
71             echo "Unknown option: ${OPTION}"
72     esac
73 done
74
75
76 if [ ! -f $OPENFLOWD_PID ]; then
77     log "No ovs-openflowd pid file: ${OPENFLOWD_PID}"
78     echo "No ovs-openflowd pid file: ${OPENFLOWD_PID}"
79 fi
80
81 if [ ! -S $OPENFLOWD_SOCK ]; then
82     log "No ovs-openflowd sock file: ${OPENFLOWD_SOCK}"
83     echo "No ovs-openflowd sock file: ${OPENFLOWD_SOCK}"
84 fi
85
86 if [ ! -d `dirname $LOG_FILE` ]; then
87     mkdir -p `dirname $LOG_FILE`
88 fi
89
90 let DP_DOWN=0
91 let OPENFLOWD_DOWN=0
92 log "===== Starting Monitor ===="
93 while `/bin/true`; do
94     # Only check for liveness if ovs-openflowd's PID file exists.  The PID
95     # file is removed when ovs-openflowd is brought down gracefully.
96     if [ -f $OPENFLOWD_PID ]; then
97         pid=`cat $OPENFLOWD_PID`
98         if [ -d /proc/$pid ]; then
99             # Check if the ovs-openflowd and datapath still can communicate
100             if [ -S $OPENFLOWD_SOCK ]; then
101                 ovs-ofctl probe -t 2 unix:$OPENFLOWD_SOCK
102                 if [ $? -ne 0 ]; then
103                     log "datapath probe failed"
104                     let DP_DOWN++
105                 else 
106                     let DP_DOWN=0
107                 fi
108             fi
109             let OPENFLOWD_DOWN=0
110         else
111             log "ovs-openflowd probe failed"
112             let OPENFLOWD_DOWN++
113         fi
114     fi
115
116     if [ $OPENFLOWD_DOWN -ge $FAIL_THRESH ]; then
117         log "Failed to probe ovs-openflowd after ${OPENFLOWD_DOWN} tries...rebooting!"
118         reboot
119     fi
120
121     if [ $DP_DOWN -ge $FAIL_THRESH ]; then
122         log "Failed to probe datapath after ${DP_DOWN} tries...rebooting!"
123         reboot
124     fi
125
126     sleep $INTERVAL 
127 done