1#!@BASH_SHELL@
2#
3# Copyright (C) 1997-2003 Sistina Software, Inc.  All rights reserved.
4# Copyright (C) 2004-2011 Red Hat, Inc.  All rights reserved.
5#
6# This program is free software; you can redistribute it and/or
7# modify it under the terms of the GNU General Public License
8# as published by the Free Software Foundation; either version 2
9# of the License, or (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software
18# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19#
20
21#
22# Use corosync-quorumtool to figure out if the specified node is a member
23# of the cluster.  Returns 1 if not a member, and
24# 0 if the node is happily running.
25#
26# Tested on RHEL6 and F17  Note that the old version of this function utilized
27# clustat, which had introspection in to the configuration.
28# If a node was not found, the old version would return '2', but the only
29# consumer of this function never cared about that value.
30#
31is_node_member_clustat()
32{
33	local node="$1"
34	local output_list
35
36	# Still having a tag while (a) online but (b) not running pacemaker
37	# (e.g. crm_node) or rgmanager not considered adequate for things like
38	# the LVM agent - so we use corosync-quorumtool instead.  The function
39	# name really should be changed.
40	#
41	# corosync 1.4.1 output looks like:
42	#
43	#  # corosync-quorumtool  -l
44	#  Nodeid     Name
45	#     1   rhel6-1
46	#     2   rhel6-2
47	#
48	# corosync 2.0.1 output looks like:
49	#  # corosync-quorumtool -l
50	#
51	#  Membership information
52	#  ----------------------
53	#      Nodeid      Votes Name
54	#           1          1 rhel7-1.priv.redhat.com
55	#           2          1 rhel7-2.priv.redhat.com
56	#
57
58	output_list=$(corosync-quorumtool -l | grep -v "^Nodeid")
59
60	# first try searching for the node in the output as both a FQDN or shortname
61	echo "$output_list" | grep -i -e " $node\$" -e " $node\..*\$" &> /dev/null && return 0
62
63	# if the node was not found in the quorum list, try any known aliases found in /etc/hosts
64	for alias in $(cat /etc/hosts | grep -e "\s$node\s" -e "\s$node\$" | tail -n 1 | sed 's/\t/ /g' | cut -f2- -d " ");
65	do
66		echo "$output_list" | grep -i -e " $alias\$" &> /dev/null && return 0
67	done
68
69	return 1
70}
71
72
73#
74# Print the local node name to stdout
75# Returns 0 if could be found, 1 if not
76# Tested on RHEL6 (cman) and Fedora 17 (corosync/pacemaker)
77#
78local_node_name()
79{
80	local node nid localid
81
82	if which magma_tool &> /dev/null; then
83		# Use magma_tool, if available.
84		line=$(magma_tool localname | grep "^Local")
85
86		if [ -n "$line" ]; then
87			echo ${line/* = /}
88			return 0
89		fi
90	fi
91
92	if which cman_tool &> /dev/null; then
93		# Use cman_tool
94
95		line=$(cman_tool status | grep -i "Node name: $1")
96		[ -n "$line" ] || return 1
97		echo ${line/*name: /}
98		return 0
99	fi
100
101	if ! which crm_node &> /dev/null; then
102		# no crm_node? :(
103		return 2
104	fi
105
106	localid=$(crm_node -i)
107	while read nid node; do
108		if [ "$nid" = "$localid" ]; then
109			echo $node
110			return 0
111		fi
112	done < <(crm_node -l)
113
114	return 1
115}
116
117