s3: smbd: Correctly set smb2req->smb1req->posix_pathnames from the calling fsp on...
[Samba.git] / ctdb / tools / ctdb_natgw
blob728cd9c9b32aaa7534cd2a9b434961c56749504d
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" "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
15 CTDB=ctdb
18 ############################################################
20 usage ()
22 cat <<EOF
23 $0 <option>
25 <option> is one of:
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
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 [ $? -ne 255 ] # ctdb nodestatus returns 255 on failure
51 get_natgw_nodes ()
53 # Result is cached in global variable natgw_nodes
54 if [ -n "$natgw_nodes" ] ; then
55 return
58 if [ ! -r "$CTDB_NATGW_NODES" ] ; then
59 return 1
62 natgw_nodes=$(cat "$CTDB_NATGW_NODES") || return 1
64 # Sanity check file contents here
65 while read _ip _options ; do
66 # Skip comments
67 case "$_ip" in
68 \#*) continue ;;
69 esac
70 case "$_options" in
71 follower-only|"") : ;;
72 *) die "${prog}: Invalid options \"${_options}\" in \"$CTDB_NATGW_NODES\""
73 esac
74 done <<EOF
75 $natgw_nodes
76 EOF
78 return 0
81 # Print the PNN and IP address of the NAT gateway leader node
82 find_leader ()
84 get_natgw_nodes || \
85 die "${prog}: NAT gateway nodes file \"$CTDB_NATGW_NODES\" not found"
86 get_nodestatus_X || \
87 die "${prog}: Unable to get status of nodes"
89 # $_ms is an @-delimited list of nodes that are allowed to be the leader
90 _ms="@"
91 while read _ip _options ; do
92 case "$_options" in
93 "") _ms="${_ms}${_ip}@" ;;
94 esac
95 done <<EOF
96 $natgw_nodes
97 EOF
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,
106 # BANNED or STOPPED)
107 awk -F '|' -v ms="$_ms" \
108 'BEGIN { ret = 2 }
109 ms ~ "@" $2 "@" &&
110 $4 == 0 && $6 == 0 && $7 == 0 && $9 == 0 { print $1, $2 ; ret=0 ; exit }
111 END { exit ret }' <<EOF ||
112 $nodestatus_X
114 # Not found? UNHEALTHY/BANNED will do, so node must not be
115 # DISCONNECTED, DISABLED or STOPPED
116 awk -F '|' -v ms="$_ms" \
117 'BEGIN { ret = 2 }
118 ms ~ "@" $2 "@" &&
119 $3 == 0 && $6 == 0 && $8 == 0 { print $1, $2 ; ret=0 ; exit }
120 END { exit ret }' <<EOF ||
121 $nodestatus_X
123 # Not found? STOPPED will do, so node must not be DISCONNECTED or
124 # DISABLED
125 awk -F '|' -v ms="$_ms" \
126 'BEGIN { ret = 2 }
127 ms ~ "@" $2 "@" &&
128 $3 == 0 && $6 == 0 { print $1, $2 ; ret=0 ; exit }
129 END { exit ret }' <<EOF
130 $nodestatus_X
134 # List all nodes in the NAT gateway group, annotating the leader node
135 nodes_list ()
137 get_natgw_nodes || \
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"
143 _leader_ip="$2"
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"
156 else
157 echo "$_ip"
159 done <<EOF
160 $natgw_nodes
164 # Print the status of all nodes in the NAT gateway group, along with a count
165 nodes_status ()
167 get_natgw_nodes || \
168 die "${prog}: NAT gateway nodes file \"$CTDB_NATGW_NODES\" not found"
169 get_nodestatus || \
170 die "${prog}: Unable to get status of nodes"
172 # $_ns is a @-delimited list of nodes in the NAT gateway group
173 _ns="@"
174 while read _ip _options ; do
175 _ns="${_ns}${_ip}@"
176 done <<EOF
177 $natgw_nodes
180 # Print status of nodes in $_ns, along with node count
181 awk -v ns="$_ns" 'ns ~ "@" $2 "@" { print $0 }' <<EOF
182 $nodestatus
186 prog=$(basename "$0")
187 cmd="$1"
189 case "$cmd" in
190 leader) find_leader ;;
191 list) nodes_list ;;
192 status) nodes_status ;;
193 *) usage ;;
194 esac