Exforsys

Home arrow Reviews arrow Oracle Database Management Utilities

Oracle Utilities - Reading the listener.log from the database

Author: Packt Publishing     Published on: 4th Jan 2010

One particular extension of the above procedure is to read the listener.log file. This file has a specific star-delimited field file format which can be advantageous, and eases the read by means of the Loader driver.

 

Ads

The file format is as follows:

Sample Code
  1.  
  2.     21-JUL-2008 00:39:50 * (CONNECT_DATA=(SID=beta)(CID=(PROGRAM=perl)(HOS
  3.     T=alpha.us.oracle.com)(USER=oracle))) * (ADDRESS=(PROTOCOL=tcp)(HOST=1
  4.     92.168.2.10)(PORT=8392)) * establish * beta * 0
  5.     21-JUL-2008 00:39:56 * (CONNECT_DATA=(SID=beta)(CID=(PROGRAM=perl)(HOS
  6.     T=alpha.us.oracle.com)(USER=oracle))) * (ADDRESS=(PROTOCOL=tcp)(HOST=1
  7.     92.168.2.10)(PORT=8398)) * establish * beta * 0
  8.     21-JUL-2008 00:40:16 * service_update * beta * 0
  9.     21-JUL-2008 00:41:19 * service_update * beta * 0
  10.     21-JUL-2008 00:44:43 * ping * 0
  11.  
Copyright exforsys.com


The file has a format that can be deduced from the above data sample:

TIMESTAMP * CONNECT DATA [* PROTOCOL INFO] * EVENT [* SID] * RETURN CODE

As you can see this format, even though it is structured, it may have a different number of fields, so at loading time this issue must be considered.

In order for us to map this table to the database, we should consider the variable number of fields to have the External Table created. We'll create a temporary table so that this doesn't create an additional transactional overhead.

Now, let's create an External Table based on this format that points to:

$ORACLE_HOME/network/log

Sample Code
  1.  
  2.     create directory NETWORK_LOG_DIR
  3.     as '$ORACLE_HOME/network/log'
  4.  
  5. Now, let's create the External Table:
  6.     create table LISTENER_LOG (
  7.       TIMESTAMP        date,
  8.       CONNECT_DATA     varchar2(2048),
  9.       PROTOCOL_INFO    varchar2(64),
  10.       EVENT            varchar2(64),
  11.       SID              varchar2(64),
  12.       RETURN_CODE      number(5)
  13.     )
  14.     organization external (
  15.       type   ORACLE_LOADER
  16.       default directory NETWORK_LOG_DIR
  17.       access parameters (
  18.         records delimited by NEWLINE
  19.         nobadfile
  20.         nodiscardfile
  21.         nologfile
  22.         fields terminated by "*" LDRTRIM
  23.         reject rows with all null fields
  24.         (
  25.           "TIMESTAMP" char date_format DATE mask "DD-MON-YYYY HH24:MI:SS
  26.     ",
  27.            "CONNECT_DATA",
  28.            "PROTOCOL_INFO",
  29.            "EVENT",
  30.            "SID",
  31.            "RETURN_CODE"
  32.         )
  33.       )
  34.       location ('listener.log')
  35.     )
  36.     reject limit unlimited
  37.  
Copyright exforsys.com


The structure of interest is specified above, so there will be several rows rejected. Seeing as this file is not fully structured, you will find some non formatted information; the bad file and the log file are not meaningful in this context.

Another application of the LISTENER_LOG External Table is usage trend analysis. This query can be issued to detect usage peak hours.

Sample Code
  1.  
  2. SQL> select to_char(round(TIMESTAMP, 'HH'), 'HH24:MI') HOUR,
  3.   2         lpad('#', count(*), '#') CX
  4.   3 from    listener_log
  5.   4 group by round(TIMESTAMP, 'HH')
  6.   5 order by 1
  7. HOUR CX
  8. ----- ------------------------------------------------
  9. 14:00 ###
  10. 15:00 ##########################
  11. 16:00 ######################
  12. 17:00 #####################
  13. 18:00 #####################
  14. 19:00 ###############
  15.  
Copyright exforsys.com


Reading the listener.log file this way allows the DBA not only to keep track of the listener behavior, but also it allows a security administrator to easily spot hacking attempts.

Let's find out who is trying to access the database with sqlplus.exe.

Sample Code
  1.  
  2. SQL> select timestamp, protocol_info
  3.   2    from listener_log
  4.   3    where connect_data like '%sqlplus.exe%'
  5.   4    /
  6.  
  7. TIMESTAMP                 PROTOCOL_INFO
  8. -------------------- ----------------------------------------------------
  9. 01-SEP-2008 14:30:37       (ADDRESS=(PROTOCOL=tcp)(HOST=192.168.2.101)
  10.                                                                  (PORT=3651))
  11. 01-SEP-2008 14:31:08       (ADDRESS=(PROTOCOL=tcp)(HOST=192.168.2.101)
  12.                                                                  (PORT=3666))
  13. 01-SEP-2008 14:31:35       (ADDRESS=(PROTOCOL=tcp)(HOST=192.168.2.101)
  14.                                                                  (PORT=3681))
  15.  
Copyright exforsys.com


The use of External Tables to analyze the listener.log can be used not only to have an in-database version of the listener.log perform periodic and programmatic analysis of the listener behavior, but also to determine usage trends and correlate information with the audit team so that unauthorized connection programs can be easily and quickly spotted. Further useful applications can be found by reading the listener.log file. There are two fields that must be further parsed to get information out of them, but parsing those fields goes beyond the scope of this chapter. The structure that the analysis should consider is detailed next:

Connect String

1. SID: The Database Oracle SID, which is populated if the connection was performed by SID, otherwise it is NULL.
2. CID: It contains two subfields, PROGRAM and HOST
3. SERVER: This field indicates the connection type, either dedicated or shared
4. SERVICE_NAME: This field is populated when the connection is performed by a Service instead of SID.
5. COMMAND: The command issued by the user.
6. SERVICE: Present only when listener commands are issued.
7. FAILOVER_MODE: In Real Application Clusters (RAC) environments this field is used if the client performed a connection due to a failover. It shows the failover mode used.

 

Ads

Protocol

1. PROTOCOL: Indicates the used to perform the connection; this will be TCP most of the times.
2. HOST: This is the client's IP Address.
3. PORT: The port number of the oracle server used to establish the connection.



 
This tutorial is part of a Oracle Database Management Utilities tutorial series. Read it from the beginning and learn yourself.

Oracle Database Management Utilities

 

Comments