ctdb-daemon: Add ctdb_vfork_exec()
[Samba.git] / ctdb / tools / ctdb_lvs
blob62ca7bc5783788d0db54c471d08cade3b5d7cb83
1 #!/bin/sh
3 if [ -z "$CTDB_BASE" ] ; then
4 export CTDB_BASE="/usr/local/etc/ctdb"
5 fi
7 . "${CTDB_BASE}/functions"
8 loadconfig "ctdb"
10 # Default LVS nodes file location
11 [ -n "$CTDB_LVS_NODES" ] || CTDB_LVS_NODES="${CTDB_BASE}/lvs_nodes"
13 [ -n "$CTDB_SOCKET" ] && export CTDB_SOCKET
15 if [ -z "$CTDB" ] ; then
16 CTDB=ctdb
19 ############################################################
21 usage ()
23 cat <<EOF
24 $0 <option>
26 <option> is one of:
27 master Display node number of master node
28 list List node number and private IP address of usable nodes in group
29 status Show status of all nodes in LVS group
30 EOF
31 exit 1
34 nodestatus_X=""
35 # Fields are:
36 # Node|IP|Disconnected|Banned|Disabled|Unhealthy|Stopped|Inactive|PartiallyOnline|ThisNode
37 get_nodestatus_X ()
39 # Result is cached in global variable nodestatus_X
40 [ -n "$nodestatus_X" ] || \
41 nodestatus_X=$($CTDB -X nodestatus all |
42 sed -e '1d' -e 's@^|@@' -e 's@|$@@')
45 get_nodestatus ()
47 # Result is cached in global variable nodestatus
48 [ -n "$nodestatus" ] || nodestatus=$($CTDB nodestatus all)
49 case $? in
50 # $CTDB nodestatus returns 255 on failure
51 0|255) return 0 ;;
52 *) return 1 ;;
53 esac
56 get_lvs_nodes ()
58 # Result is cached in global variable lvs_nodes
59 if [ -n "$lvs_nodes" ] ; then
60 return
63 if [ ! -r "$CTDB_LVS_NODES" ] ; then
64 return 1
67 lvs_nodes=$(cat "$CTDB_LVS_NODES") || return 1
69 # Sanity check file contents here
70 while read _ip _options ; do
71 # Skip comments
72 case "$_ip" in
73 \#*) continue ;;
74 esac
75 case "$_options" in
76 slave-only|"") : ;;
77 *) die "${prog}: Invalid options \"${_options}\" in \"$CTDB_LVS_NODES\""
78 esac
79 done <<EOF
80 $lvs_nodes
81 EOF
83 return 0
86 # Print PNN and IP address of given nodes meeting the criteria for
87 # usable LVS nodes. That is, either those that are healthy or, if no
88 # healthy nodes, then nodes that are active and not-disabled.
89 # Return codes: 0 = nodes found, 255 = no nodes found, 10 = error.
90 filter_nodes ()
92 # $_ns is an @-delimited list of nodes to be considered
93 _ns="$1"
95 get_nodestatus_X
96 [ -n "$nodestatus_X" ] || return 10
98 # Now filter by $_ns and by status of nodes...
100 # Note that the 2 awk invocations below have "||" between
101 # them, so the first to succeed will print the nodes.
103 # First try for a fully active and healthy node, so must not
104 # be DISABLED, UNHEALTHY or INACTIVE (last covers
105 # DISCONNECTED, BANNED or STOPPED)
106 awk -F '|' -v ns="$_ns" '
107 BEGIN { ret = 255 }
108 ns ~ "@" $2 "@" && $5 == 0 && $6 == 0 && $8 == 0 {
109 print $1, $2 ; ret=0
111 END { exit ret }
112 ' <<EOF ||
113 $nodestatus_X
115 # Not found? UNHEALTHY do, so node must not be INACTIVE or
116 # DISABLED
117 awk -F '|' -v ns="$_ns" '
118 BEGIN { ret = 255 }
119 ns ~ "@" $2 "@" && $5 == 0 && $8 == 0 {
120 print $1, $2 ; ret=0
122 END { exit ret }
123 ' <<EOF
124 $nodestatus_X
128 # Print the PNN of the LVS master node
129 find_master ()
131 get_lvs_nodes || \
132 die "${prog}: LVS nodes file \"$CTDB_LVS_NODES\" not found"
134 # $_ms is an @-delimited list of nodes that are allowed to be the master
135 _ms="@"
136 while read _ip _options ; do
137 case "$_options" in
138 "") _ms="${_ms}${_ip}@" ;;
139 esac
140 done <<EOF
141 $lvs_nodes
144 _master_candidates=$(filter_nodes "$_ms") || return $?
145 echo "${_master_candidates%% *}"
148 # List all usable nodes in the LVS group
149 nodes_list ()
151 get_lvs_nodes || \
152 die "${prog}: LVS nodes file \"$CTDB_LVS_NODES\" not found"
154 # $_ns is a @-delimited list of nodes in the LVS group
155 _ns="@"
156 while read _ip _options ; do
157 _ns="${_ns}${_ip}@"
158 done <<EOF
159 $lvs_nodes
162 _usable_nodes=$(filter_nodes "$_ns")
163 case $? in
164 0) : ;;
165 255) exit 0 ;; # Return 0 even if no usable nodes
166 *) exit 10 ;;
167 esac
169 awk '{ print $1, $2 }'<<EOF
170 $_usable_nodes
174 # Print the status of all nodes in the LVS group, along with a count
175 nodes_status ()
177 get_lvs_nodes || \
178 die "${prog}: LVS nodes file \"$CTDB_LVS_NODES\" not found"
179 get_nodestatus
180 [ -n "$nodestatus" ] || exit 10
182 # $_ns is a @-delimited list of nodes in the LVS group
183 _ns="@"
184 while read _ip _options ; do
185 _ns="${_ns}${_ip}@"
186 done <<EOF
187 $lvs_nodes
190 # Print status of nodes in $_ns, along with node count
191 awk -v ns="$_ns" 'ns ~ "@" $2 "@" { print }' <<EOF
192 $nodestatus
196 # For backward compatibility
197 prog=$(basename "$0")
198 cmd="$1"
200 case "$cmd" in
201 master) find_master ;;
202 list) nodes_list ;;
203 status) nodes_status ;;
204 *) usage ;;
205 esac