Pages

Thursday, August 28, 2014

Batch script by example. Run batch, that call sqlplus that generate csv files, batch file merge these files into one.

The Task
Need to generate csv file, which holds data collected from several schemas in same Instance.
The output file should hold data about services and ports used by applications. Each schema represents a different application.


The Flow
In order to query several schemas, the script creates new user, "query_user".
This user is granted 
SELECT ANY TABLE and CREATE VIEW permissions.
For each schema, a new view is created, queried, and then dropped.

The output of the query, is saved to csv file.

The flow in general:
Run main batch file.
Batch file calls main sqlplus script.
Open loop on schemas.

 - The sqlplus script builds a series of sqlplus scripts, each one per specific task.
 - The sqlplus script then executes the generated scripts.
 - The data is retrieved via view owned by "query_user" on another schema at a time.
 - Each generated script generates its own csv file.
Close loop on schemas.
Main sqlplus script terminates.
Batch file calls another batch script, that merges the generated csv files into one file.
Cleanup script, drop query_user, drop view, etc.

The files:
bat files
main_get_ports.bat
main_retrieve_data.bat
merge_output_files.bat
cleanup_output_files.bat

sqlplus files
main_get_ports.sql
set_connection.sql


create_query_user.sql

build_grant_permissions.sql
grant_permissions_user.sql
grant_permissions.sql

build_create_view_and_select.sql
create_view_and_select.sql
create_view.sql

select_header_procs.sql
select_header_params.sql
select_from_view_procs.sql
select_from_view_params.sql

drop_view.sql

===========================
main_get_ports.bat
===========================
ECHO OFF
SET HOME_PATH=L:\Functionality\PORTS
SET INPUT_FOLDER=L:\Functionality\PORTS\scripts
SET OUTPUT_FOLDER=L:\Functionality\PORTS
SET OUTPUT_FILE_NAME=ports_list.csv

ECHO.
ECHO -------------------------------------
ECHO Starting Running Script
ECHO -------------------------------------
ECHO.
ECHO Generating script: %OUTPUT_FOLDER%\%OUTPUT_FILE_NAME%
ECHO.

cd %HOME_PATH%
cd scripts
call main_retrieve_data.bat
cd %HOME_PATH%
call scripts\merge_output_files.bat %HOME_PATH% %INPUT_FOLDER% %OUTPUT_FOLDER% %OUTPUT_FILE_NAME%
call scripts\cleanup_output_files.bat %HOME_PATH%\scripts

ECHO -------------------------------------
ECHO Completed Successfully
ECHO -------------------------------------


===========================
main_retrieve_data.bat
===========================
sqlplus -s /nolog @main_get_ports.sql

===========================
main_get_ports.sql
===========================
@set_connection.sql
--SET TERMOUT OFF

Conn &&query_user/&&query_pass@&&query_connectstr

SET TERMOUT ON
SET SHOW OFF
SET VERIFY OFF 
SET HEAD OFF
SET LINE 500
SET FEEDBACK OFF
SET PAGES 0
SET TRIMSPOOL ON


PROMPT -----------------------;
PROMPT Grant Permissions Start
@build_grant_permissions.sql
@grant_permissions.sql
SET TERMOUT ON
PROMPT Grant Permissions Completed
PROMPT -----------------------;
PROMPT

PROMPT
PROMPT -----------------------;
PROMPT build_create_view_and_select.sql Start
@build_create_view_and_select.sql
SET TERMOUT ON
PROMPT build_create_view_and_select.sql Completed
PROMPT -----------------------;
PROMPT


PROMPT
PROMPT -----------------------;
PROMPT create_view_and_select.sql Start
@create_view_and_select.sql
SET TERMOUT ON
PROMPT create_view_and_select.sql Completed
PROMPT -----------------------;
PROMPT
EXIT;
/



===========================
create_query_user.sql
===========================
------------------------------------------------
--Login as system/sysdba to run this script
------------------------------------------------
CREATE USER query_user IDENTIFIED BY query_user;
GRANT CONNECT TO query_user;
GRANT CREATE VIEW TO query_user;
GRANT SELECT ANY TABLE TO query_user;

EXIT;


===========================
set_connection.sql
===========================
DEFINE query_user=query_user
DEFINE query_pass=query_user
DEFINE query_connectstr=inst_test
DEFINE main_user=query_user 




===========================
build_grant_permissions.sql
===========================
@set_connection.sql
SET TERMOUT OFF

Conn &&query_user/&&query_pass@&&query_connectstr
SET SHOW OFF
SET VERIFY OFF
SET HEAD OFF
SET LINE 500
SET FEEDBACK OFF
SET PAGES 0
SET TRIMS ON

-- This goes into grant_permissions.sql
SPOOL grant_permissions.sql
PROMPT -------------------------------;
PROMPT SET TERMOUT OFF
PROMPT SET SHOW OFF
PROMPT SET HEAD ON
PROMPT SET FEEDBACK OFF
PROMPT SET VERIFY OFF

--PROMPT DEFINE main_user='';
--PROMPT COLUMN main_user new_value main_user for a30;
--PROMPT SELECT SYS_CONTEXT('USERENV','SESSION_USER') main_user FROM DUAL;
--PROMPT SELECT '&&main_user' from dual;
PROMPT -------------------------------;
PROMPT

SELECT '@grant_permissions_user.sql'||' '||owner||' '||owner||' '||'&&query_connectstr' || ' '||'&&main_user' FROM ALL_TABLES WHERE TABLE_NAME = 'PROCESS';
SPOOL OFF
/


