CREATE OR REPLACE FUNCTION parse_listener_log_line 
(   p_in VARCHAR2
  , p_param IN VARCHAR2 
)  
RETURN VARCHAR2 
 AS
 l_begin NUMBER(3);
 l_end NUMBER(3);
 l_val VARCHAR2(2000);
BEGIN
  IF p_param NOT IN('SID'
                  , 'SERVICE_NAME'
                  , 'PROGRAM'
                  , 'SERVICE'
                  , 'HOST'
                  , 'USER'
                  , 'PROTOCOL'
                  , 'TYPE'
                  , 'METHOD'
                  , 'RETRIES'
                  , 'DELAY'
                  , 'PORT'
                  , 'COMMAND' 
      ) THEN
      raise_application_error(-20001,   'Invalid Parameter Value ' || p_param);
  END IF;
    l_begin := instr(UPPER(p_in),   '(' || p_param || '=');
    l_begin := instr(UPPER(p_in),   '=',   l_begin);
    l_end := instr(UPPER(p_in),   ')',   l_begin);
    l_val := SUBSTR(p_in,   l_begin + 1,   l_end -l_begin -1);
    RETURN l_val;
END;
/


CREATE directory listener_logs_dir AS
'O:\1521_logs';


CREATE TABLE listener_log
(
    log_date DATE
  , connect_string VARCHAR2(300)
  , protocol_info VARCHAR2(300)
  , ACTION VARCHAR2(15)
  , service_name VARCHAR2(15)
  , return_code NUMBER(10)
) 
organization EXTERNAL(
    type oracle_loader 
    DEFAULT directory listener_logs_dir 
    access parameters
    (
         records delimited BY newline 
         nobadfile 
         nologfile 
         nodiscardfile 
         fields terminated BY "*" lrtrim 
         missing field VALUES ARE NULL
         (
             log_date CHAR(30) date_format DATE mask "DD-MON-YYYY HH24:MI:SS"
           , connect_string
           , protocol_info
           , ACTION
           , service_name
           , return_code
          )
    ) 
    location('amss231011_listener_toberemoved.log')
) 
reject limit unlimited 
/




select a.LOG_DATE
     , a.service_name
     , DECODE(parse_listener_log_line(a.connect_string, 'HOST') 
             , '__jdbc__' , parse_listener_log_line(a.protocol_info,'HOST')
             , parse_listener_log_line(a.connect_string, 'HOST')
             ) HOST
     , DECODE(parse_listener_log_line(a.connect_string, 'HOST') 
             , '__jdbc__' , 'jdbc'
             , parse_listener_log_line(a.connect_string, 'PROGRAM')
             ) PROGRAM
     , parse_listener_log_line(a.connect_string, 'USER') "USER"
     , return_code
from listener_log a
where log_date is not null
and return_code = 0
and connect_string not like 'service%'
;


