smbd: ignore POSIX and stale opens in have_file_open_below()
[samba4-gss.git] / ctdb / tools / ctdb_lvs
blobd0249b9d4f447f782fcdc68a2a175c894078744f
1 #!/bin/sh
3 if [ -z "$CTDB_BASE" ] ; then
4 export CTDB_BASE="/usr/local/etc/ctdb"
5 fi
7 . "${CTDB_BASE}/functions"
9 load_script_options "failover" "91.lvs"
11 # Default LVS nodes file location
12 [ -n "$CTDB_LVS_NODES" ] || CTDB_LVS_NODES="${CTDB_BASE}/lvs_nodes"
14 if [ -z "$CTDB" ] ; then
15 CTDB=ctdb
18 ############################################################
20 usage ()
22 cat <<EOF
23 $0 <option>
25 <option> is one of:
26 leader Display node number of leader node
27 list List node number and private IP address of usable nodes in group
28 status Show status of all nodes in LVS group
29 EOF
30 exit 1
33 nodestatus_X=""
34 # Fields are:
35 # Node|IP|Disconnected|Unknown|Banned|Disabled|Unhealthy|Stopped|Inactive|PartiallyOnline|ThisNode
36 get_nodestatus_X ()
38 # Result is cached in global variable nodestatus_X
39 [ -n "$nodestatus_X" ] || \
40 nodestatus_X=$($CTDB -X nodestatus all |
41 sed -e '1d' -e 's@^|@@' -e 's@|$@@')
44 get_nodestatus ()
46 # Result is cached in global variable nodestatus
47 [ -n "$nodestatus" ] || nodestatus=$($CTDB nodestatus all)
48 case $? in
49 # $CTDB nodestatus returns 255 on failure
50 0|255) return 0 ;;
51 *) return 1 ;;
52 esac
55 get_lvs_nodes ()
57 # Result is cached in global variable lvs_nodes
58 if [ -n "$lvs_nodes" ] ; then
59 return
62 if [ ! -r "$CTDB_LVS_NODES" ] ; then
63 return 1
66 lvs_nodes=$(cat "$CTDB_LVS_NODES") || return 1
68 # Sanity check file contents here
69 while read _ip _options ; do
70 # Skip comments
71 case "$_ip" in
72 \#*) continue ;;
73 esac
74 case "$_options" in
75 follower-only|"") : ;;
76 *) die "${prog}: Invalid options \"${_options}\" in \"$CTDB_LVS_NODES\""
77 esac
78 done <<EOF
79 $lvs_nodes
80 EOF
82 return 0
85 # Print PNN and IP address of given nodes meeting the criteria for
86 # usable LVS nodes. That is, either those that are healthy or, if no
87 # healthy nodes, then nodes that are active and not-disabled.
88 # Return codes: 0 = nodes found, 255 = no nodes found, 10 = error.
89 filter_nodes ()
91 # $_ns is an @-delimited list of nodes to be considered
92 _ns="$1"
94 get_nodestatus_X
95 [ -n "$nodestatus_X" ] || return 10
97 # Now filter by $_ns and by status of nodes...
99 # Note that the 2 awk invocations below have "||" between
100 # them, so the first to succeed will print the nodes.
102 # First try for a fully active and healthy node, so must not
103 # be UNKNOWN, DISABLED, UNHEALTHY or INACTIVE (last covers
104 # DISCONNECTED, BANNED or STOPPED)
105 awk -F '|' -v ns="$_ns" '
106 BEGIN { ret = 255 }
107 ns ~ "@" $2 "@" && $4 == 0 && $6 == 0 && $7 == 0 && $9 == 0 {
108 print $1, $2 ; ret=0
110 END { exit ret }
111 ' <<EOF ||
112 $nodestatus_X
114 # Not found? UNHEALTHY do, so node must not be INACTIVE or
115 # DISABLED
116 awk -F '|' -v ns="$_ns" '
117 BEGIN { ret = 255 }
118 ns ~ "@" $2 "@" && $6 == 0 && $9 == 0 {
119 print $1, $2 ; ret=0
121 END { exit ret }
122 ' <<EOF
123 $nodestatus_X
127 # Print the PNN of the LVS leader node
128 find_leader ()
130 get_lvs_nodes || \
131 die "${prog}: LVS nodes file \"$CTDB_LVS_NODES\" not found"
133 # $_ms is an @-delimited list of nodes that are allowed to be the leader
134 _ms="@"
135 while read _ip _options ; do
136 case "$_options" in
137 "") _ms="${_ms}${_ip}@" ;;
138 esac
139 done <<EOF
140 $lvs_nodes
143 _leader_candidates=$(filter_nodes "$_ms") || return $?
144 echo "${_leader_candidates%% *}"
147 # List all usable nodes in the LVS group
148 nodes_list ()
150 get_lvs_nodes || \
151 die "${prog}: LVS nodes file \"$CTDB_LVS_NODES\" not found"
153 # $_ns is a @-delimited list of nodes in the LVS group
154 _ns="@"
155 while read _ip _options ; do
156 _ns="${_ns}${_ip}@"
157 done <<EOF
158 $lvs_nodes
161 _usable_nodes=$(filter_nodes "$_ns")
162 case $? in
163 0) : ;;
164 255) exit 0 ;; # Return 0 even if no usable nodes
165 *) exit 10 ;;
166 esac
168 awk '{ print $1, $2 }'<<EOF
169 $_usable_nodes
173 # Print the status of all nodes in the LVS group, along with a count
174 nodes_status ()
176 get_lvs_nodes || \
177 die "${prog}: LVS nodes file \"$CTDB_LVS_NODES\" not found"
178 get_nodestatus
179 [ -n "$nodestatus" ] || exit 10
181 # $_ns is a @-delimited list of nodes in the LVS group
182 _ns="@"
183 while read _ip _options ; do
184 _ns="${_ns}${_ip}@"
185 done <<EOF
186 $lvs_nodes
189 # Print status of nodes in $_ns, along with node count
190 awk -v ns="$_ns" 'ns ~ "@" $2 "@" { print }' <<EOF
191 $nodestatus
195 # For backward compatibility
196 prog=$(basename "$0")
197 cmd="$1"
199 case "$cmd" in
200 leader) find_leader ;;
201 list) nodes_list ;;
202 status) nodes_status ;;
203 *) usage ;;
204 esac