3 # Event script for ctdb-specific setup and other things that don't fit
6 [ -n "$CTDB_BASE" ] || \
7 export CTDB_BASE
=$
(cd -P $
(dirname "$0") ; dirname "$PWD")
12 ctdb_setup_service_state_dir
"ctdb"
14 ############################################################
18 # Find the best TDB consistency check available.
19 use_tdb_tool_check
=false
20 if which tdbtool
>/dev
/null
2>&1 && \
21 echo "help" | tdbtool |
grep -q check
; then
23 use_tdb_tool_check
=true
24 elif which tdbtool
>/dev
/null
2>&1 && which tdbdump
>/dev
/null
2>&1 ; then
26 WARNING: The installed 'tdbtool' does not offer the 'check' subcommand.
27 Using 'tdbdump' for database checks.
28 Consider updating 'tdbtool' for better checks!
30 elif which tdbdump
>/dev
/null
2>&1 ; then
32 WARNING: 'tdbtool' is not available.
33 Using 'tdbdump' to check the databases.
34 Consider installing a recent 'tdbtool' for better checks!
38 WARNING: Cannot check databases since neither
39 'tdbdump' nor 'tdbtool check' is available.
40 Consider installing tdbtool or at least tdbdump!
50 if $use_tdb_tool_check ; then
51 # tdbtool always exits with 0 :-(
52 if tdbtool
"$_db" check
2>/dev
/null |
53 grep -q "Database integrity is OK" ; then
59 tdbdump
"$_db" >/dev
/null
2>/dev
/null
64 check_persistent_databases
()
66 _dir
="${CTDB_DBDIR_PERSISTENT:-${CTDB_DBDIR:-${CTDB_VARDIR}}/persistent}"
67 mkdir
-p "$_dir" 2>/dev
/null
69 [ "${CTDB_MAX_PERSISTENT_CHECK_ERRORS:-0}" = "0" ] ||
return 0
71 for _db
in $
(ls "$_dir/"*.tdb.
*[0-9] 2>/dev
/null
) ; do
73 echo "Persistent database $_db is corrupted! CTDB will not start."
79 check_non_persistent_databases
()
81 _dir
="${CTDB_DBDIR:-${CTDB_VARDIR}}"
82 mkdir
-p "$_dir" 2>/dev
/null
84 for _db
in $
(ls "${_dir}/"*.tdb.
*[0-9] 2>/dev
/null
) ; do
86 _backup
="${_db}.$(date +'%Y%m%d.%H%M%S.%N').corrupt"
88 WARNING: database ${_db} is corrupted.
89 Moving to backup ${_backup} for later analysis.
93 # Now remove excess backups
94 ls -td "${_db}."*".corrupt" |
95 tail -n +$
((${CTDB_MAX_CORRUPT_DB_BACKUPS:-10} + 1)) |
102 update_config_from_tdb
() {
104 # Pull optional ctdb configuration data out of config.tdb
105 _key
="public_addresses:node#$(ctdb -t 1 xpnn|sed -e 's/.*://')"
106 _t
="$service_state_dir/public_addresses"
109 if ctdb pfetch config.tdb
"$_key" "$_t" 2>/dev
/null
&& \
110 [ -s "$_t" -a -n "$CTDB_PUBLIC_ADDRESSES"] && \
111 ! cmp -s "$_t" "$CTDB_PUBLIC_ADDRESSES" ; then
113 echo "CTDB public address configuration has changed."
114 echo "Extracting new configuration from database."
115 diff "$_t" "$CTDB_PUBLIC_ADDRESSES"
116 cp "$_t" "$CTDB_PUBLIC_ADDRESSES"
117 echo "Restarting CTDB"
118 service ctdb restart
&
122 set_ctdb_variables
() {
123 # set any tunables from the config file
124 set |
grep ^CTDB_SET_ | cut
-d_ -f3- |
126 varname
=`echo $v | cut -d= -f1`
127 value
=`echo $v | cut -d= -f2`
128 ctdb setvar
$varname $value ||
return 1
129 echo "Set $varname to $value"
133 monitor_system_memory
()
135 # If monitoring free memory then calculate how much there is
136 if [ -n "$CTDB_MONITOR_FREE_MEMORY_WARN" -o \
137 -n "$CTDB_MONITOR_FREE_MEMORY" ] ; then
138 free_mem
=$
(free
-m |
awk '$2 == "buffers/cache:" { print $4 }')
141 # Shutdown CTDB when memory is below the configured limit
142 if [ -n "$CTDB_MONITOR_FREE_MEMORY" ] ; then
143 if [ $free_mem -le $CTDB_MONITOR_FREE_MEMORY ] ; then
144 echo "CRITICAL: OOM - ${free_mem}MB free <= ${CTDB_MONITOR_FREE_MEMORY}MB (CTDB threshold)"
145 echo "CRITICAL: Shutting down CTDB!!!"
148 set_proc
"sysrq-trigger" "m"
155 # Warn when low on memory
156 if [ -n "$CTDB_MONITOR_FREE_MEMORY_WARN" ] ; then
157 if [ $free_mem -le $CTDB_MONITOR_FREE_MEMORY_WARN ] ; then
158 echo "WARNING: free memory is low - ${free_mem}MB free <= ${CTDB_MONITOR_FREE_MEMORY_WARN}MB (CTDB threshold)"
162 # We should never enter swap, so SwapTotal == SwapFree.
163 if [ "$CTDB_CHECK_SWAP_IS_NOT_USED" = "yes" ] ; then
164 set -- $
(get_proc
"meminfo" |
awk '$1 ~ /Swap(Total|Free):/ { print $2 }')
165 if [ "$1" != "$2" ] ; then
166 echo We are swapping
:
173 ############################################################
179 # make sure we have a blank state directory for the scripts to work with
180 rm -rf $CTDB_VARDIR/state
181 # Look at the pattern - this should not be -rf!!!
182 rm -f $ctdb_managed_dir/*
183 mkdir
-p $CTDB_VARDIR/state ||
{
185 echo "mkdir -p $CTDB_VARDIR/state - failed - $ret"
189 # make sure we drop any ips that might still be held if
190 # previous instance of ctdb got killed with -9 or similar
193 if select_tdb_checker
; then
194 check_persistent_databases ||
exit $?
195 check_non_persistent_databases
200 # Set any tunables from the config file
201 set_ctdb_variables || die
"Failed to set CTDB tunables"
205 ctdb attach ctdb.tdb persistent
206 update_config_from_tdb
&
209 monitor_system_memory
213 ctdb_standard_event_handler
"$@"