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