===========================
grant_permissions.sql
===========================
This file is generated by build_grant_permissions.sql
It looks like this:

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

SET TERMOUT OFF
SET SHOW OFF
SET HEAD ON
SET FEEDBACK OFF
SET VERIFY OFF
-------------------------------

@grant_permissions_user.sql SCHEMA_A_USER SCHEMA_A_PASS CONNECT_STR query_user
@grant_permissions_user.sql SCHEMA_B_USER SCHEMA_B_PASS CONNECT_STR query_user


===========================
grant_permissions_user.sql
===========================
SET SHOW OFF
SET VERIFY OFF
SET HEAD OFF
SET FEEDBACK OFF
SET PAGES 0
SET TRIMS ON

DEFINE user_name=&&1
DEFINE user_pass=&&2
DEFINE conn_str=&&3
DEFINE main_user=&&4

conn &&user_name/&&user_pass@&&conn_str

GRANT SELECT ON PRODUCT TO &&main_user;
GRANT SELECT ON CONFIGURATION_PROPERTIES TO &&main_user;
GRANT SELECT ON PROCESS TO &&main_user;
/


===========================
build_create_view_and_select.sql
===========================
@set_connection.sql
--SET TERMOUT OFF

Conn &&query_user/&&query_pass@&&query_connectstr

SET TERMOUT ON
SET SHOW OFF
SET VERIFY OFF
SET HEAD OFF
SET LINE 500
SET FEEDBACK OFF
SET PAGES 0
SET TRIMSPOOL ON

SPOOL create_view_and_select.sql
PROMPT -------------------------------;
--PROMPT PROMPT inside SPOOL create_view_and_select.sql
SET TERMOUT OFF
SET SHOW OFF
SET VERIFY OFF
SET HEADING ON
SET LINE 500
SET FEEDBACK OFF
SET PAGES 0
SET TRIMSPOOL ON

PROMPT -------------------------------;
PROMPT

--PROMPT SPOOL ports_list.csv
--SELECT '@create_view.sql'||' '||owner||';'||CHR(10)||'@select_from_view.sql'||' '||'ports_list_'||owner||'.csv'||';' ||CHR(10)||'SET HEADING OFF'||CHR(10) FROM ALL_TABLES WHERE TABLE_NAME = 'PROCESS';

SELECT '@create_view.sql'||' '||owner||';'FROM ALL_TABLES WHERE TABLE_NAME = 'PROCESS';
PROMPT SET HEADING ON
SELECT '@select_header_procs.sql'||' '||'ports_proc_header.csv'||';' FROM ALL_TABLES WHERE ROWNUM < 2;
SELECT '@select_header_params.sql'||' '||'ports_params_header.csv'||';' FROM ALL_TABLES WHERE ROWNUM < 2;
PROMPT SET HEADING OFF
SELECT '@create_view.sql'||' '||owner||';'||CHR(10)||'@select_from_view_procs.sql'||' '||'ports_proc_list_'||owner||'.csv'||';' FROM ALL_TABLES WHERE TABLE_NAME = 'PROCESS';
SELECT '@create_view.sql'||' '||owner||';'||CHR(10)||'@select_from_view_params.sql'||' '||'ports_params_list_'||owner||'.csv'||';' FROM ALL_TABLES WHERE TABLE_NAME = 'PROCESS';
SELECT '@drop_view.sql'||';' FROM DUAL;
--PROMPT SPOOL OFF
PROMPT /
SPOOL OFF
/


===========================
create_view_and_select.sql
===========================
This file is generated by build_create_view_and_select.sql
It looks like this:

@create_view.sql SCHEMA_A_USER;
@create_view.sql SCHEMA_B_USER;
SET HEADING ON
@select_header_procs.sql ports_proc_header.csv;
@select_header_params.sql ports_params_header.csv;
SET HEADING OFF
@create_view.sql SCHEMA_A_USER;
@select_from_view_procs.sql ports_proc_list_RUS_TEST1_SPARX.csv;

@create_view.sql SCHEMA_B_USER;
@select_from_view_procs.sql ports_proc_list_RUS_TEST2_SPARX.csv;

@create_view.sql SCHEMA_A_USER;
@select_from_view_params.sql ports_params_list_SCHEMA_A_USER.csv;

@create_view.sql SCHEMA_B_USER;
@select_from_view_params.sql ports_params_list_SCHEMA_B_USER.csv;

@drop_view.sql;
/

===========================
Handle Logic files
===========================
Following files handle the logic.
They are not important for a general example.
create_view.sql
select_header_procs.sql
select_header_params.sql

===========================
create_view.sql
===========================
DEFINE my_user=&&1;

--SET HEAD ON
--PROMPT --------------------------------------------;
--PROMPT Running create_view.sql for user '&&my_user'
--PROMPT --------------------------------------------;

CREATE OR REPLACE VIEW PROCESS_PORTS_VW AS
SELECT RTRIM('&&my_user') AS schema_name,
       RTRIM('PRODUCTS CONFIGURATION') AS source,
       item_id AS service_name,
       PRODUCTS.jvm_id AS jvm_id,
       PRODUCTS.jmx_port AS jmx_port,      
       CONFIGURATION.property_value AS property_value,
       CONFIGURATION.property_description AS property_description,
       NULL AS process_name,
       NULL AS process_type,      
       NULL AS process_port,
       NULL AS server_port
      FROM &&my_user..PRODUCTS PRODUCTS,
           &&my_user..CONFIGURATION_PROPERTIES CONFIGURATION    
 WHERE PRODUCTS.product_id = CONFIGURATION.property_id
   AND 1=1
