#!/usr/bin/perl # IBM_PROLOG_BEGIN_TAG # This is an automatically generated prolog. # # 61haes_r714 src/43haes/usr/sbin/cluster/sa/oracle/sbin/cl_oraListenerParse.sh 1.5 # # Licensed Materials - Property of IBM # # Restricted Materials of IBM # # COPYRIGHT International Business Machines Corp. 2006,2009 # 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 # @(#)26 1.5 src/43haes/usr/sbin/cluster/sa/oracle/sbin/cl_oraListenerParse.sh, hacmp.assist.oracle, 61haes_r714 3/4/09 04:05:05 #******************************************************************************** # This script is used to parse a `listener.ora' file to identify listeners and # the host names associated with each of them. The output from this script # shall be in the following format: # # \s* # \s* # . # . # . # For instance, if the listener `LISTENER1' is listening on two interfaces # namely, `host1' and `host2' the output shall look like this: # 0 # LISTENER1 host1 # LISTENER1 host2 # Multiple listeners for a SID are repeated similarly. #******************************************************************************** # We first parse various sections in listener.ora into chunks. # Then we shall find listeners related to a particular SID. This is # done by looking at sections that are identified by SID_LIST_. # Once we find all the s that are related to an SID # we can go ahead and identify their IP/HOST properties so they can # be used by the caller. open(FILE, "<$ARGV[0]") || ((print "-1\n") && exit(1)); my $SID = $ARGV[1]; #print "SID:".$SID."\n"; # Identify chunks in `listener.ora' my ($inchunk, $listener_chunk, $nchunks); my @listeners = (); my $nopen = 0; $nchunks = $inchunk = 0; my $found_chunk= 0 ; while() { chomp($_); # Get Rid of lines that contain only spaces or commented by Hash next if (($_ =~/^(\s)*$/) || ($_ =~ /^(\s|\#)*\#/)); $_ =~ s/^\s+//; $_ =~ s/\s+$//; # trim input string if ($inchunk == 1){ $listener_chunk .= $_; }else { $listener_chunk = $_; $inchunk = 1; } while ($listener_chunk =~ /^(\s)*(\w)+(\s)*\=(\s)*(\w)+(\s)*\=/ ){ $listener_chunk =~ s/^(\s)*(\w)+(\s)*\=(\s)*//; } if ($listener_chunk =~ /^(\s)*(\w)+(\s)*$/ ){ $inchunk = 1; $found_chunk= 0; } elsif ($listener_chunk =~ /^(\s)*(\w)+(\s)*\=(\s)*$/ ){ $inchunk = 1; $found_chunk= 0; } elsif ($listener_chunk =~ /^(\s)*(\w)+(\s)*\=(\s)*(\'|\"|\w|\.|\/)+/ ){ $inchunk = 0; $found_chunk= 0; } elsif ($listener_chunk =~ /^(\s)*(\w)+(\s)*\=(\s)*\(/ ) { while ($_ =~ /\(/g) { $nopen++; } while ($_ =~ /\)/g) { $nopen--; } if ($nopen > 0){ $inchunk=1; } else { $inchunk=0; $listeners[$nchunks] = $listener_chunk; $nchunks++; } $found_chunk=1; } # If niether recognizale patterns, skip chunk else { $inchunk = 0; $found_chunk= 0; } } # Not balanced brackets . This is optional and can be skipped. if ($inchunk == 1 && $found_chunk == 1) { $listeners[$nchunks] = $listener_chunk; $nchunks++; } # # Look through to see if our SID is defined in `listener.ora'. # The format should be a section beginning with SID_LIST_ # my %deflisteners=(); # list of listeners that are related to $SID. my $i = 0; while ($i <= $#listeners) { # capture LISTENER NAME for SID we are looking for my ($lisn) = ($listeners[$i] =~ /SID_LIST_(\w+)\s*=/); $deflisteners{$lisn} = 1 if (defined($lisn) && ($listeners[$i] =~ /\(\s*SID_NAME\s*=\s*$SID\s*\)/)); $i++; } # Now that we have a list of listeners that are worth looking at we shall # find the IPs/hostnames related to them. my %IPS = (); $i=0; while ($i <= $#listeners) { # Look for the typical format: # LISTENER = # (DESCRIPTION_LIST = # (DESCRIPTION = # (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1)) # (ADDRESS = (PROTOCOL = TCP)(HOST = hostname.austin.ibm.com)(PORT = 1521)) # ) # ) my ($listener, $addrs) = ($listeners[$i] =~ /^\s*(\S*)\s*=\s*\(\s*DESCRIPTION_LIST\s*=\s*\(\s*DESCRIPTION\s*=(.*)\)\s*\)\s*$/); # print "Listener:".$listener."\n" if defined $listener; if ($deflisteners{$listener} == 1) { my $j=0; while (($name, $value, $addrs) = ($addrs =~/\s*\(\s*(\S*)\s*=\s*(\S*)\s*\)(.*)/g)) { $IPS{$listener}[$j++] = $value if ($name =~ /HOST/i); } $i++; next; } # If we get here, that means we didn't find a listener formatted in the typical # way, but there are other ways they could be formatted. # Check for this one: # LISTENER = # (ADDRESS_LIST = # (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1)) # (ADDRESS = (PROTOCOL = TCP)(HOST = hostname.austin.ibm.com)(PORT = 1521)) # ) my ($listener, $addrs) = ($listeners[$i] =~ /^\s*(\S*)\s*=\s*\(\s*ADDRESS_LIST\s*=(.*)\)\s*\)\s*$/); if ($deflisteners{$listener} == 1) { my $j=0; while (($name, $value, $addrs) = ($addrs =~/\s*\(\s*(\S*)\s*=\s*(\S*)\s*\)(.*)/g)) { $IPS{$listener}[$j++] = $value if ($name =~ /HOST/i); } $i++; next; } # Finally, check for this one: # LISTENER = # (DESCRIPTION = # (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1)) # (ADDRESS = (PROTOCOL = TCP)(HOST = hostname.austin.ibm.com)(PORT = 1521)) # ) my ($listener, $addrs) = ($listeners[$i] =~ /^\s*(\S*)\s*=\s*\(\s*DESCRIPTION\s*=(.*)\)\s*\)\s*$/); if ($deflisteners{$listener} == 1) { my $j=0; while (($name, $value, $addrs) = ($addrs =~/\s*\(\s*(\S*)\s*=\s*(\S*)\s*\)(.*)/g)) { $IPS{$listener}[$j++] = $value if ($name =~ /HOST/i); } $i++; next; } $i++; } # Finally we print all the Listeners and IPs associated with them for given SID. my $j=0; my $lines=""; for my $key (keys %IPS) { my $i = 0; while ($i <= $#{$IPS{$key}}) { $lines .= $key." ".$IPS{$key}[$i++]."\n"; } $j += $i; } print $j."\n".$lines; exit(0);