summaryrefslogtreecommitdiffstats
path: root/ctdb/tools/ctdb_lvs
blob: d0249b9d4f447f782fcdc68a2a175c894078744f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/bin/sh

if [ -z "$CTDB_BASE" ] ; then
	export CTDB_BASE="/usr/local/etc/ctdb"
fi

. "${CTDB_BASE}/functions"

load_script_options "failover" "91.lvs"

# Default LVS nodes file location
[ -n "$CTDB_LVS_NODES" ] || CTDB_LVS_NODES="${CTDB_BASE}/lvs_nodes"

if [ -z "$CTDB" ] ; then
	CTDB=ctdb
fi

############################################################

usage ()
{
	cat <<EOF
$0 <option>

<option> is one of:
  leader     Display node number of leader node
  list       List node number and private IP address of usable nodes in group
  status     Show status of all nodes in LVS group
EOF
    exit 1
}

nodestatus_X=""
# Fields are:
# Node|IP|Disconnected|Unknown|Banned|Disabled|Unhealthy|Stopped|Inactive|PartiallyOnline|ThisNode
get_nodestatus_X ()
{
	# Result is cached in global variable nodestatus_X
	[ -n "$nodestatus_X" ] || \
		nodestatus_X=$($CTDB -X nodestatus all |
			       sed -e '1d' -e 's@^|@@' -e 's@|$@@')
}

get_nodestatus ()
{
	# Result is cached in global variable nodestatus
	[ -n "$nodestatus" ] || nodestatus=$($CTDB nodestatus all)
	case $? in
	# $CTDB nodestatus returns 255 on failure
	0|255) return 0 ;;
	*)     return 1 ;;
	esac
}

get_lvs_nodes ()
{
	# Result is cached in global variable lvs_nodes
	if [ -n "$lvs_nodes" ] ; then
		return
	fi

	if [ ! -r "$CTDB_LVS_NODES" ] ; then
		return 1
	fi

	lvs_nodes=$(cat "$CTDB_LVS_NODES") || return 1

	# Sanity check file contents here
	while read _ip _options ; do
		# Skip comments
		case "$_ip" in
		\#*) continue ;;
		esac
		case "$_options" in
		follower-only|"") : ;;
		*) die "${prog}: Invalid options \"${_options}\" in  \"$CTDB_LVS_NODES\""
		esac
	done <<EOF
$lvs_nodes
EOF

	return 0
}

# Print PNN and IP address of given nodes meeting the criteria for
# usable LVS nodes.  That is, either those that are healthy or, if no
# healthy nodes, then nodes that are active and not-disabled.
# Return codes: 0 = nodes found, 255 = no nodes found, 10 = error.
filter_nodes ()
{
	# $_ns is an @-delimited list of nodes to be considered
	_ns="$1"

	get_nodestatus_X
	[ -n "$nodestatus_X" ] || return 10

	# Now filter by $_ns and by status of nodes...

	# Note that the 2 awk invocations below have "||" between
	# them, so the first to succeed will print the nodes.

	# First try for a fully active and healthy node, so must not
	# be UNKNOWN, DISABLED, UNHEALTHY or INACTIVE (last covers
	# DISCONNECTED, BANNED or STOPPED)
	awk -F '|' -v ns="$_ns" '
		BEGIN { ret = 255 }
		ns ~ "@" $2 "@" && $4 == 0 && $6 == 0 && $7 == 0 && $9 == 0 {
			print $1, $2 ; ret=0
		}
		END { exit ret }
		' <<EOF ||
$nodestatus_X
EOF
	# Not found?  UNHEALTHY do, so node must not be INACTIVE or
	# DISABLED
	awk -F '|' -v ns="$_ns" '
		BEGIN { ret = 255 }
		ns ~ "@" $2 "@" && $6 == 0 && $9 == 0 {
			print $1, $2 ; ret=0
		}
		END { exit ret }
		' <<EOF
$nodestatus_X
EOF
}

# Print the PNN of the LVS leader node
find_leader ()
{
	get_lvs_nodes || \
		die "${prog}: LVS nodes file \"$CTDB_LVS_NODES\" not found"

	# $_ms is an @-delimited list of nodes that are allowed to be the leader
	_ms="@"
	while read _ip _options ; do
		case "$_options" in
		"") _ms="${_ms}${_ip}@" ;;
		esac
	done <<EOF
$lvs_nodes
EOF

	_leader_candidates=$(filter_nodes "$_ms") || return $?
	echo "${_leader_candidates%% *}"
}

# List all usable nodes in the LVS group
nodes_list ()
{
	get_lvs_nodes || \
		die "${prog}: LVS nodes file \"$CTDB_LVS_NODES\" not found"

	# $_ns is a @-delimited list of nodes in the LVS  group
	_ns="@"
	while read _ip _options ; do
		_ns="${_ns}${_ip}@"
	done <<EOF
$lvs_nodes
EOF

	_usable_nodes=$(filter_nodes "$_ns")
	case $? in
	0)   : ;;
	255) exit 0 ;; # Return 0 even if no usable nodes
	*)   exit 10 ;;
	esac

	awk '{ print $1, $2 }'<<EOF
$_usable_nodes
EOF
}

# Print the status of all nodes in the LVS group, along with a count
nodes_status ()
{
	get_lvs_nodes || \
		die "${prog}: LVS nodes file \"$CTDB_LVS_NODES\" not found"
	get_nodestatus
	[ -n "$nodestatus" ] || exit 10

	# $_ns is a @-delimited list of nodes in the LVS  group
	_ns="@"
	while read _ip _options ; do
		_ns="${_ns}${_ip}@"
	done <<EOF
$lvs_nodes
EOF

	# Print status of nodes in $_ns, along with node count
	awk -v ns="$_ns" 'ns ~ "@" $2 "@" { print }' <<EOF
$nodestatus
EOF
}

# For backward compatibility
prog=$(basename "$0")
cmd="$1"

case "$cmd" in
leader) find_leader ;;
list)   nodes_list ;;
status) nodes_status ;;
*)      usage ;;
esac