ctdb-scripts: Event scripts must end with ".script" suffix
[kai/samba-autobuild/.git] / ctdb / config / events / legacy / 20.multipathd.script
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 [ -n "$CTDB_BASE" ] || \
10     CTDB_BASE=$(d=$(dirname "$0") ; cd -P "$d" ; dirname "$PWD")
11
12 . "${CTDB_BASE}/functions"
13
14 service_name="multipathd"
15
16 load_script_options
17
18 [ -n "$CTDB_MONITOR_MPDEVICES" ] || exit 0
19
20 ctdb_setup_state_dir "service" "$service_name"
21
22 # script_state_dir set by ctdb_setup_state_dir()
23 # shellcheck disable=SC2154
24 multipath_fail="${script_state_dir}/fail"
25
26 multipathd_check_background()
27 {
28     for _device in $CTDB_MONITOR_MPDEVICES; do
29         # Check multipath knows about the device
30         _out=$(multipath -ll "$_device")
31         if [ -z "$_out" ] ; then
32             echo "ERROR: device \"${_device}\" not known to multipathd" \
33                  >"$multipath_fail"
34             exit 1
35         fi
36
37         # Check for at least 1 active path
38         if ! echo "$_out" | grep 'prio=.* status=active' >/dev/null 2>&1 ; then
39             echo "ERROR: multipath device \"${_device}\" has no active paths" \
40                  >"$multipath_fail"
41             exit 1
42         fi
43     done
44     exit 0
45 }
46
47 multipathd_check()
48 {
49     # Run the actual check in the background since the call to
50     # multipath may block
51     multipathd_check_background </dev/null >/dev/null 2>&1 &
52     _pid="$!"
53     _timeleft=10
54
55     while [ $_timeleft -gt 0 ]; do
56         _timeleft=$((_timeleft - 1))
57
58         # see if the process still exists
59         kill -0 $_pid >/dev/null 2>&1 || {
60             if wait $_pid ; then
61                 return 0
62             else
63                 cat "$multipath_fail"
64                 rm -f "$multipath_fail"
65                 return 1
66             fi
67         }
68         sleep 1
69     done
70
71     echo "ERROR: callout to multipath checks hung"
72     # If hung then this probably won't work, but worth trying...
73     kill -9 $_pid >/dev/null 2>&1
74     return 1
75 }
76
77 case "$1" in
78 monitor)
79         multipathd_check || die "multipath monitoring failed"
80         ;;
81 esac
82
83 exit 0