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
}
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"
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
}
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
echo "`/bin/date` Waiting for service $service_name to start"
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
echo "`/bin/date` Local service $service_name is up"
}
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
echo "`/bin/date` Waiting for tcp service $service_name to start"
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
2007-09-13 04:39:05 +04:00
(/usr/bin/netstat -a -n | egrep "0.0.0.0:$p\s*LISTEN" > /dev/null) || all_ok=0
2007-06-01 17:25:33 +04:00
else
2007-07-15 03:26:54 +04:00
echo "`date` - 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
echo "`/bin/date` Local tcp services for $service_name are up"
}
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
echo "`/bin/date` Waiting for local directories for $service_name"
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
echo "`/bin/date` Local directories for $service_name are available"
}
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 || {
echo "`date` ERROR: $service_name not responding to rpc requests"
exit 1
}
}
######################################################
# check a set of directories is available
# usage: ctdb_check_directories SERVICE_NAME <directories...>
######################################################
ctdb_check_directories() {
service_name="$1"
shift
wait_dirs="$*"
[ -z "$wait_dirs" ] && return;
for d in $wait_dirs; do
[ -d $d ] || {
echo "`date` ERROR: $service_name directory $d not available"
exit 1
}
done
}
######################################################
# 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;
for p in $wait_ports; do
all_ok=1
if [ -x /usr/bin/netcat ]; then
2007-09-13 04:03:18 +04:00
/usr/bin/netcat -z 127.0.0.1 $p > /dev/null || all_ok=0
2007-06-06 06:08:42 +04:00
elif [ -x /usr/bin/nc ]; then
2007-09-13 04:03:18 +04:00
/usr/bin/nc -z 127.0.0.1 $p > /dev/null || all_ok=0
2007-07-15 03:29:08 +04:00
elif [ -x /usr/bin/netstat ]; then
2007-09-13 04:03:18 +04:00
(/usr/bin/netstat -a -n | egrep "0.0.0.0:$p .*LISTEN" > /dev/null ) || all_ok=0
2007-06-06 06:08:42 +04:00
fi
[ $all_ok -eq 1 ] || {
echo "`date` ERROR: $service_name tcp port $p is not responding"
exit 1
}
done
}
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 || {
echo "`date` ERROR: $service_name - $wait_cmd returned error"
exit 1
}
}