215032ae4a7474fc9273bbc1e07510f230b90a9c
[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 SECCHAN_PID=/var/run/secchan.pid
20 SECCHAN_SOCK=/var/run/secchan.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 secchan (default: $SECCHAN_PID)"
31     echo "  -s   Unix socket for secchan (default: $SECCHAN_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             SECCHAN_PID=$OPTARG
52             ;;
53
54         s) 
55             SECCHAN_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 $SECCHAN_PID ]; then
77     log "No secchan pid file: ${SECCHAN_PID}" 
78     echo "No secchan pid file: ${SECCHAN_PID}" 
79 fi
80
81 if [ ! -S $SECCHAN_SOCK ]; then
82     log "No secchan sock file: ${SECCHAN_SOCK}" 
83     echo "No secchan sock file: ${SECCHAN_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 SECCHAN_DOWN=0
92 log "===== Starting Monitor ===="
93 while `/bin/true`; do
94     # Only check for liveness if the secchan's PID file exists.  The PID
95     # file is removed when secchan is brought down gracefully.
96     if [ -f $SECCHAN_PID ]; then
97         pid=`cat $SECCHAN_PID`
98         if [ -d /proc/$pid ]; then
99             # Check if the secchan and datapath still can communicate
100             if [ -S $SECCHAN_SOCK ]; then
101                 ovs-ofctl probe -t 2 unix:$SECCHAN_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 SECCHAN_DOWN=0
110         else
111             log "secchan probe failed"
112             let SECCHAN_DOWN++
113         fi
114     fi
115
116     if [ $SECCHAN_DOWN -ge $FAIL_THRESH ]; then
117         log "Failed to probe secchan after ${SECCHAN_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