8889cadcc26d1984d646d3072c871310f06bf778
[metze/ctdb/wip.git] / config / events.d / 60.nfs
1 #!/bin/sh
2 # script to manage nfs in a clustered environment
3
4 start_nfs() {
5         /bin/mkdir -p $CTDB_VARDIR/state/nfs
6         /bin/mkdir -p $CTDB_VARDIR/state/statd/ip
7         startstop_nfs stop
8         startstop_nfs start
9         echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle
10 }
11
12 . $CTDB_BASE/functions
13
14 service_name="nfs"
15 service_start="start_nfs"
16 service_stop="startstop_nfs stop"
17
18 loadconfig
19
20 ctdb_start_stop_service
21
22 case "$1" in 
23      init)
24         # read statd from persistent database
25         ;;
26      startup)
27         ctdb_service_start
28         mkdir -p $CTDB_VARDIR/state/statd
29         touch $CTDB_VARDIR/state/statd/update-trigger
30         ;;
31
32      shutdown)
33         ctdb_service_stop
34         ;;
35
36      takeip)
37         ctdb_service_set_reconfigure
38         ;;
39
40      releaseip)
41         ctdb_service_set_reconfigure
42         ;;
43
44       monitor)
45         if ctdb_service_needs_reconfigure ; then
46             ctdb_service_reconfigure
47             exit 0
48         fi
49
50         update_tickles 2049
51
52         # check that statd responds to rpc requests
53         # if statd is not running we try to restart it
54         if ctdb_check_rpc "STATD" 100024 1 >/dev/null ; then
55                 (service_name="nfs_statd"; ctdb_counter_init)
56         else
57                 p="rpc.statd" ; cmd="$p"
58                 cmd="${cmd}${STATD_HOSTNAME:+ -n }${STATD_HOSTNAME}"
59                 cmd="${cmd}${STATD_PORT:+ -p }${STATD_PORT}"
60                 cmd="${cmd}${STATD_OUTGOING_PORT:+ -o }${STATD_OUTGOING_PORT}"
61                 (
62                         service_name="nfs_statd"
63                         ctdb_counter_incr
64                         ctdb_check_counter_limit 10 quiet >/dev/null
65                 ) || {
66                         echo "$ctdb_check_rpc_out"
67                         echo "Trying to restart STATD [$cmd]"
68                 }
69                 $cmd
70         fi
71
72
73         # check that NFS responds to rpc requests
74         [ "$CTDB_NFS_SKIP_KNFSD_ALIVE_CHECK" = "yes" ] || {
75             if ctdb_check_rpc "NFS" 100003 3 >/dev/null ; then
76                 (service_name="nfs_knfsd"; ctdb_counter_init)
77             else
78                 (
79                         service_name="nfs_knfsd"
80                         ctdb_counter_incr
81
82                         ctdb_check_counter_equal 10 || {
83                                 echo "Trying to restart NFS service"
84                                 startstop_nfs restart >/dev/null 2>&1 &
85                                 exit 0
86                         }
87
88                         ctdb_check_counter_limit 15 quiet >/dev/null
89                 ) || {
90                         echo "$ctdb_check_rpc_out"
91                         echo "Trying to restart NFS service"
92                         startstop_nfs restart
93                         exit 1
94                 }
95             fi
96         }
97
98         # and that its directories are available
99         [ "$CTDB_NFS_SKIP_SHARE_CHECK" = "yes" ] || {
100             exportfs | grep -v '^#' | grep '^/' |
101             sed -e 's/[[:space:]]\+[^[:space:]]*$//' |
102             ctdb_check_directories
103         } || exit $?
104
105         # check that lockd responds to rpc requests
106         ctdb_check_rpc "LOCKD" 100021 1 || {
107                 echo "Trying to restart lock manager service"
108                 startstop_nfs restart
109                 startstop_nfslock restart
110                 exit 1
111         }
112
113         # mount needs special handling since it is sometimes not started
114         # correctly on RHEL5
115         if ctdb_check_rpc "MOUNTD" 100005 1 >/dev/null ; then
116                 (service_name="nfs_mountd"; ctdb_counter_init)
117         else
118         (
119                 service_name="nfs_mountd"
120                 ctdb_counter_incr
121
122                 ctdb_check_counter_equal 5 || {
123                         p="rpc.mountd"
124                         cmd="${p}${MOUNTD_PORT:+ -p }${MOUNTD_PORT}"
125                         echo "Trying to restart MOUNTD [${cmd}]"
126                         killall -q -9 $p
127                         $cmd &
128                         exit 0
129                 }
130
131                 ctdb_check_counter_limit 10 quiet >/dev/null
132         ) || {
133                 echo "$ctdb_check_rpc_out"
134                 p="rpc.mountd"
135                 cmd="${p}${MOUNTD_PORT:+ -p }${MOUNTD_PORT}"
136                 echo "Trying to restart MOUNTD [${cmd}]"
137                 killall -q -9 $p
138                 $cmd &
139                 exit 1
140         }
141         fi
142
143
144         # rquotad needs special handling since it is sometimes not started
145         # correctly on RHEL5
146         # this is not a critical service so we dont flag the node as unhealthy
147         ctdb_check_rpc "RQUOTAD" 100011 1 || {
148                 p="rpc.rquotad"
149                 cmd="${p}${RQUOTAD_PORT:+ -p }${RQUOTAD_PORT}"
150                 echo "Trying to restart RQUOTAD [${cmd}]"
151                 killall -q -9 $p
152                 $cmd &
153         }
154
155         # once every 60 seconds, update the statd state database for which
156         # clients need notifications
157         LAST_UPDATE=`stat --printf="%Y" $CTDB_VARDIR/state/statd/update-trigger`
158         CURRENT_TIME=`date +"%s"`
159         [ $CURRENT_TIME -ge $(($LAST_UPDATE + 60)) ] && {
160             mkdir -p $CTDB_VARDIR/state/statd
161             touch $CTDB_VARDIR/state/statd/update-trigger
162             $CTDB_BASE/statd-callout updatelocal &
163             $CTDB_BASE/statd-callout updateremote &
164         }
165         ;;
166
167     ipreallocated)
168         # if the ips have been reallocated, we must restart the lockmanager
169         # across all nodes and ping all statd listeners
170         [ -x $CTDB_BASE/statd-callout ] && {
171                 $CTDB_BASE/statd-callout notify &
172         } >/dev/null 2>&1
173         ;;
174     *)
175         ctdb_standard_event_handler "$@"
176         ;;
177 esac
178
179 exit 0