Import from old repository commit 61ef2b42a9c4ba8e1600f15bb0236765edc2ad45.
[cascardo/ovs.git] / utilities / ovs-monitor
1 #!/bin/sh
2
3 # Copyright (C) 2008, 2009 Nicira Networks, Inc.
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
18 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
19
20 SECCHAN_PID=/var/run/secchan.pid
21 SECCHAN_SOCK=/var/run/secchan.mgmt
22 LOG_FILE=/var/log/openflow/monitor
23 INTERVAL=1
24 FAIL_THRESH=3
25
26 usage() {
27     echo usage: $0 options
28     echo
29     echo "OPTIONS:"
30     echo "  -h   Show this message"
31     echo "  -p   PID file for secchan (default: $SECCHAN_PID)"
32     echo "  -s   Unix socket for secchan (default: $SECCHAN_SOCK)"
33     echo "  -l   File to log messages (default: $LOG_FILE)"
34     echo "  -i   Interval to send probes in seconds (default: $INTERVAL)"
35     echo "  -c   Number of failed probes before reboot (default: $FAIL_THRESH)"
36 }
37
38 log() {
39     echo `date +"%b %d %X"`:$1 
40     echo `date +"%b %d %X"`:$1 >> $LOG_FILE
41 }
42
43
44 while getopts "hp:s:l:i:c:" OPTION; do
45     case $OPTION in
46         h)
47             usage
48             exit 1
49             ;;
50
51         p) 
52             SECCHAN_PID=$OPTARG
53             ;;
54
55         s) 
56             SECCHAN_SOCK=$OPTARG
57             ;;
58
59         l) 
60             LOG_FILE=$OPTARG
61             ;;
62
63         i) 
64             INTERVAL=$OPTARG
65             ;;
66
67         c) 
68             FAIL_THRESH=$OPTARG
69             ;;
70
71         *)
72             echo "Unknown option: ${OPTION}"
73     esac
74 done
75
76
77 if [ ! -f $SECCHAN_PID ]; then
78     log "No secchan pid file: ${SECCHAN_PID}" 
79     echo "No secchan pid file: ${SECCHAN_PID}" 
80 fi
81
82 if [ ! -S $SECCHAN_SOCK ]; then
83     log "No secchan sock file: ${SECCHAN_SOCK}" 
84     echo "No secchan sock file: ${SECCHAN_SOCK}" 
85 fi
86
87 if [ ! -d `dirname $LOG_FILE` ]; then
88     mkdir -p `dirname $LOG_FILE`
89 fi
90
91 let DP_DOWN=0
92 let SECCHAN_DOWN=0
93 log "===== Starting Monitor ===="
94 while `/bin/true`; do
95     # Only check for liveness if the secchan's PID file exists.  The PID
96     # file is removed when secchan is brought down gracefully.
97     if [ -f $SECCHAN_PID ]; then
98         pid=`cat $SECCHAN_PID`
99         if [ -d /proc/$pid ]; then
100             # Check if the secchan and datapath still can communicate
101             if [ -S $SECCHAN_SOCK ]; then
102                 ovs-ofctl probe -t 2 unix:$SECCHAN_SOCK 
103                 if [ $? -ne 0 ]; then
104                     log "datapath probe failed"
105                     let DP_DOWN++
106                 else 
107                     let DP_DOWN=0
108                 fi
109             fi
110             let SECCHAN_DOWN=0
111         else
112             log "secchan probe failed"
113             let SECCHAN_DOWN++
114         fi
115     fi
116
117     if [ $SECCHAN_DOWN -ge $FAIL_THRESH ]; then
118         log "Failed to probe secchan after ${SECCHAN_DOWN} tries...rebooting!"
119         reboot
120     fi
121
122     if [ $DP_DOWN -ge $FAIL_THRESH ]; then
123         log "Failed to probe datapath after ${DP_DOWN} tries...rebooting!"
124         reboot
125     fi
126
127     sleep $INTERVAL 
128 done