c740d69a7a1fa65ee14aac0a8fd757976354b0a0
[samba.git] / ctdb / config / events.d / 20.multipathd
1 #!/bin/sh
2 # ctdb event script for monitoring the multipath daemon
3 #
4 # Configure monitporing of multipath devices by listing the device serials
5 # in /etc/ctdb/multipathd :
6 #   CTDB_MONITOR_MPDEVICES="device1 device2 ..."
7 #
8
9 PATH=/bin:/usr/bin:$PATH
10
11 . $CTDB_BASE/functions
12 loadconfig ctdb
13 loadconfig multipathd
14
15 cmd="$1"
16 shift
17
18 [ -z "$CTDB_MONITOR_MPDEVICES" ] && {
19         exit 0
20 }
21
22 MPFAILURE=$CTDB_BASE/state/multipathd/failure
23
24 multipathd_check_background()
25 {
26         for DEVICE in $CTDB_MONITOR_MPDEVICES; do
27                 # check that we can see all devices
28                 if [ -z "`multipath -ll $DEVICE`" ]; then
29                         echo Device $DEVICE not known to multipathd
30                         exit 1
31                 fi
32
33                 # check that all devices are active
34                 if [ -z "`multipath -ll $DEVICE|grep prio|grep active`" ]; then
35                         echo Device $DEVICE has no active paths
36                         exit 1
37                 fi
38         done
39         exit 0
40 }
41
42 multipathd_check()
43 {
44         # run the actual check in the background since the call to 
45         # multipath may block.
46         (
47         multipathd_check_background &
48         pid="$!"
49         timeleft=10
50
51         while [ $timeleft -gt 0 ]; do
52                 timeleft=$(($timeleft - 1))
53
54                 # see if the process still exists
55                 /bin/kill -0 $pid > /dev/null 2>&1 || {
56                         # it doesn't exist, grab its exit status
57                         wait $pid
58                         [ $? = 0 ] || {
59                                 echo "20.multipathd: multipath background update exited with status $?"
60                                 touch $MPFAILURE
61                                 exit 1
62                         }
63                         rm $MPFAILURE 2>/dev/null
64                         exit 0
65                 }
66                 sleep 1
67         done
68         echo "20.multipathd: Callout to multipath checks hung."
69         touch $MPFAILURE
70         exit 1
71         ) &
72
73         if [ -f $MPFAILURE ]; then
74                 return 1
75         else
76                 return 0
77         fi
78 }
79
80 case $cmd in
81      startup)
82         # create a state directory to keep/track the multipath device
83         # state
84         /bin/mkdir -p $CTDB_BASE/state/multipathd
85         exit 0
86         ;;
87
88      monitor)
89         multipathd_check
90         [ "$?" = "0" ] || {
91                 echo 20.multipathd: monitoring of multipathing failed
92                 exit 1
93         }
94         exit 0
95         ;;
96 esac
97
98 # ignore unknown commands
99 exit 0