2007-06-01 14:54:26 +04:00
# utility functions for ctdb event scripts
2007-06-03 16:07:07 +04:00
#######################################
# pull in a system config file, if any
loadconfig() {
name="$1"
if [ -f /etc/sysconfig/$name ]; then
. /etc/sysconfig/$name
elif [ -f /etc/default/$name ]; then
. /etc/default/$name
2007-09-14 08:14:03 +04:00
elif [ -f $CTDB_BASE/sysconfig/$name ]; then
. $CTDB_BASE/sysconfig/$name
2007-06-03 16:07:07 +04:00
fi
}
2009-01-16 15:26:57 +03:00
##############################################################
# determine on what type of system (init style) we are running
detect_init_style() {
# only do detection if not already set:
test "x$CTDB_INIT_STYLE" != "x" && return
if [ -x /sbin/startproc ]; then
CTDB_INIT_STYLE="suse"
elif [ -x /sbin/start-stop-daemon ]; then
2009-09-15 13:33:35 +04:00
CTDB_INIT_STYLE="debian"
2009-01-16 15:26:57 +03:00
else
CTDB_INIT_STYLE="redhat"
fi
}
2007-06-01 14:54:26 +04:00
2007-06-02 12:51:05 +04:00
######################################################
# simulate /sbin/service on platforms that don't have it
service() {
service_name="$1"
op="$2"
2009-01-16 15:31:02 +03:00
# do nothing, when no service was specified
test "x$service_name" = "x" && return
2007-06-02 12:51:05 +04:00
if [ -x /sbin/service ]; then
/sbin/service "$service_name" "$op"
elif [ -x /etc/init.d/$service_name ]; then
/etc/init.d/$service_name "$op"
elif [ -x /etc/rc.d/init.d/$service_name ]; then
2007-07-15 02:54:48 +04:00
/etc/rc.d/init.d/$service_name "$op"
2007-06-02 12:51:05 +04:00
fi
}
2008-02-13 00:20:20 +03:00
######################################################
# simulate /sbin/service (niced) on platforms that don't have it
nice_service() {
service_name="$1"
op="$2"
2009-01-16 15:31:02 +03:00
# do nothing, when no service was specified
test "x$service_name" = "x" && return
2008-02-13 00:20:20 +03:00
if [ -x /sbin/service ]; then
nice /sbin/service "$service_name" "$op"
elif [ -x /etc/init.d/$service_name ]; then
nice /etc/init.d/$service_name "$op"
elif [ -x /etc/rc.d/init.d/$service_name ]; then
nice /etc/rc.d/init.d/$service_name "$op"
fi
}
2007-06-17 05:57:42 +04:00
######################################################
# wait for a command to return a zero exit status
# usage: ctdb_wait_command SERVICE_NAME <command>
######################################################
ctdb_wait_command() {
service_name="$1"
wait_cmd="$2"
[ -z "$wait_cmd" ] && return;
all_ok=0
2008-01-16 14:06:44 +03:00
echo "Waiting for service $service_name to start"
2007-06-17 05:57:42 +04:00
while [ $all_ok -eq 0 ]; do
$wait_cmd > /dev/null 2>&1 && all_ok=1
ctdb status > /dev/null 2>&1 || {
echo "ctdb daemon has died. Exiting wait for $service_name"
exit 1
}
2007-07-13 02:47:02 +04:00
[ $all_ok -eq 1 ] || sleep 1
2007-06-17 05:57:42 +04:00
done
2008-01-16 14:06:44 +03:00
echo "Local service $service_name is up"
2007-06-17 05:57:42 +04:00
}
2007-06-01 14:54:26 +04:00
######################################################
# wait for a set of tcp ports
2007-06-06 06:08:42 +04:00
# usage: ctdb_wait_tcp_ports SERVICE_NAME <ports...>
2007-06-01 14:54:26 +04:00
######################################################
ctdb_wait_tcp_ports() {
service_name="$1"
shift
wait_ports="$*"
[ -z "$wait_ports" ] && return;
all_ok=0
2008-01-16 14:06:44 +03:00
echo "Waiting for tcp service $service_name to start"
2007-06-01 14:54:26 +04:00
while [ $all_ok -eq 0 ]; do
all_ok=1
for p in $wait_ports; do
2007-06-01 17:25:33 +04:00
if [ -x /usr/bin/netcat ]; then
2007-09-13 04:39:05 +04:00
/usr/bin/netcat -z 127.0.0.1 $p > /dev/null || all_ok=0
2007-06-01 17:25:33 +04:00
elif [ -x /usr/bin/nc ]; then
2007-09-13 04:39:05 +04:00
/usr/bin/nc -z 127.0.0.1 $p > /dev/null || all_ok=0
2007-07-15 03:26:54 +04:00
elif [ -x /usr/bin/netstat ]; then
2008-07-09 04:03:21 +04:00
(netstat -a -n | egrep "0.0.0.0:$p[[:space:]]*LISTEN" > /dev/null) || all_ok=0
2008-02-07 07:41:48 +03:00
elif [ -x /bin/netstat ]; then
2008-07-09 04:03:21 +04:00
(netstat -a -n | egrep "0.0.0.0:$p[[:space:]]*LISTEN" > /dev/null) || all_ok=0
2007-06-01 17:25:33 +04:00
else
2008-01-16 14:06:44 +03:00
echo "No tool to check tcp ports availabe. can not check in ctdb_wait_tcp_ports"
2007-06-01 17:25:33 +04:00
return
fi
2007-06-01 14:54:26 +04:00
done
[ $all_ok -eq 1 ] || sleep 1
2007-06-17 05:57:42 +04:00
ctdb status > /dev/null 2>&1 || {
2007-06-01 14:54:26 +04:00
echo "ctdb daemon has died. Exiting tcp wait $service_name"
exit 1
}
done
2008-01-16 14:06:44 +03:00
echo "Local tcp services for $service_name are up"
2007-06-01 14:54:26 +04:00
}
2007-06-06 06:08:42 +04:00
2007-06-01 14:54:26 +04:00
######################################################
# wait for a set of directories
2007-06-06 06:08:42 +04:00
# usage: ctdb_wait_directories SERVICE_NAME <directories...>
2007-06-01 14:54:26 +04:00
######################################################
ctdb_wait_directories() {
service_name="$1"
shift
wait_dirs="$*"
[ -z "$wait_dirs" ] && return;
all_ok=0
2008-01-16 14:06:44 +03:00
echo "Waiting for local directories for $service_name"
2007-06-01 14:54:26 +04:00
while [ $all_ok -eq 0 ]; do
all_ok=1
for d in $wait_dirs; do
[ -d $d ] || all_ok=0
done
[ $all_ok -eq 1 ] || sleep 1
2007-06-17 05:57:42 +04:00
ctdb status > /dev/null 2>&1 || {
2007-06-01 14:54:26 +04:00
echo "ctdb daemon has died. Exiting directory wait for $service_name"
exit 1
}
done
2008-01-16 14:06:44 +03:00
echo "Local directories for $service_name are available"
2007-06-01 14:54:26 +04:00
}
2007-06-06 06:08:42 +04:00
######################################################
# check that a rpc server is registered with portmap
# and responding to requests
# usage: ctdb_check_rpc SERVICE_NAME PROGNUM VERSION
######################################################
ctdb_check_rpc() {
service_name="$1"
prognum="$2"
version="$3"
rpcinfo -u localhost $prognum $version > /dev/null || {
2008-01-16 14:06:44 +03:00
echo "ERROR: $service_name not responding to rpc requests"
2007-06-06 06:08:42 +04:00
exit 1
}
}
######################################################
# check a set of directories is available
2009-10-12 09:32:49 +04:00
# return 1 on a missing directory
2008-07-23 09:35:46 +04:00
# usage: ctdb_check_directories_probe SERVICE_NAME <directories...>
2007-06-06 06:08:42 +04:00
######################################################
2008-07-23 09:35:46 +04:00
ctdb_check_directories_probe() {
2007-06-06 06:08:42 +04:00
service_name="$1"
shift
2009-10-12 09:32:49 +04:00
for d ; do
case "$d" in
*%*)
continue
;;
*)
[ -d "$d" ] || return 1
esac
2007-06-06 06:08:42 +04:00
done
2008-07-23 09:35:46 +04:00
return 0
}
######################################################
# check a set of directories is available
# usage: ctdb_check_directories SERVICE_NAME <directories...>
######################################################
ctdb_check_directories() {
2009-10-12 09:32:49 +04:00
# Note: ctdb_check_directories_probe sets both $service_name and $d.
ctdb_check_directories_probe "$@" || {
2008-07-23 09:35:46 +04:00
echo "ERROR: $service_name directory $d not available"
exit 1
}
2007-06-06 06:08:42 +04:00
}
######################################################
# check a set of tcp ports
# usage: ctdb_check_tcp_ports SERVICE_NAME <ports...>
######################################################
ctdb_check_tcp_ports() {
service_name="$1"
shift
wait_ports="$*"
[ -z "$wait_ports" ] && return;
2009-01-30 18:10:05 +03:00
# check availability of netcat or netstat first
NETCAT=""
NETSTAT=""
2009-02-05 06:44:46 +03:00
if [ -x /usr/bin/netstat ]; then
NETSTAT=/usr/bin/netstat
elif [ -x /bin/netstat ]; then
NETSTAT=/bin/netstat
elif [ -x /usr/bin/netcat ]; then
2009-01-30 18:10:05 +03:00
NETCAT=/usr/bin/netcat
elif [ -x /bin/netcat ]; then
NETCAT=/bin/netcat
elif [ -x /usr/bin/nc ]; then
NETCAT=/usr/bin/nc
elif [ -x /bin/nc ]; then
NETCAT=/bin/nc
fi
2007-06-06 06:08:42 +04:00
for p in $wait_ports; do
all_ok=1
2009-01-30 18:10:05 +03:00
if [ "x${NETCAT}" != "x" ]; then
${NETCAT} -z 127.0.0.1 $p > /dev/null || all_ok=0
elif [ "x${NETSTAT}" != "x" ]; then
2009-01-30 20:14:41 +03:00
if ! ${NETSTAT} -a -n | egrep "0.0.0.0:$p .*LISTEN" > /dev/null ; then
if ! ${NETSTAT} -a -n | egrep ":::$p .*LISTEN" > /dev/null ; then
all_ok=0
fi
fi
2009-01-30 18:41:37 +03:00
else
echo "ERROR: neither netcat (or nc) nor netstat found!"
echo "ERROR: can't monitor ${service_name} tcp port ${p}"
all_ok=0
2007-06-06 06:08:42 +04:00
fi
2009-01-30 18:10:05 +03:00
2007-06-06 06:08:42 +04:00
[ $all_ok -eq 1 ] || {
2008-01-16 14:06:44 +03:00
echo "ERROR: $service_name tcp port $p is not responding"
2007-06-06 06:08:42 +04:00
exit 1
}
done
}
2007-06-17 06:05:29 +04:00
2009-05-19 02:47:19 +04:00
######################################################
# check a unix socket
# usage: ctdb_check_unix_socket SERVICE_NAME <socket_path>
######################################################
ctdb_check_unix_socket() {
service_name="$1"
socket_path="$2"
[ -z "$socket_path" ] && return;
# check availability of netstat first
NETSTAT=""
if [ -x $(type -p netstat) ]; then
NETSTAT=$(type -p netstat)
elif [ -x /usr/bin/netstat ]; then
NETSTAT=/usr/bin/netstat
elif [ -x /bin/netstat ]; then
NETSTAT=/bin/netstat
fi
all_ok=1
if [ "x$NETSTAT" != "x" ]; then
if $NETSTAT -l -a -n | grep -qE "^unix.*LISTEN.*${socket_path}$"; then
all_ok=1
else
all_ok=0
fi
else
[ -S ${socket_path} ] && all_ok=1 || all_ok=0
fi
[ $all_ok -eq 1 ] || {
echo "ERROR: $service_name socket $socket_path not found"
exit 1
}
}
2007-06-17 06:05:29 +04:00
######################################################
# check a command returns zero status
# usage: ctdb_check_command SERVICE_NAME <command>
######################################################
ctdb_check_command() {
service_name="$1"
wait_cmd="$2"
[ -z "$wait_cmd" ] && return;
$wait_cmd > /dev/null 2>&1 || {
2008-01-16 14:06:44 +03:00
echo "ERROR: $service_name - $wait_cmd returned error"
2007-06-17 06:05:29 +04:00
exit 1
}
}
2007-10-11 01:27:38 +04:00
################################################
# kill off any TCP connections with the given IP
################################################
kill_tcp_connections() {
_IP="$1"
_failed=0
_killcount=0
connfile="$CTDB_BASE/state/connections.$_IP"
2008-07-09 04:03:21 +04:00
netstat -tn |egrep "^tcp.*[[:space:]]+$_IP:.*ESTABLISHED" | awk '{print $4" "$5}' > $connfile
2008-08-20 06:50:50 +04:00
netstat -tn |egrep "^tcp.*[[:space:]]+::ffff:$_IP:.*ESTABLISHED" | awk '{print $4" "$5}' >> $connfile
2008-08-20 03:47:00 +04:00
2007-10-11 01:27:38 +04:00
while read dest src; do
2008-08-20 03:47:00 +04:00
srcip=`echo $src | sed -e "s/:[^:]*$//"`
srcport=`echo $src | sed -e "s/^.*://"`
destip=`echo $dest | sed -e "s/:[^:]*$//"`
destport=`echo $dest | sed -e "s/^.*://"`
2008-01-16 14:06:44 +03:00
echo "Killing TCP connection $srcip:$srcport $destip:$destport"
2008-08-01 08:17:50 +04:00
ctdb killtcp $srcip:$srcport $destip:$destport >/dev/null 2>&1 || _failed=1
case $destport in
# we only do one-way killtcp for NFS and CIFS
139|445|2049) : ;;
# for all others we do 2-way
*)
ctdb killtcp $destip:$destport $srcip:$srcport >/dev/null 2>&1 || _failed=1
;;
esac
2007-10-11 01:27:38 +04:00
_killcount=`expr $_killcount + 1`
2008-08-20 03:47:00 +04:00
done < $connfile
2007-10-11 01:27:38 +04:00
/bin/rm -f $connfile
2008-08-20 03:47:00 +04:00
2007-10-11 01:27:38 +04:00
[ $_failed = 0 ] || {
2008-01-16 14:06:44 +03:00
echo "Failed to send killtcp control"
2007-10-11 01:27:38 +04:00
return;
}
[ $_killcount -gt 0 ] || {
return;
}
2009-03-24 06:05:31 +03:00
_count=0
while netstat -tn |egrep "^tcp.*[[:space:]]+$_IP:.*ESTABLISHED" > /dev/null; do
sleep 1
_count=`expr $_count + 1`
[ $_count -gt 3 ] && {
echo "Timed out killing tcp connections for IP $_IP"
return;
}
done
echo "killed $_killcount TCP connections to released IP $_IP"
}
##################################################################
# kill off the local end for any TCP connections with the given IP
##################################################################
kill_tcp_connections_local_only() {
_IP="$1"
_failed=0
_killcount=0
connfile="$CTDB_BASE/state/connections.$_IP"
netstat -tn |egrep "^tcp.*[[:space:]]+$_IP:.*ESTABLISHED" | awk '{print $4" "$5}' > $connfile
netstat -tn |egrep "^tcp.*[[:space:]]+::ffff:$_IP:.*ESTABLISHED" | awk '{print $4" "$5}' >> $connfile
while read dest src; do
srcip=`echo $src | sed -e "s/:[^:]*$//"`
srcport=`echo $src | sed -e "s/^.*://"`
destip=`echo $dest | sed -e "s/:[^:]*$//"`
destport=`echo $dest | sed -e "s/^.*://"`
echo "Killing TCP connection $srcip:$srcport $destip:$destport"
ctdb killtcp $srcip:$srcport $destip:$destport >/dev/null 2>&1 || _failed=1
_killcount=`expr $_killcount + 1`
done < $connfile
/bin/rm -f $connfile
[ $_failed = 0 ] || {
echo "Failed to send killtcp control"
return;
}
[ $_killcount -gt 0 ] || {
return;
}
2007-10-11 01:27:38 +04:00
_count=0
2008-07-09 04:03:21 +04:00
while netstat -tn |egrep "^tcp.*[[:space:]]+$_IP:.*ESTABLISHED" > /dev/null; do
2007-10-11 01:27:38 +04:00
sleep 1
_count=`expr $_count + 1`
[ $_count -gt 3 ] && {
2008-01-16 14:06:44 +03:00
echo "Timed out killing tcp connections for IP $_IP"
2007-10-11 01:27:38 +04:00
return;
}
done
2008-01-16 14:06:44 +03:00
echo "killed $_killcount TCP connections to released IP $_IP"
2007-10-11 01:27:38 +04:00
}
2008-02-11 01:35:37 +03:00
########################################################
# start/stop the nfs service on different platforms
########################################################
startstop_nfs() {
PLATFORM="unknown"
[ -x /etc/init.d/nfsserver ] && {
PLATFORM="sles"
}
[ -x /etc/init.d/nfslock ] && {
PLATFORM="rhel"
}
case $PLATFORM in
sles)
case $1 in
start)
service nfsserver start
;;
stop)
service nfsserver stop > /dev/null 2>&1
;;
esac
;;
rhel)
case $1 in
start)
service nfslock start
service nfs start
;;
stop)
service nfs stop > /dev/null 2>&1
service nfslock stop > /dev/null 2>&1
;;
esac
;;
*)
echo "Unknown platform. NFS is not supported with ctdb"
exit 1
;;
esac
}
2008-02-11 01:52:09 +03:00
########################################################
# start/stop the nfs lockmanager service on different platforms
########################################################
startstop_nfslock() {
PLATFORM="unknown"
[ -x /etc/init.d/nfsserver ] && {
PLATFORM="sles"
}
[ -x /etc/init.d/nfslock ] && {
PLATFORM="rhel"
}
case $PLATFORM in
sles)
# for sles there is no service for lockmanager
# so we instead just shutdown/restart nfs
case $1 in
start)
service nfsserver start
;;
stop)
service nfsserver stop > /dev/null 2>&1
;;
esac
;;
rhel)
case $1 in
start)
service nfslock start
;;
stop)
service nfslock stop > /dev/null 2>&1
;;
esac
;;
*)
echo "Unknown platform. NFS locking is not supported with ctdb"
exit 1
;;
esac
}
2008-04-10 00:50:12 +04:00
2009-04-08 06:49:28 +04:00
########################################################
# remove an ip address from an interface
########################################################
remove_ip() {
# the ip tool will delete all secondary IPs if this is the primary.
# To work around this _very_ annoying behaviour we have to keep a
# record of the secondaries and re-add them afterwards. yuck
secondaries=""
if ip addr list dev $2 primary | grep -q "inet $1 " ; then
secondaries=`ip addr list dev $2 secondary | grep " inet " | awk '{print $2}'`
fi
ip addr del $1 dev $2 >/dev/null 2>/dev/null || failed=1
[ -z "$secondaries" ] || {
for i in $secondaries; do
if ip addr list dev $2 | grep -q "inet $i" ; then
echo "kept secondary $i on dev $2"
else
echo "re-adding secondary address $i to dev $2"
ip addr add $i dev $2 || failed=1
fi
done
}
}
2009-09-30 15:05:16 +04:00
########################################################
# some simple logic for counting events - per eventscript
# usage: ctdb_counter_init <tag>
# ctdb_counter_incr <tag>
# ctdb_counter_limit <tag> <limit>
# e.g. <tag> = "fail-count"
# ctdb_counter_limit succeeds when count >= <limit>
########################################################
_ctdb_counter_common () {
_tag="$1"
_eventscript="${0##*/}" # basename
_counter_file="$CTDB_BASE/state/${_eventscript}-${_tag}"
mkdir -p "${_counter_file%/*}" # dirname
}
ctdb_counter_init () {
_ctdb_counter_common "$1"
echo -n > "$_counter_file"
}
ctdb_counter_incr () {
_ctdb_counter_common "$1"
# unary counting!
echo -n 1 >> "$_counter_file"
}
ctdb_counter_limit () {
_ctdb_counter_common "$1"
_limit="$2"
# unary counting!
_size=$(stat -c "%s" "$_counter_file" 2>/dev/null || echo 0)
[ $_size -ge $_limit ]
}
2008-04-10 00:50:12 +04:00
########################################################
# load a site local config file
########################################################
2008-04-10 01:01:22 +04:00
[ -x $CTDB_BASE/rc.local ] && {
2008-04-10 00:50:12 +04:00
. $CTDB_BASE/rc.local
}
2008-08-20 03:47:00 +04:00
2009-04-08 06:49:28 +04:00