UNION ALL
SELECT '&&my_user' AS schema_name,
       'PROCESS' AS source,
       NULL AS service_name,
       NULL AS jvm_id,      
       NULL AS jmx_port,
       NULL AS property_value,
       NULL AS property_description,
       process_name,
       process_type,
       process_port,
       server_port      
FROM   &&my_user..PROCESS
 WHERE 1=1
ORDER BY process_name   ;

--PROMPT --------------------------------------------;
--PROMPT create_view.sql Completed
--PROMPT --------------------------------------------;



===========================
select_header_procs.sql
===========================

DEFINE my_file=&&1;

SET COLSEP ,    
SET TERMOUT OFF
SET SHOW OFF
SET VERIFY OFF
SET LINESIZE 1000
SET FEEDBACK OFF
SET PAGES 10
SET TRIMSPOOL ON
SET UNDERLINE OFF
SET HEADING ON
SET NEWPAGE NONE

COL property_value FOR A50
COL service_name FOR A12
COL jvm_id FOR 999999
COL property_description FOR A150

SPOOL &&my_file
SELECT schema_name,
       source,
  process_name,
  process_type,
  process_port,
  server_port  
FROM PROCESS_PORTS_VW
WHERE source = 'PROCESS'
  AND ROWNUM < 2;

SPOOL OFF;
===========================
select_header_params.sql
===========================

DEFINE my_file=&&1;

SET COLSEP ,    
SET TERMOUT OFF
SET SHOW OFF
SET VERIFY OFF
SET LINESIZE 1000
SET FEEDBACK OFF
SET PAGES 10
SET TRIMSPOOL ON
SET UNDERLINE OFF
SET HEADING ON
SET NEWPAGE NONE

COL property_value FOR A50
COL service_name FOR A12
COL jvm_id FOR 999999
COL property_description FOR A150

SPOOL &&my_file
SELECT schema_name,
       source,
       service_name,
       jvm_id,
       jmx_port,
       property_value,
       property_description
FROM PROCESS_PORTS_VW
WHERE source = 'PRODUCTS CONFIGURATION'
  AND ROWNUM<2;
SPOOL OFF;


===========================
Generate csv file
===========================
Following file use the view, to retrieve data, and create csv files.
select_from_view_procs.sql
select_from_view_params.sql


===========================
select_from_view_procs.sql
===========================

DEFINE my_file=&&1;

SET COLSEP ,     
SET TERMOUT OFF
SET SHOW OFF
SET VERIFY OFF 
SET LINESIZE 1000
SET FEEDBACK OFF
SET PAGESIZE 0
SET TRIMSPOOL ON
SET UNDERLINE OFF
SET HEADING OFF

COL property_value FOR A50
COL service_name FOR A12
COL jvm_id FOR 999999
COL property_description FOR A150

SPOOL &&my_file
SELECT schema_name,
       source,
  process_name,
  process_type,
  process_port,
  server_port   
FROM PROCESS_PORTS_VW
WHERE source = 'PROCESS';

SPOOL OFF;
===========================
select_from_view_params.sql
===========================

DEFINE my_file=&&1;

SET COLSEP ,     
SET TERMOUT OFF
SET SHOW OFF
SET VERIFY OFF 
SET LINESIZE 1000
SET FEEDBACK OFF
SET PAGES 0
SET TRIMSPOOL ON
SET UNDERLINE OFF
SET HEADING OFF

COL property_value FOR A50
COL service_name FOR A12
COL jvm_id FOR 999999
COL property_description FOR A150

SPOOL &&my_file
SELECT schema_name,
       source,
  service_name,
  jvm_id,
  jmx_port,
  property_value,
  property_description
FROM PROCESS_PORTS_VW
WHERE source = 'PRODUCTS CONFIGURATION';  
SPOOL OFF;

===========================
drop_view.sql
===========================
--PROMPT --------------------------------------------;
--PROMPT Drop PROCESS_PORTS_VW
--PROMPT --------------------------------------------;

DROP VIEW PROCESS_PORTS_VW;

--PROMPT --------------------------------------------;
--PROMPT Drop PROCESS_PORTS_VW Completed
--PROMPT --------------------------------------------;


===========================
merge_output_files.bat
===========================
ECHO OFF
SET HOME_PATH=%1%
SET INPUT_FOLDER=%2%
SET OUTPUT_FOLDER=%3%
SET OUTPUT_FILE_NAME=%4%


REM ---------------------------------------------------------
REM This program would merge all files into one file.
REM Target File: OUTPUT_FILE_NAME
REM Source files: 
REM Header Files:  ports_params_header.csv
REM                ports_proc_header.csv
REM Data Files:    ports_params_list_*.csv
REM                ports_proc_list_*.csv
REM ---------------------------------------------------------


TYPE %INPUT_FOLDER%\ports_params_header.csv | find "SCHEMA_NAME" > %OUTPUT_FOLDER%\%OUTPUT_FILE_NAME%
FOR %%i IN (%INPUT_FOLDER%\ports_params_list_*.csv) DO TYPE %%i >> %OUTPUT_FOLDER%\%OUTPUT_FILE_NAME%

