#! /usr/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2009,2019 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 

# @(#)96   1.6   src/rsct/caa/cmds/ct_caa_scaffold_cluster_info.perl, topology.services, rsct_rady, rady2035a 4/8/10 16:04:13

#
# This script parses /usr/sbin/nodeq output and outputs cluster info suitable
# for digestion by libct_caa's simulation parsing functions. It is currently
# limited to getting just one cluster's worth of info from /usr/sbin/nodeq,
# because at the time of writing it wasn't clear whether /usr/bin/nodeq
# could deal with more than one cluster and, if it could, what its
# output would look like.
#

#
# NOTE: At the end of this file lives "here documents" illustrating
#       the state of nodeq command output at various stages of parsing
#       history.
#

#
# On 04/08/2010 it was learned the nodeq command was sunset in favor of
# "lscluster -m". Therefore, all discussion of and references to nodeq in
# this script should be taken as applying equally to "lscluster -m".
#

my $cluster_name;
my $local_node_number;

#
# Get this node's hostname, which is used to find the correct "local node
# number" associated with the cluster.
#

my $hostname = `/usr/bin/hostname`;
chomp $hostname;

if (-e "/usr/sbin/nodeq") {
	open INFO, "/usr/sbin/nodeq 2>/dev/null |";
} else {
	open INFO, "/usr/sbin/lscluster -m 2>/dev/null |";
}

while (<INFO>) {

	chomp;

	#
	# Remove trailing whitespace.
	#

	$_ =~ s/\s*$//;

	if (/Node name: (.*)$/) {

		if ($1 eq $hostname) {
			#
			# Setting $local_node_number to -1 means "the node number
			# found next is the local node number".
			#

			$local_node_number = -1;
		}
	} elsif (/Cluster shorthand id for node: (.*)$/ ||
				/Cluster number for node: (.*)$/) {

		if (-1 == $local_node_number) {
			$local_node_number = $1;
		}
	} elsif (/Cluster names & cluster uuids:$/ ||
				/Cluster names:$/ ||
				/Cluster names : cluster uuids: cluster type$/) {

		if ($local_node_number) {
			#
			# Get the cluster name on the next line.  The assumption is
			# there's only one, and that if there's more than one, the
			# others are on subsequent lines (as opposed to space-
			# separated on the same line.
			#

			$_ = <INFO>;
			chomp;

			#
			# Remove whitespace.
			#

			($cluster_name, $cluster_uuid, $junk) = split " ";
			$cluster_name =~ s/\s//g;

			#
			# Output:
			#
			# cluster name
			# local node number
			#

			printf "%s\n%s\n%s\n", $cluster_name, $local_node_number,
						$cluster_uuid;

			#
			# Only care about the cluster name associated with the
			# local node number.
			#

			last;
		}
	}
}

exit 0;


#
# End of main.
#


#
# "Here documents" showing the nodeq command output which parsing is
# based upon.
#

my $nodeq_output_1 = <<'EOF';
[e95n2sq09][/tmp/bubbly]> nodeq
Calling node query for all nodes
Node query number of nodes examined: 3


        Node name: e95n2sq07.ppd.pok.ibm.com
        Cluster number for node: 1
        State of node:  UP
        Network Interface used for last communication: en0
        Smoothed rtt to node: 7
        Mean Deviation in network rtt to node: 3
        Number of clusters node is a member in: 1
        Cluster names:
         CAYENNE

------------------------------

        Node name: e95n2sq08.ppd.pok.ibm.com
        Cluster number for node: 2
        State of node:  UP
        Network Interface used for last communication: en0
        Smoothed rtt to node: 7
        Mean Deviation in network rtt to node: 3
        Number of clusters node is a member in: 1
        Cluster names:
         CAYENNE

------------------------------

        Node name: e95n2sq09.ppd.pok.ibm.com
        Cluster number for node: 3
        State of node:  UP
        Network Interface used for last communication:
        Smoothed rtt to node: 0
        Mean Deviation in network rtt to node: 0
        Number of clusters node is a member in: 1
        Cluster names:
         CAYENNE
EOF

my $nodeq_output_2 = <<'EOF';
[e95n1sq10][/tmp/bubbly]> nodeq
Calling node query for all nodes
Node query number of nodes examined: 3


        Node name: e95n1sq10.ppd.pok.ibm.com
        Cluster shorthand id for node: 1
        uuid for node: 1415114950
        State of node:  UP
        Network Interface used for last communication:
        Smoothed rtt to node: 0
        Mean Deviation in network rtt to node: 0
        Number of clusters node is a member in: 1
        Cluster names & cluster uuids:
         CAA    1584323961

------------------------------

        Node name: e95n1sq11.ppd.pok.ibm.com
        Cluster shorthand id for node: 2
        uuid for node: 455574125
        State of node:  UP
        Network Interface used for last communication: en0
        Smoothed rtt to node: 7
        Mean Deviation in network rtt to node: 3
        Number of clusters node is a member in: 1
        Cluster names & cluster uuids:
         CAA    1584323961

------------------------------

        Node name: e95n1sq12.ppd.pok.ibm.com
        Cluster shorthand id for node: 3
        uuid for node: 1055117320
        State of node:  UP
        Network Interface used for last communication: en0
        Smoothed rtt to node: 7
        Mean Deviation in network rtt to node: 3
        Number of clusters node is a member in: 1
        Cluster names & cluster uuids:
         CAA    1584323961
EOF

my $nodeq_output_3 = <<'EOF';
[e95n1sq10][/tmp/bubbly]> nodeq
Calling node query for all nodes
Node query number of nodes examined: 3


	Node name: e95n2sq10.ppd.pok.ibm.com
	Cluster shorthand id for node: 1
	uuid for node: 1499754166
	State of node:  UP 
	Smoothed rtt to node: 8
	Mean Deviation in network rtt to node: 4
	Number of zones this node is a member in: 0
	Number of clusters node is a member in: 1
	Cluster names & cluster uuids:
	 CAA  	1747564955 

	Number of points_of_contact for node: 1
	Point-of-contact interface & contact state
	 en0  UP 

------------------------------

	Node name: e95n2sq11.ppd.pok.ibm.com
	Cluster shorthand id for node: 2
	uuid for node: 1352703123
	State of node:  UP 
	Smoothed rtt to node: 0
	Mean Deviation in network rtt to node: 0
	Number of zones this node is a member in: 0
	Number of clusters node is a member in: 1
	Cluster names & cluster uuids:
	 CAA  	1747564955 

	Number of points_of_contact for node: 0
	Point-of-contact interface & contact state
	 n/a 

------------------------------

	Node name: e95n2sq12.ppd.pok.ibm.com
	Cluster shorthand id for node: 3
	uuid for node: 43899884
	State of node:  UP 
	Smoothed rtt to node: 7
	Mean Deviation in network rtt to node: 3
	Number of zones this node is a member in: 0
	Number of clusters node is a member in: 1
	Cluster names & cluster uuids:
	 CAA  	1747564955 

	Number of points_of_contact for node: 1
	Point-of-contact interface & contact state
	 en0  UP 
EOF