aboutsummaryrefslogtreecommitdiff
path: root/utilities/ovs-monitor
blob: 215032ae4a7474fc9273bbc1e07510f230b90a9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/bin/sh

# Copyright (C) 2008, 2009 Nicira Networks, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

SECCHAN_PID=/var/run/secchan.pid
SECCHAN_SOCK=/var/run/secchan.mgmt
LOG_FILE=/var/log/openflow/monitor
INTERVAL=1
FAIL_THRESH=3

usage() {
    echo usage: $0 options
    echo
    echo "OPTIONS:"
    echo "  -h   Show this message"
    echo "  -p   PID file for secchan (default: $SECCHAN_PID)"
    echo "  -s   Unix socket for secchan (default: $SECCHAN_SOCK)"
    echo "  -l   File to log messages (default: $LOG_FILE)"
    echo "  -i   Interval to send probes in seconds (default: $INTERVAL)"
    echo "  -c   Number of failed probes before reboot (default: $FAIL_THRESH)"
}

log() {
    echo `date +"%b %d %X"`:$1 
    echo `date +"%b %d %X"`:$1 >> $LOG_FILE
}


while getopts "hp:s:l:i:c:" OPTION; do
    case $OPTION in
        h)
            usage
            exit 1
            ;;

        p) 
            SECCHAN_PID=$OPTARG
            ;;

        s) 
            SECCHAN_SOCK=$OPTARG
            ;;

        l) 
            LOG_FILE=$OPTARG
            ;;

        i) 
            INTERVAL=$OPTARG
            ;;

        c) 
            FAIL_THRESH=$OPTARG
            ;;

        *)
            echo "Unknown option: ${OPTION}"
    esac
done


if [ ! -f $SECCHAN_PID ]; then
    log "No secchan pid file: ${SECCHAN_PID}" 
    echo "No secchan pid file: ${SECCHAN_PID}" 
fi

if [ ! -S $SECCHAN_SOCK ]; then
    log "No secchan sock file: ${SECCHAN_SOCK}" 
    echo "No secchan sock file: ${SECCHAN_SOCK}" 
fi

if [ ! -d `dirname $LOG_FILE` ]; then
    mkdir -p `dirname $LOG_FILE`
fi

let DP_DOWN=0
let SECCHAN_DOWN=0
log "===== Starting Monitor ===="
while `/bin/true`; do
    # Only check for liveness if the secchan's PID file exists.  The PID
    # file is removed when secchan is brought down gracefully.
    if [ -f $SECCHAN_PID ]; then
        pid=`cat $SECCHAN_PID`
        if [ -d /proc/$pid ]; then
            # Check if the secchan and datapath still can communicate
            if [ -S $SECCHAN_SOCK ]; then
                ovs-ofctl probe -t 2 unix:$SECCHAN_SOCK 
                if [ $? -ne 0 ]; then
                    log "datapath probe failed"
                    let DP_DOWN++
                else 
                    let DP_DOWN=0
                fi
            fi
            let SECCHAN_DOWN=0
        else
            log "secchan probe failed"
            let SECCHAN_DOWN++
        fi
    fi

    if [ $SECCHAN_DOWN -ge $FAIL_THRESH ]; then
        log "Failed to probe secchan after ${SECCHAN_DOWN} tries...rebooting!"
        reboot
    fi

    if [ $DP_DOWN -ge $FAIL_THRESH ]; then
        log "Failed to probe datapath after ${DP_DOWN} tries...rebooting!"
        reboot
    fi

    sleep $INTERVAL 
done