ECHO. >> %OUTPUT_FOLDER%\%OUTPUT_FILE_NAME%

TYPE %INPUT_FOLDER%\ports_proc_header.csv | find "SCHEMA_NAME" >> %OUTPUT_FOLDER%\%OUTPUT_FILE_NAME%
FOR %%i IN (%INPUT_FOLDER%\ports_proc_list_*.csv) DO TYPE %%i  >> %OUTPUT_FOLDER%\%OUTPUT_FILE_NAME%


===========================
cleanup_output_files.bat
===========================
ECHO OFF
SET INPUT_FOLDER=%1%

REM ---------------------------------------------------------
REM This program would delete all temp files.
REM ---------------------------------------------------------

FOR %%i IN (%INPUT_FOLDER%\ports_params*.csv) DO DEL %%i

FOR %%i IN (%INPUT_FOLDER%\ports_proc*.csv) DO DEL %%i


Wednesday, August 13, 2014

EZCONNECT, TNSNAMES by example

General
The post below describes how to use EZCONNECT, with examples.

Configuration:
On the client side, enable EZCONNECT protocol by editing sqlnet.ora:
NAMES.DIRECTORY_PATH=(ezconnect, tnsnames)

Usage:
CONNECT username/password@[//]host[:port][/service_name]

  • // - Optional. Specify // for a URL and jdbc connection
  • host - Required. Specify the host name or IP address of the database server computer.
  • port - Optional. Specify the listening port. The default is 1521.
  • service_name - Optional. Specify the service name of the database.
                              Default value for service name, when not provided, is 
    the host of the database server computer

How to find service name:
Option A - check the listener.ora on the Oracle host server.
Option B - check the output of lsnrctl status listener_name


For example:
user@my_host:/software/oracle/101/network/admin>% lsnrctl status lsnr_inst1

LSNRCTL for Linux: Version 10.1.0.5.0 - Production on 13-AUG-2014 06:26:02

Copyright (c) 1991, 2004, Oracle.  All rights reserved.

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=my_host)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias                     lsnr_inst1
Version                   TNSLSNR for Linux: Version 10.1.0.5.0 - Production
Start Date                04-JUN-2013 11:22:50
Uptime                    248 days 13 hr. 13 min. 56 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      ON
Listener Parameter File   /software/oracle/101/network/admin/listener.ora
Listener Log File         /software/oracle/101/network/log/lsnr_inst1.log
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=my_host.my_domain.com)(PORT=1521)))
Services Summary...
Service "MYDBC" has 1 instance(s).
  Instance "hsodbc", status UNKNOWN, has 1 handler(s) for this service...
Service "srv1" has 2 instance(s).
  Instance "srv1", status UNKNOWN, has 1 handler(s) for this service...
  Instance "srv1", status READY, has 1 handler(s) for this service...
Service "srv2" has 2 instance(s).
  Instance "srv2", status UNKNOWN, has 1 handler(s) for this service...
  Instance "srv2", status READY, has 1 handler(s) for this service...
The command completed successfully


Compare this to listener.ora contents:

# listener.ora Network Configuration File: /software/oracle/101/network/admin/listener.ora
# Generated by Oracle configuration tools.

LSNR_INST1 =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS_LIST =
        (ADDRESS = (PROTOCOL = TCP)(HOST = my_host)(PORT = 1521))
       )
    )
  )

SID_LIST_LSNR_INST1 =
  (SID_LIST =
    (SID_DESC =
      (GLOBAL_DBNAME = srv1)
      (ORACLE_HOME = /software/oracle/101)
      (SID_NAME = sid_1)
    )
    (SID_DESC =
      (GLOBAL_DBNAME = srv2)
      (ORACLE_HOME = /software/oracle/101)
      (SID_NAME = sid_2)
    )
    (SID_DESC =
      (GLOBAL_DBNAME = MYDBC)
      (ORACLE_HOME = /software/oracle/101)
      (SID_NAME = sid_3)
    )
  )


Using EZconnect in SQL Developer
In Database field, put host:port/service_name

Full connection string
Another alternative to avoid tnsnames.ora, is to provide full connection string, and not the tnsnames.ora alias.
Note:
No spaces in the connection string.
The 
" around the connection string.
sqlplus user/password@"(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=my_host.my_domain.com)(PORT=1521))(CONNECT_DATA=(SID=sid_1)))"

Oracle Client version limitation
Note - EZCONNECT is available only on Oracle Client 10 and higher.

TNSNAMES
It is possible to split tnsnames.ora into several files:

This is the tnsnames.ora file:
IFILE=c:\oracle\ora92\network\ADMIN\SITE_A_tnsnames.ora
IFILE=c:\oracle\ora92\network\ADMIN\SITE_B_tnsnames.ora

And each  tnsnames.ora would look like a regular tnsnames.ora file. 
For example:           
CONN_STR_A=(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST= 88.8.88.888)(PORT = 1521))(CONNECT_DATA = (SID = orainst)))
CONN_STR_B=(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST= 99.9.99.999)(PORT = 1521))(CONNECT_DATA = (SID = orainst)))

Appendix
Oracle Reference

Tuesday, August 12, 2014

Listener Issues by example

=================================
Listener Log
=================================
To find where is the Oracle Listener log file:
find . -type f -name lsnr_orainst.log


=================================
Listener Log File location
=================================
Listener log file location can be found by running lsnrctl status
The header section got the full path for listener log file.

In Oracle 10:
/home/app/oracle/product/10.2/network/log/listener.log

