Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 252 lines (203 sloc) 7.951 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
-- java pipelined function to scan all tracefiles in a directory and get metadata from top 30 lines of each file

CREATE TYPE trace_file_type AS OBJECT
(
  file_name VARCHAR2(400),
  file_time DATE,
  file_bytes NUMBER,
  file_traceid VARCHAR2(100),
  first_time DATE,
  first_action VARCHAR2(100),
  first_module VARCHAR2(100),
  first_service VARCHAR2(100),
  sid NUMBER,
  serial NUMBER,
  error VARCHAR2(400)
);
/

create type trace_file_list as table of trace_file_type;
/

CREATE TYPE trace_file_scan_impl AS OBJECT
(
  key INTEGER,

  STATIC FUNCTION ODCITableStart(sctx OUT trace_file_scan_impl, dir VARCHAR2, regex VARCHAR2)
    RETURN NUMBER
    AS LANGUAGE JAVA
    NAME 'com.ardentperf.trace_file_helper.ODCITableStart(oracle.sql.STRUCT[], java.lang.String, java.lang.String) return java.math.BigDecimal',

  MEMBER FUNCTION ODCITableFetch(self IN OUT trace_file_scan_impl, nrows IN NUMBER,
                                 outSet OUT trace_file_list) RETURN NUMBER
    AS LANGUAGE JAVA
    NAME 'com.ardentperf.trace_file_helper.ODCITableFetch(java.math.BigDecimal, oracle.sql.ARRAY[]) return java.math.BigDecimal',

  MEMBER FUNCTION ODCITableClose(self IN trace_file_scan_impl) RETURN NUMBER
    AS LANGUAGE JAVA
    NAME 'com.ardentperf.trace_file_helper.ODCITableClose() return java.math.BigDecimal'
);
/

CREATE FUNCTION trace_file_scan(dir varchar2, regex VARCHAR2) RETURN trace_file_list
PIPELINED USING trace_file_scan_impl;
/


set define off

create and compile java source named trace_file_helper as
package com.ardentperf;
import java.io.*;
import oracle.CartridgeServices.*;
import java.math.BigDecimal;
import oracle.sql.*;
import java.sql.*;
import java.util.*;
import java.lang.*;
import java.text.*;
// stored context type

public class StoredCtx
{
  Iterator files;
  String search;
  public StoredCtx(Iterator f, String s) { files=f; search=s; }
}

public class trace_file_helper implements SQLData
{
  private BigDecimal key;

  final static BigDecimal SUCCESS = new BigDecimal(0);
  final static BigDecimal ERROR = new BigDecimal(1);

  // Implement SQLData interface.

  String sql_type;
  public String getSQLTypeName() throws SQLException
  {
    return sql_type;
  }

  public void readSQL(SQLInput stream, String typeName) throws SQLException
  {
    sql_type = typeName;
    key = stream.readBigDecimal();
  }

  public void writeSQL(SQLOutput stream) throws SQLException
  {
    stream.writeBigDecimal(key);
  }


  // type methods implementing ODCITable interface

  static public BigDecimal ODCITableStart(STRUCT[] sctx,String dir,String regex)
    throws SQLException
  {
    Connection conn = DriverManager.getConnection("jdbc:default:connection:");

    // create a stored context and store the file collection in it
    File topdir=new File(dir);
    StoredCtx ctx=new StoredCtx(Arrays.asList(topdir.listFiles()).iterator(),regex);

    // register stored context with cartridge services
    int key;
    try {
      key = ContextManager.setContext(ctx);
    } catch (CountException ce) {
      return ERROR;
    }

    // create a trace_file_scan_impl instance and store the key in it
    Object[] impAttr = new Object[1];
    impAttr[0] = new BigDecimal(key);
    StructDescriptor sd = new StructDescriptor("TRACE_FILE_SCAN_IMPL",conn);
    sctx[0] = new STRUCT(sd,conn,impAttr);

    return SUCCESS;
  }

