ldb_tdb: Add ltdb_idx_to_key() and use it in ltdb_index_filter()
[Samba.git] / ctdb / tools / ctdb_natgw
blob448d00519eb1c204dcd8bc26178147a8edca8e18
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 NAT gateway nodes file location
11 [ -n "$CTDB_NATGW_NODES" ] || CTDB_NATGW_NODES="${CTDB_BASE}/natgw_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 and private IP address of master node
28 list List private IP addresses of nodes in group, annotate master
29 status Show status of nodes in NAT gateway 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 [ $? -ne 255 ] # ctdb nodestatus returns 255 on failure
52 get_natgw_nodes ()
54 # Result is cached in global variable natgw_nodes
55 if [ -n "$natgw_nodes" ] ; then
56 return
59 if [ ! -r "$CTDB_NATGW_NODES" ] ; then
60 return 1
63 natgw_nodes=$(cat "$CTDB_NATGW_NODES") || return 1
65 # Sanity check file contents here
66 while read _ip _options ; do
67 # Skip comments
68 case "$_ip" in
69 \#*) continue ;;
70 esac
71 case "$_options" in
72 slave-only|"") : ;;
73 *) die "${prog}: Invalid options \"${_options}\" in \"$CTDB_NATGW_NODES\""
74 esac
75 done <<EOF
76 $natgw_nodes
77 EOF
79 return 0
82 # Print the PNN and IP address of the NAT gateway master node
83 find_master ()
85 get_natgw_nodes || \
86 die "${prog}: NAT gateway nodes file \"$CTDB_NATGW_NODES\" not found"
87 get_nodestatus_X || \
88 die "${prog}: Unable to get status of nodes"
90 # $_ms is an @-delimited list of nodes that are allowed to be the master
91 _ms="@"
92 while read _ip _options ; do
93 case "$_options" in
94 "") _ms="${_ms}${_ip}@" ;;
95 esac
96 done <<EOF
97 $natgw_nodes
98 EOF
100 # Now filter by $ms and by status of nodes...
102 # Note that the 3 awk invocations below have "||" between them, so
103 # the first to succeed will select the master node.
105 # First try for a fully active and healthy node, so must not be
106 # DISABLED, UNHEALTHY or INACTIVE (last covers DISCONNECTED,
107 # BANNED or STOPPED)
108 awk -F '|' -v ms="$_ms" \
109 'BEGIN { ret = 2 }
110 ms ~ "@" $2 "@" &&
111 $5 == 0 && $6 == 0 && $8 == 0 { print $1, $2 ; ret=0 ; exit }
112 END { exit ret }' <<EOF ||
113 $nodestatus_X
115 # Not found? UNHEALTHY/BANNED will do, so node must not be
116 # DISCONNECTED, DISABLED or STOPPED
117 awk -F '|' -v ms="$_ms" \
118 'BEGIN { ret = 2 }
119 ms ~ "@" $2 "@" &&
120 $3 == 0 && $5 == 0 && $7 == 0 { print $1, $2 ; ret=0 ; exit }
121 END { exit ret }' <<EOF ||
122 $nodestatus_X
124 # Not found? STOPPED will do, so node must not be DISCONNECTED or
125 # DISABLED
126 awk -F '|' -v ms="$_ms" \
127 'BEGIN { ret = 2 }
128 ms ~ "@" $2 "@" &&
129 $3 == 0 && $5 == 0 { print $1, $2 ; ret=0 ; exit }
130 END { exit ret }' <<EOF
131 $nodestatus_X
135 # List all nodes in the NAT gateway group, annotating the master node
136 nodes_list ()
138 get_natgw_nodes || \
139 die "${prog}: NAT gateway nodes file \"$CTDB_NATGW_NODES\" not found"
140 # Intentional word splitting here
141 # shellcheck disable=SC2046
142 set -- $(find_master) || \
143 die "${prog}: Unable to determine NAT gateway master node"
144 _master_ip="$2"
146 # Annotate the master node
147 while read _ip _options ; do
148 if [ "$_ip" = "$_master_ip" ] ; then
149 _options="MASTER${_options:+,}${_options}"
151 # There is no other way to do this and keep shellcheck happy.
152 # The tab character must be in the format string and the
153 # format string must contain no variables. Some shells will
154 # expand a tab if it is in an argument but others won't.
155 if [ -n "$_options" ] ; then
156 printf "%s\t%s\n" "$_ip" "$_options"
157 else
158 echo "$_ip"
160 done <<EOF
161 $natgw_nodes
165 # Print the status of all nodes in the NAT gateway group, along with a count
166 nodes_status ()
168 get_natgw_nodes || \
169 die "${prog}: NAT gateway nodes file \"$CTDB_NATGW_NODES\" not found"
170 get_nodestatus || \
171 die "${prog}: Unable to get status of nodes"
173 # $_ns is a @-delimited list of nodes in the NAT gateway group
174 _ns="@"
175 while read _ip _options ; do
176 _ns="${_ns}${_ip}@"
177 done <<EOF
178 $natgw_nodes
181 # Print status of nodes in $_ns, along with node count
182 awk -v ns="$_ns" 'ns ~ "@" $2 "@" { print $0 }' <<EOF
183 $nodestatus
187 prog=$(basename "$0")
188 cmd="$1"
190 case "$cmd" in
191 master) find_master ;;
192 list) nodes_list ;;
193 status) nodes_status ;;
194 *) usage ;;
195 esac