In Oracle 11:
/software/oracle/diag/tnslsnr/<server>/lsnr_<SID>/trace/lsnr_<SID>.log
/software/oracle/diag/tnslsnr/<server>/lsnr_<SID>/alert/log.xml

lsnr_<SID>.log - Is one file, getting bigger and bigger.
log.xml - many files, with sequence, log1.xml, log2.xml, log3.xml, etc...



=================================
How to find where is the listener log.
=================================

Option A.
SELECT value FROM V$PARAMETER 
WHERE name = 'background_dump_dest';

VALUE
------------------------------------------
/software/oracle/diag/rdbms/orainst/orainst/trace

Option B.
lsnrctl status LSNR_<SID> 

And take the path from the output
LSNRCTL for Linux: Version 11.1.0.7.0 - Production on 25-FEB-2015 21:00:34

Copyright (c) 1991, 2008, Oracle.  All rights reserved.

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=jpn-sbm-5-ora-1)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias                     LSNR_IGT
Version                   TNSLSNR for Linux: Version 11.1.0.7.0 - Production
Start Date                24-FEB-2015 21:23:22
Uptime                    0 days 23 hr. 37 min. 14 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /software/oracle/111/network/admin/listener.ora

Listener Log File         /software/oracle/111/log/diag/tnslsnr/my_server/lsnr_orainst/alert/log.xml

Option C.
Using
adrci utility.
adrci stands for  ADR Command Interpreter.

>% adrci
ADRCI: Release 11.1.0.7.0 - Production on Wed Feb 25 21:08:12 2015
Copyright (c) 1982, 2007, Oracle.  All rights reserved.
ADR base = "/software/oracle"


adrci> help

 HELP [topic]
   Available Topics:
        CREATE REPORT
        ECHO
        EXIT
        HELP
        HOST
        IPS
        PURGE
        RUN
        SET BASE
        SET BROWSER
        SET CONTROL
        SET ECHO
        SET EDITOR
        SET HOMES | HOME | HOMEPATH
        SET TERMOUT
        SHOW ALERT
        SHOW BASE
        SHOW CONTROL
        SHOW HM_RUN
        SHOW HOMES | HOME | HOMEPATH
        SHOW INCDIR
        SHOW INCIDENT
        SHOW PROBLEM
        SHOW REPORT
        SHOW TRACEFILE
        SPOOL

 There are other commands intended to be used directly by Oracle, type
 "HELP EXTENDED" to see the list

adrci> HELP EXTENDED

 HELP [topic]
   Available Topics:
        BEGIN BACKUP
        CD
        DDE
        DEFINE
        DESCRIBE
        END BACKUP
        LIST DEFINE
        MERGE ALERT
        MERGE FILE
        MIGRATE
        QUERY
        REPAIR
        SET COLUMN
        SHOW CATALOG
        SHOW DUMP
        SHOW SECTION
        SHOW TRACE
        SHOW TRACEMAP
        SWEEP
        UNDEFINE

        VIEW

 adrci> show tracefiles

This would cause all trace file to flash by. not very useful.

adrci> show tracefiles %.log
     diag/rdbms/igt/igt/trace/alert_igt.log
     diag/rdbms/igt/igt/trace/alert_last_3000.log
     diag/clients/user_oracle/host_1265807696_11/trace/sqlnet.log
     diag/tnslsnr/jpn-sbm-5-aps-2/lsnr_igt/trace/lsnr_igt.log

 adrci> show tracefiles alert_igt.log
     diag/rdbms/igt/igt/trace/alert_igt.log

adrci> show trace alert_igt.log
This would open the alert_igt.log in vi mode


=================================
service name

=================================
How to find Oracle service on a server:
ps -ef | grep pmon | grep -v grep
oracle    4354     1  0  2013 ?        00:01:59 ora_pmon_dbprod

=================================
listener_name
=================================
Default listener name, in listener.ora is the string LISTENER.
When another name is specified, one needs to provide the listener name in each lsnrctl command.
For example:

This is the listener.ora file

LSNR_DBPROD =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS_LIST =
        (ADDRESS = (PROTOCOL = TCP)(HOST = my_host)(PORT = 1521))
       )
    )
  )

SID_LIST_LSNR_DBPROD =
  (SID_LIST =
    (SID_DESC =
      (GLOBAL_DBNAME = dbprod)
      (ORACLE_HOME = /software/oracle/101)
      (SID_NAME = dbprod)
    )
  )
 
=================================
lsnrctl commands
=================================

lsnrctl commands should look like:
lsnrctl status LSNR_DBPROD
lsnrctl stop LSNR_DBPROD
lsnrctl start LSNR_DBPROD

Using lsnrctl utility

General syntax:
lsnrctl command [listener_name]

Where listener_name is the name of the listener to be administered. 
If no name is specified, then the default name, LISTENER, is assumed.

lsnrctl status [listener_name]
   Would display current
   
lsnrctl show [listener_name]
   Would display all possible lsnrctl parameters


lsnrctl show parameter [listener_name]
   Would display the parameter value.

lsnrctl services
Would list all services on the server.


=================================
Real example:
=================================
oracle@my_server:/software/oracle/diag/rdbms/sid/sid/trace>% lsnrctl status

LSNRCTL for Linux: Version 11.1.0.7.0 - Production on 23-JUL-2014 07:30:53

Copyright (c) 1991, 2008, Oracle.  All rights reserved.