  public BigDecimal ODCITableFetch(BigDecimal nrows, ARRAY[] outSet)
    throws SQLException
  {
    Connection conn = DriverManager.getConnection("jdbc:default:connection:");

    // retrieve stored context using the key
    StoredCtx ctx;
    try {
      ctx=(StoredCtx)ContextManager.getContext(key.intValue());
    } catch (InvalidKeyException ik ) {
      return ERROR;
    }

    // get the nrows parameter
    int nrowsval = nrows.intValue();

    // create a vector for the fetched rows
    Vector v = new Vector(nrowsval);
    int i=0;

    StructDescriptor outDesc =
      StructDescriptor.createDescriptor("TRACE_FILE_TYPE", conn);
    Object[] out_attr = new Object[11];

    while(nrowsval>0 && ctx.files.hasNext()){
      File f = (File)ctx.files.next();
      String fn = f.getName();
      if(!fn.matches(ctx.search)) continue;
      out_attr[0] = (Object)fn; // file_name
      out_attr[1] = (Object)new Timestamp(f.lastModified()); // file_time
      out_attr[2] = (Object)new BigDecimal(f.length()); // file_bytes
      String[] fn_s = fn.split("_",4);
      out_attr[3] = (Object)(fn_s.length==4?fn_s[3].substring(0,fn_s[3].length()-4):""); // file_traceid
      
      // now we scan the file...
      BufferedReader r;
      try {
        r=new BufferedReader(new FileReader(f));
        try {
          int nLines=0;
          String line;
          while(nLines++<30 && ((line=r.readLine())!=null)) {
            if(line.startsWith("*** ACTION NAME:")) {
              out_attr[5] = (Object)line.substring(17,line.indexOf(')')); // first_action
            } else if(line.startsWith("*** MODULE NAME:")) {
              out_attr[6] = (Object)line.substring(17,line.indexOf(')')); // first_module
            } else if(line.startsWith("*** SERVICE NAME:")) {
              out_attr[7] = (Object)line.substring(18,line.indexOf(')')); // first_service
            } else if(line.startsWith("*** SESSION ID:")) {
              out_attr[8] = (Object)new BigDecimal(line.substring(16,line.indexOf('.'))); // sid
              out_attr[9] = (Object)new BigDecimal(line.substring(line.indexOf('.')+1,line.indexOf(')'))); // serial
            } else if(line.matches(".* [0-9-]{10} [0-9:.]{12}$")) {
              SimpleDateFormat df= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
              Timestamp t=new Timestamp(df.parse(line.substring(line.length()-23)).getTime());
              out_attr[4] = (Object)t; // first_time
            } else if(line.startsWith("*** TRACE DUMP CONTINUED FROM FILE")) {
              String new_fn = line.substring(35,line.length()-4);
              r.close();
              r=new BufferedReader(new FileReader(new_fn));
              nLines=0;
            }
            
          }
        } catch(IOException e) {
          out_attr[10]=e.toString();
        } catch(ParseException e) {
          out_attr[10]=e.toString();
        };

        try {
          r.close();
        } catch(IOException e) {
          out_attr[10]=e.toString();
        };
      } catch(FileNotFoundException e) {
        out_attr[10]=e.toString();
      }
      
      v.add((Object)new STRUCT(outDesc, conn, out_attr));
      i+=1;
      nrowsval-=1;
    }

    // return if no files found
    if(i==0) return SUCCESS;

    // create the output ARRAY using the vector
    Object out_arr[] = v.toArray();
    ArrayDescriptor ad = new ArrayDescriptor("TRACE_FILE_LIST",conn);
    outSet[0] = new ARRAY(ad,conn,out_arr);

    return SUCCESS;
  }

  public BigDecimal ODCITableClose() throws SQLException {

    // retrieve stored context using the key, and remove from ContextManager
    StoredCtx ctx;
    try {
      ctx=(StoredCtx)ContextManager.clearContext(key.intValue());
    } catch (InvalidKeyException ik ) {
      return ERROR;
    }

    // any needed cleanup

    return SUCCESS;
  }
}
/



create view all_trace_files as
select 'UDUMP' directory, t.* from table(trace_file_scan('/u0001/app/oracle/admin/ardentp/udump','ardentp.*')) t
UNION ALL
select 'BDUMP' directory, t.* from table(trace_file_scan('/u0001/app/oracle/admin/ardentp/bdump','ardentp.*')) t;
/



--- permissions

call dbms_java.grant_permission('DEVELOPER', 'java.io.FilePermission', '/u0001/app/oracle/admin/ardentp/udump', 'read');
call dbms_java.grant_permission('DEVELOPER', 'java.io.FilePermission', '/u0001/app/oracle/admin/ardentp/bdump', 'read');
create directory UDUMP as '/u0001/app/oracle/admin/ardentp/udump';
create directory BDUMP as '/u0001/app/oracle/admin/ardentp/bdump';
grant read on directory UDUMP to DEVELOPER;
grant read on directory BDUMP to DEVELOPER;
Something went wrong with that request. Please try again.