debian, rhel, xenserver: Ability to collect ovs-ctl logs.
[sliver-openvswitch.git] / utilities / ovs-lib.in
1 # This is a shell function library sourced by some Open vSwitch scripts.
2 # It is not intended to be invoked on its own.
3
4 # Copyright (C) 2009, 2010, 2011, 2012 Nicira, Inc.
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at:
9 #
10 #     http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17
18 ## ----------------- ##
19 ## configure options ##
20 ## ----------------- ##
21
22 # All of these should be substituted by the Makefile at build time.
23 logdir=${OVS_LOGDIR-'@LOGDIR@'}                 # /var/log/openvswitch
24 rundir=${OVS_RUNDIR-'@RUNDIR@'}                 # /var/run/openvswitch
25 sysconfdir=${OVS_SYSCONFDIR-'@sysconfdir@'}     # /etc
26 etcdir=$sysconfdir/openvswitch                  # /etc/openvswitch
27 datadir=${OVS_PKGDATADIR-'@pkgdatadir@'}        # /usr/share/openvswitch
28 bindir=${OVS_BINDIR-'@bindir@'}                 # /usr/bin
29 sbindir=${OVS_SBINDIR-'@sbindir@'}              # /usr/sbin
30
31 # /etc/openvswitch or /var/lib/openvswitch
32 if test X"$OVS_DBDIR" != X; then
33     dbdir=$OVS_DBDIR
34 elif test X"$OVS_SYSCONFDIR" != X; then
35     dbdir=$OVS_SYSCONFDIR/openvswitch
36 else
37     dbdir='@DBDIR@'
38 fi
39
40 ovs_ctl () {
41     echo "`date -u`:$@" >> "${logdir}/ovs-ctl.log"
42     "${datadir}/scripts/ovs-ctl" "$@" 2>&1 | tee -a "${logdir}/ovs-ctl.log"
43 }
44
45 VERSION='@VERSION@'
46
47 DAEMON_CWD=/
48
49 LC_ALL=C; export LC_ALL
50
51 ## ------------- ##
52 ## LSB functions ##
53 ## ------------- ##
54
55 # Use the system's own implementations if it has any.
56 if test -e /etc/init.d/functions; then
57     . /etc/init.d/functions
58 elif test -e /etc/rc.d/init.d/functions; then
59     . /etc/rc.d/init.d/functions
60 elif test -e /lib/lsb/init-functions; then
61     . /lib/lsb/init-functions
62 fi
63
64 # Implement missing functions (e.g. OpenSUSE lacks 'action').
65 if type log_success_msg >/dev/null 2>&1; then :; else
66     log_success_msg () {
67         printf '%s.\n' "$*"
68     }
69 fi
70 if type log_failure_msg >/dev/null 2>&1; then :; else
71     log_failure_msg () {
72         printf '%s ... failed!\n' "$*"
73     }
74 fi
75 if type log_warning_msg >/dev/null 2>&1; then :; else
76     log_warning_msg () {
77         printf '%s ... (warning).\n' "$*"
78     }
79 fi
80 if type action >/dev/null 2>&1; then :; else
81     action () {
82        STRING=$1
83        shift
84        "$@"
85        rc=$?
86        if test $rc = 0; then
87             log_success_msg "$STRING"
88        else
89             log_failure_msg "$STRING"
90        fi
91        return $rc
92     }
93 fi
94
95 ## ------- ##
96 ## Daemons ##
97 ## ------- ##
98
99 pid_exists () {
100     # This is better than "kill -0" because it doesn't require permission to
101     # send a signal (so daemon_status in particular works as non-root).
102     test -d /proc/"$1"
103 }
104
105 start_daemon () {
106     priority=$1
107     wrapper=$2
108     shift; shift
109     daemon=$1
110     strace=""
111
112     # drop core files in a sensible place
113     test -d "$DAEMON_CWD" || install -d -m 755 -o root -g root "$DAEMON_CWD"
114     set "$@" --no-chdir
115     cd "$DAEMON_CWD"
116
117     # log file
118     test -d "$logdir" || install -d -m 755 -o root -g root "$logdir"
119     set "$@" --log-file="$logdir/$daemon.log"
120
121     # pidfile and monitoring
122     test -d "$rundir" || install -d -m 755 -o root -g root "$rundir"
123     set "$@" --pidfile="$rundir/$daemon.pid"
124     set "$@" --detach --monitor
125
126     # wrapper
127     case $wrapper in
128         valgrind)
129             if (valgrind --version) > /dev/null 2>&1; then
130                 set valgrind -q --leak-check=full --time-stamp=yes \
131                     --log-file="$logdir/$daemon.valgrind.log.%p" "$@"
132             else
133                 log_failure_msg "valgrind not installed, running $daemon without it"
134             fi
135             ;;
136         strace)
137             if (strace -V) > /dev/null 2>&1; then
138                 strace="strace -tt -T -s 256 -ff"
139                 if (strace -DV) > /dev/null 2>&1; then
140                     # Has the -D option.
141                     set $strace -D -o "$logdir/$daemon.strace.log" "$@"
142                     strace=""
143                 fi
144             else
145                 log_failure_msg "strace not installed, running $daemon without it"
146             fi
147             ;;
148         glibc)
149             set env MALLOC_CHECK_=2 MALLOC_PERTURB_=165 "$@"
150             ;;
151         '')
152             ;;
153         *)
154             log_failure_msg "unknown wrapper $wrapper, running $daemon without it"
155             ;;
156     esac
157
158     # priority
159     if test X"$priority" != X; then
160         set nice -n "$priority" "$@"
161     fi
162
163     action "Starting $daemon" "$@"
164
165     if test X"$strace" != X; then
166         # Strace doesn't have the -D option so we attach after the fact.
167         setsid $strace -o "$logdir/$daemon.strace.log" \
168             -p `cat $rundir/$daemon.pid` > /dev/null 2>&1 &
169     fi
170 }
171
172 stop_daemon () {
173     if test -e "$rundir/$1.pid"; then
174         if pid=`cat "$rundir/$1.pid"`; then
175             for action in TERM .1 .25 .65 1 1 1 1 KILL 1 1 1 1 FAIL; do
176                 case $action in
177                     TERM)
178                         action "Killing $1 ($pid)" kill $pid
179                         ;;
180                     KILL)
181                         action "Killing $1 ($pid) with SIGKILL" kill -9 $pid
182                         ;;
183                     FAIL)
184                         log_failure_msg "Killing $1 ($pid) failed"
185                         return 1
186                         ;;
187                     *)
188                         if pid_exists $pid >/dev/null 2>&1; then
189                             sleep $action
190                         else
191                             return 0
192                         fi
193                         ;;
194                 esac
195             done
196         fi
197     fi
198     log_success_msg "$1 is not running"
199 }
200
201 daemon_status () {
202     pidfile=$rundir/$1.pid
203     if test -e "$pidfile"; then
204         if pid=`cat "$pidfile"`; then
205             if pid_exists "$pid"; then
206                 echo "$1 is running with pid $pid"
207                 return 0
208             else
209                 echo "Pidfile for $1 ($pidfile) is stale"
210             fi
211         else
212             echo "Pidfile for $1 ($pidfile) exists but cannot be read"
213         fi
214     else
215         echo "$1 is not running"
216     fi
217     return 1
218 }
219
220 daemon_is_running () {
221     pidfile=$rundir/$1.pid
222     test -e "$pidfile" && pid=`cat "$pidfile"` && pid_exists "$pid"
223 } >/dev/null 2>&1