Connecting to (ADDRESS=(PROTOCOL=tcp)(HOST=)(PORT=1521))
STATUS of the LISTENER
------------------------
Alias                     lsnr_sid
Version                   TNSLSNR for Linux: Version 11.1.0.7.0 - Production
Start Date                25-DEC-2013 08:04:28
Uptime                    209 days 23 hr. 26 min. 24 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /software/oracle/111/network/admin/listener.ora
Listener Log File         /software/oracle/diag/tnslsnr/server-1/lsnr_sid/alert/log.xml
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=server-1)(PORT=1521)))
Services Summary...
Service "sid" has 2 instance(s).
  Instance "sid", status UNKNOWN, has 1 handler(s) for this service...
  Instance "sid", status READY, has 1 handler(s) for this service...
Service "sidXDB" has 1 instance(s).
  Instance "igt", status READY, has 1 handler(s) for this service...
Service "sid_XPT" has 1 instance(s).
  Instance "sid", status READY, has 1 handler(s) for this service...

The command completed successfully

less /software/oracle/111/network/admin/listener.ora
# listener.ora Network Configuration File: /software/oracle/111/network/admin/listener.ora
# Generated by Oracle configuration tools.

LSNR_SID =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS_LIST =
        (ADDRESS = (PROTOCOL = TCP)(HOST = server-1)(PORT = 1521))
       )
    )
  )

SID_LIST_LSNR_SID =
  (SID_LIST =
    (SID_DESC =
      (GLOBAL_DBNAME = sid)
      (ORACLE_HOME = /software/oracle/111)
      (SID_NAME = sid)
    )
  )


===================================================
oracle@server-1:/software/oracle/diag/rdbms/igt/igt/trace>% lsnrctl stop LSNR_SID

LSNRCTL for Linux: Version 11.1.0.7.0 - Production on 23-JUL-2014 07:59:10

Copyright (c) 1991, 2008, Oracle.  All rights reserved.

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=server-1)(PORT=1521)))
The command completed successfully
oracle@server-1:/software/oracle/diag/rdbms/sid/sid/trace>% lsnrctl start LSNR_SID

LSNRCTL for Linux: Version 11.1.0.7.0 - Production on 23-JUL-2014 07:59:16

Copyright (c) 1991, 2008, Oracle.  All rights reserved.

Starting /software/oracle/111/bin/tnslsnr: please wait...

TNSLSNR for Linux: Version 11.1.0.7.0 - Production
System parameter file is /software/oracle/111/network/admin/listener.ora
Log messages written to /software/oracle/111/log/diag/tnslsnr/server-1/lsnr_sid/alert/log.xml
Listening on: (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=server-1)(PORT=1521)))

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=server-1)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias                     lsnr_sid
Version                   TNSLSNR for Linux: Version 11.1.0.7.0 - Production
Start Date                23-JUL-2014 07:59:16
Uptime                    0 days 0 hr. 0 min. 0 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /software/oracle/111/network/admin/listener.ora
Listener Log File         /software/oracle/111/log/diag/tnslsnr/server-1/lsnr_sid/alert/log.xml
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=server-1)(PORT=1521)))
Services Summary...
Service "igt" has 1 instance(s).
  Instance "igt", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully

oracle@nzl-tel-1-dwh-1:/software/oracle/diag/rdbms/sid/sid/trace>% lsnrctl status LSNR_SID

LSNRCTL for Linux: Version 11.1.0.7.0 - Production on 23-JUL-2014 07:59:34

Copyright (c) 1991, 2008, Oracle.  All rights reserved.

Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=server-1)(PORT=1521)))
STATUS of the LISTENER
------------------------
Alias                     lsnr_igt
Version                   TNSLSNR for Linux: Version 11.1.0.7.0 - Production
Start Date                23-JUL-2014 07:59:16
Uptime                    0 days 0 hr. 0 min. 17 sec
Trace Level               off
Security                  ON: Local OS Authentication
SNMP                      OFF
Listener Parameter File   /software/oracle/111/network/admin/listener.ora
Listener Log File         /software/oracle/diag/tnslsnr/my_server/lsnr_orainst/alert/log.xml
Listening Endpoints Summary...
  (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=server-1)(PORT=1521)))
Services Summary...
Service "sid" has 1 instance(s).
  Instance "sid", status UNKNOWN, has 1 handler(s) for this service...
The command completed successfully
oracle@server-1:/software/oracle/diag/rdbms/sid/sid/trace>%

=================================
Control logging to Listener.log 
=================================
To stop/resume listener logging, set LOG_STATUS parameter to OFF/ON.


lsnrctl show log_status
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=server-1)(PORT=1521)))
LISTENER parameter "log_status" set to ON
The command completed successfully

lsnrctl set log_status off 
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=server-1)(PORT=1521)))
LISTENER parameter "log_status" set to OFF
The command completed successfully

To make the change permanent, edit listener.ora file.
Need to add this line:
LOGGING_<LISTENER_NAME>=OFF

*The parameters are added at the end of listener.ora file. 
For example:

LSNR_ORAINST =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS_LIST =
        (ADDRESS = (PROTOCOL = TCP)(HOST = my_server)(PORT = 1521))
       )
    )
  )

SID_LIST_LSNR_ORAINST =
  (SID_LIST =
    (SID_DESC =
      (GLOBAL_DBNAME = orainst)
      (ORACLE_HOME = /software/oracle/111)
      (SID_NAME = orainst)
    )
  )

