Eventscripts - conditionally inherit ctdbd debug level in each monitor event
[ctdb.git] / config / events.d / 00.ctdb
1 #!/bin/sh
2 ############################
3 # main event script for ctdb
4 #
5 # This script is called with one of the following sets of arguments
6 #     startup    : called when ctdb starts
7 #     shutdown   : called when ctdb shuts down
8 #     takeip     : called when an IP address is taken over
9 #     releaseip  : called when an IP address is released
10 #     recovered  : called when ctdb has finished a recovery event
11
12 . $CTDB_BASE/functions
13 loadconfig
14
15 ctdb_setup_service_state_dir "ctdb"
16
17 #
18 update_config_from_tdb() {
19
20     # Pull optional ctdb configuration data out of config.tdb
21     _key="public_addresses:node#$(ctdb -t 1 xpnn|sed -e 's/.*://')"
22     _t="$service_state_dir/public_addresses"
23     rm -f "$_t"
24
25     if ctdb pfetch config.tdb "$_key" "$_t" 2>/dev/null && \
26         [ -s "$_t" -a -n "$CTDB_PUBLIC_ADDRESSES"] && \
27         ! cmp -s "$_t" "$CTDB_PUBLIC_ADDRESSES" ; then
28
29         echo "CTDB public address configuration has changed."
30         echo "Extracting new configuration from database."
31         diff "$_t" "$CTDB_PUBLIC_ADDRESSES"
32         cp "$_t" "$CTDB_PUBLIC_ADDRESSES"
33         echo "Restarting CTDB"
34         service ctdb restart &
35     fi
36 }
37
38 case "$1" in 
39      init)
40         # make sure we have a blank state directory for the scripts to work with
41         rm -rf $CTDB_VARDIR/state
42         # Look at the pattern - this should not be -rf!!!
43         rm -f $ctdb_managed_dir/*
44         mkdir -p $CTDB_VARDIR/state || {
45             ret=$?
46             echo "mkdir -p $CTDB_VARDIR/state - failed - $ret"
47             exit $ret
48         }
49         ;;
50
51      setup)
52         # set any tunables from the config file
53         set | grep ^CTDB_SET_ | cut -d_ -f3- | 
54         while read v; do
55             varname=`echo $v | cut -d= -f1`
56             value=`echo $v | cut -d= -f2`
57             ctdb setvar $varname $value || exit 1
58             echo "Set $varname to $value"
59         done || exit 1
60         ;;
61
62     startup)
63         update_config_from_tdb &
64         ;;
65     monitor)
66         # Inherit the debug level from ctdbd on each monitor run.  If
67         # there's a more urgent need then override CTDB_CURRENT_DEBUGLEVEL
68         # using a file in $CTDB_BASE/rc.local.d/.
69         ctdb_set_current_debuglevel create
70
71         # We should never enter swap, so SwapTotal == SwapFree.
72         [ "$CTDB_CHECK_SWAP_IS_NOT_USED" = "yes" ] && {
73             if [ -n "`grep '^Swap\(Total\|Free\)' /proc/meminfo | uniq -s 10 -u`" ]; then
74                 echo We are swapping:
75                 cat /proc/meminfo
76                 ps auxfww
77             fi
78         }
79
80         # warn when we get low on memory
81         [ -z "$CTDB_MONITOR_FREE_MEMORY_WARN" ] || {
82                 FREE_MEM=`free -m | grep "buffers/cache" | while read A B C D ;do echo -n $D ; done`
83                 [ `expr "$FREE_MEM" "<" "$CTDB_MONITOR_FREE_MEMORY_WARN"` != "0" ] && {
84                         echo "Running low on memory. Free:$FREE_MEM while CTDB treshold is $CTDB_MONITOR_FREE_MEMORY_WARN"
85                 }
86         }
87
88         # monitor that we are not running out of memory
89         [ -z "$CTDB_MONITOR_FREE_MEMORY" ] || {
90                 FREE_MEM=`free -m | grep "buffers/cache" | while read A B C D ;do echo -n $D ; done`
91                 [ `expr "$FREE_MEM" "<" "$CTDB_MONITOR_FREE_MEMORY"` != "0" ] && {
92                         echo "OOM. Free:$FREE_MEM while CTDB treshold is $CTDB_MONITOR_FREE_MEMORY"
93                         cat /proc/meminfo
94                         ps auxfww
95                         echo m > /proc/sysrq-trigger
96                         ctdb disable
97                         sleep 3
98                         ctdb shutdown
99                 }
100         }
101         ;;
102
103     *)
104         ctdb_standard_event_handler "$@"
105         ;;
106 esac
107
108 # all OK
109 exit 0