Hi,
tnsping is an Oracle tool that is sort of like a ping command. Unfortunately it does not come with a tool that can be used to parse its output which is very frustrating.
Example output of tnsping are as below:
$: tnsping testp1
TNS Ping Utility for Solaris: Version 11.2.0.2.0 - Production on 16-OCT-2013 16:18:06
Copyright (c) 1997, 2010, Oracle. All rights reserved.
Used parameter files:
/db/testp1/dba/sqlnet/sqlnet.ora
Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION =(LOAD_BALANCE=off)(FAILOVER=on)(CONNECT_TIMEOUT=5)(TRANSPORT_CONNECT_TIMEOUT=3)(RETRY_COUNT=3) (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP) (Host = testp1prim.mnl.ph.com) (Port = 10666))) (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP) (Host = testp1stdby.mnl.ph.com) (Port = 10666))) (CONNECT_DATA = (SERVICE_NAME=testp1_app.mnl.ph.com)))
OK (10 msec)
$: tnsping testd1
TNS Ping Utility for Solaris: Version 11.2.0.2.0 - Production on 16-OCT-2013 16:33:09
Copyright (c) 1997, 2010, Oracle. All rights reserved.
Used parameter files:
/db/testp1/dba/sqlnet/sqlnet.ora
Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS = (COMMUNITY = tcpip.world) (PROTOCOL = TCP) (Host = testd1.mnl.ph.com) (Port = 25666)) (CONNECT_DATA = (SID = testd1) (GLOBAL_NAME = testd1.mnl.ph.com)))
OK (20 msec)
At the moment, I re-direct the output of tnsping to a file and then grep -i for the the "Attempting to contact" line. which should give me the following output:
For tnsping testp1
Attempting to contact (DESCRIPTION =(LOAD_BALANCE=off)(FAILOVER=on)(CONNECT_TIMEOUT=5)(TRANSPORT_CONNECT_TIMEOUT=3)(RETRY_COUNT=3) (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP) (Host = testp1prim.mnl.ph.com) (Port = 10666))) (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP) (Host = testp1stdby.mnl.ph.com) (Port = 10666))) (CONNECT_DATA = (SERVICE_NAME=testp1_app.mnl.ph.com)))
For tnsping testd1
Attempting to contact (DESCRIPTION = (ADDRESS = (COMMUNITY = tcpip.world) (PROTOCOL = TCP) (Host = testd1.mnl.ph.com) (Port = 25666)) (CONNECT_DATA = (SID = testd1) (GLOBAL_NAME = testd1.mnl.ph.com)))
For clarity, I manually indented and posted these output to the forum as below:
For tnsping testp1
(DESCRIPTION =
(LOAD_BALANCE=off)
(FAILOVER=on)
(CONNECT_TIMEOUT=5)
(TRANSPORT_CONNECT_TIMEOUT=3)
(RETRY_COUNT=3)
(ADDRESS_LIST =
(ADDRESS =
(PROTOCOL = TCP)
(Host = testp1prim.mnl.ph.com)
(Port = 10666)
)
)
(ADDRESS_LIST =
(ADDRESS =
(PROTOCOL = TCP)
(Host = testp1stdby.mnl.ph.com)
(Port = 10666)
)
)
(CONNECT_DATA =
(SERVICE_NAME=testp1_app.mnl.ph.com)
)
)
For tnsping testd1
(DESCRIPTION =
(ADDRESS =
(COMMUNITY = tcpip.world)
(PROTOCOL = TCP)
(Host = testd1.mnl.ph.com)
(Port = 25666)
)
(CONNECT_DATA =
(SID = testd1)
(GLOBAL_NAME = testd1.mnl.ph.com)
)
)
Now come the big questions. Can anyone help me how to re-direct the output to an array and access it like an associative array?
For example, for the tnsping testd1 output, I want to be able to access address[host] and get testd1.mnl.ph.com or connect_data[sid] and get testd1.
For the tnsping testp1, it has two (2) address_list, so I will need to differentiate between the two (2) so that one is address_list_1 and the other is address_list_2. I want to be able to access address_list_1[host] and should get testp1prim.mnl.ph.com and address_list_2[host] and should give me testp1stdby.mnl.ph.com. Some tnsping output gives me as much as four (4) values.
I honestly don't know where to start except for doing the tnsping and redirect the output to a file and grep -i for the "Attempting to contact" string.
For a start, how do I count the number of times a string appears in a line? For example, I want to be able to check how many address_list there is from the tnsping output?
Not sure how to proceed from there. If any awk expert can help me parse the output into several parts that can be re-directed to a file, I will be more than satisfied with that instead of using an array.
For example, if using awk/nawk/gawk, how do I parse the string so that I get the following result:
For tnsping testp1
(DESCRIPTION =
(LOAD_BALANCE=off)
(FAILOVER=on)
(CONNECT_TIMEOUT=5)
(TRANSPORT_CONNECT_TIMEOUT=3)
(RETRY_COUNT=3)
(ADDRESS_LIST =
(ADDRESS =
(PROTOCOL = TCP)
(Host = testp1prim.mnl.ph.com)
(Port = 10666)
)
)
(ADDRESS_LIST =
(ADDRESS =
(PROTOCOL = TCP)
(Host = testp1stdby.mnl.ph.com)
(Port = 10666)
)
)
(CONNECT_DATA =
(SERVICE_NAME=testp1_app.mnl.ph.com)
)
)
- Parse the output to create four (4) or more files below:
description.txt
address_list_01.txt, address_list_02.txt ... or up to how many address_list sections there is
connect_data.txt
description.txt should contain all lines but the text from address_list and connect_data
address_list TXT files should have information from the address section
connect_data should have information from the connect_data section
For tnsping testd1
(DESCRIPTION =
(ADDRESS =
(COMMUNITY = tcpip.world)
(PROTOCOL = TCP)
(Host = testd1.mnl.ph.com)
(Port = 25666)
)
(CONNECT_DATA =
(SID = testd1)
(GLOBAL_NAME = testd1.mnl.ph.com)
)
)
- Parse the output to create three (30 or more files
description.txt
address_list_01.txt, address_list_02.txt ... or up to how many address_list sections there is
connect_data.txt
description.txt should contain all lines but the text from address_list and connect_data
address_list TXT files should have information from the address section
connect_data should have information from the connect_data section
I hope I've explained myself well. Sorry for a long post.
Any feedback/guidance will be much appreciated. Thanks in advance.
---------- Post updated at 12:50 AM ---------- Previous update was at 12:39 AM ----------
Hi
BTW, need to do this in KSH as the boxes only have KSH, no bash :(-