SUBSCRIBE_FOR_NODE_DOWN_EVENT_LSNR_ORAINST=OFF
LOGGING_LSNR_ORAINST=OFF

=================================
Working with Named Listener
=================================
Some cammands would not work with named listener.
The workaround it to set listener name with SET CURRENT_LISTENER lsnr_<sid>  command and then there is no need to provide the listener name.

lsnrctl
LSNRCTL for Linux: Version 11.1.0.7.0 - Production on 09-JUN-2015 08:48:40
Copyright (c) 1991, 2008, Oracle.  All rights reserved.
Welcome to LSNRCTL, type "help" for information.
LSNRCTL> set CURRENT_LISTENER lsnr_<sid>
Current Listener is lsnr_igt
LSNRCTL> show log_status
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=my_server)(PORT=1521)))
lsnr_igt parameter "log_status" set to ON
The command completed successfully
LSNRCTL> set log_status off
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=my_server)(PORT=1521)))
lsnr_igt parameter "log_status" set to OFF
The command completed successfully
LSNRCTL> show log_status
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=my_server)(PORT=1521)))
lsnr_igt parameter "log_status" set to OFF
The command completed successfully

To set trace on. This would generate trc file under $ORACLE_HOME/log/diag/tnslistener/<server>/<listener_name>/trace
LSNRCTL> show trac_level
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=my_server)(PORT=1521)))
lsnr_igt parameter "trc_level" set to OFF
The command completed successfully
LSNRCTL> set trac_level admin
Connecting to (DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=my_server)(PORT=1521)))
lsnr_igt parameter "trc_level" set to admin
The command completed successfully


=================================
sqlnet.ora parameters
=================================
sqlnet.ora file is located under /software/oracle/112/network/admin/
It can have many optional parameters.

Parameters for the sqlnet.ora File Reference

Some of the more useful parameters:
----------------------------------
NAMES.DIRECTORY_PATH
----------------------------------
To specify the order of the naming methods used for client name resolution lookups.

Example: NAMES.DIRECTORY_PATH=(tnsnames,ldap,ezconnect,nis)
----------------------------------
SQLNET.EXPIRE_TIME
----------------------------------
- Specify a time interval, in minutes, to send a check to verify that client/server connections are active. 

Default value: 0 (Not Active)
Recommended value: 10
Example: SQLNET.EXPIRE_TIME=10


=================================
Static vs Dynamic Listener configuration.
=================================
$ORACLE_HOME/network/admin/listener.ora
Static Listener Configuration.
In this configuration, the listener service name is predefined, and hard codded in listener.ora file, and is mapped to a specific server:port:sid

SID_LIST_LSNR_ORAINST =
  (SID_LIST =
    (SID_DESC =
      (GLOBAL_DBNAME = orainst)
      (ORACLE_HOME = /software/oracle/111)
      (SID_NAME = orainst)
    )


  )
LSNR_ORAINST =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS_LIST =
        (ADDRESS = (PROTOCOL = TCP)(HOST = my_server)(PORT = 1521))
       )
    )
  )

Dynamic Listener Configuration.

In this configuration, the listener service is not mapped to a specific sid, and just listens on all requests on port 1521.
The PMON process dynamically registers the server service with listener service.

LISTENER =
  (DESCRIPTION_LIST =
    (DESCRIPTION =
      (ADDRESS = (PROTOCOL = TCP)(HOST = my_server)(PORT = 1521))
      (ADDRESS = (PROTOCOL = IPC)(KEY = EXTPROC1521))
    )

  )

To block dynamic registrations, add this line to listener.ora
DYNAMIC_REGISTRATION_lsnr_name = off

=================================
Listener all possible parameters
=================================
lsnrctl
LSNRCTL> help
The following operations are available
An asterisk (*) denotes a modifier or extended command:

start               stop                status
services            version             reload
save_config         trace               spawn
change_password     quit                exit

set*                show*

LSNRCTL> help show
The following operations are available after show
An asterisk (*) denotes a modifier or extended command:

rawmode                     displaymode
rules                       trc_file
trc_directory               trc_level
log_file                    log_directory
log_status                  current_listener
inbound_connect_timeout     startup_waittime
snmp_visible                save_config_on_stop
dynamic_registration

LSNRCTL> help set
The following operations are available after set
An asterisk (*) denotes a modifier or extended command:

password                    rawmode
displaymode                 trc_file
trc_directory               trc_level
log_file                    log_directory
log_status                  current_listener
inbound_connect_timeout     startup_waittime
save_config_on_stop         dynamic_registration


=================================
Listener trace:
=================================
Inside  listener.ora file set the TRACE_LEVEL_listener
TRACE_LEVEL_listener=admin


=================================
Common Lister Connection Errors
=================================
A. ORA-12162: TNS:net service name is incorrectly specified
The error: 
ORA-12162: TNS:net service name is incorrectly specified

The Solution: 
Check the environment variable ORACLE_SID and sid in the connection string.

For example, when using the command "sqlplus / as sysdba" the sid in the connection string is taken from ORACLE_SID environment variable.

B. Fatal NI connect error 12170.
In alert.log, following error appears:

***********************************************************************
Fatal NI connect error 12170.
  VERSION INFORMATION:
        TNS for Linux: Version 11.2.0.4.0 - Production
        Oracle Bequeath NT Protocol Adapter for Linux: Version 11.2.0.4.0 - Production
        TCP/IP NT Protocol Adapter for Linux: Version 11.2.0.4.0 - Production
  Time: 25-FEB-2017 17:12:15
  Tracing not turned on.
  Tns error struct:
    ns main err code: 12535   
