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