3 if [ -z "$CTDB_BASE" ] ; then
4 export CTDB_BASE
="/usr/local/etc/ctdb"
7 .
"${CTDB_BASE}/functions"
9 load_script_options
"failover" "11.natgw"
11 # Default NAT gateway nodes file location
12 [ -n "$CTDB_NATGW_NODES" ] || CTDB_NATGW_NODES
="${CTDB_BASE}/natgw_nodes"
14 if [ -z "$CTDB" ] ; then
18 ############################################################
26 leader Display node number and private IP address of leader node
27 list List private IP addresses of nodes in group, annotate leader
28 status Show status of nodes in NAT gateway group
35 # Node|IP|Disconnected|Unknown|Banned|Disabled|Unhealthy|Stopped|Inactive|PartiallyOnline|ThisNode
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@|$@@')
46 # Result is cached in global variable nodestatus
47 [ -n "$nodestatus" ] || nodestatus
=$
($CTDB nodestatus all
)
48 [ $?
-ne 255 ] # ctdb nodestatus returns 255 on failure
53 # Result is cached in global variable natgw_nodes
54 if [ -n "$natgw_nodes" ] ; then
58 if [ ! -r "$CTDB_NATGW_NODES" ] ; then
62 natgw_nodes
=$
(cat "$CTDB_NATGW_NODES") ||
return 1
64 # Sanity check file contents here
65 while read _ip _options
; do
71 follower-only|
"") : ;;
72 *) die
"${prog}: Invalid options \"${_options}\" in \"$CTDB_NATGW_NODES\""
81 # Print the PNN and IP address of the NAT gateway leader node
85 die
"${prog}: NAT gateway nodes file \"$CTDB_NATGW_NODES\" not found"
87 die
"${prog}: Unable to get status of nodes"
89 # $_ms is an @-delimited list of nodes that are allowed to be the leader
91 while read _ip _options
; do
93 "") _ms
="${_ms}${_ip}@" ;;
99 # Now filter by $ms and by status of nodes...
101 # Note that the 3 awk invocations below have "||" between them, so
102 # the first to succeed will select the leader node.
104 # First try for a fully active and healthy node, so must not be
105 # UNKNOWN, DISABLED, UNHEALTHY or INACTIVE (last covers DISCONNECTED,
107 awk -F '|' -v ms
="$_ms" \
110 $4 == 0 && $6 == 0 && $7 == 0 && $9 == 0 { print $1, $2 ; ret=0 ; exit }
111 END { exit ret }' <<EOF ||
114 # Not found? UNHEALTHY/BANNED will do, so node must not be
115 # DISCONNECTED, DISABLED or STOPPED
116 awk -F '|' -v ms
="$_ms" \
119 $3 == 0 && $6 == 0 && $8 == 0 { print $1, $2 ; ret=0 ; exit }
120 END { exit ret }' <<EOF ||
123 # Not found? STOPPED will do, so node must not be DISCONNECTED or
125 awk -F '|' -v ms
="$_ms" \
128 $3 == 0 && $6 == 0 { print $1, $2 ; ret=0 ; exit }
129 END { exit ret }' <<EOF
134 # List all nodes in the NAT gateway group, annotating the leader node
138 die
"${prog}: NAT gateway nodes file \"$CTDB_NATGW_NODES\" not found"
139 # Intentional word splitting here
140 # shellcheck disable=SC2046
141 set -- $
(find_leader
) || \
142 die
"${prog}: Unable to determine NAT gateway leader node"
145 # Annotate the leader node
146 while read _ip _options
; do
147 if [ "$_ip" = "$_leader_ip" ] ; then
148 _options
="LEADER${_options:+,}${_options}"
150 # There is no other way to do this and keep shellcheck happy.
151 # The tab character must be in the format string and the
152 # format string must contain no variables. Some shells will
153 # expand a tab if it is in an argument but others won't.
154 if [ -n "$_options" ] ; then
155 printf '%s\t%s\n' "$_ip" "$_options"
164 # Print the status of all nodes in the NAT gateway group, along with a count
168 die
"${prog}: NAT gateway nodes file \"$CTDB_NATGW_NODES\" not found"
170 die
"${prog}: Unable to get status of nodes"
172 # $_ns is a @-delimited list of nodes in the NAT gateway group
174 while read _ip _options
; do
180 # Print status of nodes in $_ns, along with node count
181 awk -v ns
="$_ns" 'ns ~ "@" $2 "@" { print $0 }' <<EOF
186 prog
=$
(basename "$0")
190 leader
) find_leader
;;
192 status
) nodes_status
;;