TNS-12535: TNS:operation timed out
    ns secondary err code: 12560
    nt main err code: 505   
TNS-00505: Operation timed out
    nt secondary err code: 110
    nt OS err code: 0
  Client address: (ADDRESS=(PROTOCOL=tcp)(HOST=10.144.3.200)(PORT=45052))
Sat Feb 25 17:12:16 2017
***********************************************************************

The Solution: 

Add to sqlnet.ora file on the server.
SQLNET.EXPIRE_TIME=10

Per Oracle Documentation:
SQLNET.EXPIRE_TIME
Purpose

To specify a time interval, in minutes, to send a check to verify that client/server connections are active. 

The following usage notes apply to this parameter:

Setting a value greater than 0 ensures that connections are not left open indefinitely, due to an abnormal client termination.

If the probe finds a terminated connection, or a connection that is no longer in use, then it returns an error, causing the server process to exit.

This parameter is primarily intended for the database server, which typically handles multiple connections at any one time.

Limitations on using this terminated connection detection feature are:

It is not allowed on bequeathed connections.

Though very small, a probe packet generates additional traffic that may downgrade network performance.

Depending on which operating system is in use, the server may need to perform additional processing to distinguish the connection probing event from other events that occur. This can also result in degraded network performance.


Default: 0

Minimum Value: 0
Recommended Value: 10



=================================
Listener.log contents
=================================
Reference
Example:

08-JUN-2015 10:21:27 * (CONNECT_DATA=(CID=(PROGRAM=JDBC Thin Client)(HOST=__jdbc__)(USER=iu))(SID=igt)) * (ADDRESS=(PROTOCOL=tcp)(HOST=172.16.14.98)(PORT=62423)) * establish * igt * 0
08-JUN-2015 10:21:27 * (CONNECT_DATA=(CID=(PROGRAM=JDBC Thin Client)(HOST=__jdbc__)(USER=iu))(SID=igt)) * (ADDRESS=(PROTOCOL=tcp)(HOST=172.16.14.98)(PORT=62421)) * establish * igt * 0
08-JUN-2015 10:21:27 * (CONNECT_DATA=(CID=(PROGRAM=JDBC Thin Client)(HOST=__jdbc__)(USER=iu))(SID=igt)) * (ADDRESS=(PROTOCOL=tcp)(HOST=172.16.14.98)(PORT=62425)) * establish * igt * 0
08-JUN-2015 10:21:27 * (CONNECT_DATA=(CID=(PROGRAM=JDBC Thin Client)(HOST=__jdbc__)(USER=iu))(SID=igt)) * (ADDRESS=(PROTOCOL=tcp)(HOST=172.16.14.98)(PORT=62467)) * establish * igt * 0

08-JUN-2015 10:21:27 * (CONNECT_DATA=(CID=(PROGRAM=JDBC Thin Client)(HOST=__jdbc__)(USER=iu))(SID=igt)) * (ADDRESS=(PROTOCOL=tcp)(HOST=172.16.14.98)(PORT=62480)) * establish * igt * 0
Listener log format is:
TIMESTAMP * CONNECT DATA [* PROTOCOL INFO] * EVENT [* SID] * RETURN CODE
Each field is delimited by *
Successful connections return *
Failed connections return error code.


=================================
Listener Common Errors
=================================
A. ERROR: ORA-12560:  TNS:protocol adapter error

What to check:
- Oracle service is up and running
- Oracle SID is set 
- tnsping <connection_string>
- Connect with ezconnect: sqlplus user/pass@SID:1521/host
-$PATH


B. ORA-12535: TNS: operation timed out
   This is general "wrapper" error.
   Look for the second error message. 


C.  TNS-03505
This error came when connect with any user except oracle user.
The issue was, missing read permission for all on tnsnames.ora and sqlnet.ora files.
error:
iu@oracle_server:/starhome/dbinstall/DBKIT>% tnsping igt

TNS Ping Utility for Linux: Version 12.2.0.1.0 - Production on 28-JUL-2019 15:29:30

Copyright (c) 1997, 2016, Oracle.  All rights reserved.

Used parameter files:
/software/ogg/oracle/12.2/product/12.2.0/dbhome_1/network/admin/sqlnet.ora

TNS-03505: Failed to resolve name

Fix:

oracle@oracle_server:~>%  chmod a+r   /software/ogg/oracle/12.2/product/12.2.0/dbhome_1/network/admin/tnsnames.ora
oracle@oracle_server:~>% chmod a+r  /software/ogg/oracle/12.2/product/12.2.0/dbhome_1/network/admin/sqlnet.ora

Test again, it is working

iu@oracle_server:/starhome/dbinstall/DBKIT>% tnsping igt

TNS Ping Utility for Linux: Version 12.2.0.1.0 - Production on 28-JUL-2019 15:29:54

Copyright (c) 1997, 2016, Oracle.  All rights reserved.

Used parameter files:
/software/ogg/oracle/12.2/product/12.2.0/dbhome_1/network/admin/sqlnet.ora


Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS = (PROTOCOL = TCP)(HOST = oracle_server)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) (SERVICE_NAME = igt)))

OK (0 msec)


Reference
lsnrctl Reference.
Oracle Reference to lsnrctl.
Listener Parameters
Listener Parameters in Oracle 10G